code.py
 1  # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  import array
 7  import math
 8  import audiocore
 9  import board
10  import audiobusio
11  
12  sample_rate = 8000
13  tone_volume = .1  # Increase or decrease this to adjust the volume of the tone.
14  frequency = 440  # Set this to the Hz of the tone you want to generate.
15  length = sample_rate // frequency  # One freqency period
16  sine_wave = array.array("H", [0] * length)
17  for i in range(length):
18      sine_wave[i] = int((math.sin(math.pi * 2 * frequency * i / sample_rate) *
19                          tone_volume + 1) * (2 ** 15 - 1))
20  
21  # For Feather M0 Express, ItsyBitsy M0 Express, Metro M0 Express
22  audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)
23  # For Feather M4 Express
24  # audio = audiobusio.I2SOut(board.D1, board.D10, board.D11)
25  # For Metro M4 Express
26  # audio = audiobusio.I2SOut(board.D3, board.D9, board.D8)
27  sine_wave_sample = audiocore.RawSample(sine_wave, sample_rate=sample_rate)
28  
29  while True:
30      audio.play(sine_wave_sample, loop=True)
31      time.sleep(1)
32      audio.stop()
33      time.sleep(1)