Skip to content

Commit

Permalink
StreamOnHost: play audio on host (earlephilhower#359)
Browse files Browse the repository at this point in the history
* StreamOnHost: actually play audio

* (spaces)

* fix comment

* update message
  • Loading branch information
d-a-v authored Jan 8, 2021
1 parent 40d0d99 commit 3349608
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 12 deletions.
118 changes: 118 additions & 0 deletions examples/StreamOnHost/AudioOutputLinuxDSP.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#ifndef _AUDIOOUTPUTNULLSLOW_H
#define _AUDIOOUTPUTNULLSLOW_H

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>

#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
7 changes: 5 additions & 2 deletions examples/StreamOnHost/AudioOutputNullSlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@
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]) {
// return false (= output buffer full)
// sometimes to let the main loop running
constexpr int everylog2 = 10;
if ((++samples & ((1<<everylog2)-1)) == 0) {
delay(1000/(hertz >> everylog2));
if (hertz > 0) {
// simulate real time
delay(1000/(hertz >> everylog2));
}
return false;
}
return true;
Expand Down
10 changes: 9 additions & 1 deletion examples/StreamOnHost/StreamOnHost.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 19 additions & 9 deletions examples/StreamOnHost/onHost
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fi

THISLIB=$(pwd)/../..
MAD=$(ls ${THISLIB}/src/libmad/*.c)
PAGER=${PAGER:-less}

cd ${ESP8266ARDUINO}/tests/host

Expand All @@ -19,38 +20,47 @@ 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 ""
echo "usage:"
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

run=""

[ -z "${FORCE32}" ] && FORCE32=0
[ -z "${AUDIO}" ] && AUDIO=1

if [ "${FORCE32}" = 0 ]; then
run=valgrind
else
run=
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

0 comments on commit 3349608

Please sign in to comment.