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 78 ~AudioReaderSource(); 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 95 juce::int64 getNextReadPosition() const; 96 97 /// Get the total length (in samples) of this audio source 98 juce::int64 getTotalLength() const; 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 getReaderInfo()110 const ReaderInfo & getReaderInfo() const { return reader->info; } 111 112 /// Return the current frame object getFrame()113 std::shared_ptr<Frame> getFrame() const { return frame; } 114 115 /// Get the estimate frame that is playing at this moment getEstimatedFrame()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...) setSpeed(int new_speed)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...) getSpeed()121 int getSpeed() const { return speed; } 122 123 /// Set Reader Reader(ReaderBase * audio_reader)124 void Reader(ReaderBase *audio_reader) { reader = audio_reader; } 125 /// Get Reader Reader()126 ReaderBase* Reader() const { return reader; } 127 128 /// Seek to a specific frame Seek(int64_t new_position)129 void Seek(int64_t new_position) { frame_number = new_position; estimated_frame = new_position; } 130 131 }; 132 133 } 134 135 #endif 136