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