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