import audiobusio
audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
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)
for i in range(length):
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15
- 1))
sine_wave_sample = audiocore.RawSample(sine_wave)
while True:
audio.play(sine_wave_sample, loop=True)
time.sleep(1)
audio.stop()
time.sleep(1)
Now you'll hear one second of a 440Hz tone, and one second of silence.
You can try changing the 440 Hz of the tone to produce a tone of a different pitch.
Try changing the number of seconds in time.sleep() to produce longer or shorter
tones.
I2S WAV File Playback
The second example plays a WAV file. You open the file in a readable format. Then,
you play the file and, once finished, print Done playing! to the serial console. You
can use any supported wave file().
Update your code.py to the following.
Click the Download Project Bundle button below to download the necessary libraries
and the code.py file in a zip file. Extract the contents of the zip file, open the folder
that matches your CircuitPython version, and copy the StreetChicken.wav file and the
code.py file to your CIRCUITPY drive.
Your CIRCUITPY drive should now look similar to the following image:
©Adafruit Industries Page 178 of 263