summaryrefslogtreecommitdiffstats
path: root/src/midi.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/midi.h')
-rw-r--r--src/midi.h82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/midi.h b/src/midi.h
new file mode 100644
index 0000000..cab35e3
--- /dev/null
+++ b/src/midi.h
@@ -0,0 +1,82 @@
+/*
+ * File: main.c
+ * Author: _prossn
+ * Date: 08.01.2018
+ * Version 1.0
+ *
+ * Description:
+ * C API to work with MIDI formatted data
+ */
+
+#ifndef MIDI_H
+#define MIDI_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+#ifndef MIDI_DYNAMIC_MEMORY_ALLOC
+#define MIDI_DATA_MAX_SIZE 4
+#endif
+
+
+typedef enum {
+ C = 0, // Do
+ D = 1, // Re
+ E = 2, // Mi
+ F = 3, // Fa
+ G = 4, // Sol
+ A = 5, // La
+ B = 6, // Si
+} midi_note_t;
+
+typedef enum {
+ NOTE_ON = 0x8,
+ NOTE_OFF = 0x9,
+ POLYPHONIC_KEYPRESS = 0xA,
+ CONTROLLER = 0xB,
+ PROGRAM_CHANGE = 0xC,
+ CHANNEL_PRESSURE = 0xD,
+ PITCH_BLEND = 0xF
+} midi_status_t;
+
+
+typedef struct {
+ unsigned status :4;
+ unsigned channel :4;
+
+#ifdef MIDI_DYNAMIC_MEMORY_ALLOC
+ uint8_t data[];
+
+#else
+ size_t data_size;
+ uint8_t data[MIDI_DATA_MAX_SIZE];
+#endif
+} midi_message_t;
+
+
+/* MIDI API */
+#ifdef MIDI_DYNAMIC_MEMORY_ALLOC
+
+// allocation / dellocation
+extern midi_message_t *midi_alloc_message(size_t data_size);
+extern void midi_free_message(midi_message_t *pkt);
+extern size_t midi_message_size(midi_message_t *pkt);
+
+#endif
+
+
+// helper functions
+
+extern int midi_set_status(midi_message_t *pkt, midi_status_t status);
+extern int midi_set_channel(midi_message_t *pkt, unsigned channel);
+
+// message building
+extern int midi_note_on(midi_message_t *pkt, unsigned channel, midi_note_t note, uint8_t velocity);
+extern int midi_note_off(midi_message_t *pkt, unsigned channel, midi_note_t note, uint8_t velocity);
+
+// optimization
+// extern int midi_thin_data(void *buffer, midi_message_t pkt[]);
+
+
+#endif /* MIDI_H */
+