Last active
August 3, 2025 17:36
-
-
Save JayGoldberg/37ef430c17d45c0d52652d40eee3e0cb to your computer and use it in GitHub Desktop.
generates a square wave using the AudioTools library and outputs it as an I2S audio stream, likely for an ESP32 or similar microcontroller with I2S capabilities.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file streams-generator-i2s.ino | |
* @author Phil Schatzmann | |
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-generator-i2s/README.md | |
* @copyright GPLv3 | |
*/ | |
#include "AudioTools.h" | |
AudioInfo info(44100, 1, 16); | |
SquareWaveGenerator<int16_t> waveGen(400, 16000); // subclass of SoundGenerator with max amplitude of 32000 (16000) | |
GeneratedSoundStream<int16_t> sound(waveGen); // Stream generated from sine wave | |
I2SStream i2s; | |
StreamCopy copier(i2s, sound); // copies sound into i2s | |
const int I2S_BCLK_PIN = 2; | |
const int I2S_WS_PIN = 1; | |
const int I2S_DATA_PIN = 3; | |
// Arduino Setup | |
void setup(void) { | |
Serial.begin(115200); | |
while (!Serial) | |
; | |
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning); | |
Serial.println("starting I2S..."); | |
auto config = i2s.defaultConfig(TX_MODE); | |
config.pin_bck = I2S_BCLK_PIN; | |
config.pin_ws = I2S_WS_PIN; | |
config.pin_data = I2S_DATA_PIN; | |
config.copyFrom(info); | |
// IMPORTANT: For many DACs (like MAX98357A), you might need I2S_LSB_FORMAT | |
// Check your DAC's datasheet for the correct I2S format. | |
// config.i2s_format = I2S_LSB_FORMAT; // Uncomment if your DAC needs this | |
i2s.begin(config); | |
Serial.println("start wave generator..."); | |
waveGen.begin(info, N_B4); // override the existing constructor frequency with B4 note | |
Serial.println("start sound source..."); | |
sound.begin(); | |
Serial.println("playing generated wave..."); | |
} | |
void loop() { | |
copier.copy(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment