17 #include "../ReaderBase.h"
18 #include "../RendererBase.h"
19 #include "../AudioReaderSource.h"
20 #include "../AudioDevices.h"
21 #include "../Settings.h"
32 AudioDeviceManagerSingleton *AudioDeviceManagerSingleton::m_pInstance = NULL;
37 return AudioDeviceManagerSingleton::Instance(44100, 2);
43 static std::mutex mutex;
44 std::lock_guard<std::mutex> lock(mutex);
50 AudioIODevice *foundAudioIODevice = NULL;
51 m_pInstance->initialise_error =
"";
52 m_pInstance->currentAudioDevice.name =
"";
53 m_pInstance->currentAudioDevice.type =
"";
54 m_pInstance->defaultSampleRate = 0.0;
58 Settings::Instance()->PLAYBACK_AUDIO_DEVICE_NAME};
61 if (requested_device.
type.isEmpty() && !requested_device.
name.isEmpty()) {
62 for (
const auto t : mgr->getAvailableDeviceTypes()) {
64 for (
const auto n : t->getDeviceNames()) {
65 if (requested_device.
name.trim().equalsIgnoreCase(n.trim())) {
66 requested_device.
type = t->getTypeName();
74 std::vector<openshot::AudioDeviceInfo> devices{ { requested_device } };
75 for (
const auto t : mgr->getAvailableDeviceTypes()) {
77 for (
const auto n : t->getDeviceNames()) {
79 devices.push_back(device);
84 for (
auto attempt_device : devices) {
85 m_pInstance->currentAudioDevice = attempt_device;
88 m_pInstance->audioDeviceManager.initialiseWithDefaultDevices(0, channels);
91 if (!attempt_device.type.isEmpty()) {
92 m_pInstance->audioDeviceManager.setCurrentAudioDeviceType(attempt_device.type,
true);
96 AudioDeviceManager::AudioDeviceSetup deviceSetup = AudioDeviceManager::AudioDeviceSetup();
97 deviceSetup.inputChannels = 0;
98 deviceSetup.outputChannels = channels;
103 int possible_rates[] { rate, 48000, 44100, 22050 };
104 for(
int attempt_rate : possible_rates) {
106 m_pInstance->defaultSampleRate = attempt_rate;
107 deviceSetup.sampleRate = attempt_rate;
108 m_pInstance->audioDeviceManager.setAudioDeviceSetup(deviceSetup,
true);
112 juce::String audio_error = m_pInstance->audioDeviceManager.initialise(
122 m_pInstance->initialise_error = audio_error.toStdString();
126 foundAudioIODevice = m_pInstance->audioDeviceManager.getCurrentAudioDevice();
127 if (foundAudioIODevice && foundAudioIODevice->getCurrentSampleRate() == attempt_rate) {
133 if (foundAudioIODevice) {
144 void AudioDeviceManagerSingleton::CloseAudioDevice()
147 audioDeviceManager.closeAudioDevice();
148 audioDeviceManager.removeAllChangeListeners();
149 audioDeviceManager.dispatchPendingMessages();
157 :
juce::Thread(
"audio-playback")
165 , time_thread(
"audio-buffer")
171 AudioPlaybackThread::~AudioPlaybackThread()
181 auto starting_frame = 1;
182 source =
new AudioReaderSource(reader, starting_frame);
197 std::shared_ptr<openshot::Frame> AudioPlaybackThread::getFrame()
199 if (source)
return source->
getFrame();
200 return std::shared_ptr<openshot::Frame>();
204 void AudioPlaybackThread::Seek(int64_t new_position)
207 source->
Seek(new_position);
212 void AudioPlaybackThread::Play() {
218 void AudioPlaybackThread::Stop() {
224 void AudioPlaybackThread::run()
226 while (!threadShouldExit())
228 if (source && !transport.isPlaying() && is_playing) {
230 AudioDeviceManagerSingleton *audioInstance =
234 audioInstance->audioDeviceManager.addAudioCallback(&player);
237 time_thread.startThread();
246 transport.setPosition(0);
247 transport.setGain(1.0);
250 mixer.addInputSource(&transport,
false);
251 player.setSource(&mixer);
256 while (!threadShouldExit() && transport.isPlaying() && is_playing)
257 std::this_thread::sleep_for(std::chrono::milliseconds(2));
264 transport.setSource(NULL);
266 player.setSource(NULL);
267 audioInstance->audioDeviceManager.removeAudioCallback(&player);
274 time_thread.stopThread(-1);
Source file for AudioPlaybackThread class.
Header file for global Settings class.
Singleton wrapper for AudioDeviceManager (to prevent multiple instances).
static AudioDeviceManagerSingleton * Instance()
Override with default sample rate & channels (44100, 2) and no preferred audio device.
juce::AudioDeviceManager audioDeviceManager
Public device manager property.
void Seek(int64_t new_position)
Seek to a specific frame.
void Reader(ReaderBase *audio_reader)
Set Reader.
std::shared_ptr< Frame > getFrame() const
Return the current frame object.
void setVideoCache(openshot::VideoCacheThread *newCache)
Set playback video cache thread (for pre-roll reference)
This abstract class is the base class, used by all readers in libopenshot.
openshot::ReaderInfo info
Information about the current media file.
This namespace is the default namespace for all code in the openshot library.
This struct hold information about Audio Devices.
int channels
The number of audio channels used in the audio stream.
int sample_rate
The number of audio samples per second (44100 is a common sample rate)