code.py
 1  # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  """
 4  CircuitPython I2S Tone playback example.
 5  Plays a tone for one second on, one
 6  second off, in a loop.
 7  """
 8  import time
 9  import array
10  import math
11  import audiocore
12  import board
13  import audiobusio
14  
15  audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
16  
17  tone_volume = 0.1  # Increase this to increase the volume of the tone.
18  frequency = 440  # Set this to the Hz of the tone you want to generate.
19  length = 8000 // frequency
20  sine_wave = array.array("h", [0] * length)
21  for i in range(length):
22      sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
23  sine_wave_sample = audiocore.RawSample(sine_wave)
24  
25  while True:
26      audio.play(sine_wave_sample, loop=True)
27      time.sleep(1)
28      audio.stop()
29      time.sleep(1)