[203] | 1 | //midi.c: |
---|
| 2 | |
---|
[687] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - August 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroardsp a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroardsp is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
[686] | 25 | #include "libroardsp.h" |
---|
[203] | 26 | |
---|
[367] | 27 | struct { |
---|
| 28 | uint16_t id; |
---|
| 29 | float freq; |
---|
| 30 | char * name; |
---|
| 31 | } _libroar_notes[] = { |
---|
| 32 | {ROAR_MIDI_NOTE_C , 261.625565, "C"}, |
---|
| 33 | {ROAR_MIDI_NOTE_Cs, 277.182631, "C#"}, |
---|
| 34 | {ROAR_MIDI_NOTE_D , 293.664768, "D"}, |
---|
| 35 | {ROAR_MIDI_NOTE_Ds, 311.126984, "D#"}, |
---|
| 36 | {ROAR_MIDI_NOTE_E , 329.627557, "E"}, |
---|
| 37 | {ROAR_MIDI_NOTE_F , 349.228231, "F"}, |
---|
| 38 | {ROAR_MIDI_NOTE_Fs, 369.994423, "F#"}, |
---|
| 39 | {ROAR_MIDI_NOTE_G , 391.995436, "G"}, |
---|
| 40 | {ROAR_MIDI_NOTE_Gs, 415.304698, "G#"}, |
---|
| 41 | {ROAR_MIDI_NOTE_A , 440.000000, "A"}, |
---|
| 42 | {ROAR_MIDI_NOTE_As, 466.163762, "A#"}, |
---|
| 43 | {ROAR_MIDI_NOTE_B , 493.883301, "B"}, |
---|
| 44 | {0, 0, NULL} |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | char * roar_midi_note2name (uint16_t note) { |
---|
| 48 | int i; |
---|
| 49 | |
---|
| 50 | for (i = 0; _libroar_notes[i].name != NULL; i++) |
---|
| 51 | if ( _libroar_notes[i].id == note ) |
---|
| 52 | return _libroar_notes[i].name; |
---|
| 53 | |
---|
| 54 | return NULL; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | uint16_t roar_midi_name2note (char * note) { |
---|
| 58 | int i; |
---|
| 59 | |
---|
| 60 | for (i = 0; _libroar_notes[i].name != NULL; i++) |
---|
| 61 | if ( strcasecmp(_libroar_notes[i].name, note) == 0 ) |
---|
| 62 | return _libroar_notes[i].id; |
---|
| 63 | |
---|
| 64 | return ROAR_MIDI_NOTE_NONE; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | float roar_midi_note2freq (uint16_t note) { |
---|
| 68 | int i; |
---|
| 69 | |
---|
| 70 | for (i = 0; _libroar_notes[i].name != NULL; i++) |
---|
| 71 | if ( _libroar_notes[i].id == note ) |
---|
| 72 | return _libroar_notes[i].freq; |
---|
| 73 | |
---|
| 74 | return -1; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | int roar_midi_find_octave (char * note); |
---|
| 78 | int roar_midi_add_octave (struct roar_note_octave * note) { |
---|
| 79 | note->name[0] = 0; |
---|
| 80 | return -1; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int roar_midi_notefill (struct roar_note_octave * note) { |
---|
| 84 | int oct; |
---|
| 85 | |
---|
| 86 | if ( !note ) |
---|
| 87 | return -1; |
---|
| 88 | |
---|
| 89 | if ( note->note && note->name[0] == 0 ) { |
---|
| 90 | roar_midi_add_octave(note); |
---|
| 91 | } else if ( !note->note && note->name[0] != 0 ) { |
---|
| 92 | return -1; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | oct = 1 << abs(note->octave); |
---|
| 96 | |
---|
| 97 | note->freq = roar_midi_note2freq(note->note); |
---|
| 98 | |
---|
| 99 | if ( note->octave < 0 ) { |
---|
| 100 | note->freq /= oct; |
---|
| 101 | } else { |
---|
| 102 | note->freq *= oct; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | return 0; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | int roar_midi_gen_tone (struct roar_note_octave * note, int16_t * samples, float t, int rate, int channels, int type, void * opts) { |
---|
[1104] | 109 | #ifdef ROAR_HAVE_LIBM |
---|
[367] | 110 | int i, c; |
---|
| 111 | float ct; |
---|
| 112 | float step = M_PI*2*note->freq/rate; |
---|
| 113 | int16_t s; |
---|
[369] | 114 | float (*op)(float x) = NULL; |
---|
[367] | 115 | |
---|
| 116 | /* |
---|
| 117 | rate: 1/s |
---|
| 118 | t : 1s |
---|
| 119 | freq: 1/s |
---|
| 120 | rew : 1s |
---|
| 121 | */ |
---|
| 122 | |
---|
[369] | 123 | if ( type == ROAR_MIDI_TYPE_SINE ) { |
---|
| 124 | op = sinf; |
---|
| 125 | } else { |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if ( op == NULL ) |
---|
| 130 | return -1; |
---|
| 131 | |
---|
[367] | 132 | for (ct = 0, i = 0; ct <= t; ct += step, i += channels) { |
---|
[369] | 133 | s = 32767*op(ct); |
---|
[367] | 134 | |
---|
| 135 | for (c = 0; c < channels; c++) |
---|
| 136 | samples[i+c] = s; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | return 0; |
---|
[1104] | 140 | #else |
---|
| 141 | return -1; |
---|
| 142 | #endif |
---|
[367] | 143 | } |
---|
| 144 | |
---|
[370] | 145 | |
---|
| 146 | int roar_midi_play_note (struct roar_stream * stream, struct roar_note_octave * note, float len) { |
---|
| 147 | return -1; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | int roar_midi_basic_init (struct roar_midi_basic_state * state) { |
---|
| 151 | if (!state) |
---|
| 152 | return -1; |
---|
| 153 | |
---|
| 154 | state->len.mul = 1; |
---|
| 155 | state->len.div = 60; |
---|
| 156 | |
---|
| 157 | state->note.note = ROAR_MIDI_NOTE_NONE; |
---|
| 158 | state->note.octave = 0; |
---|
| 159 | |
---|
| 160 | return 0; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | int roar_midi_basic_play (struct roar_stream * stream, struct roar_midi_basic_state * state, char * notes) { |
---|
| 164 | struct roar_midi_basic_state is; |
---|
| 165 | char cn[ROAR_MIDI_MAX_NOTENAME_LEN+1] = {0}; |
---|
| 166 | int i; |
---|
| 167 | int have = 0; |
---|
| 168 | struct roar_note_octave * n; |
---|
| 169 | |
---|
| 170 | if ( !notes ) |
---|
| 171 | return -1; |
---|
| 172 | |
---|
| 173 | if ( state == NULL ) { |
---|
| 174 | state = &is; |
---|
| 175 | roar_midi_basic_init(state); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | n = &(state->note); |
---|
| 179 | |
---|
| 180 | for (; *notes != 0; notes++) { |
---|
| 181 | if ( *notes == '>' ) { |
---|
| 182 | n->octave++; |
---|
| 183 | } else if ( *notes == '<' ) { |
---|
| 184 | n->octave--; |
---|
| 185 | } else if ( *notes != ' ' ) { |
---|
| 186 | have++; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if (have) { |
---|
| 190 | roar_midi_play_note(stream, n, 60 * state->len.mul / state->len.div); |
---|
| 191 | have = 0; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | return 0; |
---|
| 196 | } |
---|
| 197 | |
---|
[203] | 198 | //ll |
---|