OpenShot Library | libopenshot  0.1.9
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 "../../include/ReaderBase.h"
33 #include "../../include/RendererBase.h"
34 #include "../../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
This class is used to playback a video from a reader.
Definition: QtPlayer.h:46
AudioDeviceManager audioDeviceManager
Public device manager property.
int getSpeed() const
Get Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
The audio playback thread.
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
The private part of QtPlayer class, which contains an audio thread and video thread, and controls the video timing and audio synchronization code.
Definition: PlayerPrivate.h:47
This class is used to expose any ReaderBase derived class as an AudioSource in JUCE.
This namespace is the default namespace for all code in the openshot library.
Singleton wrapper for AudioDeviceManager (to prevent multiple instances).
void setSpeed(int new_speed)
Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
Exception when too many seek attempts happen.
Definition: Exceptions.h:254