Skip to content

Commit

Permalink
Thread audio decoding so we can stream from webradios
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleroy committed Mar 15, 2024
1 parent 0d9db2f commit f9f7754
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a2tools (2.1.3.1) bionic; urgency=medium
a2tools (2.1.4.1) bionic; urgency=medium

* Add sound streaming

Expand Down
1 change: 0 additions & 1 deletion src/pwm-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ SOURCES := main.c \
../lib/extended_conio.c \
../lib/clrzone.c \
../surl-server/helpers/hgr-convert.c \
../surl-server/helpers/stream.c \
../surl-server/helpers/ffmpeg.c \
../lib/array_sort.c

Expand Down
80 changes: 43 additions & 37 deletions src/pwm-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <sys/time.h>
#include <sys/wait.h>
#include <dirent.h>
#include <pthread.h>

#include "ffmpeg.h"
#include "simple_serial.h"
Expand Down Expand Up @@ -34,18 +35,29 @@ static void send_end_of_stream(void) {

#define ABS(x) ((x) < 0 ? -(x) : (x))

void *ffmpeg_decode(void *arg) {
decode_data *th_data = (decode_data *)arg;

ffmpeg_to_raw_snd(th_data);
return (void *)0;
}

int main(int argc, char *argv[]) {
int num = 0;
unsigned char c;
struct timeval samp_start, samp_end;
unsigned long secs;
unsigned long microsecs;
unsigned long elapsed;
int max = 0;
int max = 256;
int i;
unsigned char *data = NULL;
unsigned char *img_data = NULL;
size_t cur = 0, size = 0, img_size = 0;
pthread_t decode_thread;
decode_data *th_data = malloc(sizeof(decode_data));
int ready = 0;
int stop = 0;

if (simple_serial_open() != 0) {
printf("Can't open serial\n");
Expand All @@ -61,48 +73,37 @@ int main(int argc, char *argv[]) {
sample_rate = atoi(argv[2]);
}

ffmpeg_to_raw_snd(argv[1], sample_rate, &data, &size, &img_data, &img_size);
memset(th_data, 0, sizeof(decode_data));
th_data->url = argv[1];
th_data->sample_rate = sample_rate;
pthread_mutex_init(&th_data->mutex, NULL);


for (cur = 0; cur < size; cur++) {
int offset = data[cur] - 128;
if (ABS(offset) > max)
max = ABS(offset);
}
printf("max %d\n", max);
if (max == 0) {
max = 128;
}
// for (cur = 0; cur < size; cur++) {
// int offset = data[cur] - 128;
// offset = offset * 128/max;
// data[cur] = offset + 128;
// }
pthread_create(&decode_thread, NULL, *ffmpeg_decode, (void *)th_data);

max = 256;

FILE *fptest = fopen("5bits.raw","wb");
for (cur = 0; cur < size; cur++) {
fputc((data[cur] * MAX_LEVEL/max) * (max/MAX_LEVEL), fptest);
printf("waiting for data\n");
while(!ready && !stop) {
pthread_mutex_lock(&th_data->mutex);
ready = th_data->data_ready;
stop = th_data->stop;
pthread_mutex_unlock(&th_data->mutex);
}
fclose(fptest);

printf("Max volume: %d\n", max);

gettimeofday(&samp_start, 0);
printf("starting\n");
cur = 0;
while (1) {
unsigned char *data = NULL;
size_t size;

/* test samples */
for (i = 0; i < END_OF_STREAM; i++) {
printf("send %d\n", i);
send_sample(i);
usleep(500);
printf("send %d again\n", i);
send_sample(i);
cgetc();
}
pthread_mutex_lock(&th_data->mutex);
data = th_data->data;
size = th_data->size;
pthread_mutex_unlock(&th_data->mutex);

for (cur = 0; cur < size; cur++) {
if (cur == size) {
break;
}
send_sample(data[cur] * MAX_LEVEL/max);
cur++;
if (num == sample_rate) {
gettimeofday(&samp_end, 0);
secs = (samp_end.tv_sec - samp_start.tv_sec)*1000000;
Expand Down Expand Up @@ -144,6 +145,9 @@ int main(int argc, char *argv[]) {
break;
case CH_ESC:
printf("Stop\n");
pthread_mutex_lock(&th_data->mutex);
th_data->stop = 1;
pthread_mutex_unlock(&th_data->mutex);
goto done;
}
}
Expand All @@ -154,6 +158,8 @@ int main(int argc, char *argv[]) {

done:
send_sample(0);
free(data);
free(th_data->data);
free(th_data->img_data);
free(th_data);
//send_end_of_stream();
}
8 changes: 4 additions & 4 deletions src/stp/stp_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static char *password = NULL;

extern char *welcome_header;

char *stp_get_start_url(void) {
char *stp_get_start_url(char *header, char *default_url) {
FILE *fp;
char *start_url = NULL;
char *last_start_url = NULL;
Expand Down Expand Up @@ -74,7 +74,7 @@ char *stp_get_start_url(void) {
if (strchr(last_password,'\n'))
*strchr(last_password,'\n') = '\0';
} else {
last_start_url = strdup("ftp://ftp.apple.asimov.net/");
last_start_url = strdup(default_url);
last_login = strdup("");
last_password = strdup("");
}
Expand All @@ -86,8 +86,8 @@ char *stp_get_start_url(void) {
}

gotoxy(0, 14);
dputs("Please enter the server's root URL,\r\n"
"or Enter to reuse the last one:\r\n\r\n"
dputs(header);
dputs("\r\nHit Enter to reuse the last one:\r\n\r\n"
"'");
dputs(last_start_url);
dputs("'\r\n\r\n\r\n"
Expand Down
2 changes: 1 addition & 1 deletion src/stp/stp_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ char *stp_url_up(char *url);
char stp_list_scroll(signed char shift);
void stp_list_search(unsigned char new_search);
char *stp_build_login_url(char *url);
char *stp_get_start_url(void);
char *stp_get_start_url(char *header, char *default_url);
void stp_update_list(char full_update);
int stp_get_data(char *url, const surl_response **resp);
void stp_print_header(char *url);
Expand Down
2 changes: 1 addition & 1 deletion src/stp/stp_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int main(void) {

surl_ping();

url = stp_get_start_url();
url = stp_get_start_url("Please enter the server's root URL.", "ftp://ftp.apple.asimov.net/");
url = stp_build_login_url(url);

stp_print_footer();
Expand Down
2 changes: 1 addition & 1 deletion src/surl-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $(PROGRAM): $(SOURCES)
$(shell curl-config --libs) \
$(shell libpng-config --libs) \
$(shell pkg-config --libs SDL_image) \
-ljq -lgumbo \
-ljq -lgumbo -lpthread \
-lavfilter -lavformat -lavcodec -lavutil -lswresample \
-o $@

Expand Down
Loading

0 comments on commit f9f7754

Please sign in to comment.