The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_AUDIOFORMATWRITER_JUCEHEADER__
  19. #define __JUCE_AUDIOFORMATWRITER_JUCEHEADER__
  20. #include "../../io/files/juce_FileOutputStream.h"
  21. #include "juce_AudioFormatReader.h"
  22. #include "../audio_sources/juce_AudioSource.h"
  23. #include "../../threads/juce_TimeSliceThread.h"
  24. class AudioThumbnail;
  25. //==============================================================================
  26. /**
  27. Writes samples to an audio file stream.
  28. A subclass that writes a specific type of audio format will be created by
  29. an AudioFormat object.
  30. After creating one of these with the AudioFormat::createWriterFor() method
  31. you can call its write() method to store the samples, and then delete it.
  32. @see AudioFormat, AudioFormatReader
  33. */
  34. class JUCE_API AudioFormatWriter
  35. {
  36. protected:
  37. //==============================================================================
  38. /** Creates an AudioFormatWriter object.
  39. @param destStream the stream to write to - this will be deleted
  40. by this object when it is no longer needed
  41. @param formatName the description that will be returned by the getFormatName()
  42. method
  43. @param sampleRate the sample rate to use - the base class just stores
  44. this value, it doesn't do anything with it
  45. @param numberOfChannels the number of channels to write - the base class just stores
  46. this value, it doesn't do anything with it
  47. @param bitsPerSample the bit depth of the stream - the base class just stores
  48. this value, it doesn't do anything with it
  49. */
  50. AudioFormatWriter (OutputStream* destStream,
  51. const String& formatName,
  52. double sampleRate,
  53. unsigned int numberOfChannels,
  54. unsigned int bitsPerSample);
  55. public:
  56. /** Destructor. */
  57. virtual ~AudioFormatWriter();
  58. //==============================================================================
  59. /** Returns a description of what type of format this is.
  60. E.g. "AIFF file"
  61. */
  62. const String getFormatName() const throw() { return formatName; }
  63. //==============================================================================
  64. /** Writes a set of samples to the audio stream.
  65. Note that if you're trying to write the contents of an AudioSampleBuffer, you
  66. can use AudioSampleBuffer::writeToAudioWriter().
  67. @param samplesToWrite an array of arrays containing the sample data for
  68. each channel to write. This is a zero-terminated
  69. array of arrays, and can contain a different number
  70. of channels than the actual stream uses, and the
  71. writer should do its best to cope with this.
  72. If the format is fixed-point, each channel will be formatted
  73. as an array of signed integers using the full 32-bit
  74. range -0x80000000 to 0x7fffffff, regardless of the source's
  75. bit-depth. If it is a floating-point format, you should treat
  76. the arrays as arrays of floats, and just cast it to an (int**)
  77. to pass it into the method.
  78. @param numSamples the number of samples to write
  79. */
  80. virtual bool write (const int** samplesToWrite,
  81. int numSamples) = 0;
  82. //==============================================================================
  83. /** Reads a section of samples from an AudioFormatReader, and writes these to
  84. the output.
  85. This will take care of any floating-point conversion that's required to convert
  86. between the two formats. It won't deal with sample-rate conversion, though.
  87. If numSamplesToRead < 0, it will write the entire length of the reader.
  88. @returns false if it can't read or write properly during the operation
  89. */
  90. bool writeFromAudioReader (AudioFormatReader& reader,
  91. int64 startSample,
  92. int64 numSamplesToRead);
  93. /** Reads some samples from an AudioSource, and writes these to the output.
  94. The source must already have been initialised with the AudioSource::prepareToPlay() method
  95. @param source the source to read from
  96. @param numSamplesToRead total number of samples to read and write
  97. @param samplesPerBlock the maximum number of samples to fetch from the source
  98. @returns false if it can't read or write properly during the operation
  99. */
  100. bool writeFromAudioSource (AudioSource& source,
  101. int numSamplesToRead,
  102. int samplesPerBlock = 2048);
  103. /** Writes some samples from an AudioSampleBuffer.
  104. */
  105. bool writeFromAudioSampleBuffer (const AudioSampleBuffer& source,
  106. int startSample, int numSamples);
  107. //==============================================================================
  108. /** Returns the sample rate being used. */
  109. double getSampleRate() const throw() { return sampleRate; }
  110. /** Returns the number of channels being written. */
  111. int getNumChannels() const throw() { return numChannels; }
  112. /** Returns the bit-depth of the data being written. */
  113. int getBitsPerSample() const throw() { return bitsPerSample; }
  114. /** Returns true if it's a floating-point format, false if it's fixed-point. */
  115. bool isFloatingPoint() const throw() { return usesFloatingPointData; }
  116. //==============================================================================
  117. /**
  118. Provides a FIFO for an AudioFormatWriter, allowing you to push incoming
  119. data into a buffer which will be flushed to disk by a background thread.
  120. */
  121. class ThreadedWriter
  122. {
  123. public:
  124. /** Creates a ThreadedWriter for a given writer and a thread.
  125. The writer object which is passed in here will be owned and deleted by
  126. the ThreadedWriter when it is no longer needed.
  127. To stop the writer and flush the buffer to disk, simply delete this object.
  128. */
  129. ThreadedWriter (AudioFormatWriter* writer,
  130. TimeSliceThread& backgroundThread,
  131. int numSamplesToBuffer);
  132. /** Destructor. */
  133. ~ThreadedWriter();
  134. /** Pushes some incoming audio data into the FIFO.
  135. If there's enough free space in the buffer, this will add the data to it,
  136. If the FIFO is too full to accept this many samples, the method will return
  137. false - then you could either wait until the background thread has had time to
  138. consume some of the buffered data and try again, or you can give up
  139. and lost this block.
  140. The data must be an array containing the same number of channels as the
  141. AudioFormatWriter object is using. None of these channels can be null.
  142. */
  143. bool write (const float** data, int numSamples);
  144. /** Allows you to specify a thumbnail that this writer should update with the
  145. incoming data.
  146. The thumbnail will be cleared and will the writer will begin adding data to
  147. it as it arrives. Pass a null pointer to stop the writer updating any thumbnails.
  148. */
  149. void setThumbnailToUpdate (AudioThumbnail* thumbnailToUpdate);
  150. /** @internal */
  151. class Buffer; // (only public for VC6 compatibility)
  152. private:
  153. friend class ScopedPointer<Buffer>;
  154. ScopedPointer<Buffer> buffer;
  155. };
  156. protected:
  157. //==============================================================================
  158. /** The sample rate of the stream. */
  159. double sampleRate;
  160. /** The number of channels being written to the stream. */
  161. unsigned int numChannels;
  162. /** The bit depth of the file. */
  163. unsigned int bitsPerSample;
  164. /** True if it's a floating-point format, false if it's fixed-point. */
  165. bool usesFloatingPointData;
  166. /** The output stream for Use by subclasses. */
  167. OutputStream* output;
  168. /** Used by AudioFormatWriter subclasses to copy data to different formats. */
  169. template <class DestSampleType, class SourceSampleType, class DestEndianness>
  170. struct WriteHelper
  171. {
  172. typedef AudioData::Pointer <DestSampleType, DestEndianness, AudioData::Interleaved, AudioData::NonConst> DestType;
  173. typedef AudioData::Pointer <SourceSampleType, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const> SourceType;
  174. static void write (void* destData, int numDestChannels, const int** source,
  175. int numSamples, const int sourceOffset = 0) throw()
  176. {
  177. for (int i = 0; i < numDestChannels; ++i)
  178. {
  179. const DestType dest (addBytesToPointer (destData, i * DestType::getBytesPerSample()), numDestChannels);
  180. if (*source != 0)
  181. {
  182. dest.convertSamples (SourceType (*source + sourceOffset), numSamples);
  183. ++source;
  184. }
  185. else
  186. {
  187. dest.clearSamples (numSamples);
  188. }
  189. }
  190. }
  191. };
  192. private:
  193. String formatName;
  194. friend class ThreadedWriter;
  195. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioFormatWriter);
  196. };
  197. #endif // __JUCE_AUDIOFORMATWRITER_JUCEHEADER__