2014-01-27 19:03:46 +08:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for AudioPlaybackThread class
|
|
|
|
|
* @author Duzy Chan <code@duzy.info>
|
2014-03-23 01:12:29 -05:00
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org> *
|
2014-01-27 19:03:46 +08:00
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2014-03-30 23:21:30 -05:00
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "AudioPlaybackThread.h"
|
2021-10-27 14:34:05 -04:00
|
|
|
#include "Settings.h"
|
2014-01-27 19:03:46 +08:00
|
|
|
|
Audio: New device name lookup
- A new AudioDevices class replaces the AudioDeviceInfo struct.
It has a single method, getNames(), which:
* creates an AudioDeviceManager (NOT using the singleton)
* scans for available devices
* returns the results as a std::vector containing
std::pair<std::string, std::string> objects
(The AudioDevices device manager is never initialize()d, so
no devices are opened; it should be safe to use even DURING
playback, without disruption.)
By using STL containers (rather than a custom struct) to return
the results, Python is able to consume the output as a native
list of tuples.
AudioDeviceInfo is still present for compatibility, but deprecated.
- Eliminated some unnecessary conversions (like):
* calls to std::string::c_str, when passing to juce::String.
juce::String accepts std::string directly.
* calls to juce::String::toRawUTF8, when creating std::string.
There's a juce::String::ToStdString, which is better.
2021-08-24 13:03:46 -04:00
|
|
|
#include "../ReaderBase.h"
|
|
|
|
|
#include "../RendererBase.h"
|
|
|
|
|
#include "../AudioReaderSource.h"
|
|
|
|
|
#include "../AudioDevices.h"
|
|
|
|
|
#include "../Settings.h"
|
|
|
|
|
|
2020-09-02 02:07:54 -04:00
|
|
|
#include <thread> // for std::this_thread::sleep_for
|
|
|
|
|
#include <chrono> // for std::chrono::milliseconds
|
|
|
|
|
|
2021-10-27 14:34:05 -04:00
|
|
|
using namespace juce;
|
|
|
|
|
|
2014-01-27 19:03:46 +08:00
|
|
|
namespace openshot
|
|
|
|
|
{
|
2017-03-21 10:56:19 -05:00
|
|
|
|
|
|
|
|
// Global reference to device manager
|
|
|
|
|
AudioDeviceManagerSingleton *AudioDeviceManagerSingleton::m_pInstance = NULL;
|
|
|
|
|
|
|
|
|
|
// Create or Get an instance of the device manager singleton
|
2019-04-23 16:45:02 -05:00
|
|
|
AudioDeviceManagerSingleton *AudioDeviceManagerSingleton::Instance()
|
2017-03-21 10:56:19 -05:00
|
|
|
{
|
|
|
|
|
if (!m_pInstance) {
|
|
|
|
|
// Create the actual instance of device manager only once
|
|
|
|
|
m_pInstance = new AudioDeviceManagerSingleton;
|
Audio: New device name lookup
- A new AudioDevices class replaces the AudioDeviceInfo struct.
It has a single method, getNames(), which:
* creates an AudioDeviceManager (NOT using the singleton)
* scans for available devices
* returns the results as a std::vector containing
std::pair<std::string, std::string> objects
(The AudioDevices device manager is never initialize()d, so
no devices are opened; it should be safe to use even DURING
playback, without disruption.)
By using STL containers (rather than a custom struct) to return
the results, Python is able to consume the output as a native
list of tuples.
AudioDeviceInfo is still present for compatibility, but deprecated.
- Eliminated some unnecessary conversions (like):
* calls to std::string::c_str, when passing to juce::String.
juce::String accepts std::string directly.
* calls to juce::String::toRawUTF8, when creating std::string.
There's a juce::String::ToStdString, which is better.
2021-08-24 13:03:46 -04:00
|
|
|
auto* mgr = &m_pInstance->audioDeviceManager;
|
2017-03-26 23:51:03 -07:00
|
|
|
|
Audio: New device name lookup
- A new AudioDevices class replaces the AudioDeviceInfo struct.
It has a single method, getNames(), which:
* creates an AudioDeviceManager (NOT using the singleton)
* scans for available devices
* returns the results as a std::vector containing
std::pair<std::string, std::string> objects
(The AudioDevices device manager is never initialize()d, so
no devices are opened; it should be safe to use even DURING
playback, without disruption.)
By using STL containers (rather than a custom struct) to return
the results, Python is able to consume the output as a native
list of tuples.
AudioDeviceInfo is still present for compatibility, but deprecated.
- Eliminated some unnecessary conversions (like):
* calls to std::string::c_str, when passing to juce::String.
juce::String accepts std::string directly.
* calls to juce::String::toRawUTF8, when creating std::string.
There's a juce::String::ToStdString, which is better.
2021-08-24 13:03:46 -04:00
|
|
|
// Get preferred audio device name and type (if any)
|
|
|
|
|
auto selected_device = juce::String(
|
|
|
|
|
Settings::Instance()->PLAYBACK_AUDIO_DEVICE_NAME);
|
|
|
|
|
auto selected_type = juce::String(
|
|
|
|
|
Settings::Instance()->PLAYBACK_AUDIO_DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
if (selected_type.isEmpty() && !selected_device.isEmpty()) {
|
|
|
|
|
// Look up type for the selected device
|
|
|
|
|
for (const auto t : mgr->getAvailableDeviceTypes()) {
|
|
|
|
|
for (const auto n : t->getDeviceNames()) {
|
|
|
|
|
if (selected_device.trim().equalsIgnoreCase(n.trim())) {
|
|
|
|
|
selected_type = t->getTypeName();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!selected_type.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!selected_type.isEmpty())
|
|
|
|
|
m_pInstance->audioDeviceManager.setCurrentAudioDeviceType(selected_type, true);
|
2019-04-24 09:41:52 -05:00
|
|
|
|
2017-03-26 23:51:03 -07:00
|
|
|
// Initialize audio device only 1 time
|
2019-04-29 17:05:13 -05:00
|
|
|
juce::String audio_error = m_pInstance->audioDeviceManager.initialise (
|
Audio: New device name lookup
- A new AudioDevices class replaces the AudioDeviceInfo struct.
It has a single method, getNames(), which:
* creates an AudioDeviceManager (NOT using the singleton)
* scans for available devices
* returns the results as a std::vector containing
std::pair<std::string, std::string> objects
(The AudioDevices device manager is never initialize()d, so
no devices are opened; it should be safe to use even DURING
playback, without disruption.)
By using STL containers (rather than a custom struct) to return
the results, Python is able to consume the output as a native
list of tuples.
AudioDeviceInfo is still present for compatibility, but deprecated.
- Eliminated some unnecessary conversions (like):
* calls to std::string::c_str, when passing to juce::String.
juce::String accepts std::string directly.
* calls to juce::String::toRawUTF8, when creating std::string.
There's a juce::String::ToStdString, which is better.
2021-08-24 13:03:46 -04:00
|
|
|
0, // number of input channels
|
|
|
|
|
2, // number of output channels
|
|
|
|
|
nullptr, // no XML settings..
|
|
|
|
|
true, // select default device on failure
|
|
|
|
|
selected_device // preferredDefaultDeviceName
|
|
|
|
|
);
|
2019-04-04 00:55:47 -05:00
|
|
|
|
|
|
|
|
// Persist any errors detected
|
2019-04-29 17:05:13 -05:00
|
|
|
if (audio_error.isNotEmpty()) {
|
Audio: New device name lookup
- A new AudioDevices class replaces the AudioDeviceInfo struct.
It has a single method, getNames(), which:
* creates an AudioDeviceManager (NOT using the singleton)
* scans for available devices
* returns the results as a std::vector containing
std::pair<std::string, std::string> objects
(The AudioDevices device manager is never initialize()d, so
no devices are opened; it should be safe to use even DURING
playback, without disruption.)
By using STL containers (rather than a custom struct) to return
the results, Python is able to consume the output as a native
list of tuples.
AudioDeviceInfo is still present for compatibility, but deprecated.
- Eliminated some unnecessary conversions (like):
* calls to std::string::c_str, when passing to juce::String.
juce::String accepts std::string directly.
* calls to juce::String::toRawUTF8, when creating std::string.
There's a juce::String::ToStdString, which is better.
2021-08-24 13:03:46 -04:00
|
|
|
m_pInstance->initialise_error = audio_error.toStdString();
|
2019-04-04 00:55:47 -05:00
|
|
|
} else {
|
|
|
|
|
m_pInstance->initialise_error = "";
|
|
|
|
|
}
|
2017-03-21 10:56:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_pInstance;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 14:38:42 -05:00
|
|
|
// Close audio device
|
|
|
|
|
void AudioDeviceManagerSingleton::CloseAudioDevice()
|
|
|
|
|
{
|
|
|
|
|
// Close Audio Device
|
|
|
|
|
audioDeviceManager.closeAudioDevice();
|
|
|
|
|
audioDeviceManager.removeAllChangeListeners();
|
|
|
|
|
audioDeviceManager.dispatchPendingMessages();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-06 02:22:05 -06:00
|
|
|
// Constructor
|
2014-01-27 19:03:46 +08:00
|
|
|
AudioPlaybackThread::AudioPlaybackThread()
|
2019-09-21 00:14:32 -04:00
|
|
|
: juce::Thread("audio-playback")
|
2014-01-27 19:03:46 +08:00
|
|
|
, player()
|
|
|
|
|
, transport()
|
|
|
|
|
, mixer()
|
|
|
|
|
, source(NULL)
|
|
|
|
|
, sampleRate(0.0)
|
|
|
|
|
, numChannels(0)
|
2016-01-30 17:12:41 -06:00
|
|
|
, buffer_size(12000)
|
2014-05-24 16:07:42 -05:00
|
|
|
, is_playing(false)
|
2016-01-18 15:20:55 -06:00
|
|
|
, time_thread("audio-buffer")
|
2014-01-31 16:27:16 +08:00
|
|
|
{
|
2016-01-18 15:20:55 -06:00
|
|
|
}
|
2014-01-31 16:27:16 +08:00
|
|
|
|
2014-03-23 01:12:29 -05:00
|
|
|
// Destructor
|
2014-01-31 16:27:16 +08:00
|
|
|
AudioPlaybackThread::~AudioPlaybackThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 01:12:29 -05:00
|
|
|
// Set the reader object
|
2019-09-21 00:14:32 -04:00
|
|
|
void AudioPlaybackThread::Reader(openshot::ReaderBase *reader) {
|
2015-12-24 16:44:45 -06:00
|
|
|
if (source)
|
|
|
|
|
source->Reader(reader);
|
|
|
|
|
else {
|
|
|
|
|
// Create new audio source reader
|
|
|
|
|
source = new AudioReaderSource(reader, 1, buffer_size);
|
|
|
|
|
source->setLooping(true); // prevent this source from terminating when it reaches the end
|
2014-05-24 16:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set local vars
|
|
|
|
|
sampleRate = reader->info.sample_rate;
|
|
|
|
|
numChannels = reader->info.channels;
|
|
|
|
|
|
2016-01-18 15:20:55 -06:00
|
|
|
// TODO: Update transport or audio source's sample rate, incase the sample rate
|
|
|
|
|
// is different than the original Reader
|
|
|
|
|
|
2015-12-04 01:10:40 -06:00
|
|
|
// Mark as 'playing'
|
2014-05-24 16:07:42 -05:00
|
|
|
Play();
|
2015-12-24 16:44:45 -06:00
|
|
|
}
|
2014-01-31 16:27:16 +08:00
|
|
|
|
2014-03-23 01:12:29 -05:00
|
|
|
// Get the current frame object (which is filling the buffer)
|
2019-09-21 00:14:32 -04:00
|
|
|
std::shared_ptr<openshot::Frame> AudioPlaybackThread::getFrame()
|
2014-01-31 23:07:36 +08:00
|
|
|
{
|
|
|
|
|
if (source) return source->getFrame();
|
2019-09-21 00:14:32 -04:00
|
|
|
return std::shared_ptr<openshot::Frame>();
|
2014-01-31 23:07:36 +08:00
|
|
|
}
|
|
|
|
|
|
2014-03-23 01:12:29 -05:00
|
|
|
// Get the currently playing frame number
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t AudioPlaybackThread::getCurrentFramePosition()
|
2014-02-14 10:19:50 +08:00
|
|
|
{
|
|
|
|
|
return source ? source->getEstimatedFrame() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 01:12:29 -05:00
|
|
|
// Seek the audio thread
|
2017-09-28 16:03:01 -05:00
|
|
|
void AudioPlaybackThread::Seek(int64_t new_position)
|
2014-03-23 01:12:29 -05:00
|
|
|
{
|
|
|
|
|
source->Seek(new_position);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-24 16:07:42 -05:00
|
|
|
// Play the audio
|
|
|
|
|
void AudioPlaybackThread::Play() {
|
|
|
|
|
// Start playing
|
|
|
|
|
is_playing = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop the audio
|
|
|
|
|
void AudioPlaybackThread::Stop() {
|
|
|
|
|
// Stop playing
|
|
|
|
|
is_playing = false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 01:12:29 -05:00
|
|
|
// Start audio thread
|
2014-01-31 16:27:16 +08:00
|
|
|
void AudioPlaybackThread::run()
|
2014-01-27 19:03:46 +08:00
|
|
|
{
|
2014-05-24 16:07:42 -05:00
|
|
|
while (!threadShouldExit())
|
|
|
|
|
{
|
|
|
|
|
if (source && !transport.isPlaying() && is_playing) {
|
2014-02-10 17:16:33 -06:00
|
|
|
|
2017-03-26 23:51:03 -07:00
|
|
|
// Start new audio device (or get existing one)
|
2014-05-24 16:07:42 -05:00
|
|
|
// Add callback
|
2019-04-23 16:45:02 -05:00
|
|
|
AudioDeviceManagerSingleton::Instance()->audioDeviceManager.addAudioCallback(&player);
|
2014-01-31 16:27:16 +08:00
|
|
|
|
2014-05-24 16:07:42 -05:00
|
|
|
// Create TimeSliceThread for audio buffering
|
2016-01-18 15:20:55 -06:00
|
|
|
time_thread.startThread();
|
2014-02-10 17:16:33 -06:00
|
|
|
|
2014-05-24 16:07:42 -05:00
|
|
|
// Connect source to transport
|
|
|
|
|
transport.setSource(
|
|
|
|
|
source,
|
|
|
|
|
buffer_size, // tells it to buffer this many samples ahead
|
2016-01-18 15:20:55 -06:00
|
|
|
&time_thread,
|
2014-05-24 16:07:42 -05:00
|
|
|
sampleRate,
|
|
|
|
|
numChannels);
|
|
|
|
|
transport.setPosition(0);
|
|
|
|
|
transport.setGain(1.0);
|
2014-02-10 17:16:33 -06:00
|
|
|
|
2014-05-24 16:07:42 -05:00
|
|
|
// Connect transport to mixer and player
|
|
|
|
|
mixer.addInputSource(&transport, false);
|
|
|
|
|
player.setSource(&mixer);
|
2014-01-31 16:27:16 +08:00
|
|
|
|
2014-05-24 16:07:42 -05:00
|
|
|
// Start the transport
|
|
|
|
|
transport.start();
|
2014-01-31 16:27:16 +08:00
|
|
|
|
2015-02-04 23:56:43 -06:00
|
|
|
while (!threadShouldExit() && transport.isPlaying() && is_playing)
|
2020-09-02 02:07:54 -04:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
2015-12-24 16:44:45 -06:00
|
|
|
|
|
|
|
|
// Stop audio and shutdown transport
|
|
|
|
|
Stop();
|
|
|
|
|
transport.stop();
|
|
|
|
|
|
|
|
|
|
// Kill previous audio
|
|
|
|
|
transport.setSource(NULL);
|
|
|
|
|
|
|
|
|
|
player.setSource(NULL);
|
2019-04-23 16:45:02 -05:00
|
|
|
AudioDeviceManagerSingleton::Instance()->audioDeviceManager.removeAudioCallback(&player);
|
2015-12-24 16:44:45 -06:00
|
|
|
|
|
|
|
|
// Remove source
|
|
|
|
|
delete source;
|
|
|
|
|
source = NULL;
|
2014-05-24 16:07:42 -05:00
|
|
|
|
2015-12-04 01:10:40 -06:00
|
|
|
// Stop time slice thread
|
2016-01-18 15:20:55 -06:00
|
|
|
time_thread.stopThread(-1);
|
2015-12-04 01:10:40 -06:00
|
|
|
}
|
2014-05-24 16:07:42 -05:00
|
|
|
}
|
2014-03-23 01:12:29 -05:00
|
|
|
|
2014-01-27 19:03:46 +08:00
|
|
|
}
|
|
|
|
|
}
|