diff --git a/config.c b/config.c index 89cb01a..1034614 100644 --- a/config.c +++ b/config.c @@ -51,3 +51,12 @@ const char *lookupSounds(char input) { return ""; } +int cfgreadudpport(void) { + int port; + if (!config_lookup_int(&cfg, "udpport", &port)) { + printf("FEHLER beim lesen von: udpport, Standardwert(8888) wird bereitgestellt.\n"); + port = 8888; + } + return port; +} + diff --git a/config.h b/config.h index a73afe5..69a66bf 100644 --- a/config.h +++ b/config.h @@ -8,5 +8,6 @@ void cfginit(void); void cfgdestroy(void); void cfgreinit(void); const char *lookupSounds(char input); +int cfgreadudpport(void); diff --git a/csoundbox.c b/csoundbox.c index 3fe2f7c..72dc725 100644 --- a/csoundbox.c +++ b/csoundbox.c @@ -1,18 +1,54 @@ -#include -#include #include #include -#include "csoundbox.h" -#include "config.h" #include #include -#define BITS 8 +#include + +#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(); -} diff --git a/csoundbox.cfg b/csoundbox.cfg index f75fc5c..6bc89e9 100644 --- a/csoundbox.cfg +++ b/csoundbox.cfg @@ -6,3 +6,5 @@ soundmap = { ); } + +udpport = 8888; diff --git a/csoundbox.h b/csoundbox.h index b26edb6..e697e05 100644 --- a/csoundbox.h +++ b/csoundbox.h @@ -1,4 +1,3 @@ - -void playSound(const char *path); + void inputLocal(void); void printCursesWelcome(void); diff --git a/makefile b/makefile index 0c51581..1525a6a 100644 --- a/makefile +++ b/makefile @@ -7,7 +7,7 @@ CFLAGS = -Wall -O3 -DVERSION=\"$(VERSION)\" #LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread LDFLAGS = -lmpg123 -lao -lconfig -lncurses -OBJ = csoundbox.o config.o +OBJ = csoundbox.o config.o sound.o udpserver.o all: $(OBJ) $(CC) $(CFLAGS) -o csoundbox $(OBJ) $(LDFLAGS) diff --git a/sound.c b/sound.c new file mode 100644 index 0000000..fd2a833 --- /dev/null +++ b/sound.c @@ -0,0 +1,54 @@ +#include +#include + + +#include "sound.h" + +#define BITS 8 + +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(); +} diff --git a/sound.h b/sound.h new file mode 100644 index 0000000..c4a549e --- /dev/null +++ b/sound.h @@ -0,0 +1 @@ +void playSound(const char *path); diff --git a/udpserver.c b/udpserver.c new file mode 100644 index 0000000..824de20 --- /dev/null +++ b/udpserver.c @@ -0,0 +1,94 @@ +#include //printf +#include //memset, strtok +#include //exit(0); +#include +#include +#include //close() + +#include "udpserver.h" +#include "sound.h" +#include "config.h" + +#define BUFLEN 512 //Max length of buffer +//#define PORT 8888 + + +#include "udpserver.h" +void die(char *s) +{ + perror(s); + //exit(1); +} + + +void udpserver(void) +{ + struct sockaddr_in si_me, si_other; + + int PORT = cfgreadudpport(); + + int s, slen = sizeof(si_other) , recv_len; + char buf[BUFLEN]; + + //create a UDP socket + if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) + { + die("socket"); + } + + // zero out the structure + memset((char *) &si_me, 0, sizeof(si_me)); + + si_me.sin_family = AF_INET; + si_me.sin_port = htons(PORT); + si_me.sin_addr.s_addr = htonl(INADDR_ANY); + + //bind socket to port + if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1) + { + die("bind"); + } + + //keep listening for data + while(1) + { + //printf("Waiting for data..."); + //fflush(stdout); + memset(buf,'\0', BUFLEN); + //try to receive some data, this is a blocking call + if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, (unsigned int *)&slen)) == -1) + { + die("recvfrom()"); + } + + //print details of the client/peer and the data received + printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); + printf("Data: %s\n" , buf); + + if (!processUDPInput(buf)) { + break; + } + + //now reply the client with the same data + if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) + { + die("sendto()"); + } + } + + close(s); +} + +int processUDPInput(char *input) { + /** + * Returns 0 on exit + * Return 1 on success + */ + + if (strcmp(input, "exit") == 0) { + return 0; + } + playSound(lookupSounds(input[0])); + return 1; +} + diff --git a/udpserver.h b/udpserver.h new file mode 100644 index 0000000..2547aa5 --- /dev/null +++ b/udpserver.h @@ -0,0 +1,3 @@ +void die(char *s); //For debugging +void udpserver(void); //start the server by calling this function +int processUDPInput(char *input);