From 79067e661558983f6811f1281f21c5073d0d19c6 Mon Sep 17 00:00:00 2001 From: falkTX Date: Wed, 10 Mar 2021 14:22:10 +0000 Subject: [PATCH] eg-sampler: Convert an arbitrary non-mono audio files to mono Signed-off-by: falkTX --- plugins/eg-sampler.lv2/sampler.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/plugins/eg-sampler.lv2/sampler.c b/plugins/eg-sampler.lv2/sampler.c index 6c64df51..9675003d 100644 --- a/plugins/eg-sampler.lv2/sampler.c +++ b/plugins/eg-sampler.lv2/sampler.c @@ -95,6 +95,23 @@ typedef struct { Sample* sample; } SampleMessage; +/** + Convert an arbitrary non-mono audio buffer to mono. + + This simply ignores the data on all channels but the first. +*/ +static sf_count_t +convert_to_mono(float *data, sf_count_t num_input_frames, uint32_t channels) +{ + sf_count_t num_output_frames = 0; + + for (sf_count_t i = 0; i < num_input_frames * channels; i += channels) { + data[num_output_frames++] = data[i]; + } + + return num_output_frames; +} + /** Load a new sample and return it. @@ -115,9 +132,7 @@ load_sample(LV2_Log_Logger* logger, const char* path) bool error = true; if (!sndfile || !info->frames) { lv2_log_error(logger, "Failed to open %s\n", path); - } else if (info->channels != 1) { - lv2_log_error(logger, "%s has %d channels\n", path, info->channels); - } else if (!(data = (float*)malloc(sizeof(float) * info->frames))) { + } else if (!(data = (float*)malloc(sizeof(float) * info->frames * info->channels))) { lv2_log_error(logger, "Failed to allocate memory for sample\n"); } else { error = false; @@ -131,9 +146,14 @@ load_sample(LV2_Log_Logger* logger, const char* path) } sf_seek(sndfile, 0ul, SEEK_SET); - sf_read_float(sndfile, data, info->frames); + sf_read_float(sndfile, data, info->frames * info->channels); sf_close(sndfile); + if (info->channels != 1) { + info->frames = convert_to_mono(data, info->frames, info->channels); + info->channels = 1; + } + // Fill sample struct and return it sample->data = data; sample->path = (char*)malloc(path_len + 1);