Repository navigation
Expand file tree
/
Copy pathconversion.cpp
More file actions
169 lines (156 loc) · 6.9 KB
/
Copy pathconversion.cpp
File metadata and controls
169 lines (156 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "engine/framework/audio/conversion.h"
#include "engine/framework/audio/resampling.h"
#include <algorithm>
#include <stdexcept>
namespace engine::audio {
std::vector<float> mixdown_interleaved_to_mono_average(
const std::vector<float> & interleaved_samples,
int channel_count,
MonoMixAccumulation accumulation) {
if (channel_count <= 0) {
throw std::runtime_error("audio channel count must be positive");
}
if (interleaved_samples.size() % static_cast<size_t>(channel_count) != 0) {
throw std::runtime_error("interleaved audio sample count must be divisible by channel count");
}
if (channel_count == 1) {
return interleaved_samples;
}
const size_t frames = interleaved_samples.size() / static_cast<size_t>(channel_count);
std::vector<float> mono(frames, 0.0F);
for (size_t frame = 0; frame < frames; ++frame) {
if (accumulation == MonoMixAccumulation::Float64) {
double sum = 0.0;
for (int channel = 0; channel < channel_count; ++channel) {
sum += interleaved_samples[frame * static_cast<size_t>(channel_count) + static_cast<size_t>(channel)];
}
mono[frame] = static_cast<float>(sum / static_cast<double>(channel_count));
} else {
float sum = 0.0F;
for (int channel = 0; channel < channel_count; ++channel) {
sum += interleaved_samples[frame * static_cast<size_t>(channel_count) + static_cast<size_t>(channel)];
}
mono[frame] = sum / static_cast<float>(channel_count);
}
}
return mono;
}
std::vector<float> duplicate_mono_to_interleaved_channels(
const std::vector<float> & mono_samples,
int target_channel_count) {
if (target_channel_count <= 0) {
throw std::runtime_error("target channel count must be positive");
}
std::vector<float> interleaved(mono_samples.size() * static_cast<size_t>(target_channel_count), 0.0F);
for (size_t frame = 0; frame < mono_samples.size(); ++frame) {
for (int channel = 0; channel < target_channel_count; ++channel) {
interleaved[frame * static_cast<size_t>(target_channel_count) + static_cast<size_t>(channel)] =
mono_samples[frame];
}
}
return interleaved;
}
std::vector<float> deinterleave_to_planar_channels(
const std::vector<float> & interleaved_samples,
int channel_count) {
if (channel_count <= 0) {
throw std::runtime_error("audio channel count must be positive");
}
if (interleaved_samples.size() % static_cast<size_t>(channel_count) != 0) {
throw std::runtime_error("interleaved audio sample count must be divisible by channel count");
}
const int64_t frames = static_cast<int64_t>(interleaved_samples.size() / static_cast<size_t>(channel_count));
std::vector<float> planar(static_cast<size_t>(frames * channel_count), 0.0F);
#ifdef _OPENMP
#pragma omp parallel for collapse(2) if(frames * channel_count >= 1 << 14)
#endif
for (int64_t frame = 0; frame < frames; ++frame) {
for (int channel = 0; channel < channel_count; ++channel) {
planar[static_cast<size_t>(channel * frames + frame)] =
interleaved_samples[static_cast<size_t>(frame * channel_count + channel)];
}
}
return planar;
}
std::vector<float> interleave_planar_channels(
const std::vector<float> & planar_samples,
int channel_count,
int64_t frame_count) {
if (channel_count <= 0 || frame_count < 0) {
throw std::runtime_error("planar audio interleave shape is invalid");
}
if (planar_samples.size() != static_cast<size_t>(channel_count * frame_count)) {
throw std::runtime_error("planar audio sample count does not match channel/frame shape");
}
std::vector<float> interleaved(static_cast<size_t>(frame_count * channel_count), 0.0F);
#ifdef _OPENMP
#pragma omp parallel for collapse(2) if(frame_count * channel_count >= 1 << 14)
#endif
for (int64_t frame = 0; frame < frame_count; ++frame) {
for (int channel = 0; channel < channel_count; ++channel) {
interleaved[static_cast<size_t>(frame * channel_count + channel)] =
planar_samples[static_cast<size_t>(channel * frame_count + frame)];
}
}
return interleaved;
}
std::vector<float> extract_interleaved_channel(
const std::vector<float> & interleaved_samples,
int channel_count,
int channel_index) {
if (channel_count <= 0 || channel_index < 0 || channel_index >= channel_count) {
throw std::runtime_error("interleaved audio channel selection is invalid");
}
if (interleaved_samples.size() % static_cast<size_t>(channel_count) != 0) {
throw std::runtime_error("interleaved audio sample count must be divisible by channel count");
}
const size_t frames = interleaved_samples.size() / static_cast<size_t>(channel_count);
std::vector<float> channel(frames, 0.0F);
for (size_t frame = 0; frame < frames; ++frame) {
channel[frame] = interleaved_samples[frame * static_cast<size_t>(channel_count) + static_cast<size_t>(channel_index)];
}
return channel;
}
std::vector<float> convert_wav_to_mono_linear_resampled(
const WavData & wav,
int target_sample_rate_hz) {
if (wav.sample_rate <= 0 || target_sample_rate_hz <= 0) {
throw std::runtime_error("audio sample rates must be positive");
}
auto mono = mixdown_interleaved_to_mono_average(wav.samples, wav.channels);
if (wav.sample_rate != target_sample_rate_hz) {
mono = resample_mono_linear(mono, wav.sample_rate, target_sample_rate_hz);
}
return mono;
}
std::vector<float> convert_interleaved_audio_to_mono_linear_resampled(
const std::vector<float> & interleaved_samples,
int sample_rate_hz,
int channel_count,
int target_sample_rate_hz) {
return convert_wav_to_mono_linear_resampled(
WavData{sample_rate_hz, channel_count, interleaved_samples},
target_sample_rate_hz);
}
std::vector<float> convert_interleaved_audio_to_mono_torchaudio_sinc_hann_resampled(
const std::vector<float> & interleaved_samples,
int sample_rate_hz,
int channel_count,
int target_sample_rate_hz,
const TorchaudioSincHannResampleOptions & options,
MonoMixAccumulation accumulation) {
if (sample_rate_hz <= 0 || target_sample_rate_hz <= 0) {
throw std::runtime_error("audio sample rates must be positive");
}
auto mono = mixdown_interleaved_to_mono_average(interleaved_samples, channel_count, accumulation);
if (sample_rate_hz != target_sample_rate_hz) {
mono = resample_mono_torchaudio_sinc_hann(mono, sample_rate_hz, target_sample_rate_hz, options);
}
return mono;
}
std::vector<float> read_wav_f32_as_mono_linear_resampled(
const std::filesystem::path & path,
int target_sample_rate_hz) {
return convert_wav_to_mono_linear_resampled(read_wav_f32(path), target_sample_rate_hz);
}
} // namespace engine::audio