From 3349608b114e839087a328f196d3d93dbe6e008f Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 8 Jan 2021 16:41:03 +0100 Subject: [PATCH] StreamOnHost: play audio on host (#359) * StreamOnHost: actually play audio * (spaces) * fix comment * update message --- examples/StreamOnHost/AudioOutputLinuxDSP.h | 118 ++++++++++++++++++++ examples/StreamOnHost/AudioOutputNullSlow.h | 7 +- examples/StreamOnHost/StreamOnHost.ino | 10 +- examples/StreamOnHost/onHost | 30 +++-- 4 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 examples/StreamOnHost/AudioOutputLinuxDSP.h diff --git a/examples/StreamOnHost/AudioOutputLinuxDSP.h b/examples/StreamOnHost/AudioOutputLinuxDSP.h new file mode 100644 index 0000000..743410e --- /dev/null +++ b/examples/StreamOnHost/AudioOutputLinuxDSP.h @@ -0,0 +1,118 @@ +/* + AudioOutput + Base class of an audio output player + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _AUDIOOUTPUTNULLSLOW_H +#define _AUDIOOUTPUTNULLSLOW_H + +#include +#include +#include +#include +#include +#include +#include + +#include "AudioOutput.h" + +class AudioOutputNullSlow : public AudioOutput +{ + public: + AudioOutputNullSlow() { }; + ~AudioOutputNullSlow() {}; + virtual bool begin() { samples = 0; startms = millis(); return true; } + + virtual bool ConsumeSample(int16_t sample[2]) { + + if (fd < 0) { + fd = open("/dev/dsp", O_RDWR); + if (fd < 0) { + perror("open of /dev/dsp failed (Try with 'padsp this-exec')"); + exit(1); + } + } + + if (channels && lastchannels != channels) { + Serial.printf("CHANNELS=%d\n", channels); + int arg = channels; /* mono or stereo */ + int status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); + if (status == -1) { + perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); + exit(1); + } else if (arg != channels) { + perror("unable to set number of channels"); + exit(1); + } + lastchannels = channels; + } + + if (lastchannels > 0 && hertz && lasthertz != hertz) { + Serial.printf("FREQ=%d\n", hertz); + int arg = hertz*4/lastchannels; /* sampling rate */ + int status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); + if (status == -1) { + perror("SOUND_PCM_WRITE_RATE ioctl failed"); + exit(1); + } + lasthertz = hertz; + } + + if (bps && lastbps != bps) { + Serial.printf("BPS=%d\n", bps); + int arg = bps; /* sample size */ + int status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); + if (status == -1) { + perror("SOUND_PCM_WRITE_BITS ioctl failed"); + exit(1); + } else if (arg != bps) { + perror("unable to set sample size"); + exit(1); + } + lastbps = bps; + } + + if ((++samples & ((1<<9)-1)) == 0) { + // let the main loop a chance to run + return false; + } + + if (write(fd, sample, sizeof(sample)) != sizeof(sample)) { + perror("doing sound"); + exit(1); + } + + return true; + } + + virtual bool stop() { endms = millis(); return true; }; + unsigned long GetMilliseconds() { return endms - startms; } + int GetSamples() { return samples; } + int GetFrequency() { return hertz; } + + protected: + unsigned long startms; + unsigned long endms; + int samples; + int lastchannels = -1; + int lasthertz = -1; + int lastbps = -1; + int fd = -1; +}; + +#endif diff --git a/examples/StreamOnHost/AudioOutputNullSlow.h b/examples/StreamOnHost/AudioOutputNullSlow.h index 95548f6..e85cd9e 100644 --- a/examples/StreamOnHost/AudioOutputNullSlow.h +++ b/examples/StreamOnHost/AudioOutputNullSlow.h @@ -26,7 +26,7 @@ class AudioOutputNullSlow : public AudioOutput { public: - AudioOutputNullSlow(int hertz) { SetRate(hertz); }; + AudioOutputNullSlow() { }; ~AudioOutputNullSlow() {}; virtual bool begin() { samples = 0; startms = millis(); return true; } virtual bool ConsumeSample(int16_t sample[2]) { @@ -34,7 +34,10 @@ class AudioOutputNullSlow : public AudioOutput // sometimes to let the main loop running constexpr int everylog2 = 10; if ((++samples & ((1<> everylog2)); + if (hertz > 0) { + // simulate real time + delay(1000/(hertz >> everylog2)); + } return false; } return true; diff --git a/examples/StreamOnHost/StreamOnHost.ino b/examples/StreamOnHost/StreamOnHost.ino index e2a66f7..1308c37 100644 --- a/examples/StreamOnHost/StreamOnHost.ino +++ b/examples/StreamOnHost/StreamOnHost.ino @@ -8,7 +8,13 @@ #include "AudioFileSourceICYStream.h" #include "AudioFileSourceBuffer.h" #include "AudioGeneratorMP3.h" +#if AUDIO +#pragma message("Outputting audio") +#include "AudioOutputLinuxDSP.h" +#else +#pragma message("No audio") #include "AudioOutputNullSlow.h" +#endif // To run, set your ESP8266 build to 160MHz, update the SSID info, and upload. @@ -23,6 +29,8 @@ const char* password = STAPSK; // Randomly picked URL const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am"; +//const char *URL="http://stream2.pvpjamz.com:8706/stream"; +// that one is not well decoded: //const char *URL="http://icecast.radiofrance.fr/franceinter-lofi.mp3"; AudioGeneratorMP3 *mp3; @@ -82,7 +90,7 @@ void setup() file->RegisterMetadataCB(MDCallback, (void*)"ICY"); buff = new AudioFileSourceBuffer(file, 2048); buff->RegisterStatusCB(StatusCallback, (void*)"buffer"); - out = new AudioOutputNullSlow(44100); + out = new AudioOutputNullSlow(); mp3 = new AudioGeneratorMP3(); mp3->RegisterStatusCB(StatusCallback, (void*)"mp3"); mp3->begin(buff, out); diff --git a/examples/StreamOnHost/onHost b/examples/StreamOnHost/onHost index a02939f..0148456 100755 --- a/examples/StreamOnHost/onHost +++ b/examples/StreamOnHost/onHost @@ -9,6 +9,7 @@ fi THISLIB=$(pwd)/../.. MAD=$(ls ${THISLIB}/src/libmad/*.c) +PAGER=${PAGER:-less} cd ${ESP8266ARDUINO}/tests/host @@ -19,7 +20,7 @@ if [ "$1" = "clean" ]; then exit 0 elif [ "$1" = diff ]; then cd ${THISLIB}/examples - diff -u StreamMP3FromHTTP/StreamMP3FromHTTP.ino ${ino}/${ino}.ino + diff -u StreamMP3FromHTTP/StreamMP3FromHTTP.ino ${ino}/${ino}.ino | ${PAGER} exit 0 else echo "" @@ -27,30 +28,39 @@ else echo " $0" echo " $0 clean" echo " $0 diff" - echo " FORCE32=0 $0 (run with valgrind)" - echo " FORCE32=1 $0 (run in 32 bits)" + echo " AUDIO=a VALGRIND=v FORCE32=f $0" + echo " a=1 play sound (use padsp, open /dev/dsp)" + echo " v=1 run in native mode (FORCE32=0) with valgrind" + echo " f=1 run in 32 bits mode (if gcc-multilib is installed)" echo "variable ESP8266ARDUINO must point to esp8266 Arduino core directory" echo "" [ "$1" = "-h" ] && exit 0 sleep 1 fi -[ -z "${FORCE32}" ] && FORCE32=0 +run="" -if [ "${FORCE32}" = 0 ]; then - run=valgrind -else - run= +[ -z "${FORCE32}" ] && FORCE32=0 +[ -z "${AUDIO}" ] && AUDIO=1 + +if [ "${AUDIO}" = 1 ]; then + run="${run} padsp" fi +if [ "${VALGRIND}" = 1 ]; then + FORCE32=0 + run="$run valgrind" +fi + +touch ${THISLIB}/examples/${ino}/${ino}.ino # rebuild + eval make FORCE32=${FORCE32} -j \ USERCSOURCES=\"${MAD}\" \ USERCXXSOURCES=\"${THISLIB}/src/AudioFileSourceBuffer.cpp ${THISLIB}/src/AudioLogger.cpp ${THISLIB}/src/AudioGeneratorMP3.cpp ${THISLIB}/src/AudioFileSourceICYStream.cpp ${THISLIB}/src/AudioFileSourceHTTPStream.cpp\" \ - USERCFLAGS=\"-I${THISLIB}/src/\" \ + USERCFLAGS=\"-I${THISLIB}/src/ -DAUDIO=${AUDIO}\" \ ${THISLIB}/examples/${ino}/${ino} set -x $run ./bin/${ino}/${ino} "$@" stty sane -