ape  0.5.0
Audio Programming Environment
outputfile.h
Go to the documentation of this file.
1 #ifndef CPPAPE_OUTPUTFILE_H
2 #define CPPAPE_OUTPUTFILE_H
3 
4 #include "baselib.h"
5 #include "misc.h"
6 #include "processor.h"
7 
8 namespace ape
9 {
14  {
15  public:
16 
20  enum class BitDepth
21  {
22  Int8 = 8,
23  Int16 = 16,
24  Int24 = 24,
25  Float32 = 32
26  };
27 
31  OutputAudioFile() : fd(0), channels(0) {}
32 
63  OutputAudioFile(const char* whereWithExtension, int numChannels, double sampleRate, BitDepth bitsPerSample = BitDepth::Int24, float quality = 1)
64  {
65  channels = numChannels;
66 
67  fd = getInterface().createAudioOutputFile(
68  &getInterface(),
69  whereWithExtension,
70  sampleRate,
71  numChannels,
72  (int)bitsPerSample,
73  quality
74  );
75 
76  if (!fd)
77  abort("Couldn't create audio file");
78  }
79 
107  OutputAudioFile(const char* whereWithExtension, const IOConfig& cfg, BitDepth bitsPerSample = BitDepth::Int24, float quality = 1)
108  : OutputAudioFile(whereWithExtension, cfg.outputs, cfg.sampleRate, bitsPerSample, quality)
109  {
110 
111  }
112 
117  : fd(other.fd), channels(other.channels)
118  {
119  other.fd = 0;
120  other.channels = 0;
121  }
122 
127  {
128  if (fd)
129  {
130  getInterface().closeAudioFile(&getInterface(), fd);
131  }
132 
133  fd = other.fd;
134  channels = other.channels;
135 
136  other.fd = 0;
137  other.channels = 0;
138 
139  return *this;
140  }
141 
145  void write(const umatrix<const float>& matrix)
146  {
147  if (!fd)
148  abort("Audio file not initialized");
149 
150  if (channels != matrix.channels())
151  abort("Mismatched channel count");
152 
153  write(matrix.samples(), matrix.pointers());
154  }
155 
162  template<typename Container>
163  auto write(const Container& c) -> decltype(c.size(), c.data(), void())
164  {
165  if (channels != 1)
166  abort("Mismatched channel count");
167 
168  write(c.size(), c.data());
169  }
170 
177  void write(std::size_t numSamples, const float* data)
178  {
179  getInterface().writeAudioFile(&getInterface(), fd, (int)numSamples, &data);
180  }
181 
188  void write(std::size_t numSamples, const float* const* data)
189  {
190  getInterface().writeAudioFile(&getInterface(), fd, (int)numSamples, data);
191  }
192 
197  {
198  if (fd)
199  getInterface().closeAudioFile(&getInterface(), fd);
200  }
201 
202  private:
203  int fd;
204  int channels;
205  };
206 
207 }
208 
209 #endif
ape::getInterface
APE_SharedInterface & getInterface()
Acquire the low-level C API.
ape::IOConfig
Configuration structure with information needed for running a plugin.
Definition: processor.h:15
ape::OutputAudioFile::OutputAudioFile
OutputAudioFile(const char *whereWithExtension, int numChannels, double sampleRate, BitDepth bitsPerSample=BitDepth::Int24, float quality=1)
Create an output file ready to be streamed.
Definition: outputfile.h:63
ape::OutputAudioFile::write
void write(std::size_t numSamples, const float *data)
Stream a flat buffer.
Definition: outputfile.h:177
ape::umatrix< const float >
ape::umatrix::pointers
auto pointers()
Returns a possibly cv-qualified T * const*
Definition: misc.h:341
ape::OutputAudioFile::BitDepth::Float32
ape::OutputAudioFile
Provides capability to record streamed audio asynchronously to a file on disk.
Definition: outputfile.h:13
abort
void abort(const char *reason)
Terminate the script (not the host application!) safely, with a reason. All resources will automatica...
ape::OutputAudioFile::OutputAudioFile
OutputAudioFile()
Default-initialize an output file.
Definition: outputfile.h:31
ape::umatrix::channels
std::size_t channels() const noexcept
Definition: misc.h:328
ape::OutputAudioFile::OutputAudioFile
OutputAudioFile(const char *whereWithExtension, const IOConfig &cfg, BitDepth bitsPerSample=BitDepth::Int24, float quality=1)
Create an output file ready to be streamed.
Definition: outputfile.h:107
misc.h
ape::OutputAudioFile::write
void write(const umatrix< const float > &matrix)
Stream the matrix to the file.
Definition: outputfile.h:145
ape::OutputAudioFile::write
auto write(const Container &c) -> decltype(c.size(), c.data(), void())
Stream a container supporting .size() and .data()
Definition: outputfile.h:163
processor.h
ape
Definition: audiofile.h:7
ape::OutputAudioFile::~OutputAudioFile
~OutputAudioFile()
Close this file, if it hasn't been moved or properly initialized
Definition: outputfile.h:196
ape::OutputAudioFile::BitDepth::Int8
ape::umatrix::samples
std::size_t samples() const noexcept
Definition: misc.h:324
ape::OutputAudioFile::BitDepth::Int24
ape::OutputAudioFile::write
void write(std::size_t numSamples, const float *const *data)
Stream a flat rectangular buffer
Definition: outputfile.h:188
ape::OutputAudioFile::OutputAudioFile
OutputAudioFile(OutputAudioFile &&other)
Move and take ownership of the other file
Definition: outputfile.h:116
baselib.h
ape::OutputAudioFile::BitDepth
BitDepth
For non-compressed formats supporting it, provides a choice of bitdepths
Definition: outputfile.h:20
ape::OutputAudioFile::BitDepth::Int16
ape::OutputAudioFile::operator=
OutputAudioFile & operator=(OutputAudioFile &&other)
Move-assign and take ownership of the other file
Definition: outputfile.h:126