Added getopt to select mode #1; Added udpserver #2

This commit is contained in:
2017-12-15 12:03:53 +01:00
parent 1afa4504ab
commit c298de57a8
10 changed files with 208 additions and 54 deletions

View File

@@ -1,18 +1,54 @@
#include <ao/ao.h>
#include <mpg123.h>
#include <stdlib.h>
#include <stdio.h>
#include "csoundbox.h"
#include "config.h"
#include <string.h>
#include <ncurses.h>
#define BITS 8
#include <getopt.h>
#include "csoundbox.h"
#include "config.h"
#include "sound.h"
#include "udpserver.h"
int main(int argc, char *argv[]) {
const char *short_options = "hsi:l";
struct option long_options[] = {
{"server", no_argument, NULL, 's'},
{"ip", required_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{"local", no_argument, NULL, 'l'}
};
int c;
//initialize libconfig
cfginit();
inputLocal();
while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) {
switch (c) {
case 's':
//start server
printf("Server.\n");
udpserver();
break;
case 'h':
//show the help page
break;
case 'i':
//connect to a csoundbox server
break;
case 'l':
default:
//start local
inputLocal();
break;
}
}
if (argc < 2) {
inputLocal();
}
cfgdestroy();
return 0;
}
@@ -44,49 +80,4 @@ void printCursesWelcome(void) {
mvprintw(y, x - (strlen(msg) / 2), msg);
}
void playSound(const char *path) {
mpg123_handle *mh;
unsigned char *buffer;
size_t buffer_size;
size_t done;
int err;
int driver;
ao_device *dev;
ao_sample_format format;
int channels, encoding;
long rate;
/* initializations */
ao_initialize();
driver = ao_default_driver_id();
mpg123_init();
mh = mpg123_new(NULL, &err);
buffer_size = mpg123_outblock(mh);
buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));
/* open the file and get the decoding format */
mpg123_open(mh, path);
mpg123_getformat(mh, &rate, &channels, &encoding);
/* set the output format and open the output device */
format.bits = mpg123_encsize(encoding) * BITS;
format.rate = rate;
format.channels = channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
dev = ao_open_live(driver, &format, NULL);
/* decode and play */
while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
ao_play(dev, (char *)buffer, done);
/* clean up */
free(buffer);
ao_close(dev);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
ao_shutdown();
}