How to generate a simple tone on speaker
Closed this issue · 7 comments
spleenware commented
Can you add an example sketch on how to generate a simple tone to play on the built-in speaker (over I2S, I assume?).
technoblogy commented
Here's a simple version:
void beep (int freq, int duration) {
I2S.setAllPins(7, 5, 6, 6, 6); // sckPin, fsPin, sdPin, outSdPin, inSdPin
const int samplerate = 8000;
int halfwave = samplerate / freq, amplitude = 500;
unsigned long start = millis();
if (!I2S.begin(I2S_PHILIPS_MODE, samplerate, 16)) return; // Error
while (millis() < start + duration) {
if (count % halfwave == 0) amplitude = -1 * amplitude;
I2S.write(amplitude); I2S.write(amplitude);
}
I2S.end();
}
The frequency is in Hz and the duration in milliseconds.
spleenware commented
Which header files are needed, and how is I2S object constructed? (specifically for ESP32/T-Deck target)
technoblogy commented
You need:
#include <I2S.h>
I also found I needed this:
#include "soc/periph_defs.h" // Not sure why necessary
It's based on the SimpleTone example which is in the ESP32 core at:
/Users/david/Library/Arduino15/packages/esp32/hardware/esp32/2.0.12/libraries/I2S/examples/SimpleTone
spleenware commented
Ah, thank you. I hear a beep now! :-)
aaron-924 commented
Is there a reason why "Play MP3" doesn't work in the UnitTest code?
github-actions commented
This issue is stale because it has been open for 30 days with no activity.
github-actions commented
This issue was closed because it has been inactive for 14 days since being marked as stale.