Skip to content

MacOS example making incorrect waveform / bug in transform code #94

@larsmob

Description

@larsmob

I was playing around with the MacOS example. First, I noticed that the waveform seemed very gained/amplified.
Then I noticed the data is actually wrapping around - one example:
-27649.0, -28673.0, -29953.0, -31489.0, 32767.0, 31999.0, 31743.0, 32511.0, -31489.0, -29441.0,
Something is wrong.

I started by looking at your swift code.
I verified the waveform looks good here: let clamped = min(max(-2.0, val), 2.0)
But you could consider clamping at +/-4 instead. (Not sure what is supposed to be max, but I can easily produce values even higher than 4).

Then I removed the .transform(MicStream.toSampleStream) and looked at the data - the bytes are swapped (LSB comes first).
So then I wrote this to handle byte swapping and the two's complement representation:

   int UInt16Max = pow(2, 16).toInt();
  void _raw(samples) {
    List<int> result = [];
    for (var i = 0; i < samples.length~/2; i++) {
      int a = samples[2*i + 1];
      int b = samples[2*i];
      int c = 256*a + b;
      if (2*c > UInt16Max) {
        c = -UInt16Max + c;
      }
      result.add(c);
    }
    // result now has correct data
    ...
  }

  stream = MicStream.microphone(
        audioSource: AudioSource.DEFAULT,
        sampleRate: AppDefaults.fs,
        channelConfig: ChannelConfig.CHANNEL_IN_MONO,
        audioFormat: AudioFormat.ENCODING_PCM_16BIT);
  listener = stream!
    .listen(_raw);

Data looks good now. I haven't looked carefully at your code, but something is quite wrong with the transform.

Thanks for a very nice piece of software.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions