OpenShot Library | libopenshot  0.2.5
AudioPlaybackThread.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for AudioPlaybackThread class
4  * @author Duzy Chan <code@duzy.info>
5  * @author Jonathan Thomas <jonathan@openshot.org>
6  *
7  * @ref License
8  */
9 
10 /* LICENSE
11  *
12  * Copyright (c) 2008-2019 OpenShot Studios, LLC
13  * <http://www.openshotstudios.com/>. This file is part of
14  * OpenShot Library (libopenshot), an open-source project dedicated to
15  * delivering high quality video editing and animation solutions to the
16  * world. For more information visit <http://www.openshot.org/>.
17  *
18  * OpenShot Library (libopenshot) is free software: you can redistribute it
19  * and/or modify it under the terms of the GNU Lesser General Public License
20  * as published by the Free Software Foundation, either version 3 of the
21  * License, or (at your option) any later version.
22  *
23  * OpenShot Library (libopenshot) is distributed in the hope that it will be
24  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
30  */
31 
32 #ifndef OPENSHOT_AUDIO_PLAYBACK_THREAD_H
33 #define OPENSHOT_AUDIO_PLAYBACK_THREAD_H
34 
35 #include "../ReaderBase.h"
36 #include "../RendererBase.h"
37 #include "../AudioReaderSource.h"
38 #include "../AudioDeviceInfo.h"
39 #include "../Settings.h"
40 
41 namespace openshot
42 {
43  struct SafeTimeSliceThread : juce::TimeSliceThread
44  {
45  SafeTimeSliceThread(const String & s) : juce::TimeSliceThread(s) {}
46  void run()
47  {
48  try {
49  juce::TimeSliceThread::run();
50  } catch (const TooManySeeks & e) {
51  // ...
52  }
53  }
54  };
55 
56  /**
57  * @brief Singleton wrapper for AudioDeviceManager (to prevent multiple instances).
58  */
60  private:
61  /// Default constructor (Don't allow user to create an instance of this singleton)
63 
64  /// Private variable to keep track of singleton instance
65  static AudioDeviceManagerSingleton * m_pInstance;
66 
67  public:
68  /// Error found during JUCE initialise method
69  std::string initialise_error;
70 
71  /// List of valid audio device names
72  std::vector<openshot::AudioDeviceInfo> audio_device_names;
73 
74  /// Override with no channels and no preferred audio device
76 
77  /// Public device manager property
78  AudioDeviceManager audioDeviceManager;
79 
80  /// Close audio device
81  void CloseAudioDevice();
82  };
83 
84  /**
85  * @brief The audio playback thread
86  */
87  class AudioPlaybackThread : juce::Thread
88  {
89  AudioSourcePlayer player;
90  AudioTransportSource transport;
91  MixerAudioSource mixer;
92  AudioReaderSource *source;
93  double sampleRate;
94  int numChannels;
95  juce::WaitableEvent play;
96  juce::WaitableEvent played;
97  int buffer_size;
98  bool is_playing;
99  SafeTimeSliceThread time_thread;
100 
101  /// Constructor
103  /// Destructor
105 
106  /// Set the current thread's reader
107  void Reader(openshot::ReaderBase *reader);
108 
109  /// Get the current frame object (which is filling the buffer)
110  std::shared_ptr<openshot::Frame> getFrame();
111 
112  /// Get the current frame number being played
113  int64_t getCurrentFramePosition();
114 
115  /// Play the audio
116  void Play();
117 
118  /// Seek the audio thread
119  void Seek(int64_t new_position);
120 
121  /// Stop the audio playback
122  void Stop();
123 
124  /// Start thread
125  void run();
126 
127  /// Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
128  void setSpeed(int new_speed) { if (source) source->setSpeed(new_speed); }
129 
130  /// Get Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
131  int getSpeed() const { if (source) return source->getSpeed(); else return 1; }
132 
133  /// Get Audio Error (if any)
134  std::string getError()
135  {
137  }
138 
139  /// Get Audio Device Names (if any)
140  std::vector<openshot::AudioDeviceInfo> getAudioDeviceNames()
141  {
143  };
144 
145  friend class PlayerPrivate;
146  friend class QtPlayer;
147  };
148 
149 }
150 
151 #endif // OPENSHOT_AUDIO_PLAYBACK_THREAD_H
openshot::AudioDeviceManagerSingleton::initialise_error
std::string initialise_error
Error found during JUCE initialise method.
Definition: AudioPlaybackThread.h:69
openshot::SafeTimeSliceThread::run
void run()
Definition: AudioPlaybackThread.h:46
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:39
openshot::AudioDeviceManagerSingleton::audioDeviceManager
AudioDeviceManager audioDeviceManager
Public device manager property.
Definition: AudioPlaybackThread.h:78
openshot::AudioDeviceManagerSingleton::Instance
static AudioDeviceManagerSingleton * Instance()
Override with no channels and no preferred audio device.
Definition: AudioPlaybackThread.cpp:41
openshot::AudioPlaybackThread
The audio playback thread.
Definition: AudioPlaybackThread.h:88
openshot::TooManySeeks
Exception when too many seek attempts happen.
Definition: Exceptions.h:370
openshot::AudioDeviceManagerSingleton::audio_device_names
std::vector< openshot::AudioDeviceInfo > audio_device_names
List of valid audio device names.
Definition: AudioPlaybackThread.h:72
openshot::AudioDeviceManagerSingleton
Singleton wrapper for AudioDeviceManager (to prevent multiple instances).
Definition: AudioPlaybackThread.h:59
openshot::SafeTimeSliceThread
Definition: AudioPlaybackThread.h:44
openshot::PlayerPrivate
The private part of QtPlayer class, which contains an audio thread and video thread,...
Definition: PlayerPrivate.h:49
openshot::AudioReaderSource::setSpeed
void setSpeed(int new_speed)
Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster,...
Definition: AudioReaderSource.h:120
openshot::QtPlayer
This class is used to playback a video from a reader.
Definition: QtPlayer.h:48
openshot::SafeTimeSliceThread::SafeTimeSliceThread
SafeTimeSliceThread(const String &s)
Definition: AudioPlaybackThread.h:45
openshot::AudioDeviceManagerSingleton::CloseAudioDevice
void CloseAudioDevice()
Close audio device.
Definition: AudioPlaybackThread.cpp:85
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
openshot::AudioReaderSource::getSpeed
int getSpeed() const
Get Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster,...
Definition: AudioReaderSource.h:122
openshot::AudioReaderSource
This class is used to expose any ReaderBase derived class as an AudioSource in JUCE.
Definition: AudioReaderSource.h:48