Skip to content

Commit ef1b897

Browse files
authored
Merge pull request #533 from papahabla/patch-1
Update CircuitPython_I2S_Tone.py
2 parents 98dd70f + 1cddf91 commit ef1b897

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Adafruit_MAX98357/CircuitPython_I2S_Tone.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
import board
66
import audiobusio
77

8-
tone_volume = 0.1 # Increase this to increase the volume of the tone.
8+
sample_rate = 8000
9+
tone_volume = .1 # Increase or decrease this to adjust the volume of the tone.
910
frequency = 440 # Set this to the Hz of the tone you want to generate.
10-
length = 8000 // frequency
11+
length = sample_rate // frequency # One freqency period
1112
sine_wave = array.array("H", [0] * length)
1213
for i in range(length):
13-
sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / 18)) * tone_volume * (2 ** 15))
14+
sine_wave[i] = int((math.sin(math.pi * 2 * frequency * i / sample_rate) *
15+
tone_volume + 1) * (2 ** 15 - 1))
1416

1517
# For Feather M0 Express, ItsyBitsy M0 Express, Metro M0 Express
1618
audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)
1719
# For Feather M4 Express
1820
# audio = audiobusio.I2SOut(board.D1, board.D10, board.D11)
1921
# For Metro M4 Express
2022
# audio = audiobusio.I2SOut(board.D3, board.D9, board.D8)
21-
sine_wave_sample = audioio.RawSample(sine_wave)
23+
sine_wave_sample = audioio.RawSample(sine_wave, sample_rate=sample_rate)
2224

2325
while True:
2426
audio.play(sine_wave_sample, loop=True)

0 commit comments

Comments
 (0)