Skip to content

Commit

Permalink
added playTone and beep cmds (pr3y#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster committed Aug 6, 2024
1 parent cba38b0 commit cda5ba1
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
7 changes: 0 additions & 7 deletions src/core/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,3 @@ void updateTimeStr(struct tm timeInfo) {
snprintf(timeStr, sizeof(timeStr), "%02d:%02d", timeInfo.tm_hour, timeInfo.tm_min);
}

void _tone(unsigned int frequency, unsigned long duration = 0UL) {
#if defined(BUZZ_PIN)
tone(BUZZ_PIN, frequency, duration);
//#elif defined(HAS_NS4168_SPKR)
//TODO: alt. implementation using the speaker
#endif
}
2 changes: 1 addition & 1 deletion src/core/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ extern String wui_pwd;
extern int tmz;

void setup_gpio();
void _tone(unsigned int frequency, unsigned long duration);

15 changes: 15 additions & 0 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ bool processSerialCommand(String cmd_str) {
}
#endif

#if defined(HAS_NS4168_SPKR) || defined(BUZZ_PIN)
if(cmd_str.startsWith("tone" ) || cmd_str.startsWith("beep" )) {
const char* args = cmd_str.c_str() + 4;
unsigned long frequency = 500UL;
unsigned long duration = 500UL; // default to 2 sec
if(strlen(args)>1) sscanf(args, " %lu %lu", &frequency, &duration); // try to read the args, keep the defaults if missing
//Serial.print((int) frequency);
//Serial.print((int) duration);
_tone(frequency, duration);
//delay(1000);
//playTone(frequency, duration, 1); // sine
return true;
}
#endif

#if defined(HAS_NS4168_SPKR) //M5StickCs doesn't have speakers.. they have buzzers on pin 02 that only beeps in different frequencies
if(cmd_str.startsWith("music_player " ) ) { // || cmd_str.startsWith("play " )
String song = cmd_str.substring(13, cmd_str.length());
Expand Down
76 changes: 75 additions & 1 deletion src/modules/others/audio.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "audio.h"
#include <ESP8266Audio.h>
#include <AudioGeneratorMIDI.h>
#include "AudioGeneratorMIDI.h"
#include "AudioFileSourceFunction.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"
#include <ESP8266SAM.h>
#include "core/mykeyboard.h"

Expand Down Expand Up @@ -117,4 +120,75 @@ bool isAudioFile(String filepath) {
return filepath.endsWith(".opus") || filepath.endsWith(".rtttl") ||
filepath.endsWith(".wav") || filepath.endsWith(".mod") || filepath.endsWith(".mp3") ;
}


void playTone(unsigned int frequency, unsigned long duration, short waveType)
{
// derived from https://github.com/earlephilhower/ESP8266Audio/blob/master/examples/PlayWAVFromFunction/PlayWAVFromFunction.ino

if(frequency==0 || duration==0) return;

float hz = frequency;

AudioGeneratorWAV* wav;
AudioFileSourceFunction* file;
AudioOutputI2S* out = new AudioOutputI2S();
out->SetPinout(BCLK, WCLK, DOUT);

file = new AudioFileSourceFunction( duration/1000.0); // , 1, 44100
//
// you can set (sec, channels, hz, bit/sample) but you should care about
// the trade-off between performance and the audio quality
//
// file = new AudioFileSourceFunction(sec, channels, hz, bit/sample);
// channels : default = 1
// hz : default = 8000 (8000, 11025, 22050, 44100, 48000, etc.)
// bit/sample : default = 16 (8, 16, 32)

// ===== set your sound function =====

if(waveType==0) { // square
file->addAudioGenerators([&](const float time) {
float v = ( sin(hz * time) >= 0 ) ? 1.0f : -1.0f;; // generate square wave
v *= 0.1; // scale
return v;
});
}
else if(waveType==1) { // sine
file->addAudioGenerators([&](const float time) {
float v = sin(TWO_PI * hz * time); // generate sine wave
v *= fmod(time, 1.f); // change linear
v *= 0.1; // scale
return v;
});
}
// TODO: more wave types: triangle, sawtooth
//
// sound function should have one argument(float) and one return(float)
// param : float (current time [sec] of the song)
// return : float (the amplitude of sound which varies from -1.f to +1.f)

wav = new AudioGeneratorWAV();
wav->begin(file, out);

while (wav->isRunning()) {
if (!wav->loop() || checkAnyKeyPress()) wav->stop();
}

delete file;
delete wav;
delete out;
}

#endif


void _tone(unsigned int frequency, unsigned long duration) {
#if defined(BUZZ_PIN)
tone(BUZZ_PIN, frequency, duration);
#elif defined(HAS_NS4168_SPKR)
// alt. implementation using the speaker
playTone(frequency, duration, 0);
#endif
}

6 changes: 5 additions & 1 deletion src/modules/others/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ bool playAudioRTTTLString(String song);

bool tts(String text);

bool isAudioFile(String filePath);
bool isAudioFile(String filePath);

void playTone(unsigned int frequency, unsigned long duration = 0UL, short waveType=0);

void _tone(unsigned int frequency, unsigned long duration = 0UL);

0 comments on commit cda5ba1

Please sign in to comment.