# SPDX-FileCopyrightText: Copyright (c) 2023 Randall Bohn (dexter)
#
# SPDX-License-Identifier: MIT
# Assisted by ProtoEngineer https://chat.openai.com/g/g-t57yKlbup-protoengineer

import board
import audiobusio
import time
import array
import math
import audiocore

# Set up the sine wave parameters
sample_rate = 8000
tone_volume = 0.1
frequency = 440
length = sample_rate // frequency
sine_wave = array.array("H", [0] * length)
for i in range(length):
    sine_wave[i] = int((math.sin(math.pi * 2 * frequency * i / sample_rate) * tone_volume + 1) * (2**15 - 1))

# Initialize I2S
audio = audiobusio.I2SOut(bit_clock=board.A1, word_select=board.CS, data=board.A0)
sine_wave_sample = audiocore.RawSample(sine_wave, sample_rate=sample_rate)

if __name__ == "__main__":
    # Play the tone for 0.6 seconds and be silent for 0.4 seconds in a loop
    while True:
        audio.play(sine_wave_sample, loop=True)
        time.sleep(0.6)
        audio.stop()
        time.sleep(0.4)