| 8 | #include <array> |
| 9 | |
| 10 | class ExampleRunner { |
| 11 | Wav inputWav; |
| 12 | std::string outputPrefix; |
| 13 | |
| 14 | template<class Processor> |
| 15 | void monoProcessor(std::string name, Processor &&processor) { |
| 16 | Wav outputWav; |
| 17 | outputWav.sampleRate = inputWav.sampleRate; |
| 18 | outputWav.channels = 1; |
| 19 | |
| 20 | srand(1); |
| 21 | processor.configure(outputWav.sampleRate); |
| 22 | |
| 23 | int durationSamples = inputWav.samples.size()/inputWav.channels; |
| 24 | outputWav.samples.resize(durationSamples); |
| 25 | for (int i = 0; i < durationSamples; ++i) { |
| 26 | outputWav.samples[i] = processor.process(inputWav.samples[i*inputWav.channels]); |
| 27 | } |
| 28 | outputWav.write(outputPrefix + name + ".wav"); |
| 29 | if (!outputWav.result) { |
| 30 | std::cerr << outputWav.result.reason << "\n"; |
| 31 | std::exit(1); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | template<int channels, bool averageOutputs=true, class Processor> |
| 36 | void multiProcessor(std::string name, Processor &&processor) { |
| 37 | static_assert(channels%2 == 0, "there must be an even number of channels"); |
| 38 | |
| 39 | Wav outputWav; |
| 40 | outputWav.sampleRate = inputWav.sampleRate; |
| 41 | outputWav.channels = 2; |
| 42 | |
| 43 | srand(1); |
| 44 | processor.configure(outputWav.sampleRate); |
| 45 | |
| 46 | int durationSamples = inputWav.samples.size()/inputWav.channels; |
| 47 | outputWav.samples.resize(durationSamples*2); |
| 48 | for (int i = 0; i < durationSamples; ++i) { |
| 49 | |
| 50 | // Duplicate input channels as many times as needed |
| 51 | std::array<double, channels> array; |
| 52 | for (int c = 0; c < channels; ++c) { |
| 53 | int inputChannel = c%inputWav.channels; |
| 54 | array[c] = inputWav.samples[i*inputWav.channels + inputChannel]; |
| 55 | } |
| 56 | |
| 57 | array = processor.process(array); |
| 58 | |
| 59 | if (averageOutputs) { |
| 60 | // Mix down into stereo for example output |
| 61 | double left = 0, right = 0; |
| 62 | for (int c = 0; c < channels; c += 2) { |
| 63 | left += array[c]; |
| 64 | right += array[c + 1]; |
| 65 | } |
| 66 | |
| 67 | constexpr double scaling = 2.0/channels; |
nothing calls this directly
no outgoing calls
no test coverage detected