From 582e6e70ac2585a711b60ed629c3b85d3f0a6617 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Mon, 26 Mar 2018 08:00:56 -0700 Subject: [PATCH] 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. --- README.md | 4 ++- .../PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino | 34 +++++++++++++++++++ src/AudioGeneratorRTTTL.cpp | 21 ++++++++---- 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino diff --git a/README.md b/README.md index 9fb986e..7ca5963 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ESP8266Audio - supports ESP8266 & ESP32 [![Build Status](https://travis-ci.org/earlephilhower/ESP8266Audio.svg?branch=master)](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. diff --git a/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino b/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino new file mode 100644 index 0000000..ea34692 --- /dev/null +++ b/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino @@ -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); + } +} + diff --git a/src/AudioGeneratorRTTTL.cpp b/src/AudioGeneratorRTTTL.cpp index e5d5037..34fe901 100644 --- a/src/AudioGeneratorRTTTL.cpp +++ b/src/AudioGeneratorRTTTL.cpp @@ -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++; } }