Skip to content

Commit c37fce1

Browse files
authored
Merge pull request #2555 from kattni/stemma-audio-amp
Add STEMMA amp code.
2 parents 563d2ce + 300b1eb commit c37fce1

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Adafruit_STEMMA_Audio_Amp/Arduino/PWMAudio_Mono_RP2040/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
2+
// SPDX-FileCopyrightText: Earle F. Philhower, III
3+
//
4+
// SPDX-License-Identifier: MIT
5+
6+
/*
7+
This example plays a tune through a mono amplifier using a simple sine wave.
8+
9+
Released to the public domain by Earle F. Philhower, III <earlephilhower@yahoo.com>
10+
11+
Adapted from stereo original example 2023 by Kattni Rembor
12+
*/
13+
14+
#include <PWMAudio.h>
15+
16+
PWMAudio pwm(0, true); // GP0 = left, GP1 = right
17+
18+
const int freq = 48000; // Output frequency for PWM
19+
20+
int16_t mono = 0;
21+
22+
const int notes[] = { 784, 880, 698, 349, 523 };
23+
const int dly[] = { 400, 500, 700, 500, 1000 };
24+
const int noteCnt = sizeof(notes) / sizeof(notes[0]);
25+
26+
int freqMono = 1;
27+
28+
double sineTable[128]; // Precompute sine wave in 128 steps
29+
30+
unsigned int cnt = 0;
31+
void cb() {
32+
while (pwm.availableForWrite()) {
33+
double now = ((double)cnt) / (double)freq;
34+
int freqScale = freqMono << 7; // Prescale by 128 to avoid FP math later on
35+
pwm.write((int16_t)(mono * sineTable[(int)(now * freqScale) & 127]));
36+
cnt++;
37+
}
38+
}
39+
40+
void setup() {
41+
// Set up sine table for waveform generation
42+
for (int i = 0; i < 128; i++) {
43+
sineTable[i] = sin(i * 2.0 * 3.14159 / 128.0);
44+
}
45+
pwm.setBuffers(4, 32); // Give larger buffers since we're are 48khz sample rate
46+
pwm.onTransmit(cb);
47+
pwm.begin(freq);
48+
}
49+
50+
void loop() {
51+
delay(1000);
52+
mono = 0;
53+
Serial.println("loop");
54+
for (int i = 0; i < noteCnt; i++) {
55+
freqMono = notes[i];
56+
mono = 5000;
57+
delay(dly[i]);
58+
}
59+
mono = 0;
60+
delay(3000);
61+
}
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)