|
| 1 | +// Demonstration of AudioOutputI2SQuad_F32 four channel I2S output object. |
| 2 | +// Greg Raven KF5N October 2025. |
| 3 | +// The first left and right channels are output on pin 7, which drives the |
| 4 | +// Teensy Audio Adapter (Teensy 4.1). The second left and right channels are output on pin 32 (Teensy 4.1). |
| 5 | + |
| 6 | +#include <Arduino.h> |
| 7 | +#include <Audio.h> |
| 8 | +#include <OpenAudio_ArduinoLibrary.h> |
| 9 | +#include <AudioStream_F32.h> |
| 10 | + |
| 11 | +const float sample_rate_Hz = 48000.0; |
| 12 | +const int audio_block_samples = 128; // Always 128 |
| 13 | + |
| 14 | +AudioControlSGTL5000 sgtl5000_1; |
| 15 | +AudioSettings_F32 audio_settings(sample_rate_Hz, audio_block_samples); |
| 16 | +AudioSynthWaveformSine_F32 tone0(audio_settings), tone1(audio_settings), tone2(audio_settings), tone3(audio_settings); |
| 17 | +AudioOutputI2SQuad_F32 i2s_out(audio_settings); |
| 18 | + |
| 19 | +AudioConnection_F32 connect0(tone0, 0, i2s_out, 0); |
| 20 | +AudioConnection_F32 connect1(tone1, 0, i2s_out, 1); |
| 21 | +AudioConnection_F32 connect2(tone2, 0, i2s_out, 2); |
| 22 | +AudioConnection_F32 connect3(tone3, 0, i2s_out, 3); |
| 23 | + |
| 24 | +void setup() { |
| 25 | + |
| 26 | + Serial.begin(115200); |
| 27 | + |
| 28 | + /* check for CrashReport stored from previous run */ |
| 29 | + if (CrashReport) { |
| 30 | + Serial.print(CrashReport); |
| 31 | + } |
| 32 | + |
| 33 | + uint32_t* dmaErrors; |
| 34 | + dmaErrors = (uint32_t*)(0x400E8000 + 0x4); |
| 35 | + Serial.printf("DMA errors = %u\n", *dmaErrors); |
| 36 | + |
| 37 | + sgtl5000_1.enable(); |
| 38 | + sgtl5000_1.setAddress(LOW); |
| 39 | + AudioMemory(10); |
| 40 | + AudioMemory_F32(10); |
| 41 | + sgtl5000_1.volume(0.8); // Set headphone volume. |
| 42 | + sgtl5000_1.unmuteHeadphone(); |
| 43 | + Serial.printf("Please listen for tones!\n"); |
| 44 | + tone0.amplitude(0.1); |
| 45 | + tone0.frequency(261.63); |
| 46 | + tone1.amplitude(0.1); |
| 47 | + tone1.frequency(329.63); |
| 48 | + tone2.amplitude(0.004); |
| 49 | + tone2.frequency(392.0); |
| 50 | + tone3.amplitude(0.1); |
| 51 | + tone3.frequency(440.0); |
| 52 | + tone0.begin(); |
| 53 | + tone1.begin(); |
| 54 | + tone2.begin(); |
| 55 | + tone3.begin(); |
| 56 | +} |
| 57 | + |
| 58 | +void loop() { |
| 59 | + |
| 60 | + delay(5000); |
| 61 | + // After 5 seconds, turn off all tones. |
| 62 | + tone0.end(); |
| 63 | + tone1.end(); |
| 64 | + tone2.end(); |
| 65 | + tone3.end(); |
| 66 | + |
| 67 | + delay(2000); |
| 68 | + // After 2 seconds, turn on tone0. |
| 69 | + tone0.begin(); |
| 70 | + |
| 71 | + delay(2000); |
| 72 | + // After 2 seconds, turn on tone1. |
| 73 | + tone1.begin(); |
| 74 | + |
| 75 | + delay(2000); |
| 76 | + // After 2 seconds, turn on tone2. |
| 77 | + tone2.begin(); |
| 78 | + |
| 79 | + delay(2000); |
| 80 | + // After 2 seconds, turn on tone3. |
| 81 | + tone3.begin(); |
| 82 | +} |
0 commit comments