'samples' must already be large enough to contain the full output size
| 34 | |
| 35 | // 'samples' must already be large enough to contain the full output size |
| 36 | static bool ConvertAudio(AudioFormat input_format, SDL_AudioSpec &output_spec, int input_size_bytes, |
| 37 | std::vector<unsigned char> &samples) |
| 38 | { |
| 39 | SDL_AudioCVT cvt; |
| 40 | SDL_AudioFormat sdl_input_format; |
| 41 | switch (input_format.format) |
| 42 | { |
| 43 | case AudioFormat::SampleFormat::PCM_SINT16: |
| 44 | sdl_input_format = AUDIO_S16LSB; |
| 45 | break; |
| 46 | case AudioFormat::SampleFormat::PCM_UINT8: |
| 47 | sdl_input_format = AUDIO_U8; |
| 48 | break; |
| 49 | default: |
| 50 | LogWarning("Unknown input sample format"); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | int ret = |
| 55 | SDL_BuildAudioCVT(&cvt, sdl_input_format, input_format.channels, input_format.frequency, |
| 56 | output_spec.format, output_spec.channels, output_spec.freq); |
| 57 | if (ret < 0) |
| 58 | { |
| 59 | LogWarning("Failed to build AudioCVT"); |
| 60 | return false; |
| 61 | } |
| 62 | else if (ret == 0) |
| 63 | { |
| 64 | // No conversion needed |
| 65 | return true; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | cvt.len = input_size_bytes; |
| 70 | size_t neededSize = std::max(1, cvt.len_mult) * cvt.len; |
| 71 | if (samples.size() < neededSize) |
| 72 | { |
| 73 | LogInfo("Expanding sample output buffer from %zu to %zu bytes", samples.size(), |
| 74 | neededSize); |
| 75 | samples.resize(neededSize); |
| 76 | } |
| 77 | cvt.buf = (Uint8 *)samples.data(); |
| 78 | SDL_ConvertAudio(&cvt); |
| 79 | samples.resize(cvt.len_cvt); |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | class SDLSampleData : public BackendSampleData |
| 85 | { |
no test coverage detected