Skip to content

Commit 80b3ea3

Browse files
committed
Add STEMMA amp code.
1 parent 62a5a4d commit 80b3ea3

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

Adafruit_STEMMA_Audio_Amp/Arduino/PWMAudio_Mono_RP2040/.qt_py_rp2040.test.only

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
This example plays a tune through a mono amplifier using a simple sine wave.
3+
4+
Released to the public domain by Earle F. Philhower, III <earlephilhower@yahoo.com>
5+
6+
Adapted from stereo original example 2023 by Kattni Rembor
7+
*/
8+
9+
#include <PWMAudio.h>
10+
11+
PWMAudio pwm(0, true); // GP0 = left, GP1 = right
12+
13+
const int freq = 48000; // Output frequency for PWM
14+
15+
int16_t mono = 0;
16+
17+
const int notes[] = { 784, 880, 698, 349, 523 };
18+
const int dly[] = { 400, 500, 700, 500, 1000 };
19+
const int noteCnt = sizeof(notes) / sizeof(notes[0]);
20+
21+
int freqMono = 1;
22+
23+
double sineTable[128]; // Precompute sine wave in 128 steps
24+
25+
unsigned int cnt = 0;
26+
void cb() {
27+
while (pwm.availableForWrite()) {
28+
double now = ((double)cnt) / (double)freq;
29+
int freqScale = freqMono << 7; // Prescale by 128 to avoid FP math later on
30+
pwm.write((int16_t)(mono * sineTable[(int)(now * freqScale) & 127]));
31+
cnt++;
32+
}
33+
}
34+
35+
void setup() {
36+
// Set up sine table for waveform generation
37+
for (int i = 0; i < 128; i++) {
38+
sineTable[i] = sin(i * 2.0 * 3.14159 / 128.0);
39+
}
40+
pwm.setBuffers(4, 32); // Give larger buffers since we're are 48khz sample rate
41+
pwm.onTransmit(cb);
42+
pwm.begin(freq);
43+
}
44+
45+
void loop() {
46+
delay(1000);
47+
mono = 0;
48+
Serial.println("loop");
49+
for (int i = 0; i < noteCnt; i++) {
50+
freqMono = notes[i];
51+
mono = 5000;
52+
delay(dly[i]);
53+
}
54+
mono = 0;
55+
delay(3000);
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
CircuitPython PWM Audio Short Tone Tune Demo
7+
8+
Plays a five-note tune on a loop.
9+
"""
10+
import time
11+
import array
12+
import math
13+
import board
14+
from audiocore import RawSample
15+
from audiopwmio import PWMAudioOut as AudioOut
16+
17+
# Increase this to increase the volume of the tone.
18+
tone_volume = 0.1
19+
# The tones are provided as a frequency in Hz. You can change the current tones or
20+
# add your own to make a new tune. Follow the format with commas between values.
21+
tone_frequency = [784, 880, 698, 349, 523]
22+
23+
audio = AudioOut(board.A0)
24+
25+
while True:
26+
# Play each tone in succession.
27+
for frequency in tone_frequency:
28+
# Compute the sine wave for the current frequency.
29+
length = 8000 // frequency
30+
sine_wave = array.array("H", [0] * length)
31+
for index in range(length):
32+
sine_wave[index] = int((1 + math.sin(math.pi * 2 * index / length))
33+
* tone_volume * (2 ** 15 - 1))
34+
35+
sine_wave_sample = RawSample(sine_wave)
36+
37+
# Play the current frequency.
38+
audio.play(sine_wave_sample, loop=True)
39+
time.sleep(0.5)
40+
audio.stop()
41+
time.sleep(1)
42+
43+
# All done playing all tones; start over from the beginning.

0 commit comments

Comments
 (0)