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