8 enum class bit_depth { pcm16 = 1, pcm24, pcm32, float32 };
10 constexpr
auto get_description(bit_depth b) {
12 case bit_depth::pcm16:
return "signed 16-bit pcm";
13 case bit_depth::pcm24:
return "signed 24-bit pcm";
14 case bit_depth::pcm32:
return "signed 32-bit pcm";
15 case bit_depth::float32:
return "32-bit float";
19 enum class format { wav = 1, aif };
21 constexpr
auto get_extension(format f) {
23 case format::wav:
return "wav";
24 case format::aif:
return "aif";
29 template <
typename It>
30 auto interleave(It b, It e) {
34 using value_type = std::decay_t<decltype(*cbegin(*b))>;
35 using return_type = std::vector<value_type>;
37 const auto channels = std::distance(b, e);
42 const auto frames = std::distance(cbegin(*b), cend(*b));
47 for (
auto it = b; it != e; ++it) {
48 if (std::distance(cbegin(*it), cend(*it)) != frames) {
49 throw std::runtime_error{
"All channels must have equal length."};
53 return_type interleaved(channels * frames);
54 for (
auto i = 0ul; i != channels; ++i) {
55 for (
auto j = 0ul; j != frames; ++j) {
56 interleaved[j * channels + i] = b[i][j];
64 template <
typename It>
65 auto deinterleave(It b, It e,
size_t channels) {
67 throw std::runtime_error{
"Channels must be non-zero."};
69 const auto in_size = std::distance(b, e);
70 if (in_size % channels) {
71 throw std::runtime_error{
72 "Input size must be divisible by number of channels."};
75 const auto frames = in_size / channels;
76 using value_type = std::decay_t<decltype(*b)>;
77 std::vector<std::vector<value_type>> deinterleaved(
78 channels, std::vector<value_type>(frames));
80 for (
auto i = 0ul; b != e; ++b, ++i) {
81 const auto channel = i % channels;
82 const auto sample = i / channels;
83 deinterleaved[channel][sample] = *b;
90 void write_interleaved(
const char* fname,
99 void write(
const char* fname,
104 bit_depth bit_depth) {
105 write_interleaved(fname, data, num, 1, sr, format, bit_depth);
108 template <
typename T>
109 void write(
const char* fname,
113 bit_depth bit_depth) {
114 write(fname, data.data(), data.size(), sr, format, bit_depth);
117 template <
typename T>
119 std::vector<std::vector<T>> signal;
125 template <
typename It>
126 void write(
const char* fname,
131 bit_depth bit_depth) {
132 const auto channels = std::distance(b, e);
136 const auto interleaved = interleave(b, e);
137 write_interleaved(fname,
Definition: audio_file.h:118
Definition: audio_file.h:6