summaryrefslogtreecommitdiffstats
path: root/src/midi.h
blob: 7ee42b77fdb209c479b83aba21168f439d084e04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * 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_OFF            = 0x8,
    NOTE_ON             = 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;


// starts at C2
const unsigned midi_notes_steps_table[] = {
    36, 38, 40, 41, 43, 45, 47, // C2 - B2
    
//    48, 50, 52, 53, 55, 57, 59, // C3 - B3
//    60, 62, 64, 65, 67, 69, 71, // C4 - B4
    
    // hardware bugfix
    // TODO se a compilation macro
    48, 50, 52, 53, 55, 57, 62, // C3 - B3
    60, 59, 64, 65, 67, 69, 71, // C4 - B4
};

/* 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(const midi_message_t *pkt);
extern size_t midi_message_size(const 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 */