From 1635ab06c278f66b94e0fd12e5e08358f4bf00ec Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Mon, 10 Feb 2014 17:16:33 -0600 Subject: [PATCH] Fixed bug in initialization of AudioReaderSource, and a bug in the AudioPlaybackThread which was causing the thread to prematurely end. --- src/AudioReaderSource.cpp | 21 ++++++++++++--------- src/Qt/AudioPlaybackThread.cpp | 22 +++++++++++++++------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/AudioReaderSource.cpp b/src/AudioReaderSource.cpp index 3c764503..1d5e70d6 100644 --- a/src/AudioReaderSource.cpp +++ b/src/AudioReaderSource.cpp @@ -52,7 +52,7 @@ AudioReaderSource::~AudioReaderSource() // Get more samples from the reader void AudioReaderSource::GetMoreSamplesFromReader() { - + cout << "GetMoreSamplesFromReader" << endl; // Determine the amount of samples needed to fill up this buffer int amount_needed = position; // replace these used samples int amount_remaining = size - amount_needed; // these are unused samples, and need to be carried forward @@ -131,34 +131,34 @@ void AudioReaderSource::GetMoreSamplesFromReader() { // Get the next block of audio samples void AudioReaderSource::getNextAudioBlock (const AudioSourceChannelInfo& info) { + cout << "getNextAudioBlock" << endl; int buffer_samples = buffer->getNumSamples(); int buffer_channels = buffer->getNumChannels(); if (info.numSamples > 0) { - int start = position; int number_to_copy = 0; // Do we need more samples? if ((reader && reader->IsOpen() && !frame) or - (reader && reader->IsOpen() && buffer_samples - start < info.numSamples)) + (reader && reader->IsOpen() && buffer_samples - position < info.numSamples)) // Refill buffer from reader GetMoreSamplesFromReader(); // Determine how many samples to copy - if (start + info.numSamples <= buffer_samples) + if (position + info.numSamples <= buffer_samples) { // copy the full amount requested number_to_copy = info.numSamples; } - else if (start > buffer_samples) + else if (position > buffer_samples) { // copy nothing number_to_copy = 0; } - else if (buffer_samples - start > 0) + else if (buffer_samples - position > 0) { // only copy what is left in the buffer - number_to_copy = buffer_samples - start; + number_to_copy = buffer_samples - position; } else { @@ -171,7 +171,7 @@ void AudioReaderSource::getNextAudioBlock (const AudioSourceChannelInfo& info) { // Loop through each channel and copy some samples for (int channel = 0; channel < buffer_channels; channel++) - info.buffer->copyFrom(channel, info.startSample, *buffer, channel, start, number_to_copy); + info.buffer->copyFrom(channel, info.startSample, *buffer, channel, position, number_to_copy); // Update the position of this audio source position += number_to_copy; @@ -205,7 +205,10 @@ long long AudioReaderSource::getNextReadPosition() const long long AudioReaderSource::getTotalLength() const { // Get the length - return buffer->getNumSamples(); + if (reader) + return reader->info.sample_rate * reader->info.duration; + else + return 0; } // Determines if this audio source should repeat when it reaches the end diff --git a/src/Qt/AudioPlaybackThread.cpp b/src/Qt/AudioPlaybackThread.cpp index a8e06575..04dc3ce9 100644 --- a/src/Qt/AudioPlaybackThread.cpp +++ b/src/Qt/AudioPlaybackThread.cpp @@ -64,7 +64,7 @@ namespace openshot { sampleRate = reader->info.sample_rate; numChannels = reader->info.channels; - source = new AudioReaderSource(reader, 10000, 1024*5); + source = new AudioReaderSource(reader, 1, 10000); } tr1::shared_ptr AudioPlaybackThread::getFrame() @@ -75,21 +75,21 @@ namespace openshot void AudioPlaybackThread::run() { + // Init audio device audioDeviceManager.initialise ( 0, /* number of input channels */ - 2, /* number of output channels */ + numChannels, /* number of output channels */ 0, /* no XML settings.. */ true /* select default device on failure */); + + // Add callback audioDeviceManager.addAudioCallback(&player); - mixer.addInputSource(&transport, false); - player.setSource(&mixer); // Create TimeSliceThread for audio buffering SafeTimeSliceThread thread("audio-buffer"); - - // Start thread thread.startThread(); + // Connect source to transport transport.setSource( source, 10000, // tells it to buffer this many samples ahead @@ -98,9 +98,17 @@ namespace openshot numChannels); transport.setPosition(0); transport.setGain(1.0); + + // Connect transport to mixer and player + mixer.addInputSource(&transport, false); + player.setSource(&mixer); + + cout << "starting transport" << endl; transport.start(); - while (!threadShouldExit() && transport.isPlaying()) { + //TODO: re-add this code !threadShouldExit() && + while (transport.isPlaying()) { + cout << "... still playing" << endl; sleep(1); }