diff --git a/README.md b/README.md index 1e104d7f10196b40d077de7f69ae63910e4b65d9..64af80b233e16b95ac888250e7723d5921e1d477 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,15 @@ `ttdaw` is a tiny terminal based digital audio workstation made for fun, experimentation and learning more about audio, MIDI and terminal applications in general. +## Compile and run + +```sh +make +./ttdaw -c 28:0 -s soundfonts/general-808.sf2 -p 1 +``` + +List all available MIDI devices with `acconect -l`. + ## Soundfonts - https://dev.nando.audio/pages/soundfonts.html diff --git a/main.c b/main.c index 1df6ec3e617c652f381c6da6c3c461de19fba5ed..2bc69271a9f91218bc3e4967ac23c893bd015e11 100644 --- a/main.c +++ b/main.c @@ -13,19 +13,21 @@ void help(const char *argv0) { printf("Usage: %s [options]\n" "\nAvailable options:\n" " -l,--list list available devices\n" - " -p,--port=client:port device port\n" + " -c,--client=client:port device client and port\n" " -s,--soundfont=file.sf2 soundfont file\n" + " -p,--preset=1 soundfont preset\n" " -h,--help this help\n" " -v,--version show version\n", argv0); } int main(int argc, char *argv[]) { - const char short_options[] = "lp:s:hv"; + const char short_options[] = "lc:s:p:hv"; const struct option long_options[] = { { "list", 0, NULL, 'l' }, - { "port", 1, NULL, 'p' }, + { "client", 1, NULL, 'c' }, { "soundfont", 1, NULL, 's' }, + { "preset", 1, NULL, 'p' }, { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'v' }, { 0 }, @@ -33,6 +35,7 @@ }; char *port_name = NULL; char *soundfont_file = NULL; + int soundfont_preset = -1; int opt; while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { @@ -40,12 +43,15 @@ switch (opt) { case 'l': fprintf(stderr, "List feature is NOT implemented yet.\n"); return 0; - case 'p': + case 'c': port_name = optarg; break; case 's': soundfont_file = optarg; break; + case 'p': + soundfont_preset = atoi(optarg); + break; case 'h': help(argv[0]); return 0; @@ -62,21 +68,25 @@ return 0; } } - if (port_name == NULL || soundfont_file == NULL) { + if (port_name == NULL || soundfont_file == NULL || soundfont_preset < 0) { fprintf(stdout, "Missing options. Check help.\n"); - fprintf(stdout, "Port and soundfile are required fields.\n"); + fprintf(stdout, "Port, soundfile and preset are required fields.\n"); return 1; } fprintf(stdout, "> Device port: %s\n", port_name); fprintf(stdout, "> Soundfont: %s\n", soundfont_file); + fprintf(stdout, "> SF preset: %d\n", soundfont_preset); // Create mutex. initialize_mutex(); // Create synth thread. pthread_t synth_thread; - SynthArgs synth_args = { soundfont_file }; + SynthArgs synth_args = { + .soundfont_file = soundfont_file, + .soundfont_preset = soundfont_preset, + }; if (pthread_create(&synth_thread, NULL, synth, (void*)&synth_args) != 0) { fprintf(stderr, "Error creating synth thread\n"); diff --git a/midi.c b/midi.c index f38982618f632c78242af57c61593f43bd5f411c..92df3d3052999735fb21e92056121cc5751e3392 100644 --- a/midi.c +++ b/midi.c @@ -109,7 +109,6 @@ break; } shared_data.action = 1; - shared_data.preset = 3; pthread_cond_signal(&cond_synth); pthread_mutex_unlock(&mutex); diff --git a/mutex.h b/mutex.h index 2f3c6537abebaf950eb59322b3c475769f53e982..78b4cdf7511fda0d36429d87d31e55dfdb2fed73 100644 --- a/mutex.h +++ b/mutex.h @@ -7,7 +7,6 @@ typedef struct { int note; int state; int velocity; - int preset; int action; } SharedData; diff --git a/synth.c b/synth.c index 05c3664d046897adcc1d38ec7159615bb135f456..d65ba4147e6adebf2d270ff1eb6a27ec02f55f3f 100644 --- a/synth.c +++ b/synth.c @@ -65,10 +65,10 @@ SDL_LockMutex(g_Mutex); if (shared_data.state == 0) { - tsf_note_off(g_TinySoundFont, shared_data.preset, shared_data.note); + tsf_note_off(g_TinySoundFont, args->soundfont_preset, shared_data.note); } else { float normalized_velocity = (float)shared_data.velocity / 127.0f; - tsf_note_on(g_TinySoundFont, shared_data.preset, shared_data.note, normalized_velocity); + tsf_note_on(g_TinySoundFont, args->soundfont_preset, shared_data.note, normalized_velocity); } SDL_UnlockMutex(g_Mutex); diff --git a/synth.h b/synth.h index 90fd410bda6110fdf32a2efc23f6247305454d48..7e08157760a77da2a0b3983491ff7561f5284152 100644 --- a/synth.h +++ b/synth.h @@ -7,6 +7,7 @@ #define AUDIO_CHANNELS 2 typedef struct { char *soundfont_file; + int soundfont_preset; } SynthArgs; void *synth(void *arg);