summaryrefslogtreecommitdiff
path: root/experiments
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2024-10-08 06:05:51 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2024-10-08 06:05:51 +0200
commit65ffdf410ca5c36f059bee8619f87147f802a82e (patch)
tree047d9b679497a7b5e367e8b1b1f5e5f24d945af2 /experiments
parent562c07f32366687dcb032b9a2628b9c85a01432e (diff)
downloadttdaw-65ffdf410ca5c36f059bee8619f87147f802a82e.tar.gz
Renamed to experiments and added watch for changes
Diffstat (limited to 'experiments')
-rw-r--r--experiments/.gitignore5
-rw-r--r--experiments/Makefile22
-rw-r--r--experiments/alsa_midi_controller.c124
-rw-r--r--experiments/soundfont_basic.c81
-rw-r--r--experiments/soundfont_from_file.c76
-rw-r--r--experiments/watch_file_changes.c30
-rw-r--r--experiments/watch_file_changes.txt3
7 files changed, 341 insertions, 0 deletions
diff --git a/experiments/.gitignore b/experiments/.gitignore
new file mode 100644
index 0000000..aceb7f8
--- /dev/null
+++ b/experiments/.gitignore
@@ -0,0 +1,5 @@
+alsa_midi_controller
+soundfont_basic
+soundfont_from_file
+watch_file_changes
+
diff --git a/experiments/Makefile b/experiments/Makefile
new file mode 100644
index 0000000..ad822f5
--- /dev/null
+++ b/experiments/Makefile
@@ -0,0 +1,22 @@
+CC := cc
+CFLAGS := -Wall
+LDFLAGS := -lm -ldl -lpthread -lasound
+
+all: \
+ soundfont_basic \
+ soundfont_from_file \
+ alsa_midi_controller \
+ watch_file_changes
+
+soundfont_basic: soundfont_basic.c
+ $(CC) $(CFLAGS) soundfont_basic.c ../minisdl_audio.c $(LDFLAGS) -o soundfont_basic
+
+soundfont_from_file: soundfont_from_file.c
+ $(CC) $(CFLAGS) soundfont_from_file.c ../minisdl_audio.c $(LDFLAGS) -o soundfont_from_file
+
+alsa_midi_controller: alsa_midi_controller.c
+ $(CC) $(CFLAGS) alsa_midi_controller.c $(LDFLAGS) -o alsa_midi_controller
+
+watch_file_changes: watch_file_changes.c
+ $(CC) $(CFLAGS) watch_file_changes.c $(LDFLAGS) -o watch_file_changes
+
diff --git a/experiments/alsa_midi_controller.c b/experiments/alsa_midi_controller.c
new file mode 100644
index 0000000..3181c29
--- /dev/null
+++ b/experiments/alsa_midi_controller.c
@@ -0,0 +1,124 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <alloca.h>
+#include <signal.h>
+#include <alsa/asoundlib.h>
+
+#define CLIENT_NAME "ttdaw"
+#define MAX_MIDI_PORTS 1
+
+static snd_seq_t *seq_handle;
+static snd_seq_addr_t *ports;
+static int stop = 0;
+
+static void sighandler(int sig ATTRIBUTE_UNUSED) {
+ stop = 1;
+}
+
+int main(void) {
+ fprintf(stdout, "Example: Reading MIDI input\n");
+
+ 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);
+ }
+
+ // Connecting ports.
+ char *port_name = "28:0";
+ ports = realloc(ports, MAX_MIDI_PORTS * sizeof(snd_seq_addr_t));
+ if (snd_seq_parse_address(seq_handle, &ports[0], port_name) < 0) {
+ fprintf(stderr, "Invalid port %s.\n", port_name);
+ exit(1);
+ }
+
+ // Listing assigned ports.
+ fprintf(stdout, "Ports:\n");
+ for (int j = 0; j < MAX_MIDI_PORTS; j++) {
+ fprintf(stdout, " client: %d, port: %d\n", ports[j].client, ports[j].port);
+ }
+
+ // 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);
+ }
+ }
+
+ if (snd_seq_nonblock(seq_handle, 1) < 0) {
+ fprintf(stderr, "Set nonblock mode failed.");
+ exit(1);
+ }
+
+ signal(SIGINT, sighandler);
+ signal(SIGTERM, sighandler);
+
+ // Reading MIDI device.
+ struct pollfd *pfds;
+ size_t npfds;
+
+ npfds = snd_seq_poll_descriptors_count(seq_handle, POLLIN);
+ pfds = alloca(sizeof(*pfds) * npfds);
+
+ for (;;) {
+ snd_seq_poll_descriptors(seq_handle, pfds, npfds, POLLIN);
+ if (poll(pfds, npfds, -1) < 0) {
+ break;
+ }
+
+ for (;;) {
+ snd_seq_event_t *ev;
+
+ if (snd_seq_event_input(seq_handle, &ev) < 0) {
+ break;
+ }
+
+ if (ev) {
+ switch (ev->type) {
+ case SND_SEQ_EVENT_NOTEON:
+ printf("%3d:%-3dNote on %2d, note %d, velocity: %3d\n",
+ ev->source.client, ev->source.port,
+ ev->data.note.channel,
+ ev->data.note.note,
+ ev->data.note.velocity);
+ break;
+
+ case SND_SEQ_EVENT_NOTEOFF:
+ printf("%3d:%-3dNote off\t%2d, note %d\n",
+ ev->source.client, ev->source.port,
+ ev->data.note.channel,
+ ev->data.note.note);
+ break;
+
+
+ }
+ }
+ }
+
+ fflush(stdout);
+
+ if (stop) {
+ break;
+ }
+ }
+
+ fprintf(stdout, "Finishing up\n");
+ snd_seq_close(seq_handle);
+ return 0;
+}
+
diff --git a/experiments/soundfont_basic.c b/experiments/soundfont_basic.c
new file mode 100644
index 0000000..91800ad
--- /dev/null
+++ b/experiments/soundfont_basic.c
@@ -0,0 +1,81 @@
+#define TSF_IMPLEMENTATION
+#include "../tsf.h"
+
+#include "../minisdl_audio.h"
+
+//This is a minimal SoundFont with a single loopin saw-wave sample/instrument/preset (484 bytes)
+const static unsigned char MinimalSoundFont[] = {
+#define TEN0 0,0,0,0,0,0,0,0,0,0
+ 'R','I','F','F',220,1,0,0,'s','f','b','k',
+ 'L','I','S','T',88,1,0,0,'p','d','t','a',
+ 'p','h','d','r',76,TEN0,TEN0,TEN0,TEN0,0,0,0,0,TEN0,0,0,0,0,0,0,0,255,0,255,0,1,TEN0,0,0,0,
+ 'p','b','a','g',8,0,0,0,0,0,0,0,1,0,0,0,'p','m','o','d',10,TEN0,0,0,0,'p','g','e','n',8,0,0,0,41,0,0,0,0,0,0,0,
+ 'i','n','s','t',44,TEN0,TEN0,0,0,0,0,0,0,0,0,TEN0,0,0,0,0,0,0,0,1,0,
+ 'i','b','a','g',8,0,0,0,0,0,0,0,2,0,0,0,'i','m','o','d',10,TEN0,0,0,0,
+ 'i','g','e','n',12,0,0,0,54,0,1,0,53,0,0,0,0,0,0,0,
+ 's','h','d','r',92,TEN0,TEN0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,49,0,0,0,34,86,0,0,60,0,0,0,1,TEN0,TEN0,TEN0,TEN0,0,0,0,0,0,0,0,
+ 'L','I','S','T',112,0,0,0,'s','d','t','a','s','m','p','l',100,0,0,0,86,0,119,3,31,7,147,10,43,14,169,17,58,21,189,24,73,28,204,31,73,35,249,38,46,42,71,46,250,48,150,53,242,55,126,60,151,63,108,66,126,72,207,
+ 70,86,83,100,72,74,100,163,39,241,163,59,175,59,179,9,179,134,187,6,186,2,194,5,194,15,200,6,202,96,206,159,209,35,213,213,216,45,220,221,223,76,227,221,230,91,234,242,237,105,241,8,245,118,248,32,252
+};
+
+// Holds the global instance pointer
+static tsf* g_TinySoundFont;
+
+// Callback function called by the audio thread
+static void AudioCallback(void* data, Uint8 *stream, int len) {
+ // Note we don't do any thread concurrency control here because in this
+ // example all notes are started before the audio playback begins.
+ // If you do play notes while the audio thread renders output you
+ // will need a mutex of some sort.
+ int SampleCount = (len / (2 * sizeof(short))); //2 output channels
+ tsf_render_short(g_TinySoundFont, (short*)stream, SampleCount, 0);
+}
+
+int main(int argc, char *argv[]) {
+ // Define the desired audio output format we request
+ SDL_AudioSpec OutputAudioSpec;
+ OutputAudioSpec.freq = 44100;
+ OutputAudioSpec.format = AUDIO_S16;
+ OutputAudioSpec.channels = 2;
+ OutputAudioSpec.samples = 4096;
+ OutputAudioSpec.callback = AudioCallback;
+
+ // Initialize the audio system
+ if (SDL_AudioInit(NULL) < 0) {
+ fprintf(stderr, "Could not initialize audio hardware or driver\n");
+ return 1;
+ }
+
+ // Load the SoundFont from the memory block
+ g_TinySoundFont = tsf_load_memory(MinimalSoundFont, sizeof(MinimalSoundFont));
+ if (!g_TinySoundFont) {
+ fprintf(stderr, "Could not load soundfont\n");
+ return 1;
+ }
+
+ // Set the rendering output mode to 44.1khz and -10 decibel gain
+ tsf_set_output(g_TinySoundFont, TSF_STEREO_INTERLEAVED, OutputAudioSpec.freq, -10);
+
+ // Start two notes before starting the audio playback
+ tsf_note_on(g_TinySoundFont, 0, 48, 1.0f); //C2
+ tsf_note_on(g_TinySoundFont, 0, 52, 1.0f); //E2
+
+ // Request the desired audio output format
+ if (SDL_OpenAudio(&OutputAudioSpec, NULL) < 0) {
+ fprintf(stderr, "Could not open the audio hardware or the desired audio output format\n");
+ return 1;
+ }
+
+ // Start the actual audio playback here
+ // The audio thread will begin to call our AudioCallback function
+ SDL_PauseAudio(0);
+
+ // Let the audio callback play some sound for 3 seconds
+ SDL_Delay(3000);
+
+ // We could call tsf_close(g_TinySoundFont) and SDL_DestroyMutex(g_Mutex)
+ // here to free the memory and resources but we just let the OS clean up
+ // because the process ends here.
+ return 0;
+}
+
diff --git a/experiments/soundfont_from_file.c b/experiments/soundfont_from_file.c
new file mode 100644
index 0000000..8518744
--- /dev/null
+++ b/experiments/soundfont_from_file.c
@@ -0,0 +1,76 @@
+#include "../minisdl_audio.h"
+
+#define TSF_IMPLEMENTATION
+#include "../tsf.h"
+
+// Holds the global instance pointer
+static tsf* g_TinySoundFont;
+
+// A Mutex so we don't call note_on/note_off while rendering audio samples
+static SDL_mutex* g_Mutex;
+
+static void AudioCallback(void* data, Uint8 *stream, int len) {
+ // Render the audio samples in float format
+ int SampleCount = (len / (2 * sizeof(float))); //2 output channels
+ SDL_LockMutex(g_Mutex); //get exclusive lock
+ tsf_render_float(g_TinySoundFont, (float*)stream, SampleCount, 0);
+ SDL_UnlockMutex(g_Mutex);
+}
+
+int main(int argc, char *argv[]) {
+ int i, Notes[7] = { 48, 50, 52, 53, 55, 57, 59 };
+
+ // Define the desired audio output format we request
+ SDL_AudioSpec OutputAudioSpec;
+ OutputAudioSpec.freq = 44100;
+ OutputAudioSpec.format = AUDIO_F32;
+ OutputAudioSpec.channels = 2;
+ OutputAudioSpec.samples = 4096;
+ OutputAudioSpec.callback = AudioCallback;
+
+ // Initialize the audio system
+ if (SDL_AudioInit(TSF_NULL) < 0) {
+ fprintf(stderr, "Could not initialize audio hardware or driver\n");
+ return 1;
+ }
+
+ // Load the SoundFont from a file
+ g_TinySoundFont = tsf_load_filename("../soundfonts/florestan-subset.sf2");
+ if (!g_TinySoundFont) {
+ fprintf(stderr, "Could not load SoundFont\n");
+ return 1;
+ }
+
+ // Set the SoundFont rendering output mode
+ tsf_set_output(g_TinySoundFont, TSF_STEREO_INTERLEAVED, OutputAudioSpec.freq, 0);
+
+ // Create the mutex
+ g_Mutex = SDL_CreateMutex();
+
+ // Request the desired audio output format
+ if (SDL_OpenAudio(&OutputAudioSpec, TSF_NULL) < 0) {
+ fprintf(stderr, "Could not open the audio hardware or the desired audio output format\n");
+ return 1;
+ }
+
+ // Start the actual audio playback here
+ // The audio thread will begin to call our AudioCallback function
+ SDL_PauseAudio(0);
+
+ // Loop through all the presets in the loaded SoundFont
+ for (i = 0; i < tsf_get_presetcount(g_TinySoundFont); i++) {
+ //Get exclusive mutex lock, end the previous note and play a new note
+ printf("Play note %d with preset #%d '%s'\n", Notes[i % 7], i, tsf_get_presetname(g_TinySoundFont, i));
+ SDL_LockMutex(g_Mutex);
+ tsf_note_off(g_TinySoundFont, i - 1, Notes[(i - 1) % 7]);
+ tsf_note_on(g_TinySoundFont, i, Notes[i % 7], 1.0f);
+ SDL_UnlockMutex(g_Mutex);
+ SDL_Delay(1000);
+ }
+
+ // We could call tsf_close(g_TinySoundFont) and SDL_DestroyMutex(g_Mutex)
+ // here to free the memory and resources but we just let the OS clean up
+ // because the process ends here.
+ return 0;
+}
+
diff --git a/experiments/watch_file_changes.c b/experiments/watch_file_changes.c
new file mode 100644
index 0000000..04fd3b2
--- /dev/null
+++ b/experiments/watch_file_changes.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <time.h>
+
+int main(void) {
+ struct stat file_stat;
+ time_t last_mod_time;
+ const char* file = "watch_file_changes.txt";
+
+ stat(file, &file_stat);
+ last_mod_time = file_stat.st_mtime;
+
+ while (1) {
+ sleep(1);
+ stat(file, &file_stat);
+ if (file_stat.st_mtime != last_mod_time) {
+ last_mod_time = file_stat.st_mtime;
+
+ struct tm* time_info = localtime(&last_mod_time);
+ char formatted_time[9];
+ strftime(formatted_time, sizeof(formatted_time), "%H:%M:%S", time_info);
+
+ printf("File %s changed at %s\n", file, formatted_time);
+ }
+ }
+ printf("hi\n");
+ return 0;
+}
+
diff --git a/experiments/watch_file_changes.txt b/experiments/watch_file_changes.txt
new file mode 100644
index 0000000..9de6d55
--- /dev/null
+++ b/experiments/watch_file_changes.txt
@@ -0,0 +1,3 @@
+test1
+test2
+