1 /**
2  * @file
3  * @brief Header file for AudioBufferSource 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_AUDIOBUFFERSOURCE_H
32 #define OPENSHOT_AUDIOBUFFERSOURCE_H
33 
34 #include <iomanip>
35 #include <OpenShotAudio.h>
36 
37 /// This namespace is the default namespace for all code in the openshot library
38 namespace openshot
39 {
40 
41 	/**
42 	 * @brief This class is used to expose an AudioSampleBuffer as an AudioSource in JUCE.
43 	 *
44 	 * The <a href="http://www.juce.com/">JUCE</a> library cannot play audio directly from an AudioSampleBuffer, so this class exposes
45 	 * an AudioSampleBuffer as a AudioSource, so that JUCE can play the audio.
46 	 */
47 	class AudioBufferSource : public juce::PositionableAudioSource
48 	{
49 	private:
50 		int position;
51 		bool repeat;
52 		juce::AudioSampleBuffer *buffer;
53 
54 	public:
55 		/// @brief Default constructor
56 		/// @param audio_buffer This buffer contains the samples you want to play through JUCE.
57 		AudioBufferSource(juce::AudioSampleBuffer *audio_buffer);
58 
59 		/// Destructor
60 		~AudioBufferSource();
61 
62 		/// @brief Get the next block of audio samples
63 		/// @param info This struct informs us of which samples are needed next.
64 		void getNextAudioBlock (const juce::AudioSourceChannelInfo& info);
65 
66 		/// Prepare to play this audio source
67 		void prepareToPlay(int, double);
68 
69 		/// Release all resources
70 		void releaseResources();
71 
72 		/// @brief Set the next read position of this source
73 		/// @param newPosition The sample # to start reading from
74 		void setNextReadPosition (juce::int64 newPosition);
75 
76 		/// Get the next read position of this source
77 		juce::int64 getNextReadPosition() const;
78 
79 		/// Get the total length (in samples) of this audio source
80 		juce::int64 getTotalLength() const;
81 
82 		/// Determines if this audio source should repeat when it reaches the end
83 		bool isLooping() const;
84 
85 		/// @brief Set if this audio source should repeat when it reaches the end
86 		/// @param shouldLoop Determines if the audio source should repeat when it reaches the end
87 		void setLooping (bool shouldLoop);
88 
89 		/// Update the internal buffer used by this source
90 		void setBuffer (juce::AudioSampleBuffer *audio_buffer);
91 	};
92 
93 }
94 
95 #endif
96