Professional Documents
Culture Documents
==============================================================================
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
/*******************************************************************************
The block below describes the properties of this PIP. A PIP is a short snippet
of code that can be read by the Projucer and used to generate a JUCE project.
BEGIN_JUCE_PIP_METADATA
name: PlayingSoundFilesTutorial
version: 1.0.0
vendor: JUCE
website: http://juce.com
description: Plays audio files.
type: Component
mainClass: MainContentComponent
useLocalCopy: 1
END_JUCE_PIP_METADATA
*******************************************************************************/
#pragma once
//==============================================================================
class MainContentComponent : public juce::AudioAppComponent,
public juce::ChangeListener
{
public:
MainContentComponent()
: state (Stopped)
{
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);
formatManager.registerBasicFormats(); // [1]
transportSource.addChangeListener (this); // [2]
~MainContentComponent() override
{
shutdownAudio();
}
transportSource.getNextAudioBlock (bufferToFill);
}
private:
enum TransportState
{
Stopped,
Starting,
Playing,
Stopping
};
switch (state)
{
case Stopped: // [3]
stopButton.setEnabled (false);
playButton.setEnabled (true);
transportSource.setPosition (0.0);
break;
void openButtonClicked()
{
chooser = std::make_unique<juce::FileChooser> ("Select a Wave file to
play...",
juce::File{},
"*.wav");
// [7]
auto chooserFlags = juce::FileBrowserComponent::openMode
| juce::FileBrowserComponent::canSelectFiles;
chooser->launchAsync (chooserFlags, [this] (const FileChooser& fc)
// [8]
{
auto file = fc.getResult();
if (file != File{})
// [9]
{
auto* reader = formatManager.createReaderFor (file);
// [10]
if (reader != nullptr)
{
auto newSource =
std::make_unique<juce::AudioFormatReaderSource> (reader, true); // [11]
transportSource.setSource (newSource.get(), 0, nullptr, reader-
>sampleRate); // [12]
playButton.setEnabled (true);
// [13]
readerSource.reset (newSource.release());
// [14]
}
}
});
}
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_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};