External, Non-PPA KXStudio Repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.2KB

  1. From 79067e661558983f6811f1281f21c5073d0d19c6 Mon Sep 17 00:00:00 2001
  2. From: falkTX <falktx@falktx.com>
  3. Date: Wed, 10 Mar 2021 14:22:10 +0000
  4. Subject: [PATCH] eg-sampler: Convert an arbitrary non-mono audio files to mono
  5. Signed-off-by: falkTX <falktx@falktx.com>
  6. ---
  7. plugins/eg-sampler.lv2/sampler.c | 28 ++++++++++++++++++++++++----
  8. 1 file changed, 24 insertions(+), 4 deletions(-)
  9. diff --git a/plugins/eg-sampler.lv2/sampler.c b/plugins/eg-sampler.lv2/sampler.c
  10. index 6c64df51..9675003d 100644
  11. --- a/plugins/eg-sampler.lv2/sampler.c
  12. +++ b/plugins/eg-sampler.lv2/sampler.c
  13. @@ -95,6 +95,23 @@ typedef struct {
  14. Sample* sample;
  15. } SampleMessage;
  16. +/**
  17. + Convert an arbitrary non-mono audio buffer to mono.
  18. +
  19. + This simply ignores the data on all channels but the first.
  20. +*/
  21. +static sf_count_t
  22. +convert_to_mono(float *data, sf_count_t num_input_frames, uint32_t channels)
  23. +{
  24. + sf_count_t num_output_frames = 0;
  25. +
  26. + for (sf_count_t i = 0; i < num_input_frames * channels; i += channels) {
  27. + data[num_output_frames++] = data[i];
  28. + }
  29. +
  30. + return num_output_frames;
  31. +}
  32. +
  33. /**
  34. Load a new sample and return it.
  35. @@ -115,9 +132,7 @@ load_sample(LV2_Log_Logger* logger, const char* path)
  36. bool error = true;
  37. if (!sndfile || !info->frames) {
  38. lv2_log_error(logger, "Failed to open %s\n", path);
  39. - } else if (info->channels != 1) {
  40. - lv2_log_error(logger, "%s has %d channels\n", path, info->channels);
  41. - } else if (!(data = (float*)malloc(sizeof(float) * info->frames))) {
  42. + } else if (!(data = (float*)malloc(sizeof(float) * info->frames * info->channels))) {
  43. lv2_log_error(logger, "Failed to allocate memory for sample\n");
  44. } else {
  45. error = false;
  46. @@ -131,9 +146,14 @@ load_sample(LV2_Log_Logger* logger, const char* path)
  47. }
  48. sf_seek(sndfile, 0ul, SEEK_SET);
  49. - sf_read_float(sndfile, data, info->frames);
  50. + sf_read_float(sndfile, data, info->frames * info->channels);
  51. sf_close(sndfile);
  52. + if (info->channels != 1) {
  53. + info->frames = convert_to_mono(data, info->frames, info->channels);
  54. + info->channels = 1;
  55. + }
  56. +
  57. // Fill sample struct and return it
  58. sample->data = data;
  59. sample->path = (char*)malloc(path_len + 1);