diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | example3.c | 14 | ||||
| -rw-r--r-- | example4.c | 54 |
5 files changed, 78 insertions, 8 deletions
@@ -1,4 +1,5 @@ example1 example2 example3 +example4 @@ -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 @@ -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 @@ -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 <stdio.h> +#include <stdlib.h> +#include <alsa/asoundlib.h> + +#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; +} + |
