Skip to content

Commit

Permalink
Add file load button
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Jun 12, 2022
1 parent b1d887c commit 69b0441
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
25 changes: 20 additions & 5 deletions src/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

MainComponent::MainComponent() : _djPlayer{_threadPool, _formatManager}
{
setAudioDevices();
setupAudioDevices();

addAndMakeVisible(_sideBarLeft);
addAndMakeVisible(_sideBarRight);
Expand All @@ -18,6 +18,7 @@ MainComponent::MainComponent() : _djPlayer{_threadPool, _formatManager}
else { _djPlayer.startPlayback(); }
};

_sideBarLeft.onLoadClicked = [this]() { loadFile(); };
_sideBarLeft.onCueClicked = [this]() { _djPlayer.positionRelative(0.0); };
_sideBarRight.onTempoDeltaChanged = [this](double delta) { _djPlayer.speed((100.0 + delta) / 100.0); };
_sideBarRight.onWaveformZoomChanged = [this](double zoom) { _display.waveformZoom(zoom); };
Expand Down Expand Up @@ -60,13 +61,27 @@ void MainComponent::resized()
grid.performLayout(getLocalBounds().reduced(2));
}

auto MainComponent::setAudioDevices() -> void
auto MainComponent::loadFile() -> void
{
auto const* msg = "Please select the audio file you want to load...";
auto const dir = juce::File::getSpecialLocation(juce::File::userMusicDirectory);
auto const fileFlags = juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles;

auto load = [this](auto const& chooser)
{
auto file = chooser.getResult();
if (!file.existsAsFile()) { return; }
_djPlayer.loadFile(file);
};

_fileChooser = std::make_unique<juce::FileChooser>(msg, dir, _formatManager.getWildcardForAllFormats());
_fileChooser->launchAsync(fileFlags, load);
}

auto MainComponent::setupAudioDevices() -> void
{
_formatManager.registerBasicFormats();
_deviceManager.initialiseWithDefaultDevices(0, 2);
_deviceManager.addAudioCallback(&_audioPlayer);
_audioPlayer.setSource(&_djPlayer);

auto file = juce::File{"/home/tobante/Downloads/Lars_Huismann_Echo.mp3"};
_djPlayer.loadFile(file);
}
5 changes: 4 additions & 1 deletion src/MainComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ struct MainComponent final : juce::Component
auto resized() -> void override;

private:
auto setAudioDevices() -> void;
auto loadFile() -> void;
auto setupAudioDevices() -> void;

juce::ThreadPool _threadPool{juce::SystemStats::getNumCpus()};

Expand All @@ -32,5 +33,7 @@ struct MainComponent final : juce::Component
ta::MainSection _jogWheel;
ta::Display _display{_formatManager, _djPlayer};

std::unique_ptr<juce::FileChooser> _fileChooser{};

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainComponent) // NOLINT
};
2 changes: 1 addition & 1 deletion src/UI/Section/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ auto Display::djPlayerFileAnalysisFinished(BeatTrackResult const& result) -> voi
return;
}

_bpm.setText("Error:" + result.errorMessage(), juce::sendNotification);
_bpm.setText("Error detecting BPM", juce::sendNotification);
}
} // namespace ta
8 changes: 7 additions & 1 deletion src/UI/Section/SideBarLeft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ namespace ta
SideBarLeft::SideBarLeft()
{
addAndMakeVisible(_placeholder);
addAndMakeVisible(_loadButton);
addAndMakeVisible(_cueButton);
addAndMakeVisible(_playButton);

_loadButton.onClick = [this]()
{
if (onLoadClicked) { onLoadClicked(); }
};
_cueButton.onClick = [this]()
{
if (onCueClicked) { onCueClicked(); }
Expand All @@ -32,7 +37,8 @@ auto SideBarLeft::resized() -> void
grid.templateRows = fillArray(Track(1_fr), 12);
grid.templateColumns = fillArray(Track(1_fr), 1);
grid.items.addArray({
GridItem(_placeholder).withArea(GridItem::Span(10), {}),
GridItem(_loadButton).withArea(GridItem::Span(1), {}),
GridItem(_placeholder).withArea(GridItem::Span(9), {}),
GridItem(_cueButton).withArea(GridItem::Span(1), {}),
GridItem(_playButton).withArea(GridItem::Span(1), {}),
});
Expand Down
2 changes: 2 additions & 0 deletions src/UI/Section/SideBarLeft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ struct SideBarLeft final : juce::Component

auto resized() -> void override;

std::function<void()> onLoadClicked{};
std::function<void()> onCueClicked{};
std::function<void()> onPlayClicked{};

private:
Placeholder _placeholder{"", juce::Colours::transparentBlack};
juce::TextButton _loadButton{"Load"};
juce::TextButton _cueButton{"Cue"};
juce::TextButton _playButton{"Play"};

Expand Down

0 comments on commit 69b0441

Please sign in to comment.