Audio plugin host https://kx.studio/carla
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.

235 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2015 ROLI Ltd.
  5. Copyright (C) 2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of either:
  7. a) the GPL v2 (or any later version)
  8. b) the Affero GPL v3
  9. Details of these licenses can be found at: www.gnu.org/licenses
  10. Water 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. */
  15. #ifndef WATER_AUDIOFORMATREADER_H_INCLUDED
  16. #define WATER_AUDIOFORMATREADER_H_INCLUDED
  17. #include "../buffers/AudioDataConverters.h"
  18. #include "../text/StringPairArray.h"
  19. #include "CarlaJuceUtils.hpp"
  20. namespace water {
  21. //==============================================================================
  22. /**
  23. Reads samples from an audio file stream.
  24. A subclass that reads a specific type of audio format will be created by
  25. an AudioFormat object.
  26. @see AudioFormat, AudioFormatWriter
  27. */
  28. class AudioFormatReader
  29. {
  30. protected:
  31. //==============================================================================
  32. /** Creates an AudioFormatReader object.
  33. @param sourceStream the stream to read from - this will be deleted
  34. by this object when it is no longer needed. (Some
  35. specialised readers might not use this parameter and
  36. can leave it as nullptr).
  37. @param formatName the description that will be returned by the getFormatName()
  38. method
  39. */
  40. AudioFormatReader (InputStream* sourceStream,
  41. const String& formatName);
  42. public:
  43. /** Destructor. */
  44. virtual ~AudioFormatReader();
  45. //==============================================================================
  46. /** Returns a description of what type of format this is.
  47. E.g. "AIFF"
  48. */
  49. const String& getFormatName() const noexcept { return formatName; }
  50. //==============================================================================
  51. /** Reads samples from the stream.
  52. @param destSamples an array of buffers into which the sample data for each
  53. channel will be written.
  54. If the format is fixed-point, each channel will be written
  55. as an array of 32-bit signed integers using the full
  56. range -0x80000000 to 0x7fffffff, regardless of the source's
  57. bit-depth. If it is a floating-point format, you should cast
  58. the resulting array to a (float**) to get the values (in the
  59. range -1.0 to 1.0 or beyond)
  60. If the format is stereo, then destSamples[0] is the left channel
  61. data, and destSamples[1] is the right channel.
  62. The numDestChannels parameter indicates how many pointers this array
  63. contains, but some of these pointers can be null if you don't want to
  64. read data for some of the channels
  65. @param numDestChannels the number of array elements in the destChannels array
  66. @param startSampleInSource the position in the audio file or stream at which the samples
  67. should be read, as a number of samples from the start of the
  68. stream. It's ok for this to be beyond the start or end of the
  69. available data - any samples that are out-of-range will be returned
  70. as zeros.
  71. @param numSamplesToRead the number of samples to read. If this is greater than the number
  72. of samples that the file or stream contains. the result will be padded
  73. with zeros
  74. @param fillLeftoverChannelsWithCopies if true, this indicates that if there's no source data available
  75. for some of the channels that you pass in, then they should be filled with
  76. copies of valid source channels.
  77. E.g. if you're reading a mono file and you pass 2 channels to this method, then
  78. if fillLeftoverChannelsWithCopies is true, both destination channels will be filled
  79. with the same data from the file's single channel. If fillLeftoverChannelsWithCopies
  80. was false, then only the first channel would be filled with the file's contents, and
  81. the second would be cleared. If there are many channels, e.g. you try to read 4 channels
  82. from a stereo file, then the last 3 would all end up with copies of the same data.
  83. @returns true if the operation succeeded, false if there was an error. Note
  84. that reading sections of data beyond the extent of the stream isn't an
  85. error - the reader should just return zeros for these regions
  86. @see readMaxLevels
  87. */
  88. bool read (int* const* destSamples,
  89. int numDestChannels,
  90. int64 startSampleInSource,
  91. int numSamplesToRead,
  92. bool fillLeftoverChannelsWithCopies);
  93. /** Fills a section of an AudioSampleBuffer from this reader.
  94. This will convert the reader's fixed- or floating-point data to
  95. the buffer's floating-point format, and will try to intelligently
  96. cope with mismatches between the number of channels in the reader
  97. and the buffer.
  98. */
  99. void read (AudioSampleBuffer* buffer,
  100. int startSampleInDestBuffer,
  101. int numSamples,
  102. int64 readerStartSample,
  103. bool useReaderLeftChan,
  104. bool useReaderRightChan);
  105. //==============================================================================
  106. /** The sample-rate of the stream. */
  107. double sampleRate;
  108. /** The number of bits per sample, e.g. 16, 24, 32. */
  109. unsigned int bitsPerSample;
  110. /** The total number of samples in the audio stream. */
  111. int64 lengthInSamples;
  112. /** The total number of channels in the audio stream. */
  113. unsigned int numChannels;
  114. /** Indicates whether the data is floating-point or fixed. */
  115. bool usesFloatingPointData;
  116. /** A set of metadata values that the reader has pulled out of the stream.
  117. Exactly what these values are depends on the format, so you can
  118. check out the format implementation code to see what kind of stuff
  119. they understand.
  120. */
  121. StringPairArray metadataValues;
  122. /** The input stream, for use by subclasses. */
  123. InputStream* input;
  124. //==============================================================================
  125. /** Subclasses must implement this method to perform the low-level read operation.
  126. Callers should use read() instead of calling this directly.
  127. @param destSamples the array of destination buffers to fill. Some of these
  128. pointers may be null
  129. @param numDestChannels the number of items in the destSamples array. This
  130. value is guaranteed not to be greater than the number of
  131. channels that this reader object contains
  132. @param startOffsetInDestBuffer the number of samples from the start of the
  133. dest data at which to begin writing
  134. @param startSampleInFile the number of samples into the source data at which
  135. to begin reading. This value is guaranteed to be >= 0.
  136. @param numSamples the number of samples to read
  137. */
  138. virtual bool readSamples (int** destSamples,
  139. int numDestChannels,
  140. int startOffsetInDestBuffer,
  141. int64 startSampleInFile,
  142. int numSamples) = 0;
  143. protected:
  144. //==============================================================================
  145. /** Used by AudioFormatReader subclasses to copy data to different formats. */
  146. template <class DestSampleType, class SourceSampleType, class SourceEndianness>
  147. struct ReadHelper
  148. {
  149. typedef AudioData::Pointer<DestSampleType, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst> DestType;
  150. typedef AudioData::Pointer<SourceSampleType, SourceEndianness, AudioData::Interleaved, AudioData::Const> SourceType;
  151. template <typename TargetType>
  152. static void read (TargetType* const* destData, int destOffset, int numDestChannels,
  153. const void* sourceData, int numSourceChannels, int numSamples) noexcept
  154. {
  155. for (int i = 0; i < numDestChannels; ++i)
  156. {
  157. if (void* targetChan = destData[i])
  158. {
  159. DestType dest (targetChan);
  160. dest += destOffset;
  161. if (i < numSourceChannels)
  162. dest.convertSamples (SourceType (addBytesToPointer (sourceData, i * SourceType::getBytesPerSample()), numSourceChannels), numSamples);
  163. else
  164. dest.clearSamples (numSamples);
  165. }
  166. }
  167. }
  168. };
  169. /** Used by AudioFormatReader subclasses to clear any parts of the data blocks that lie
  170. beyond the end of their available length.
  171. */
  172. static void clearSamplesBeyondAvailableLength (int** destSamples, int numDestChannels,
  173. int startOffsetInDestBuffer, int64 startSampleInFile,
  174. int& numSamples, int64 fileLengthInSamples)
  175. {
  176. jassert (destSamples != nullptr);
  177. const int64 samplesAvailable = fileLengthInSamples - startSampleInFile;
  178. if (samplesAvailable < numSamples)
  179. {
  180. for (int i = numDestChannels; --i >= 0;)
  181. if (destSamples[i] != nullptr)
  182. zeromem (destSamples[i] + startOffsetInDestBuffer, sizeof (int) * (size_t) numSamples);
  183. numSamples = (int) samplesAvailable;
  184. }
  185. }
  186. private:
  187. String formatName;
  188. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioFormatReader)
  189. };
  190. }
  191. #endif // WATER_AUDIOFORMATREADER_H_INCLUDED