1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Moved simple sound player to audio_utils module

This commit is contained in:
hogliux 2016-10-18 18:36:32 +01:00
parent 1fcae3675c
commit a347689d96
7 changed files with 462 additions and 268 deletions

View file

@ -0,0 +1,273 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
//==============================================================================
// This is an AudioTransportSource which will own it's assigned source
struct AudioSourceOwningTransportSource : public AudioTransportSource
{
AudioSourceOwningTransportSource (PositionableAudioSource* s) : source (s)
{
AudioTransportSource::setSource (s);
}
~AudioSourceOwningTransportSource()
{
setSource (nullptr);
}
private:
ScopedPointer<PositionableAudioSource> source;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioSourceOwningTransportSource)
};
//==============================================================================
// An AudioSourcePlayer which will remove itself from the AudioDeviceManager's
// callback list once it finishes playing its source
struct AutoRemovingTransportSource : public AudioTransportSource, private Timer
{
AutoRemovingTransportSource (MixerAudioSource& mixerToUse, AudioTransportSource* ts, bool ownSource,
int samplesPerBlock, double sampleRate)
: mixer (mixerToUse), transportSource (ts, ownSource)
{
jassert (ts != nullptr);
setSource (transportSource);
prepareToPlay (samplesPerBlock, sampleRate);
start();
mixer.addInputSource (this, true);
startTimerHz (10);
}
~AutoRemovingTransportSource()
{
setSource (nullptr);
}
void timerCallback() override
{
if (! transportSource->isPlaying())
mixer.removeInputSource (this);
}
private:
MixerAudioSource& mixer;
OptionalScopedPointer<AudioTransportSource> transportSource;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AutoRemovingTransportSource)
};
// An AudioSource which simply outputs a buffer
class AudioSampleBufferSource : public PositionableAudioSource
{
public:
AudioSampleBufferSource (AudioSampleBuffer* audioBuffer, bool ownBuffer, bool playOnAllChannels)
: buffer (audioBuffer, ownBuffer),
position (0), looping (false),
playAcrossAllChannels (playOnAllChannels)
{}
//==============================================================================
void setNextReadPosition (int64 newPosition) override
{
jassert (newPosition >= 0);
if (looping)
newPosition = newPosition % static_cast<int64> (buffer->getNumSamples());
position = jmin (buffer->getNumSamples(), static_cast<int> (newPosition));
}
int64 getNextReadPosition() const override { return static_cast<int64> (position); }
int64 getTotalLength() const override { return static_cast<int64> (buffer->getNumSamples()); }
bool isLooping() const override { return looping; }
void setLooping (bool shouldLoop) override { looping = shouldLoop; }
//==============================================================================
void prepareToPlay (int, double) override {}
void releaseResources() override {}
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
bufferToFill.clearActiveBufferRegion();
const int bufferSize = buffer->getNumSamples();
const int samplesNeeded = bufferToFill.numSamples;
const int samplesToCopy = jmin (bufferSize - position, samplesNeeded);
if (samplesToCopy > 0)
{
int maxInChannels = buffer->getNumChannels();
int maxOutChannels = bufferToFill.buffer->getNumChannels();
if (! playAcrossAllChannels)
maxOutChannels = jmin (maxOutChannels, maxInChannels);
for (int i = 0; i < maxOutChannels; ++i)
bufferToFill.buffer->copyFrom (i, bufferToFill.startSample, *buffer,
i % maxInChannels, position, samplesToCopy);
}
position += samplesNeeded;
if (looping)
position %= bufferSize;
}
private:
//==============================================================================
OptionalScopedPointer<AudioSampleBuffer> buffer;
int position;
bool looping, playAcrossAllChannels;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioSampleBufferSource)
};
SoundPlayer::SoundPlayer()
: sampleRate (44100.0), bufferSize (512)
{
formatManager.registerBasicFormats();
player.setSource (&mixer);
}
SoundPlayer::~SoundPlayer()
{
mixer.removeAllInputs();
player.setSource (nullptr);
}
void SoundPlayer::play (const File& file)
{
if (file.existsAsFile())
play (formatManager.createReaderFor (file), true);
}
void SoundPlayer::play (const void* resourceData, size_t resourceSize)
{
if (resourceData != nullptr && resourceSize > 0)
{
MemoryInputStream* mem = new MemoryInputStream (resourceData, resourceSize, false);
play (formatManager.createReaderFor (mem), true);
}
}
void SoundPlayer::play (AudioFormatReader* reader, bool deleteWhenFinished)
{
if (reader != nullptr)
play (new AudioFormatReaderSource (reader, deleteWhenFinished), true);
}
void SoundPlayer::play (AudioSampleBuffer* buffer, bool deleteWhenFinished, bool playOnAllOutputChannels)
{
if (buffer != nullptr)
play (new AudioSampleBufferSource (buffer, deleteWhenFinished, playOnAllOutputChannels), true);
}
void SoundPlayer::play (PositionableAudioSource* audioSource, bool deleteWhenFinished)
{
if (audioSource != nullptr)
{
AudioTransportSource* transport = dynamic_cast<AudioTransportSource*> (audioSource);
if (transport == nullptr)
{
if (deleteWhenFinished)
{
transport = new AudioSourceOwningTransportSource (audioSource);
}
else
{
transport = new AudioTransportSource();
transport->setSource (audioSource);
deleteWhenFinished = true;
}
}
transport->start();
transport->prepareToPlay (bufferSize, sampleRate);
new AutoRemovingTransportSource (mixer, transport, deleteWhenFinished, bufferSize, sampleRate);
}
else
{
if (deleteWhenFinished)
delete audioSource;
}
}
void SoundPlayer::playTestSound()
{
const int soundLength = (int) sampleRate;
const double frequency = 440.0;
const float amplitude = 0.5f;
const double phasePerSample = double_Pi * 2.0 / (sampleRate / frequency);
AudioSampleBuffer* newSound = new AudioSampleBuffer (1, soundLength);
for (int i = 0; i < soundLength; ++i)
newSound->setSample (0, i, amplitude * (float) std::sin (i * phasePerSample));
newSound->applyGainRamp (0, 0, soundLength / 10, 0.0f, 1.0f);
newSound->applyGainRamp (0, soundLength - soundLength / 4, soundLength / 4, 1.0f, 0.0f);
play (newSound, true, true);
}
//==============================================================================
void SoundPlayer::audioDeviceIOCallback (const float** inputChannelData,
int numInputChannels,
float** outputChannelData,
int numOutputChannels,
int numSamples)
{
player.audioDeviceIOCallback (inputChannelData, numInputChannels,
outputChannelData, numOutputChannels,
numSamples);
}
void SoundPlayer::audioDeviceAboutToStart (AudioIODevice* device)
{
if (device != nullptr)
{
sampleRate = device->getCurrentSampleRate();
bufferSize = device->getCurrentBufferSizeSamples();
}
player.audioDeviceAboutToStart (device);
}
void SoundPlayer::audioDeviceStopped()
{
player.audioDeviceStopped();
}
void SoundPlayer::audioDeviceError (const String& errorMessage)
{
player.audioDeviceError (errorMessage);
}

View file

@ -0,0 +1,130 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_SOUNDPLAYER_H_INCLUDED
#define JUCE_SOUNDPLAYER_H_INCLUDED
//==============================================================================
/**
A simple sound player that you can add to the AudioDeviceManager to play
simple sounds.
@see AudioProcessor, AudioProcessorGraph
*/
class JUCE_API SoundPlayer : public AudioIODeviceCallback
{
public:
//==============================================================================
SoundPlayer ();
/** Destructor. */
virtual ~SoundPlayer();
//==============================================================================
/** Plays a sound from a file. */
void play (const File& file);
/** Convenient method to play sound from a JUCE resource. */
void play (const void* resourceData, size_t resourceSize);
/** Plays the sound from an audio format reader.
If deleteWhenFinished is true then the format reader will be
automatically deleted once the sound has finished playing.
*/
void play (AudioFormatReader* buffer, bool deleteWhenFinished = false);
/** Plays the sound from a positionable audio source.
This will output the sound coming from a positionable audio source.
This gives you slightly more control over the sound playback compared
to the other playSound methods. For example, if you would like to
stop the sound prematurely you can call this method with a
TransportAudioSource and then call audioSource->stop. Note that,
you must call audioSource->start to start the playback, if your
audioSource is a TransportAudioSource.
The audio device manager will not hold any references to this audio
source once the audio source has stopped playing for any reason,
for example when the sound has finished playing or when you have
called audioSource->stop. Therefore, calling audioSource->start() on
a finished audioSource will not restart the sound again. If this is
desired simply call playSound with the same audioSource again.
@param audioSource the audio source to play
@param deleteWhenFinished If this is true then the audio source will
be deleted once the device manager has finished
playing.
*/
void play (PositionableAudioSource* audioSource, bool deleteWhenFinished = false);
/** Plays the sound from an audio sample buffer.
This will output the sound contained in an audio sample buffer. If
deleteWhenFinished is true then the audio sample buffer will be
automatically deleted once the sound has finished playing.
If playOnAllOutputChannels is true, then if there are more output channels
than buffer channels, then the ones that are available will be re-used on
multiple outputs so that something is sent to all output channels. If it
is false, then the buffer will just be played on the first output channels.
*/
void play (AudioSampleBuffer* buffer,
bool deleteWhenFinished = false,
bool playOnAllOutputChannels = false);
/** Plays a beep through the current audio device.
This is here to allow the audio setup UI panels to easily include a "test"
button so that the user can check where the audio is coming from.
*/
void playTestSound();
//==============================================================================
/** @internal */
void audioDeviceIOCallback (const float**, int, float**, int, int) override;
/** @internal */
void audioDeviceAboutToStart (AudioIODevice*) override;
/** @internal */
void audioDeviceStopped() override;
/** @internal */
void audioDeviceError (const String& errorMessage) override;
private:
//==============================================================================
AudioFormatManager formatManager;
AudioSourcePlayer player;
MixerAudioSource mixer;
OwnedArray<AudioSource> sources;
//==============================================================================
double sampleRate;
int bufferSize;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SoundPlayer)
};
#endif // JUCE_SOUNDPLAYER_H_INCLUDED