0

I am trying to mix multiple WAV files into one using mixing algorithms I found in chatGPT answers and other websites.

I tried with simple step of mixing audio using the below code:

    double mixingRatio = 0.5;

    Uint8List? list = fileData1;
    for(int i = 0; i < fileData1.length; i++) {
      int mixedValue = ((1 - mixingRatio) * fileData1[i] + mixingRatio * fileData2[i]).round().clamp(0, 255);
      list[i] = mixedValue;
    }

    return list;
  }

There are too many clips and even though I am clamping (0-255). It still clips as seen in the picture. The input files are all normalized and confirmed from Audacity.

enter image description here

Looking for a mathematical solution to this simple problem. I don't want to use FFmpeg because it does not work with realtime data.

Thanks

EDIT:

This is how i am importing Raw Audio.

enter image description here

Muhammad Faizan
  • 353
  • 4
  • 15
  • What do the waveforms of the inputs look like? Also, Audacity indicates that your (output?) audio uses 32-bit floating-point samples, not 8-bit samples. If the inputs also use 32-bit floating point samples, averaging the individual bytes as you're doing isn't going to generate the correct results. – jamesdlin Apr 09 '23 at 01:09
  • Its a PCM file imported into audacity with unsigned 8 bit PCM option selected. The format you see on the left is set up by the audacity itself with only 3 formats available there: 16 bit PCM, 24 bit PCM and 32 bit float. Changing these formats is little to no effect on wavform. Imported audio file is 32 bit float with sampling rate of 44100. But when i read the files in flutter as Bytes, it returns me Uint8List of data which i have to mix with other files to produce results. – Muhammad Faizan Apr 09 '23 at 10:04
  • 8 bit PCM is pretty rare. Do the input files have a filename type (wav, au?). In audacity are you using File/Import/Audio or File/Import/Raw Data? If using Raw Data, update the question with a screenshot of the "Import Raw Data" dialog box correctly filled in. – Richard Heap Apr 09 '23 at 10:42
  • I am importing Wav by just drag and drop. but for the result. I upload it using rawData as you mentioned. I will update the answer with screenshot. – Muhammad Faizan Apr 09 '23 at 12:29

1 Answers1

0

In 8 bit unsigned PCM, the valid range is 0-255, with 128 as the 'zero' value. (Values above 128 are in the top half of the waveform and below 128 in the bottom half.)

Before you can perform maths (e.g. scaling, addition) on these values you need to normalise them. This is actually as simple as subtracting 128, so that you end up with an int in the range -128 to 127. You can now scale and add these values.

Of course, before storing the resulting, clamped (between -128 and 127) value back into an unsigned PCM byte array, you need to add back the bias value.

For example:

  final mixedValue = ((1 - mixingRatio) * (fileData1[i] - 128) +
              mixingRatio * (fileData2[i] - 128))
          .round()
          .clamp(-128, 127) +
      128;

By the way, you seem to be overwriting fileData1 with the result. This may not be what you intended. Double check this, because this may be your issue. The two formulae give different values. Try this:

void main() {
  print(oldWay(0.5, 113, 143)); // prints 113
  print(newWay(0.5, 113, 143)); // prints 128 (the correct value)
}

int oldWay(double r, int a, int b) {
  return ((1 - r) * a + r * a).round().clamp(0, 255);
}

int newWay(double r, int a, int b) {
  return ((1 - r) * (a - 128) + r * (b - 128)).round().clamp(-128, 127) + 128;
}
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • Sadly, it didn't work. When i compared my mixedValue result with yours they are almost in same. FORMAT: MIXED VALUES: "MY CODE" | "UPDATED CODE". MIXED VALUES: 53 | 52 MIXED VALUES: 135 | 135 MIXED VALUES: 132 | 132 MIXED VALUES: 156 | 156 – Muhammad Faizan Apr 09 '23 at 12:45
  • Thanks for updating your answer but you made a mistake in old way formula, you accidentally put a in the place of b. Formula should be: ((1 - r) * a + r * b).round().clamp(0, 255); – Muhammad Faizan Apr 09 '23 at 14:25