Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/run on host, output to null device #299

Merged
merged 2 commits into from Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 108 additions & 0 deletions examples/StreamOnHost/StreamOnHost.ino
@@ -0,0 +1,108 @@
#include <Arduino.h>

#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "AudioFileSourceICYStream.h"
#include "AudioFileSourceBuffer.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputNull.h"

// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.

// Enter your WiFi setup here:
#ifndef STASSID
#define STASSID ""
#define STAPSK ""
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

// Randomly picked URL
const char *URL="http://icecast.radiofrance.fr/franceinter-lofi.mp3";
//const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am";

AudioGeneratorMP3 *mp3;
AudioFileSourceICYStream *file;
AudioFileSourceBuffer *buff;
AudioOutputNull *out;

// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
(void) isUnicode; // Punt this ball for now
// Note that the type and string may be in PROGMEM, so copy them to RAM for printf
char s1[32], s2[64];
strncpy_P(s1, type, sizeof(s1));
s1[sizeof(s1)-1]=0;
strncpy_P(s2, string, sizeof(s2));
s2[sizeof(s2)-1]=0;
Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2);
Serial.flush();
}

// Called when there's a warning or error (like a buffer underflow or decode hiccup)
void StatusCallback(void *cbData, int code, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
// Note that the string may be in PROGMEM, so copy it to RAM for printf
char s1[64];
strncpy_P(s1, string, sizeof(s1));
s1[sizeof(s1)-1]=0;
Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
Serial.flush();
}


void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println("Connecting to WiFi");

WiFi.disconnect();
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);

// Try forever
while (WiFi.status() != WL_CONNECTED) {
Serial.println("...Connecting to WiFi");
delay(1000);
}
Serial.println("Connected");

audioLogger = &Serial;
file = new AudioFileSourceICYStream(URL);
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, 4096);
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
out = new AudioOutputNull();
mp3 = new AudioGeneratorMP3();
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
mp3->begin(buff, out);
}


void loop()
{
static int lastms = 0;

if (mp3->isRunning()) {
if (millis()-lastms > 1000) {
lastms = millis();
Serial.printf("Running for %d ms...\n", lastms);
Serial.flush();
}
if (!mp3->loop()) mp3->stop();
} else {
Serial.printf("MP3 done\n");
delay(1000);
}
}

50 changes: 50 additions & 0 deletions examples/StreamOnHost/onHost
@@ -0,0 +1,50 @@
#!/bin/bash

ino=${PWD##*/}

if [ ! -d "${ESP8266ARDUINO}/tests/host" ]; then
echo "\${ESP8266ARDUINO} should point to ESP8266 Arduino core directory"
exit 1
fi

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

cd ${ESP8266ARDUINO}/tests/host

if [ "$1" = "clean" ]; then
make clean
cd ${THISLIB}
rm -f src/*.o src/libmad/*.o
exit 0
fi

if [ "$1" = "-h" ]; then
echo "usage:"
echo " $0"
echo " $0 clean"
echo " FORCE32=0 $0 (run with valgrind)"
echo " FORCE32=1 $0 (run in 32 bits)"
echo "variable ESP8266ARDUINO must point to esp8266 Arduino core directory"
exit 0
fi

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

if [ "${FORCE32}" = 0 ]; then
run=valgrind
else
run=
fi

eval make FORCE32=${FORCE32} V=1 -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/\" \
${THISLIB}/examples/${ino}/${ino}

set -x

$run ./bin/${ino}/${ino} "$@"
stty sane

2 changes: 1 addition & 1 deletion src/AudioGeneratorMP3.cpp
Expand Up @@ -302,7 +302,7 @@ extern "C" {
{
return 8192;
}
#elif defined(ESP8266)
#elif defined(ESP8266) && !defined(CORE_MOCK)
#include <cont.h>
extern cont_t g_cont;

Expand Down