28 #include "../include/AudioReaderSource.h"
31 using namespace openshot;
34 AudioReaderSource::AudioReaderSource(
ReaderBase *audio_reader, int64 starting_frame_number,
int buffer_size)
35 : reader(audio_reader), frame_number(starting_frame_number), original_frame_number(starting_frame_number),
36 size(buffer_size), position(0), frame_position(0), estimated_frame(0), speed(1) {
39 buffer =
new juce::AudioSampleBuffer(reader->
info.
channels, size);
54 void AudioReaderSource::GetMoreSamplesFromReader()
57 int amount_needed = position;
58 int amount_remaining = size - amount_needed;
66 estimated_frame = frame_number;
69 juce::AudioSampleBuffer *new_buffer =
new juce::AudioSampleBuffer(reader->
info.
channels, size);
73 if (amount_remaining > 0) {
74 for (
int channel = 0; channel < buffer->getNumChannels(); channel++)
75 new_buffer->addFrom(channel, 0, *buffer, channel, position, amount_remaining);
77 position = amount_remaining;
83 while (amount_needed > 0 && speed == 1 && frame_number >= 1 && frame_number <= reader->info.video_length) {
86 if (frame_position == 0) {
89 frame = reader->
GetFrame(frame_number);
90 frame_number = frame_number + speed;
101 bool frame_completed =
false;
102 int amount_to_copy = 0;
104 amount_to_copy = frame->GetAudioSamplesCount() - frame_position;
105 if (amount_to_copy > amount_needed) {
107 amount_to_copy = amount_needed;
111 amount_needed -= amount_to_copy;
112 frame_completed =
true;
117 for (
int channel = 0; channel < new_buffer->getNumChannels(); channel++)
118 new_buffer->addFrom(channel, position, *frame->GetAudioSampleBuffer(), channel, frame_position, amount_to_copy);
121 position += amount_to_copy;
127 frame_position += amount_to_copy;
140 juce::AudioSampleBuffer* AudioReaderSource::reverse_buffer(juce::AudioSampleBuffer* buffer)
142 int number_of_samples = buffer->getNumSamples();
143 int channels = buffer->getNumChannels();
146 AudioSampleBuffer *reversed =
new juce::AudioSampleBuffer(channels, number_of_samples);
149 for (
int channel = 0; channel < channels; channel++)
152 for (
int s = number_of_samples - 1; s >= 0; s--, n++)
153 reversed->getWritePointer(channel)[n] = buffer->getWritePointer(channel)[s];
159 for (
int channel = 0; channel < channels; channel++)
161 buffer->addFrom(channel, 0, reversed->getReadPointer(channel), number_of_samples, 1.0f);
173 int buffer_samples = buffer->getNumSamples();
174 int buffer_channels = buffer->getNumChannels();
176 if (info.numSamples > 0) {
177 int number_to_copy = 0;
182 if ((reader && reader->
IsOpen() && !frame) or
183 (reader && reader->
IsOpen() && buffer_samples - position < info.numSamples))
185 GetMoreSamplesFromReader();
188 info.buffer->clear();
193 if (position + info.numSamples <= buffer_samples)
196 number_to_copy = info.numSamples;
198 else if (position > buffer_samples)
203 else if (buffer_samples - position > 0)
206 number_to_copy = buffer_samples - position;
216 if (number_to_copy > 0)
219 for (
int channel = 0; channel < buffer_channels; channel++)
220 info.buffer->copyFrom(channel, info.startSample, *buffer, channel, position, number_to_copy);
223 position += number_to_copy;
228 estimated_frame += double(info.numSamples) / double(estimated_samples_per_frame);
242 if (newPosition >= 0 && newPosition < buffer->getNumSamples())
243 position = newPosition;
280 buffer = audio_buffer;
void setBuffer(AudioSampleBuffer *audio_buffer)
Update the internal buffer used by this source.
float duration
Length of time (in seconds)
void getNextAudioBlock(const AudioSourceChannelInfo &info)
Get the next block of audio samples.
void setLooping(bool shouldLoop)
Set if this audio source should repeat when it reaches the end.
This abstract class is the base class, used by all readers in libopenshot.
void releaseResources()
Release all resources.
~AudioReaderSource()
Destructor.
Exception when a reader is closed, and a frame is requested.
bool isLooping() const
Determines if this audio source should repeat when it reaches the end.
long long getNextReadPosition() const
Get the next read position of this source.
void setNextReadPosition(long long newPosition)
Set the next read position of this source.
ReaderInfo info
Information about the current media file.
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Exception for frames that are out of bounds.
virtual tr1::shared_ptr< Frame > GetFrame(long int number)=0
void prepareToPlay(int, double)
Prepare to play this audio source.
int channels
The number of audio channels used in the audio stream.
long long getTotalLength() const
Get the total length (in samples) of this audio source.
int GetSamplesPerFrame(Fraction fps, int sample_rate, int channels)
Calculate the # of samples per video frame (for the current frame number)
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Exception when too many seek attempts happen.
virtual bool IsOpen()=0
A thread safe version of GetFrame.