Skip to content

Commit 1ae1def

Browse files
authored
Merge pull request #2987 from adafruit/pcm510x_examples
PCM510x CP and Arduino examples
2 parents a11851d + 721b62d commit 1ae1def

File tree

8 files changed

+4559
-0
lines changed

8 files changed

+4559
-0
lines changed

PCM510x_Examples/Arduino_Audio_Playback/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*
6+
This example plays a 'raw' PCM file from memory to I2S
7+
*/
8+
9+
#include <I2S.h>
10+
11+
#include "startup.h" // audio file in flash
12+
13+
// Create the I2S port using a PIO state machine
14+
I2S i2s(OUTPUT);
15+
16+
// GPIO pin numbers on Feather RP2040
17+
#define pBCLK D9 // BITCLOCK
18+
#define pWS D10 // LRCLOCK
19+
#define pDOUT D11 // DATA
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
while (!Serial) delay(10);
24+
Serial.println("I2S playback demo");
25+
}
26+
27+
void loop() {
28+
}
29+
30+
void setup1() {
31+
i2s.setBCLK(pBCLK);
32+
i2s.setDATA(pDOUT);
33+
i2s.setBitsPerSample(16);
34+
}
35+
36+
void loop1() {
37+
// the main loop will tell us when it wants us to play!
38+
play_i2s(startupAudioData, sizeof(startupAudioData), startupSampleRate);
39+
delay(1000);
40+
}
41+
42+
void play_i2s(const uint8_t *data, uint32_t len, uint32_t rate) {
43+
// start I2S at the sample rate with 16-bits per sample
44+
if (!i2s.begin(rate)) {
45+
Serial.println("Failed to initialize I2S!");
46+
delay(500);
47+
i2s.end();
48+
return;
49+
}
50+
51+
for(uint32_t i=0; i<len; i++) {
52+
uint16_t sample = (uint16_t)data[i] << 6; // our data is 10 bit but we want 16 bit so we add some gain
53+
// write the same sample twice, once for left and once for the right channel
54+
i2s.write(sample);
55+
i2s.write(sample);
56+
}
57+
i2s.end();
58+
}

PCM510x_Examples/Arduino_Audio_Playback/startup.h

Lines changed: 4377 additions & 0 deletions
Large diffs are not rendered by default.

PCM510x_Examples/Arduino_Tone/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText: 2016 Sandeep Mistry
2+
// SPDX-FileCopyrightText: 2022 Earle F. Philhower, III
3+
// SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
/*
8+
This example generates a square wave based tone at a specified frequency
9+
and sample rate. Then outputs the data using the I2S interface to a
10+
MAX08357 I2S Amp Breakout board.
11+
12+
created 17 November 2016
13+
by Sandeep Mistry
14+
modified for RP2040 by Earle F. Philhower, III <earlephilhower@yahoo.com>
15+
16+
17+
bool setBCLK(pin_size_t pin);
18+
- This assigns two adjacent pins - the pin after this one (one greater)
19+
is the WS (word select) signal, which toggles before the sample for
20+
each channel is sent
21+
22+
bool setDATA(pin_size_t pin);
23+
- Sets the DOUT pin, can be any valid GPIO pin
24+
*/
25+
26+
#include <I2S.h>
27+
28+
// Create the I2S port using a PIO state machine
29+
I2S i2s(OUTPUT);
30+
31+
// GPIO pin numbers on Feather RP2040
32+
#define pBCLK D9 // BITCLOCK
33+
#define pWS D10 // LRCLOCK
34+
#define pDOUT D11 // DATA
35+
36+
const int frequency = 440; // frequency of square wave in Hz
37+
const int amplitude = 500; // amplitude of square wave
38+
const int sampleRate = 16000; // 16 KHz is a good quality
39+
40+
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
41+
42+
int16_t sample = amplitude; // current sample value
43+
int count = 0;
44+
45+
void setup() {
46+
Serial.begin(115200);
47+
while (!Serial) delay(10);
48+
Serial.println("I2S simple tone");
49+
50+
i2s.setBCLK(pBCLK);
51+
i2s.setDATA(pDOUT);
52+
i2s.setBitsPerSample(16);
53+
54+
// start I2S at the sample rate with 16-bits per sample
55+
if (!i2s.begin(sampleRate)) {
56+
Serial.println("Failed to initialize I2S!");
57+
while (1); // do nothing
58+
}
59+
60+
}
61+
62+
void loop() {
63+
if (count % halfWavelength == 0) {
64+
// invert the sample every half wavelength count multiple to generate square wave
65+
sample = -1 * sample;
66+
}
67+
68+
// write the same sample twice, once for left and once for the right channel
69+
i2s.write(sample);
70+
i2s.write(sample);
71+
72+
// increment the counter for the next sample
73+
count++;
74+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S Tone playback example.
5+
Plays a tone for one second on, one
6+
second off, in a loop.
7+
"""
8+
import time
9+
import array
10+
import math
11+
import audiocore
12+
import board
13+
import audiobusio
14+
15+
audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
16+
17+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
18+
frequency = 440 # Set this to the Hz of the tone you want to generate.
19+
length = 8000 // frequency
20+
sine_wave = array.array("h", [0] * length)
21+
for i in range(length):
22+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
23+
sine_wave_sample = audiocore.RawSample(sine_wave)
24+
25+
while True:
26+
audio.play(sine_wave_sample, loop=True)
27+
time.sleep(1)
28+
audio.stop()
29+
time.sleep(1)
413 KB
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S WAV file playback.
5+
Plays a WAV file once.
6+
"""
7+
import audiocore
8+
import board
9+
import audiobusio
10+
11+
audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
12+
13+
with open("StreetChicken.wav", "rb") as wave_file:
14+
wav = audiocore.WaveFile(wave_file)
15+
16+
print("Playing wav file!")
17+
audio.play(wav)
18+
while audio.playing:
19+
pass
20+
21+
print("Done!")

0 commit comments

Comments
 (0)