From 1eeba03da0222a117054eca53e7be8ee1e8bd970 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Mon, 7 Oct 2024 17:41:54 +0200 Subject: Added ALSA MIDI example --- .gitignore | 1 + Makefile | 5 ++++- README.md | 12 ++++++++++++ example3.c | 14 +++++++------- example4.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 example4.c diff --git a/.gitignore b/.gitignore index c21c3a6..47fd165 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ example1 example2 example3 +example4 diff --git a/Makefile b/Makefile index 51e4b2d..727e920 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,11 @@ tests: build-portmidi $(CC) -Wall example2.c minisdl_audio.c -lm -ldl -lpthread -o example2 $(CC) -Wall example3.c -L./portmidi -lportmidi -o example3 -run-example3: +run-example3: tests LD_LIBRARY_PATH=./portmidi ./example3 +run-example4: + $(CC) example4.c -lasound -lm -o example4 && ./example4 + build-portmidi: cd portmidi && cmake . && make diff --git a/README.md b/README.md index 379eb42..442c27e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,17 @@ # Tiny terminal DAW +`ttdaw` is a tiny terminal based digital audio workstation made for fun and +experimentation and learning more about audio and MIDI in general. + +## Documentations + +- https://developer.apple.com/documentation/coremidi +- https://www.alsa-project.org/alsa-doc/alsa-lib/seq.html + +## Reading material + +- https://www.izotope.com/en/learn/fundamental-frequency-harmonics.html + ## Soundfonts - https://dev.nando.audio/pages/soundfonts.html diff --git a/example3.c b/example3.c index 9dea995..a2103f2 100644 --- a/example3.c +++ b/example3.c @@ -20,12 +20,12 @@ int main() { } /* // Open a MIDI input device */ - /* stream = Pm_OpenInput(&err, NULL, NULL, NUM_INPUTS, NULL, 0); */ - /* if (err != pmNoError) { */ - /* fprintf(stderr, "Error opening MIDI input device: %s\n", Pm_GetErrorText(err)); */ - /* Pm_Terminate(); */ - /* exit(1); */ - /* } */ + stream = (PortMidiStream *)Pm_OpenInput(&err, NULL, NULL, NUM_INPUTS, NULL, 0); + if (err != pmNoError) { + fprintf(stderr, "Error opening MIDI input device: %s\n", Pm_GetErrorText(err)); + Pm_Terminate(); + exit(1); + } /* // Read MIDI messages from the input device */ /* while (1) { */ @@ -43,7 +43,7 @@ int main() { /* } */ /* // Close the MIDI input device */ - /* Pm_Close(stream); */ + Pm_Close(stream); // Terminate PortMIDI Pm_Terminate(); diff --git a/example4.c b/example4.c new file mode 100644 index 0000000..c14e5bc --- /dev/null +++ b/example4.c @@ -0,0 +1,54 @@ +#include +#include +#include + +#define CLIENT_NAME "ttdaw" +#define MAX_MIDI_PORTS 4 + +static snd_seq_t *seq_handle; +static snd_seq_addr_t *ports; +static int rate = 44100; + +int main(void) { + fprintf(stdout, "Example: Reading MIDI input\n"); + + snd_seq_t *seq_handle; + + if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0) { + fprintf(stderr, "Error opening ALSA sequencer.\n"); + exit(1); + } + + if (snd_seq_set_client_name(seq_handle, CLIENT_NAME) < 0) { + fprintf(stderr, "Could not set up client name.\n"); + exit(1); + } + + if (snd_seq_create_simple_port(seq_handle, CLIENT_NAME, + SND_SEQ_PORT_CAP_WRITE | + SND_SEQ_PORT_CAP_SUBS_WRITE, + SND_SEQ_PORT_TYPE_MIDI_GENERIC | + SND_SEQ_PORT_TYPE_APPLICATION) < 0) { + fprintf(stderr, "Error creating sequencer port.\n"); + exit(1); + } + + char *port_name = "28:0"; + if (snd_seq_parse_address(seq_handle, &ports[0], port_name) < 0) { + fprintf(stderr, "Invalid port %s.\n", port_name); + exit(1); + } + + // Connecting ports. + for (int i = 0; i < MAX_MIDI_PORTS; ++i) { + int err = snd_seq_connect_from(seq_handle, 0, ports[i].client, ports[i].port); + if (err < 0) { + fprintf(stderr, "Cannot connect from port %d:%d - %s", ports[i].client, ports[i].port, snd_strerror(err)); + exit(1); + } + } + + snd_seq_close(seq_handle); + return 0; +} + -- cgit v1.2.3