From edde2c0aeccfc4c3235b2aed240dc9791ba93823 Mon Sep 17 00:00:00 2001 From: structix Date: Wed, 13 Dec 2017 19:11:31 +0100 Subject: [PATCH] Added config files --- config.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 12 ++++++++++++ csoundbox.c | 22 +++++++++++++++++---- csoundbox.cfg | 8 ++++++++ csoundbox.h | 2 +- makefile | 4 ++-- 6 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 config.c create mode 100644 config.h create mode 100644 csoundbox.cfg diff --git a/config.c b/config.c new file mode 100644 index 0000000..c5e8846 --- /dev/null +++ b/config.c @@ -0,0 +1,53 @@ +#include +#include "config.h" +#include + +config_t cfg; +const config_setting_t *setting; + +void cfginit(void) { + + config_init(&cfg); + if (!config_read_file(&cfg, "csoundbox.cfg")) { + fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + //return(EXIT_FAILURE); + } +} + +void cfgdestroy(void) { + //free ram + config_destroy(&cfg); +} + +void cfgreinit(void) { + cfgdestroy(); + cfginit(); +} + + +const char *lookupSounds(char input) { + setting = config_lookup(&cfg, "soundmap.sounds"); + const char *temp; + if(setting != NULL) { + int count = config_setting_length(setting); + int i; + + + for(i = 0; i < count; ++i) { + config_setting_t *sound = config_setting_get_elem(setting, i); + + /* Only output the record if all of the expected fields are present. */ + const char *key; + + if (config_setting_lookup_string(sound, "key", &key)) + if (key[0] == input) { + config_setting_lookup_string(sound, "path", &temp); + return temp; + } + } + + } + return "false"; +} + diff --git a/config.h b/config.h new file mode 100644 index 0000000..a73afe5 --- /dev/null +++ b/config.h @@ -0,0 +1,12 @@ +typedef struct { + char *beep; + char *path[50]; + char *key[50]; +} sounds; + +void cfginit(void); +void cfgdestroy(void); +void cfgreinit(void); +const char *lookupSounds(char input); + + diff --git a/csoundbox.c b/csoundbox.c index 6cda646..38571cf 100644 --- a/csoundbox.c +++ b/csoundbox.c @@ -3,18 +3,32 @@ #include #include #include "csoundbox.h" - +#include "config.h" +#include +#include #define BITS 8 int main(int argc, char *argv[]) { - + cfginit(); + playSound(lookupSounds('a')); + char input; + initscr(); + nonl(); //no newline + keypad(stdscr, FALSE); //Disable the F1-12 keypad + curs_set(0); //Disable the cursor + while ((input = getch()) != 13) { + playSound(lookupSounds(input)); + } + + endwin(); + cfgdestroy(); return 0; } -void playSound(char *path) { - mpg123_handle *mh; +void playSound(const char *path) { + mpg123_handle *mh; unsigned char *buffer; size_t buffer_size; size_t done; diff --git a/csoundbox.cfg b/csoundbox.cfg new file mode 100644 index 0000000..f75fc5c --- /dev/null +++ b/csoundbox.cfg @@ -0,0 +1,8 @@ +soundmap = { + sounds = ( {path = "ballsofsteel.mp3"; + key = "a"}, + {path = "neck.mp3"; + key = "b"} + ); + +} diff --git a/csoundbox.h b/csoundbox.h index f83094f..fc99fff 100644 --- a/csoundbox.h +++ b/csoundbox.h @@ -1,2 +1,2 @@ -void playSound(char *path); +void playSound(const char *path); diff --git a/makefile b/makefile index dcde6db..0c51581 100644 --- a/makefile +++ b/makefile @@ -5,9 +5,9 @@ VERSION = 1.0 CC = cc CFLAGS = -Wall -O3 -DVERSION=\"$(VERSION)\" #LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread -LDFLAGS = -lmpg123 -lao +LDFLAGS = -lmpg123 -lao -lconfig -lncurses -OBJ = csoundbox.o +OBJ = csoundbox.o config.o all: $(OBJ) $(CC) $(CFLAGS) -o csoundbox $(OBJ) $(LDFLAGS)