Skip to content

Distortion on 8-bit audio sample caused by audiomixer.MixerVoice.level #10574

@relic-se

Description

@relic-se

CircuitPython version and board name

Adafruit CircuitPython 10.0.0-beta.2-7-g117b5d1ed9 on 2025-08-12; Adafruit Fruit Jam with rp2350b

Code/REPL

import audiobusio
import audiocore
import audiomixer
import board
import time

import adafruit_tlv320

# setup audio
i2c = board.I2C()
dac = adafruit_tlv320.TLV320DAC3100(i2c)

# load wave file
wave = audiocore.WaveFile("test16.wav")

# set sample rate & bit depth
dac.configure_clocks(
    sample_rate=wave.sample_rate,
    bit_depth=wave.bits_per_sample,
)

# use headphones
dac.headphone_output = True
dac.headphone_volume = -15  # dB

# setup audio output
audio_config = {
    "channel_count": wave.channel_count,
    "sample_rate": wave.sample_rate,
    "bits_per_sample": wave.bits_per_sample,
    "samples_signed": wave.bits_per_sample >= 16,
}
print(audio_config)
audio = audiobusio.I2SOut(board.I2S_BCLK, board.I2S_WS, board.I2S_DIN)
mixer = audiomixer.Mixer(**audio_config)
audio.play(mixer)
mixer.play(wave, loop=True)

while True:
    for i in range(11):
        mixer.voice[0].level = (10 - i) / 10
        print(mixer.voice[0].level)
        time.sleep(2)

Behavior

When playing an unsigned 8-bit WAV file, significant distortion is heard when the audio level approaches lower values. This effect is not heard when playing 16-bit WAV files.

REPL Output:

{'bits_per_sample': 8, 'samples_signed': False, 'channel_count': 1, 'sample_rate': 11025}
1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0.0

Description

This is likely due to an error in converting unsigned to signed samples somewhere within

uint32_t word = unpack8(hsrc[i]);
if (MP_LIKELY(!self->base.samples_signed)) {
word = tosigned16(word);
}
word = mult16signed(word, level);
hword_buffer[i] = pack8(word);
.

Additional information

The audio files were generated using the following commands:

$ ffmpeg -i test.wav -f wav -ar 11025 -ac 1 -acodec pcm_u8 test8.wav
$ ffmpeg -i test.wav -f wav -ar 11025 -ac 1 -acodec pcm_s16le test16.wav

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions