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