OpenShot Audio Library | OpenShotAudio  0.3.1
juce_AudioFormatWriter.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
44 class JUCE_API AudioFormatWriter
45 {
46 protected:
47  //==============================================================================
61  AudioFormatWriter (OutputStream* destStream,
62  const String& formatName,
63  double sampleRate,
64  unsigned int numberOfChannels,
65  unsigned int bitsPerSample);
66 
67  //==============================================================================
81  AudioFormatWriter (OutputStream* destStream,
82  const String& formatName,
83  double sampleRate,
84  const AudioChannelSet& audioChannelLayout,
85  unsigned int bitsPerSample);
86 
87 public:
89  virtual ~AudioFormatWriter();
90 
91  //==============================================================================
96  const String& getFormatName() const noexcept { return formatName; }
97 
98  //==============================================================================
117  virtual bool write (const int** samplesToWrite, int numSamples) = 0;
118 
128  virtual bool flush();
129 
130  //==============================================================================
141  bool writeFromAudioReader (AudioFormatReader& reader,
142  int64 startSample,
143  int64 numSamplesToRead);
144 
154  bool writeFromAudioSource (AudioSource& source,
155  int numSamplesToRead,
156  int samplesPerBlock = 2048);
157 
158 
160  bool writeFromAudioSampleBuffer (const AudioBuffer<float>& source,
161  int startSample, int numSamples);
162 
164  bool writeFromFloatArrays (const float* const* channels, int numChannels, int numSamples);
165 
166  //==============================================================================
168  double getSampleRate() const noexcept { return sampleRate; }
169 
171  int getNumChannels() const noexcept { return (int) numChannels; }
172 
174  int getBitsPerSample() const noexcept { return (int) bitsPerSample; }
175 
177  bool isFloatingPoint() const noexcept { return usesFloatingPointData; }
178 
179  //==============================================================================
185  {
186  public:
195  TimeSliceThread& backgroundThread,
196  int numSamplesToBuffer);
197 
199  ~ThreadedWriter();
200 
213  bool write (const float* const* data, int numSamples);
214 
216  class JUCE_API IncomingDataReceiver
217  {
218  public:
219  IncomingDataReceiver() = default;
220  virtual ~IncomingDataReceiver() = default;
221 
222  virtual void reset (int numChannels, double sampleRate, int64 totalSamplesInSource) = 0;
223  virtual void addBlock (int64 sampleNumberInSource, const AudioBuffer<float>& newData,
224  int startOffsetInBuffer, int numSamples) = 0;
225  };
226 
234  void setDataReceiver (IncomingDataReceiver*);
235 
239  void setFlushInterval (int numSamplesPerFlush) noexcept;
240 
241  private:
242  class Buffer;
243  std::unique_ptr<Buffer> buffer;
244  };
245 
246 protected:
247  //==============================================================================
249  double sampleRate;
250 
252  unsigned int numChannels;
253 
255  unsigned int bitsPerSample;
256 
259 
262 
265 
267  template <class DestSampleType, class SourceSampleType, class DestEndianness>
268  struct WriteHelper
269  {
272 
273  static void write (void* destData, int numDestChannels, const int* const* source,
274  int numSamples, const int sourceOffset = 0) noexcept
275  {
276  for (int i = 0; i < numDestChannels; ++i)
277  {
278  const DestType dest (addBytesToPointer (destData, i * DestType::getBytesPerSample()), numDestChannels);
279 
280  if (*source != nullptr)
281  {
282  dest.convertSamples (SourceType (*source + sourceOffset), numSamples);
283  ++source;
284  }
285  else
286  {
287  dest.clearSamples (numSamples);
288  }
289  }
290  }
291  };
292 
293 private:
294  String formatName;
295  friend class ThreadedWriter;
296 
297  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioFormatWriter)
298 };
299 
300 } // namespace juce
void clearSamples(int numSamples) const noexcept
const String & getFormatName() const noexcept
bool isFloatingPoint() const noexcept
int getBitsPerSample() const noexcept
double getSampleRate() const noexcept
void convertSamples(Pointer source, int numSamples) const noexcept
int getNumChannels() const noexcept