OpenShot Library | libopenshot  0.1.9
AudioReaderSource.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for AudioReaderSource class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef OPENSHOT_AUDIOREADERSOURCE_H
29 #define OPENSHOT_AUDIOREADERSOURCE_H
30 
31 /// Do not include the juce unittest headers, because it collides with unittest++
32 #define __JUCE_UNITTEST_JUCEHEADER__
33 
34 #ifndef _NDEBUG
35  /// Define NO debug for JUCE on mac os
36  #define _NDEBUG
37 #endif
38 
39 #include <iomanip>
40 #include "JuceLibraryCode/JuceHeader.h"
41 #include "ReaderBase.h"
42 
43 using namespace std;
44 
45 /// This namespace is the default namespace for all code in the openshot library
46 namespace openshot
47 {
48 
49  /**
50  * @brief This class is used to expose any ReaderBase derived class as an AudioSource in JUCE.
51  *
52  * This allows any reader to play audio through JUCE (our audio framework).
53  */
54  class AudioReaderSource : public PositionableAudioSource
55  {
56  private:
57  int position; /// The position of the audio source (index of buffer)
58  bool repeat; /// Repeat the audio source when finished
59  int size; /// The size of the internal buffer
60  AudioSampleBuffer *buffer; /// The audio sample buffer
61  int speed; /// The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
62 
63  ReaderBase *reader; /// The reader to pull samples from
64  int64_t original_frame_number; /// The current frame to read from
65  int64_t frame_number; /// The current frame number
66  std::shared_ptr<Frame> frame; /// The current frame object that is being read
67  int64_t frame_position; /// The position of the current frame's buffer
68  double estimated_frame; /// The estimated frame position of the currently playing buffer
69  int estimated_samples_per_frame; /// The estimated samples per frame of video
70 
71  /// Get more samples from the reader
72  void GetMoreSamplesFromReader();
73 
74  /// Reverse an audio buffer (for backwards audio)
75  juce::AudioSampleBuffer* reverse_buffer(juce::AudioSampleBuffer* buffer);
76 
77  public:
78 
79  /// @brief Constructor that reads samples from a reader
80  /// @param audio_reader This reader provides constant samples from a ReaderBase derived class
81  /// @param starting_frame_number This is the frame number to start reading samples from the reader.
82  /// @param buffer_size The max number of samples to keep in the buffer at one time.
83  AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number, int buffer_size);
84 
85  /// Destructor
87 
88  /// @brief Get the next block of audio samples
89  /// @param info This struct informs us of which samples are needed next.
90  void getNextAudioBlock (const AudioSourceChannelInfo& info);
91 
92  /// Prepare to play this audio source
93  void prepareToPlay(int, double);
94 
95  /// Release all resources
96  void releaseResources();
97 
98  /// @brief Set the next read position of this source
99  /// @param newPosition The sample # to start reading from
100  void setNextReadPosition (int64 newPosition);
101 
102  /// Get the next read position of this source
103  int64 getNextReadPosition() const;
104 
105  /// Get the total length (in samples) of this audio source
106  int64 getTotalLength() const;
107 
108  /// Determines if this audio source should repeat when it reaches the end
109  bool isLooping() const;
110 
111  /// @brief Set if this audio source should repeat when it reaches the end
112  /// @param shouldLoop Determines if the audio source should repeat when it reaches the end
113  void setLooping (bool shouldLoop);
114 
115  /// Update the internal buffer used by this source
116  void setBuffer (AudioSampleBuffer *audio_buffer);
117 
118  const ReaderInfo & getReaderInfo() const { return reader->info; }
119 
120  /// Return the current frame object
121  std::shared_ptr<Frame> getFrame() const { return frame; }
122 
123  /// Get the estimate frame that is playing at this moment
124  int64_t getEstimatedFrame() const { return int64_t(estimated_frame); }
125 
126  /// Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
127  void setSpeed(int new_speed) { speed = new_speed; }
128  /// Get Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
129  int getSpeed() const { return speed; }
130 
131  /// Set Reader
132  void Reader(ReaderBase *audio_reader) { reader = audio_reader; }
133  /// Get Reader
134  ReaderBase* Reader() const { return reader; }
135 
136  /// Seek to a specific frame
137  void Seek(int64_t new_position) { frame_number = new_position; estimated_frame = new_position; }
138 
139  };
140 
141 }
142 
143 #endif
Header file for ReaderBase class.
int getSpeed() const
Get Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
ReaderBase * Reader() const
Get Reader.
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
std::shared_ptr< Frame > getFrame() const
Return the current frame object.
void Seek(int64_t new_position)
Seek to a specific frame.
This struct contains info about a media file, such as height, width, frames per second, etc...
Definition: ReaderBase.h:59
int64_t getEstimatedFrame() const
Get the estimate frame that is playing at this moment.
void Reader(ReaderBase *audio_reader)
Set Reader.
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
const ReaderInfo & getReaderInfo() const
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.
void setSpeed(int new_speed)
Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)