diff --git a/Adafruit_STEMMA_Audio_Amp/Arduino/PWMAudio_Mono_RP2040/.qt_py_rp2040.test.only b/Adafruit_STEMMA_Audio_Amp/Arduino/PWMAudio_Mono_RP2040/.qt_py_rp2040.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/Adafruit_STEMMA_Audio_Amp/Arduino/PWMAudio_Mono_RP2040/PWMAudio_Mono_RP2040.ino b/Adafruit_STEMMA_Audio_Amp/Arduino/PWMAudio_Mono_RP2040/PWMAudio_Mono_RP2040.ino new file mode 100644 index 000000000..13caa521d --- /dev/null +++ b/Adafruit_STEMMA_Audio_Amp/Arduino/PWMAudio_Mono_RP2040/PWMAudio_Mono_RP2040.ino @@ -0,0 +1,56 @@ +/* + This example plays a tune through a mono amplifier using a simple sine wave. + + Released to the public domain by Earle F. Philhower, III + + Adapted from stereo original example 2023 by Kattni Rembor +*/ + +#include + +PWMAudio pwm(0, true); // GP0 = left, GP1 = right + +const int freq = 48000; // Output frequency for PWM + +int16_t mono = 0; + +const int notes[] = { 784, 880, 698, 349, 523 }; +const int dly[] = { 400, 500, 700, 500, 1000 }; +const int noteCnt = sizeof(notes) / sizeof(notes[0]); + +int freqMono = 1; + +double sineTable[128]; // Precompute sine wave in 128 steps + +unsigned int cnt = 0; +void cb() { + while (pwm.availableForWrite()) { + double now = ((double)cnt) / (double)freq; + int freqScale = freqMono << 7; // Prescale by 128 to avoid FP math later on + pwm.write((int16_t)(mono * sineTable[(int)(now * freqScale) & 127])); + cnt++; + } +} + +void setup() { + // Set up sine table for waveform generation + for (int i = 0; i < 128; i++) { + sineTable[i] = sin(i * 2.0 * 3.14159 / 128.0); + } + pwm.setBuffers(4, 32); // Give larger buffers since we're are 48khz sample rate + pwm.onTransmit(cb); + pwm.begin(freq); +} + +void loop() { + delay(1000); + mono = 0; + Serial.println("loop"); + for (int i = 0; i < noteCnt; i++) { + freqMono = notes[i]; + mono = 5000; + delay(dly[i]); + } + mono = 0; + delay(3000); +} diff --git a/Adafruit_STEMMA_Audio_Amp/CircuitPython/code.py b/Adafruit_STEMMA_Audio_Amp/CircuitPython/code.py new file mode 100644 index 000000000..18877fb67 --- /dev/null +++ b/Adafruit_STEMMA_Audio_Amp/CircuitPython/code.py @@ -0,0 +1,43 @@ +# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +CircuitPython PWM Audio Short Tone Tune Demo + +Plays a five-note tune on a loop. +""" +import time +import array +import math +import board +from audiocore import RawSample +from audiopwmio import PWMAudioOut as AudioOut + +# Increase this to increase the volume of the tone. +tone_volume = 0.1 +# The tones are provided as a frequency in Hz. You can change the current tones or +# add your own to make a new tune. Follow the format with commas between values. +tone_frequency = [784, 880, 698, 349, 523] + +audio = AudioOut(board.A0) + +while True: + # Play each tone in succession. + for frequency in tone_frequency: + # Compute the sine wave for the current frequency. + length = 8000 // frequency + sine_wave = array.array("H", [0] * length) + for index in range(length): + sine_wave[index] = int((1 + math.sin(math.pi * 2 * index / length)) + * tone_volume * (2 ** 15 - 1)) + + sine_wave_sample = RawSample(sine_wave) + + # Play the current frequency. + audio.play(sine_wave_sample, loop=True) + time.sleep(0.5) + audio.stop() + time.sleep(1) + + # All done playing all tones; start over from the beginning.