[2448] | 1 | //ssynth.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 5 | * |
---|
| 6 | * This file is part of roard 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 | * RoarAudio 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 | |
---|
| 25 | #include "roard.h" |
---|
| 26 | |
---|
[2453] | 27 | float ssynth_polys[SSYNTH_POLY_POLYMAX][SSYNTH_POLY_COEFF] = { |
---|
| 28 | {0.300000, 0.958333, -0.550000, 0.091667}, |
---|
| 29 | {0.700010, -0.083333, -0.150000, 0.033333} |
---|
| 30 | }; |
---|
| 31 | |
---|
[2450] | 32 | int ssynth_init_config(void) { |
---|
| 33 | memset(&ssynth_conf, 0, sizeof(ssynth_conf)); |
---|
| 34 | |
---|
| 35 | return 0; |
---|
| 36 | } |
---|
| 37 | |
---|
[2452] | 38 | #define _err() streams_delete(sid); return -1 |
---|
[2450] | 39 | int ssynth_init (void) { |
---|
[2452] | 40 | struct roar_stream_server * ss; |
---|
| 41 | struct roar_stream * s; |
---|
| 42 | int sid; |
---|
| 43 | |
---|
[2450] | 44 | if ( !ssynth_conf.enable ) |
---|
| 45 | return 0; |
---|
| 46 | |
---|
[2452] | 47 | memset(&g_ssynth, 0, sizeof(g_ssynth)); |
---|
| 48 | g_ssynth.stream = -1; |
---|
| 49 | |
---|
| 50 | if ( (sid = streams_new()) == -1 ) |
---|
| 51 | return -1; |
---|
| 52 | |
---|
| 53 | if ( streams_set_client(sid, g_self_client) == -1 ) { |
---|
| 54 | _err(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | if ( streams_set_dir(sid, ROAR_DIR_BRIDGE, 1) == -1 ) { |
---|
| 58 | _err(); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if ( streams_set_flag(sid, ROAR_FLAG_PRIMARY) == -1 ) { |
---|
| 62 | _err(); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if ( streams_set_name(sid, "Simple Synthesizer") == -1 ) { |
---|
| 66 | _err(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | if ( streams_get(sid, &ss) == -1 ) { |
---|
| 70 | _err(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | s = ROAR_STREAM(ss); |
---|
| 74 | |
---|
| 75 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 76 | |
---|
| 77 | s->info.channels = 1; |
---|
| 78 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 79 | |
---|
[2468] | 80 | g_ssynth.stream = sid; |
---|
[2452] | 81 | |
---|
| 82 | return 0; |
---|
[2450] | 83 | } |
---|
| 84 | |
---|
| 85 | int ssynth_free (void) { |
---|
[2469] | 86 | int i; |
---|
| 87 | |
---|
[2450] | 88 | if ( !ssynth_conf.enable ) |
---|
| 89 | return 0; |
---|
| 90 | |
---|
[2469] | 91 | for (i = 0; i < SSYNTH_NOTES_MAX; i++) { |
---|
| 92 | if ( g_ssynth.notes[i].buf != NULL ) { |
---|
| 93 | roar_buffer_free(g_ssynth.notes[i].buf); |
---|
| 94 | g_ssynth.notes[i].buf = NULL; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
[2452] | 98 | return streams_delete(g_ssynth.stream); |
---|
[2450] | 99 | } |
---|
| 100 | |
---|
[2467] | 101 | int ssynth_update (void) { |
---|
[2469] | 102 | struct roar_stream_server * ss; |
---|
| 103 | struct roar_stream * s; |
---|
| 104 | struct roar_buffer * buf; |
---|
[2470] | 105 | struct roar_buffer * outbuf; |
---|
| 106 | void * outbufdata; |
---|
| 107 | void * indbufs[SSYNTH_NOTES_MAX+1]; |
---|
| 108 | int curin = 0; |
---|
[2469] | 109 | size_t buflen; |
---|
| 110 | size_t needlen; |
---|
| 111 | int i; |
---|
| 112 | |
---|
| 113 | if ( !ssynth_conf.enable ) |
---|
| 114 | return 0; |
---|
| 115 | |
---|
| 116 | if ( streams_get(g_ssynth.stream, &ss) == -1 ) { |
---|
| 117 | return -1; |
---|
| 118 | } |
---|
| 119 | |
---|
[2470] | 120 | memset(indbufs, 0, sizeof(indbufs)); |
---|
| 121 | |
---|
[2469] | 122 | s = ROAR_STREAM(ss); |
---|
| 123 | |
---|
| 124 | needlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); |
---|
| 125 | |
---|
| 126 | for (i = 0; i < SSYNTH_NOTES_MAX; i++) { |
---|
| 127 | if ( g_ssynth.notes[i].stage != SSYNTH_STAGE_UNUSED ) { |
---|
| 128 | if ( g_ssynth.notes[i].buf == NULL ) { |
---|
| 129 | if ( roar_buffer_new(&buf, needlen) == -1 ) |
---|
| 130 | continue; |
---|
| 131 | |
---|
| 132 | g_ssynth.notes[i].buf = buf; |
---|
| 133 | } else { |
---|
| 134 | buf = g_ssynth.notes[i].buf; |
---|
| 135 | |
---|
| 136 | if ( roar_buffer_get_len(buf, &buflen) == -1 ) |
---|
| 137 | continue; |
---|
| 138 | |
---|
| 139 | if ( buflen < needlen ) { |
---|
| 140 | if ( roar_buffer_set_len(buf, needlen) == -1 ) |
---|
| 141 | continue; |
---|
| 142 | } |
---|
| 143 | } |
---|
[2470] | 144 | |
---|
| 145 | if ( roar_buffer_get_data(buf, &(indbufs[curin])) == -1 ) |
---|
| 146 | continue; |
---|
| 147 | |
---|
[2471] | 148 | if ( ssynth_note_render(i, indbufs[curin]) == -1 ) |
---|
| 149 | continue; |
---|
| 150 | |
---|
[2470] | 151 | curin++; |
---|
[2469] | 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
[2470] | 155 | if ( curin > 0 ) { |
---|
| 156 | if ( roar_buffer_new(&outbuf, needlen) == -1 ) |
---|
| 157 | return -1; |
---|
| 158 | |
---|
| 159 | if ( roar_buffer_get_data(outbuf, &outbufdata) == -1 ) { |
---|
| 160 | roar_buffer_free(outbuf); |
---|
| 161 | return -1; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | if ( mix_clients(outbufdata, g_sa->bits, indbufs, ROAR_OUTPUT_BUFFER_SAMPLES) == -1 ) { |
---|
| 165 | roar_buffer_free(outbuf); |
---|
| 166 | return -1; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | if ( stream_add_buffer(g_ssynth.stream, outbuf) == -1 ) { |
---|
| 170 | roar_buffer_free(outbuf); |
---|
| 171 | return -1; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | return 0; |
---|
[2467] | 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
[2457] | 179 | int ssynth_note_new(struct roar_note_octave * note, char vv) { |
---|
| 180 | int i; |
---|
| 181 | |
---|
| 182 | for (i = 0; i < SSYNTH_NOTES_MAX; i++) { |
---|
| 183 | if ( g_ssynth.notes[i].stage == SSYNTH_STAGE_UNUSED ) { |
---|
| 184 | // TODO: do some error checking here |
---|
| 185 | g_ssynth.notes[i].vv_down = vv; |
---|
| 186 | memcpy(&(g_ssynth.notes[i].note), note, sizeof(struct roar_note_octave)); |
---|
| 187 | roar_synth_init(&(g_ssynth.notes[i].synth), &(g_ssynth.notes[i].note), g_sa->rate); |
---|
| 188 | ssynth_note_set_stage(i, SSYNTH_STAGE_KEYSTROKE); |
---|
| 189 | return i; |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | return -1; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | int ssynth_note_free(int id) { |
---|
| 197 | g_ssynth.notes[id].stage = SSYNTH_STAGE_UNUSED; |
---|
| 198 | return 0; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | int ssynth_note_find(struct roar_note_octave * note) { |
---|
[2465] | 202 | struct roar_note_octave * cn; |
---|
| 203 | int i; |
---|
| 204 | |
---|
| 205 | for (i = 0; i < SSYNTH_NOTES_MAX; i++) { |
---|
| 206 | if ( g_ssynth.notes[i].stage == SSYNTH_STAGE_UNUSED ) |
---|
| 207 | continue; |
---|
| 208 | |
---|
| 209 | cn = &(g_ssynth.notes[i].note); |
---|
| 210 | |
---|
| 211 | if ( !(note->note == cn->note && note->octave == cn->octave) ) |
---|
| 212 | continue; |
---|
| 213 | |
---|
| 214 | return i; |
---|
| 215 | } |
---|
| 216 | |
---|
[2457] | 217 | return -1; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | int ssynth_note_set_stage(int id, int stage) { |
---|
| 221 | int r = -1; |
---|
| 222 | |
---|
| 223 | switch (stage) { |
---|
| 224 | case SSYNTH_STAGE_UNUSED: |
---|
| 225 | r = ssynth_note_free(id); |
---|
| 226 | break; |
---|
| 227 | case SSYNTH_STAGE_KEYSTROKE: |
---|
| 228 | r = roar_fader_init(&(g_ssynth.notes[id].fader), ssynth_polys[SSYNTH_POLY_KEYDOWN], SSYNTH_POLY_COEFF); |
---|
| 229 | break; |
---|
| 230 | case SSYNTH_STAGE_MIDSECTION: |
---|
| 231 | r = 0; |
---|
| 232 | break; |
---|
| 233 | case SSYNTH_STAGE_KEYRELEASE: |
---|
| 234 | r = roar_fader_init(&(g_ssynth.notes[id].fader), ssynth_polys[SSYNTH_POLY_KEYUP], SSYNTH_POLY_COEFF); |
---|
| 235 | break; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | if ( r == 0 ) |
---|
| 239 | g_ssynth.notes[id].stage = stage; |
---|
| 240 | |
---|
| 241 | return r; |
---|
| 242 | } |
---|
| 243 | |
---|
[2471] | 244 | int ssynth_note_render (int id, void * data) { |
---|
| 245 | if ( g_sa->bits != 16 ) |
---|
| 246 | return -1; |
---|
| 247 | |
---|
| 248 | return roar_synth_pcmout_i161(&(g_ssynth.notes[id].synth), data, ROAR_OUTPUT_BUFFER_SAMPLES); |
---|
| 249 | } |
---|
| 250 | |
---|
[2463] | 251 | int ssynth_note_on (struct roar_note_octave * note, char vv) { |
---|
[2464] | 252 | return ssynth_note_new(note, vv); |
---|
[2463] | 253 | } |
---|
| 254 | |
---|
| 255 | int ssynth_note_off (struct roar_note_octave * note, char vv) { |
---|
[2464] | 256 | int id; |
---|
| 257 | |
---|
| 258 | if ( (id = ssynth_note_find(note)) == -1 ) |
---|
| 259 | return -1; |
---|
| 260 | |
---|
| 261 | // add support to for keyups... |
---|
| 262 | |
---|
| 263 | return ssynth_note_free(id); |
---|
[2463] | 264 | } |
---|
| 265 | |
---|
[2460] | 266 | int ssynth_eval_message (struct midi_message * mes) { |
---|
[2463] | 267 | if ( !ssynth_conf.enable ) |
---|
| 268 | return -1; |
---|
| 269 | |
---|
| 270 | switch (mes->type) { |
---|
| 271 | case MIDI_TYPE_NOTE_OFF: |
---|
| 272 | return ssynth_note_off(&(mes->d.note), mes->vv); |
---|
| 273 | break; |
---|
| 274 | case MIDI_TYPE_NOTE_ON: |
---|
| 275 | return ssynth_note_off(&(mes->d.note), mes->vv); |
---|
| 276 | break; |
---|
| 277 | default: |
---|
| 278 | /* ignore all other events at the moment */ |
---|
| 279 | return 0; |
---|
| 280 | } |
---|
| 281 | |
---|
[2460] | 282 | return -1; |
---|
| 283 | } |
---|
| 284 | |
---|
[2448] | 285 | //ll |
---|