Added threading

This commit is contained in:
2017-12-15 16:10:36 +01:00
parent 4651ee674a
commit 27233290cf
6 changed files with 53 additions and 11 deletions

2
.gitignore vendored
View File

@@ -53,3 +53,5 @@ dkms.conf
*.mp3
/csoundbox
/sounds
/csoundbox.cfg

View File

@@ -3,6 +3,7 @@
#include <string.h>
#include <ncurses.h>
#include <getopt.h>
#include <ao/ao.h>
#include "csoundbox.h"
#include "config.h"
@@ -50,6 +51,7 @@ int main(int argc, char *argv[]) {
inputLocal();
}
cfgdestroy();
ao_shutdown();
return 0;
}
@@ -57,13 +59,15 @@ void inputLocal(void) {
initscr();
nonl(); //no newline
noecho();
keypad(stdscr, FALSE); //Disable the F1-12 keypad
keypad(stdscr, TRUE); //Disable the F1-12 keypad
curs_set(0); //Disable the cursor
printCursesWelcome();
char input;
while ((input = getch()) != 13) {
int input;
//Key 13 --> ENTER
//Key 265 --> F1
while ((input = getch()) != 13 && input != 265) {
playSound(lookupSounds(input));
}
@@ -74,17 +78,22 @@ void inputNetwork(char *server) {
initscr();
nonl(); //no newline
noecho();
keypad(stdscr, FALSE); //Disable the F1-12 keypad
keypad(stdscr, TRUE); //Disable the F1-12 keypad
curs_set(0); //Disable the cursor
printCursesWelcome();
char input[2];
while ((input[0] = getch()) != 13) {
int inp;
//Key 13 --> ENTER
//Key 265 --> F1
while ((input[0] = inp = getch()) != 13 && inp != 265) {
input[1] = '\0';
sendKeyUDP(server, input);
}
if (inp == 265) {
sendKeyUDP(server, "exit");
}
endwin();
}
@@ -95,7 +104,9 @@ void printCursesWelcome(void) {
y /= 2;
x /= 2;
const char *msg = "Press enter to exit.";
const char *msg2 = "Press F1 to kill the server + client";
mvprintw(y, x - (strlen(msg) / 2), msg);
mvprintw(y + 1, x - (strlen(msg2) / 2), msg2);
}

10
csoundbox.cfg.example Normal file
View File

@@ -0,0 +1,10 @@
soundmap = {
sounds = ( {path = "ballsofsteel.mp3";
key = "a"},
{path = "neck.mp3";
key = "b"}
);
}
udpport = 8888;

View File

@@ -3,9 +3,9 @@
VERSION = 1.0
CC = cc
CFLAGS = -Wall -O3 -DVERSION=\"$(VERSION)\"
CFLAGS = -Wall -g -O3 -DVERSION=\"$(VERSION)\"
#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread
LDFLAGS = -lmpg123 -lao -lconfig -lncurses
LDFLAGS = -lmpg123 -lao -lconfig -lncurses -lpthread
OBJ = csoundbox.o config.o sound.o udpserver.o

24
sound.c
View File

@@ -1,12 +1,26 @@
#include <ao/ao.h>
#include <mpg123.h>
#include <pthread.h>
#include <error.h>
#include <string.h>
#include <ncurses.h>
#include "sound.h"
#include "config.h"
#define BITS 8
void playSound(const char *path) {
pthread_t pth; //thread identifier
//Create thread
pthread_create(&pth, NULL, playThreadFunc, (void *) path);
pthread_detach(pth); //detach to call this function multiple times
}
void *playThreadFunc(void *arg) {
const char *path = (const char *) arg;
mpg123_handle *mh;
unsigned char *buffer;
size_t buffer_size;
@@ -50,5 +64,9 @@ void playSound(const char *path) {
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
ao_shutdown();
//ao_shutdown(); //don't call inside a thread
pthread_exit(NULL);
}

View File

@@ -1 +1,2 @@
void playSound(const char *path);
void *playThreadFunc(void *arg);