From 0a2b0d3d51da9f4027cc01bcd7656d664334d04c Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios <29906257+CedarGroveStudios@users.noreply.github.com> Date: Mon, 6 Aug 2018 18:10:02 -0700 Subject: [PATCH] Accomodate the zero baseline, reduce distortion Changed the array type to allow I2S signed values. Eliminated the baseline in the ``math.sin`` portion of the code. Updated the calculated value range to represent a maximum value of 32767 rather than 32678 to avoid distortion caused by value wrap-around. --- Adafruit_UDA1334A/CircuitPython_I2S_Tone.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_UDA1334A/CircuitPython_I2S_Tone.py b/Adafruit_UDA1334A/CircuitPython_I2S_Tone.py index ac13a0ad..2e71c275 100755 --- a/Adafruit_UDA1334A/CircuitPython_I2S_Tone.py +++ b/Adafruit_UDA1334A/CircuitPython_I2S_Tone.py @@ -8,9 +8,9 @@ import audiobusio tone_volume = 0.1 # Increase this to increase the volume of the tone. frequency = 440 # Set this to the Hz of the tone you want to generate. length = 8000 // frequency -sine_wave = array.array("H", [0] * length) +sine_wave = array.array("h", [0] * length) for i in range(length): - sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / 18)) * tone_volume * (2 ** 15)) + sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 -1)) # For Feather M0 Express, ItsyBitsy M0 Express, Metro M0 Express audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)