55 int samplesPerBlock,
double requiredSampleRate)
56 : mixer (mixerToUse), transportSource (ts, ownSource)
76 if (! transportSource->isPlaying())
92 : buffer (audioBuffer, ownBuffer),
93 playAcrossAllChannels (playOnAllChannels)
102 newPosition = newPosition %
static_cast<int64> (buffer->getNumSamples());
104 position =
jmin (buffer->getNumSamples(),
static_cast<int> (newPosition));
111 void setLooping (
bool shouldLoop)
override { looping = shouldLoop; }
121 const int bufferSize = buffer->getNumSamples();
122 const int samplesNeeded = bufferToFill.
numSamples;
123 const int samplesToCopy =
jmin (bufferSize - position, samplesNeeded);
125 if (samplesToCopy > 0)
127 int maxInChannels = buffer->getNumChannels();
130 if (! playAcrossAllChannels)
131 maxOutChannels =
jmin (maxOutChannels, maxInChannels);
133 for (
int i = 0; i < maxOutChannels; ++i)
135 i % maxInChannels, position, samplesToCopy);
138 position += samplesNeeded;
141 position %= bufferSize;
148 bool looping =
false, playAcrossAllChannels;
153SoundPlayer::SoundPlayer()
154 : sampleRate (44100.0), bufferSize (512)
156 formatManager.registerBasicFormats();
157 player.setSource (&mixer);
174 if (resourceData !=
nullptr && resourceSize > 0)
183 if (reader !=
nullptr)
189 if (buffer !=
nullptr)
195 if (audioSource !=
nullptr)
199 if (transport ==
nullptr)
201 if (deleteWhenFinished)
208 transport->
setSource (audioSource, 0,
nullptr, fileSampleRate);
209 deleteWhenFinished =
true;
220 if (deleteWhenFinished)
227 auto soundLength = (
int) sampleRate;
228 double frequency = 440.0;
229 float amplitude = 0.5f;
235 for (
int i = 0; i < soundLength; ++i)
236 newSound->setSample (0, i, amplitude * (
float)
std::sin (i * phasePerSample));
238 newSound->applyGainRamp (0, 0, soundLength / 10, 0.0f, 1.0f);
239 newSound->applyGainRamp (0, soundLength - soundLength / 4, soundLength / 4, 1.0f, 0.0f);
241 play (newSound,
true,
true);
246 int numInputChannels,
247 float*
const* outputChannelData,
248 int numOutputChannels,
253 outputChannelData, numOutputChannels,
254 numSamples, context);
259 if (device !=
nullptr)
void setNextReadPosition(int64 newPosition) override
Tells the stream to move to a new position.
void releaseResources() override
Allows the source to release anything it no longer needs after playback has stopped.
void setLooping(bool shouldLoop) override
Tells the source whether you'd like it to play in a loop.
void prepareToPlay(int, double) override
Tells the source to prepare for playing.
int64 getNextReadPosition() const override
Returns the position from which the next block will be returned.
void getNextAudioBlock(const AudioSourceChannelInfo &bufferToFill) override
Called repeatedly to fetch subsequent blocks of audio data.
int64 getTotalLength() const override
Returns the total length of the stream (in samples).
bool isLooping() const override
Returns true if this source is actually playing in a loop.
A multi-channel buffer containing floating point audio samples.
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
void copyFrom(int destChannel, int destStartSample, const AudioBuffer &source, int sourceChannel, int sourceStartSample, int numSamples) noexcept
Copies samples from another buffer to this one.
virtual void audioDeviceError(const String &errorMessage)
This can be overridden to be told if the device generates an error while operating.
Base class for an audio device with synchronised input and output channels.
virtual double getCurrentSampleRate()=0
Returns the sample rate that the device is currently using.
virtual int getCurrentBufferSizeSamples()=0
Returns the buffer size that the device is currently using.
void setSource(AudioSource *newSource)
Changes the current audio source to play from.
void audioDeviceIOCallbackWithContext(const float *const *inputChannelData, int totalNumInputChannels, float *const *outputChannelData, int totalNumOutputChannels, int numSamples, const AudioIODeviceCallbackContext &context) override
Implementation of the AudioIODeviceCallbackWithContext method.
void audioDeviceStopped() override
Implementation of the AudioIODeviceCallback method.
void audioDeviceAboutToStart(AudioIODevice *device) override
Implementation of the AudioIODeviceCallback method.
An AudioSource that takes a PositionableAudioSource and allows it to be played, stopped,...
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Implementation of the AudioSource method.
void setSource(PositionableAudioSource *newSource, int readAheadBufferSize=0, TimeSliceThread *readAheadThread=nullptr, double sourceSampleRateToCorrectFor=0.0, int maxNumChannels=2)
Sets the reader that is being used as the input source.
void start()
Starts playing (if a source has been selected).
Represents a local file or directory.
bool existsAsFile() const
Checks whether the file exists and is a file rather than a directory.
An AudioSource that mixes together the output of a set of other AudioSources.
void removeInputSource(AudioSource *input)
Removes an input source.
void removeAllInputs()
Removes all the input sources.
void addInputSource(AudioSource *newInput, bool deleteWhenRemoved)
Adds an input source to the mixer.
Holds a pointer to an object which can optionally be deleted when this pointer goes out of scope.
A type of AudioSource which can be repositioned.
void audioDeviceAboutToStart(AudioIODevice *) override
Called to indicate that the device is about to start calling back.
void play(const File &file)
Plays a sound from a file.
~SoundPlayer() override
Destructor.
void audioDeviceStopped() override
Called to indicate that the device has stopped.
void audioDeviceError(const String &errorMessage) override
This can be overridden to be told if the device generates an error while operating.
void playTestSound()
Plays a beep through the current audio device.
void audioDeviceIOCallbackWithContext(const float *const *, int, float *const *, int, int, const AudioIODeviceCallbackContext &) override
Processes a block of incoming and outgoing audio data.
Makes repeated callbacks to a virtual method at a specified time interval.
void startTimerHz(int timerFrequencyHz) noexcept
Starts the timer with an interval specified in Hertz.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
long long int64
A platform-independent 64-bit integer type.
Additional information that may be passed to the AudioIODeviceCallback.
Used by AudioSource::getNextAudioBlock().
int numSamples
The number of samples in the buffer which the callback is expected to fill with data.
void clearActiveBufferRegion() const
Convenient method to clear the buffer if the source is not producing any data.
AudioBuffer< float > * buffer
The destination buffer to fill with audio data.
int startSample
The first sample in the buffer from which the callback is expected to write data.
void timerCallback() override
The user-defined callback routine that actually gets called periodically.
Commonly used mathematical constants.