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

Web radio chunked demo #48

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Original test code from issue #47
  • Loading branch information
wmarkow committed Mar 18, 2020
commit 5dd69aeabb93564c2baf3ed3c9270cb5c4cd792e
107 changes: 107 additions & 0 deletions examples/WebRadioChunkedDemo/WebRadioChunkedDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <Arduino.h>
#include <VS1053.h>
#include <WiFi.h>

#define VS1053_CS 5
#define VS1053_DCS 16
#define VS1053_DREQ 4

#define VOLUME 80

#define VS_BUFF_SIZE 32
#define RING_BUF_SIZE 20000
#define CLIENT_BUFF 2048

VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);
WiFiClient client;

const char *ssid = "TP-Link";
const char *password = "xxxxxxxx";

const char *host = "icecast.radiofrance.fr";
const char *path = "/franceculture-midfi.mp3";
int httpPort = 80;

uint16_t rcount = 0;
uint8_t *ringbuf;
uint16_t rbwindex = 0;
uint8_t *mp3buff;
uint16_t rbrindex = RING_BUF_SIZE - 1;

inline bool ringspace() {
return (rcount < RING_BUF_SIZE);
}

void putring(uint8_t b) {
*(ringbuf + rbwindex) = b;
if (++rbwindex == RING_BUF_SIZE) {
rbwindex = 0;
}
rcount++;
}
uint8_t getring() {
if (++rbrindex == RING_BUF_SIZE) {
rbrindex = 0;
}
rcount--;
return *(ringbuf + rbrindex);
}

void playRing(uint8_t b) {
static int bufcnt = 0;
mp3buff[bufcnt++] = b;
if (bufcnt == sizeof(mp3buff)) {
player.playChunk(mp3buff, bufcnt);
bufcnt = 0;
}
}

void setup() {
Serial.begin(115200);
mp3buff = (uint8_t*) malloc(VS_BUFF_SIZE);
ringbuf = (uint8_t*) malloc(RING_BUF_SIZE);
SPI.begin();
player.begin();
player.switchToMp3Mode();
player.setVolume(VOLUME);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (!client.connect(host, httpPort)) {
Serial.println("Connection failed");
return;
}
client.print(
String("GET ") + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n"
+ "Icy-MetaData: 1" + "\r\n" + "Connection: close\r\n\r\n");
}

void loop() {
uint32_t maxfilechunk;

if (!client.connected()) {
Serial.println("Reconnecting...");
if (client.connect(host, httpPort)) {
client.print(
String("GET ") + path + " HTTP/1.1\r\n" + "Host: " + host
+ "\r\n" + "Icy-MetaData: 1" + "\r\n"
+ "Connection: close\r\n\r\n");
}
}

maxfilechunk = client.available();
if (maxfilechunk > 0) {
if (maxfilechunk > CLIENT_BUFF) {
maxfilechunk = CLIENT_BUFF;
}
while (ringspace() && maxfilechunk--) {
putring(client.read());
}
yield();
while (rcount && (player.data_request())) {
playRing(getring());
}
}
}