Add RTTTL example and use a non-square waveform
Seems like I2S DACs don't like to be presented with perfect bang-bang square waveforms, so build a very simple model and use that to avoid abrupt changes in values. Sounds about as good as you're going to get with this. Also add a simple example showing how to play a RTTTL string.
This commit is contained in:
1 parent
abeba9dd89
commit
582e6e70ac
3 files changed
+51
-8
No files matched your search
@@ -1,5 +1,5 @@
|
||||
# ESP8266Audio - supports ESP8266 & ESP32 [](https://travis-ci.org/earlephilhower/ESP8266Audio)
|
||||
Arduino library for parsing and decoding MOD, WAV, MP3, FLAC, MIDI, and AAC files and playing them on an I2S DAC or even using a software-simulated delta-sigma DAC with dynamic 32x-128x oversampling.
|
||||
Arduino library for parsing and decoding MOD, WAV, MP3, FLAC, MIDI, AAC, and RTTL files and playing them on an I2S DAC or even using a software-simulated delta-sigma DAC with dynamic 32x-128x oversampling.
|
||||
|
||||
ESP8266 is fully supported and most mature, but ESP32 is also mostly there with built-in DAC as well as external ones.
|
||||
|
||||
@@ -143,6 +143,8 @@ AudioGeneratorMIDI: Plays a MIDI file using a wavetable synthesizer and a Sound
|
||||
|
||||
AudioGeneratorAAC: Requires about 30KB of heap and plays a mono or stereo AAC file using the Helix fixed-point AAC decoder.
|
||||
|
||||
AudioGeneratorRTTTL: Enjoy the pleasures of monophonic, 4-octave ringtones on your ESP8266. Very low memory and CPU requirements for simple tunes.
|
||||
|
||||
## AudioOutput classes
|
||||
AudioOutput: Base class for all output drivers. Takes a sample at a time and returns true/false if there is buffer space for it. If it returns false, it is the calling object's (AudioGenerator's) job to keep the data that didn't fit and try again later.
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "AudioFileSourcePROGMEM.h"
|
||||
#include "AudioGeneratorRTTTL.h"
|
||||
#include "AudioOutputI2S.h"
|
||||
|
||||
const char rudolph[] PROGMEM =
|
||||
"Rudolph the Red Nosed Raindeer:d=8,o=5,b=250:g,4a,g,4e,4c6,4a,2g.,g,a,g,a,4g,4c6,2b.,4p,f,4g,f,4d,4b,4a,2g.,g,a,g,a,4g,4a,2e.,4p,g,4a,a,4e,4c6,4a,2g.,g,a,g,a,4g,4c6,2b.,4p,f,4g,f,4d,4b,4a,2g.,g,a,g,a,4g,4d6,2c.6,4p,4a,4a,4c6,4a,4g,4e,2g,4d,4e,4g,4a,4b,4b,2b,4c6,4c6,4b,4a,4g,4f,2d,g,4a,g,4e,4c6,4a,2g.,g,a,g,a,4g,4c6,2b.,4p,f,4g,f,4d,4b,4a,2g.,4g,4a,4g,4a,2g,2d6,1c.6.";
|
||||
// Plenty more at: http://mines.lumpylumpy.com/Electronics/Computers/Software/Cpp/MFC/RingTones.RTTTL
|
||||
|
||||
AudioGeneratorRTTTL *rtttl;
|
||||
AudioFileSourcePROGMEM *file;
|
||||
AudioOutputI2S *out;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
Serial.printf("RTTTL start\n");
|
||||
file = new AudioFileSourcePROGMEM( rudolph, strlen_P(rudolph) );
|
||||
out = new AudioOutputI2S();
|
||||
rtttl = new AudioGeneratorRTTTL();
|
||||
rtttl->begin(file, out);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (rtttl->isRunning()) {
|
||||
if (!rtttl->loop()) rtttl->stop();
|
||||
} else {
|
||||
Serial.printf("RTTTL done\n");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,16 @@ bool AudioGeneratorRTTTL::isRunning()
|
||||
return running;
|
||||
}
|
||||
|
||||
// Can't do pure square wave, the I2S DACs don't like being fed them
|
||||
static int16_t period[64] PROGMEM = {
|
||||
250, 500, 1000, 2000, 4000, 8000, 16000, 32000, 32000, 31000, 32000, 31000,
|
||||
32000, 31000, 32000, 31000, 32000, 31000, 32000, 32100, 32000, 31000, 32000,
|
||||
31000, 32000, 32000, 16000, 8000, 4000, 2000, 1000, 500,
|
||||
-250, -500, -1000, -2000, -4000, -8000, -16000, -32000, -32000, -31000,
|
||||
-32000, -31000, -32000, -31000, -32000, -31000, -32000, -31000, -32000,
|
||||
-32100, -32000, -31000, -32000, -31000, -32000, -32000, -16000, -8000,
|
||||
-4000, -2000, -1000, -500 };
|
||||
|
||||
bool AudioGeneratorRTTTL::loop()
|
||||
{
|
||||
if (!running) goto done; // Nothing to do here!
|
||||
@@ -71,16 +81,13 @@ bool AudioGeneratorRTTTL::loop()
|
||||
samplesSent++;
|
||||
}
|
||||
} else {
|
||||
int16_t hi[2] = { 32767, 32767 };
|
||||
int16_t lo[2] = { -32767, -32767 };
|
||||
while (samplesSent < ttlSamples) {
|
||||
int samplesSentFP10 = samplesSent << 10;
|
||||
int rem = samplesSentFP10 % ttlSamplesPerWaveFP10;
|
||||
if (rem > (ttlSamplesPerWaveFP10 / 2)) {
|
||||
if (!output->ConsumeSample(hi)) goto done;
|
||||
} else {
|
||||
if (!output->ConsumeSample(lo)) goto done;
|
||||
}
|
||||
rem = (64 * rem) / ttlSamplesPerWaveFP10;
|
||||
int16_t val = pgm_read_word(&period[rem]);
|
||||
int16_t s[2] = { val, val };
|
||||
if (!output->ConsumeSample(s)) goto done;
|
||||
samplesSent++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user