OpenShot Library | libopenshot  0.2.6
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 <OpenShotAudio.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 frame_number; /// The current frame number
58  std::shared_ptr<Frame> frame; /// The current frame object that is being read
59  int64_t frame_position; /// The position of the current frame's buffer
60  double estimated_frame; /// The estimated frame position of the currently playing buffer
61  int estimated_samples_per_frame; /// The estimated samples per frame of video
62 
63  /// Get more samples from the reader
64  void GetMoreSamplesFromReader();
65 
66  /// Reverse an audio buffer (for backwards audio)
67  juce::AudioSampleBuffer* reverse_buffer(juce::AudioSampleBuffer* buffer);
68 
69  public:
70 
71  /// @brief Constructor that reads samples from a reader
72  /// @param audio_reader This reader provides constant samples from a ReaderBase derived class
73  /// @param starting_frame_number This is the frame number to start reading samples from the reader.
74  /// @param buffer_size The max number of samples to keep in the buffer at one time.
75  AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number, int buffer_size);
76 
77  /// Destructor
79 
80  /// @brief Get the next block of audio samples
81  /// @param info This struct informs us of which samples are needed next.
82  void getNextAudioBlock (const juce::AudioSourceChannelInfo& info);
83 
84  /// Prepare to play this audio source
85  void prepareToPlay(int, double);
86 
87  /// Release all resources
88  void releaseResources();
89 
90  /// @brief Set the next read position of this source
91  /// @param newPosition The sample # to start reading from
92  void setNextReadPosition (juce::int64 newPosition);
93 
94  /// Get the next read position of this source
96 
97  /// Get the total length (in samples) of this audio source
99 
100  /// Determines if this audio source should repeat when it reaches the end
101  bool isLooping() const;
102 
103  /// @brief Set if this audio source should repeat when it reaches the end
104  /// @param shouldLoop Determines if the audio source should repeat when it reaches the end
105  void setLooping (bool shouldLoop);
106 
107  /// Update the internal buffer used by this source
108  void setBuffer (juce::AudioSampleBuffer *audio_buffer);
109 
110  const ReaderInfo & getReaderInfo() const { return reader->info; }
111 
112  /// Return the current frame object
113  std::shared_ptr<Frame> getFrame() const { return frame; }
114 
115  /// Get the estimate frame that is playing at this moment
116  int64_t getEstimatedFrame() const { return int64_t(estimated_frame); }
117 
118  /// Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
119  void setSpeed(int new_speed) { speed = new_speed; }
120  /// Get Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)
121  int getSpeed() const { return speed; }
122 
123  /// Set Reader
124  void Reader(ReaderBase *audio_reader) { reader = audio_reader; }
125  /// Get Reader
126  ReaderBase* Reader() const { return reader; }
127 
128  /// Seek to a specific frame
129  void Seek(int64_t new_position) { frame_number = new_position; estimated_frame = new_position; }
130 
131  };
132 
133 }
134 
135 #endif
Header file for ReaderBase class.
void setBuffer(juce::AudioSampleBuffer *audio_buffer)
Update the internal buffer used by this source.
void setNextReadPosition(juce::int64 newPosition)
Set the next read position of this source.
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.
juce::int64 getTotalLength() const
Get the total length (in samples) of this audio source.
void setLooping(bool shouldLoop)
Set if this audio source should repeat when it reaches the end.
AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number, int buffer_size)
Constructor that reads samples from a reader.
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
void releaseResources()
Release all resources.
juce::int64 getNextReadPosition() const
Get the next read position of this source.
std::shared_ptr< Frame > getFrame() const
Return the current frame object.
void getNextAudioBlock(const juce::AudioSourceChannelInfo &info)
Get the next block of audio samples.
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:60
int64_t getEstimatedFrame() const
Get the estimate frame that is playing at this moment.
void Reader(ReaderBase *audio_reader)
Set Reader.
#define int64
Definition: Clip.h:35
const ReaderInfo & getReaderInfo() const
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
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.
Definition: Compressor.h:46
void prepareToPlay(int, double)
Prepare to play this audio source.
bool isLooping() const
Determines if this audio source should repeat when it reaches the end.
void setSpeed(int new_speed)
Set Speed (The speed and direction to playback a reader (1=normal, 2=fast, 3=faster, -1=rewind, etc...)