From 3413f193c1951cb724eb563f4beab472971e73c8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 4 Mar 2023 12:57:30 +0100 Subject: Add opus/flac audio options (only supported my mp4/mkv) --- src/sound.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/sound.cpp') diff --git a/src/sound.cpp b/src/sound.cpp index 9083f1e..24e979b 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -227,20 +227,38 @@ static int pa_sound_device_read(pa_handle *p) { return success ? 0 : -1; } -int sound_device_get_by_name(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size) { +static pa_sample_format_t audio_format_to_pulse_audio_format(AudioFormat audio_format) { + switch(audio_format) { + case S16: return PA_SAMPLE_S16LE; + case S32: return PA_SAMPLE_S32LE; + } + assert(false); + return PA_SAMPLE_S16LE; +} + +static int audio_format_to_get_bytes_per_sample(AudioFormat audio_format) { + switch(audio_format) { + case S16: return 2; + case S32: return 4; + } + assert(false); + return 2; +} + +int sound_device_get_by_name(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format) { pa_sample_spec ss; - ss.format = PA_SAMPLE_S16LE; + ss.format = audio_format_to_pulse_audio_format(audio_format); ss.rate = 48000; ss.channels = num_channels; - int error; pa_buffer_attr buffer_attr; buffer_attr.tlength = -1; buffer_attr.prebuf = -1; buffer_attr.minreq = -1; - buffer_attr.maxlength = period_frame_size * 2 * num_channels; // 2 bytes/sample, @num_channels channels + buffer_attr.maxlength = period_frame_size * audio_format_to_get_bytes_per_sample(audio_format) * num_channels; // 2/4 bytes/sample, @num_channels channels buffer_attr.fragsize = buffer_attr.maxlength; + int error = 0; pa_handle *handle = pa_sound_device_new(nullptr, description, device_name, description, &ss, &buffer_attr, &error); if(!handle) { fprintf(stderr, "pa_sound_device_new() failed: %s. Audio input device %s might not be valid\n", pa_strerror(error), description); @@ -337,4 +355,4 @@ std::vector get_pulseaudio_inputs() { pa_mainloop_free(main_loop); return inputs; -} \ No newline at end of file +} -- cgit v1.2.3