Skip to content

Commit

Permalink
Adds unfinished examples with callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
6r1d committed Mar 7, 2021
1 parent 5d4150a commit 420f46e
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ examples/virtual_input_detailed/virtual_input
test/set_client_name/main
test/set_port_name/main
test/test_last_bytearray_byte/main
examples/get_full_port_name/get_full_port_name
examples/input_bytes_callback/input
examples/virtual_input_callback/virtual_input

8 changes: 8 additions & 0 deletions examples/input_bytes_callback/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CFLAGS=-Wall -O2 -g $(shell pkg-config --cflags alsa) $(shell pkg-config --cflags glib-2.0) -I../../include -I../include
LIBS=-pthread $(shell pkg-config --libs glib-2.0) $(shell pkg-config --libs alsa)

all:
$(CC) -o input input.c $(CFLAGS) $(LIBS)

clean:
rm -f main
81 changes: 81 additions & 0 deletions examples/input_bytes_callback/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdbool.h>
// Keeps process running until Ctrl-C is pressed.
// Contains a SIGINT handler and keep_process_running variable.
#include "util/exit_handling.h"
#include "util/output_handling.h"
// Main RMR header file
#include "midi/midi_handling.h"

Alsa_MIDI_data * data;
MIDI_in_data * input_data;

MIDI_port * current_midi_port;

RMR_Port_config * port_config;

MIDI_message * msg;
error_message * err_msg;

void message_handler(
double timestamp,
unsigned char * buf,
long count,
void * user_data
) {
print_midi_msg_buf(buf, count);
}

int main() {
// Allocate a MIDI_in_data instance, assign a
// MIDI message queue and an error queue
prepare_input_data_with_queues(&input_data);

// Create a port configuration with default values
setup_port_config(&port_config, MP_IN);
// Start a port with a provided configruation
start_port(&data, port_config);

// Allocate memory for a MIDI_port instance
init_midi_port(&current_midi_port);

// Assign amidi_data to input_data instance
assign_midi_data(input_data, data);

// Prepare to handle input through a callback
set_MIDI_in_callback(input_data, message_handler, NULL);

// Count the MIDI ports,
// open if a port containing "Synth" is available
if (find_midi_port(data, current_midi_port, "rmr", false) > 0) {
print_midi_port(current_midi_port);
open_port(true, data, current_midi_port->id, current_midi_port->port_info_name, input_data);
// Don't exit until Ctrl-C is pressed;
// Look up "output_handling.h"
keep_process_running = 1;
}

// Add a SIGINT handler to set keep_process_running to 0
// so the program can exit
signal(SIGINT, sigint_handler);

// Run until SIGINT is received
while (keep_process_running) {
while (g_async_queue_length_unlocked(input_data->error_async_queue)) {
// Read an error message from an error queue,
// simply deallocate it for now
err_msg = g_async_queue_try_pop(input_data->midi_async_queue);
if (err_msg != NULL) free_error_message(err_msg);
}
}

// Close a MIDI input port,
// shutdown the input thread,
// do cleanup
destroy_midi_input(data, input_data);

// Destroy a port configuration
destroy_port_config(port_config);

// Exit without an error
return 0;
}
8 changes: 8 additions & 0 deletions examples/virtual_input_callback/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CFLAGS=-Wall -O2 -g $(shell pkg-config --cflags alsa) $(shell pkg-config --cflags glib-2.0) -I../../include -I../include -lm
LIBS=-pthread $(shell pkg-config --libs glib-2.0) $(shell pkg-config --libs alsa)

all:
$(CC) -o virtual_input virtual_input.c $(CFLAGS) $(LIBS)

clean:
rm -f main
74 changes: 74 additions & 0 deletions examples/virtual_input_callback/virtual_input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdbool.h>
// Keeps process running until Ctrl-C is pressed.
// Contains a SIGINT handler and keep_process_running variable.
#include "util/exit_handling.h"
#include "util/output_handling.h"
#include "util/midi_parsing.h"
// Main RMR header file
#include "midi/midi_handling.h"

Alsa_MIDI_data * data;
MIDI_in_data * input_data;

MIDI_message * msg;
error_message * err_msg;

RMR_Port_config * port_config;

void message_handler(
double timestamp,
unsigned char * buf,
long count,
void * user_data
) {
print_midi_msg_buf(buf, count);
}

int main() {
// Allocate a MIDI_in_data instance, assign a
// MIDI message queue and an error queue
prepare_input_data_with_queues(&input_data);

// Create a port configuration with default values
setup_port_config(&port_config, MP_VIRTUAL_IN);
// Start a port with a provided configruation
start_port(&data, port_config);

// Assign amidi_data to input_data instance
assign_midi_data(input_data, data);

// Open a new port with a pre-set name
open_virtual_port(data, "rmr", input_data);

// Prepare to handle input through a callback
set_MIDI_in_callback(input_data, message_handler, NULL);

// Don't exit until Ctrl-C is pressed;
// Look up "output_handling.h"
keep_process_running = 1;

// Add a SIGINT handler to set keep_process_running to 0
// so the program can exit
signal(SIGINT, sigint_handler);

// Run until SIGINT is received
while (keep_process_running) {
while (g_async_queue_length_unlocked(input_data->error_async_queue)) {
// Read an error message from an error queue,
// simply deallocate it for now
err_msg = g_async_queue_try_pop(input_data->midi_async_queue);
if (err_msg != NULL) free_error_message(err_msg);
}
}

// Close a MIDI input port, shutdown the input thread,
// do cleanup
destroy_midi_input(data, input_data);

// Destroy a port configuration
destroy_port_config(port_config);

// Exit without an error
return 0;
}

0 comments on commit 420f46e

Please sign in to comment.