6r1d / midi-message-parser

A C library for parsing MIDI messages.
MIT License
0 stars 0 forks source link

Parser extension #6

Closed 6r1d closed 3 years ago

6r1d commented 3 years ago

I want this parser to be able to extract more data. To do that, I am considering using a struct, containing a command_type and a union of different structs for each of them.

6r1d commented 3 years ago

Current idea for implementation: extend midi_message_t.

Something along the lines of:

#include <stdint.h>

struct midi_note_message
{
    uint8_t note;
    double frequency;
    uint8_t velocity;
    uint8_t channel;
} midi_note_message;

struct midi_cc_message
{
    uint8_t control_id;
    uint8_t value;
    uint8_t channel;
} midi_cc_message;

typedef struct {
    uint8_t  command_type;
    uint8_t  channel;
    uint32_t bytes_length;

    union data {
        struct midi_note_message note_message;
        struct midi_cc_message cc_message;
    } msg_data;

    uint8_t  bytes[];
} midi_message_t;

Approach from these StackOverflow questions: 1, 2.

Note to frequency conversion from MusicDSP projects seems useful for the task.

#include <math.h>

double midi_note_to_freq(char keynum) {
    return 440.0 * pow(2.0, ((double)keynum - 69.0) / 12.0);
}