diff --git a/PCM510x_Examples/Arduino_Audio_Playback/.feather_rp2040.test.only b/PCM510x_Examples/Arduino_Audio_Playback/.feather_rp2040.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/PCM510x_Examples/Arduino_Audio_Playback/Arduino_Audio_Playback.ino b/PCM510x_Examples/Arduino_Audio_Playback/Arduino_Audio_Playback.ino new file mode 100644 index 000000000..463b0f6ab --- /dev/null +++ b/PCM510x_Examples/Arduino_Audio_Playback/Arduino_Audio_Playback.ino @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/* + This example plays a 'raw' PCM file from memory to I2S +*/ + +#include + +#include "startup.h" // audio file in flash + +// Create the I2S port using a PIO state machine +I2S i2s(OUTPUT); + +// GPIO pin numbers on Feather RP2040 +#define pBCLK D9 // BITCLOCK +#define pWS D10 // LRCLOCK +#define pDOUT D11 // DATA + +void setup() { + Serial.begin(115200); + while (!Serial) delay(10); + Serial.println("I2S playback demo"); +} + +void loop() { +} + +void setup1() { + i2s.setBCLK(pBCLK); + i2s.setDATA(pDOUT); + i2s.setBitsPerSample(16); +} + +void loop1() { + // the main loop will tell us when it wants us to play! + play_i2s(startupAudioData, sizeof(startupAudioData), startupSampleRate); + delay(1000); +} + +void play_i2s(const uint8_t *data, uint32_t len, uint32_t rate) { + // start I2S at the sample rate with 16-bits per sample + if (!i2s.begin(rate)) { + Serial.println("Failed to initialize I2S!"); + delay(500); + i2s.end(); + return; + } + + for(uint32_t i=0; i + + + bool setBCLK(pin_size_t pin); + - This assigns two adjacent pins - the pin after this one (one greater) + is the WS (word select) signal, which toggles before the sample for + each channel is sent + + bool setDATA(pin_size_t pin); + - Sets the DOUT pin, can be any valid GPIO pin +*/ + +#include + +// Create the I2S port using a PIO state machine +I2S i2s(OUTPUT); + +// GPIO pin numbers on Feather RP2040 +#define pBCLK D9 // BITCLOCK +#define pWS D10 // LRCLOCK +#define pDOUT D11 // DATA + +const int frequency = 440; // frequency of square wave in Hz +const int amplitude = 500; // amplitude of square wave +const int sampleRate = 16000; // 16 KHz is a good quality + +const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave + +int16_t sample = amplitude; // current sample value +int count = 0; + +void setup() { + Serial.begin(115200); + while (!Serial) delay(10); + Serial.println("I2S simple tone"); + + i2s.setBCLK(pBCLK); + i2s.setDATA(pDOUT); + i2s.setBitsPerSample(16); + + // start I2S at the sample rate with 16-bits per sample + if (!i2s.begin(sampleRate)) { + Serial.println("Failed to initialize I2S!"); + while (1); // do nothing + } + +} + +void loop() { + if (count % halfWavelength == 0) { + // invert the sample every half wavelength count multiple to generate square wave + sample = -1 * sample; + } + + // write the same sample twice, once for left and once for the right channel + i2s.write(sample); + i2s.write(sample); + + // increment the counter for the next sample + count++; +} diff --git a/PCM510x_Examples/CircuitPython_Tone/code.py b/PCM510x_Examples/CircuitPython_Tone/code.py new file mode 100644 index 000000000..0c9047d1e --- /dev/null +++ b/PCM510x_Examples/CircuitPython_Tone/code.py @@ -0,0 +1,29 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython I2S Tone playback example. +Plays a tone for one second on, one +second off, in a loop. +""" +import time +import array +import math +import audiocore +import board +import audiobusio + +audio = audiobusio.I2SOut(board.D9, board.D10, board.D11) + +tone_volume = 0.1 # Increase this to increase the volume of the tone. +frequency = 440 # Set this to the Hz of the tone you want to generate. +length = 8000 // frequency +sine_wave = array.array("h", [0] * length) +for i in range(length): + sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1)) +sine_wave_sample = audiocore.RawSample(sine_wave) + +while True: + audio.play(sine_wave_sample, loop=True) + time.sleep(1) + audio.stop() + time.sleep(1) diff --git a/PCM510x_Examples/CircuitPython_WAV/StreetChicken.wav b/PCM510x_Examples/CircuitPython_WAV/StreetChicken.wav new file mode 100644 index 000000000..55d4eb0f2 Binary files /dev/null and b/PCM510x_Examples/CircuitPython_WAV/StreetChicken.wav differ diff --git a/PCM510x_Examples/CircuitPython_WAV/code.py b/PCM510x_Examples/CircuitPython_WAV/code.py new file mode 100644 index 000000000..ddfe982e6 --- /dev/null +++ b/PCM510x_Examples/CircuitPython_WAV/code.py @@ -0,0 +1,21 @@ +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +CircuitPython I2S WAV file playback. +Plays a WAV file once. +""" +import audiocore +import board +import audiobusio + +audio = audiobusio.I2SOut(board.D9, board.D10, board.D11) + +with open("StreetChicken.wav", "rb") as wave_file: + wav = audiocore.WaveFile(wave_file) + + print("Playing wav file!") + audio.play(wav) + while audio.playing: + pass + +print("Done!")