1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| class SimpleThumbnailComponent : public juce::Component, private juce::ChangeListener { public: SimpleThumbnailComponent(int sourceSamplesPerThumbnailSample, juce::AudioFormatManager& formatManager, juce::AudioThumbnailCache& cache) : thumbnail(sourceSamplesPerThumbnailSample, formatManager, cache) { thumbnail.addChangeListener(this); }
void paint(juce::Graphics& g) override { juce::Rectangle<int> thumbnailBounds(10, 100, getWidth() - 20, getHeight() - 120);
if (thumbnail.getNumChannels() == 0) paintIfNoFileLoaded(g, thumbnailBounds); else paintIfFileLoaded(g, thumbnailBounds); }
void thumbnailChanged() { repaint(); }
void paintIfNoFileLoaded(juce::Graphics& g, const juce::Rectangle<int>& thumbnailBounds) { g.setColour(juce::Colours::darkgrey); g.fillRect(thumbnailBounds); g.setColour(juce::Colours::white); g.drawFittedText("No File Loaded", thumbnailBounds, juce::Justification::centred, 1); }
void paintIfFileLoaded(juce::Graphics& g, const juce::Rectangle<int>& thumbnailBounds) { g.setColour(juce::Colours::white); g.fillRect(thumbnailBounds);
g.setColour(juce::Colours::red);
auto audioLength = (float)thumbnail.getTotalLength(); thumbnail.drawChannels(g, thumbnailBounds, 0.0, audioLength, 1.0f); }
void changeListenerCallback(juce::ChangeBroadcaster* source) override { if (source == &thumbnail) thumbnailChanged(); }
void setFile(const juce::File& file) { thumbnail.setSource(new juce::FileInputSource(file)); }
private:
juce::AudioThumbnail thumbnail;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SimpleThumbnailComponent)
};
class MainContentComponent : public juce::AudioAppComponent, public juce::ChangeListener, private juce::Timer { public: MainContentComponent() : state (Stopped), thumbnailCache (5), thumbnailComp (512, formatManager, thumbnailCache) { addAndMakeVisible (&openButton); openButton.setButtonText ("Open..."); openButton.onClick = [this] { openButtonClicked(); };
addAndMakeVisible (&playButton); playButton.setButtonText ("Play"); playButton.onClick = [this] { playButtonClicked(); }; playButton.setColour (juce::TextButton::buttonColourId, juce::Colours::green); playButton.setEnabled (false);
addAndMakeVisible (&stopButton); stopButton.setButtonText ("Stop"); stopButton.onClick = [this] { stopButtonClicked(); }; stopButton.setColour (juce::TextButton::buttonColourId, juce::Colours::red); stopButton.setEnabled (false);
addAndMakeVisible(&thumbnailComp);
setSize (600, 400);
formatManager.registerBasicFormats(); transportSource.addChangeListener (this); setAudioChannels (2, 2);
}
~MainContentComponent() override { shutdownAudio(); }
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override { transportSource.prepareToPlay (samplesPerBlockExpected, sampleRate); }
void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override { if (readerSource.get() == nullptr) bufferToFill.clearActiveBufferRegion(); else transportSource.getNextAudioBlock (bufferToFill); }
void releaseResources() override { transportSource.releaseResources(); }
void resized() override { openButton.setBounds (10, 10, getWidth() - 20, 20); playButton.setBounds (10, 40, getWidth() - 20, 20); stopButton.setBounds (10, 70, getWidth() - 20, 20); juce::Rectangle<int> thumbnailBounds(10, 100, getWidth() - 20, getHeight() - 120); thumbnailComp.setBounds(thumbnailBounds); }
void changeListenerCallback (juce::ChangeBroadcaster* source) override { if (source == &transportSource) transportSourceChanged(); }
void timerCallback() override { ; }
private: enum TransportState { Stopped, Starting, Playing, Stopping };
void changeState (TransportState newState) { if (state != newState) { state = newState;
switch (state) { case Stopped: stopButton.setEnabled (false); playButton.setEnabled (true); transportSource.setPosition (0.0); break;
case Starting: playButton.setEnabled (false); transportSource.start(); break;
case Playing: stopButton.setEnabled (true); break;
case Stopping: transportSource.stop(); break;
default: jassertfalse; break; } } }
void transportSourceChanged() { changeState (transportSource.isPlaying() ? Playing : Stopped); } void openButtonClicked() { chooser = std::make_unique<juce::FileChooser> ("Select a Wave file to play...", juce::File{}, "*.wav"); auto chooserFlags = juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles;
chooser->launchAsync (chooserFlags, [this] (const juce::FileChooser& fc) { auto file = fc.getResult();
if (file != juce::File{}) { auto* reader = formatManager.createReaderFor (file);
if (reader != nullptr) { auto newSource = std::make_unique<juce::AudioFormatReaderSource> (reader, true); transportSource.setSource (newSource.get(), 0, nullptr, reader->sampleRate); playButton.setEnabled (true); thumbnailComp.setFile(file); readerSource.reset (newSource.release()); } } }); }
void playButtonClicked() { changeState (Starting); }
void stopButtonClicked() { changeState (Stopping); }
juce::TextButton openButton; juce::TextButton playButton; juce::TextButton stopButton;
std::unique_ptr<juce::FileChooser> chooser;
juce::AudioFormatManager formatManager; std::unique_ptr<juce::AudioFormatReaderSource> readerSource; juce::AudioTransportSource transportSource; TransportState state; juce::AudioThumbnailCache thumbnailCache; SimpleThumbnailComponent thumbnailComp; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent) };
|