17template <
typename SampleType>
23 const size_t numFrames = buffer.getNumFrames();
24 const size_t numChannels = buffer.getNumChannels();
27 drwav_data_format drWavFormat;
28 drWavFormat.container = drwav_container_riff;
29 drWavFormat.channels =
static_cast<drwav_uint16> (numChannels);
30 drWavFormat.sampleRate =
static_cast<drwav_uint32> (sampleRateHz);
35 drWavFormat.format = DR_WAVE_FORMAT_PCM;
36 drWavFormat.bitsPerSample = 16;
39 drWavFormat.format = DR_WAVE_FORMAT_PCM;
40 drWavFormat.bitsPerSample = 24;
43 drWavFormat.format = DR_WAVE_FORMAT_PCM;
44 drWavFormat.bitsPerSample = 32;
47 drWavFormat.format = DR_WAVE_FORMAT_IEEE_FLOAT;
48 drWavFormat.bitsPerSample = 32;
52 if (! drwav_init_file_write (&drWavHandle, fileName.c_str(), &drWavFormat,
nullptr))
59 std::vector<
float> pcmData (numFrames * numChannels);
61 for (size_t frame = 0; frame < numFrames; ++frame)
63 for (size_t channel = 0; channel < numChannels; ++channel)
64 pcmData[frame * numChannels + channel] = buffer[channel][frame];
67 drwav_write_pcm_frames (&drWavHandle, numFrames, pcmData.data());
73 std::vector<int16_t> pcmData (numFrames * numChannels);
74 constexpr double scale = 32767.0;
76 for (size_t frame = 0; frame < numFrames; ++frame)
78 for (size_t channel = 0; channel < numChannels; ++channel)
80 const double sample =
static_cast<
double> (buffer[channel][frame]);
81 const int16_t pcmValue =
static_cast<int16_t> (std::round (
hart::clamp (scale * sample
, -scale
, scale
)));
82 pcmData[frame * numChannels + channel] = pcmValue;
86 drwav_write_pcm_frames (&drWavHandle, numFrames, pcmData.data());
92 std::vector<int32_t> pcmData (numFrames * numChannels);
93 constexpr double scale = 2147483647.0;
95 for (size_t frame = 0; frame < numFrames; ++frame)
97 for (size_t channel = 0; channel < numChannels; ++channel)
99 const double sample =
static_cast<
double> (buffer[channel][frame]);
100 const int32_t pcmValue =
static_cast<int32_t> (std::round (
hart::clamp (scale * sample
, -scale
, scale
)));
101 pcmData[frame * numChannels + channel] = pcmValue;
105 drwav_write_pcm_frames (&drWavHandle, numFrames, pcmData.data());
111 std::vector<uint8_t> pcmData (numFrames * numChannels * 3);
112 constexpr double scale = 8388607.0;
114 for (size_t frame = 0; frame < numFrames; ++frame)
116 for (size_t channel = 0; channel < numChannels; ++channel)
118 const double sample =
static_cast<
double> (buffer[channel][frame]);
119 const int32_t pcmValue =
static_cast<int32_t> (std::round (
hart::clamp (scale * sample
, -scale
, scale
)));
120 size_t idx = (frame * numChannels + channel) * 3;
121 pcmData[idx] =
static_cast<uint8_t> (pcmValue & 0xff);
122 pcmData[idx + 1] =
static_cast<uint8_t> ((pcmValue >> 8) & 0xff);
123 pcmData[idx + 2] =
static_cast<uint8_t> ((pcmValue >> 16) & 0xff);
127 drwav_write_pcm_frames (&drWavHandle, numFrames, pcmData.data());
132 drwav_uninit (&drWavHandle);
static void writeBuffer(const AudioBuffer< SampleType > &buffer, const std::string &fileName, double sampleRateHz, WavFormat wavFormat=WavFormat::pcm24)
NumericType clamp(const NumericType &value, const NumericType &low, const NumericType &high)
std::clamp() replacement for C++11
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)