[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 | |
---|
| 80 | g_ssynth.stream = sid; |
---|
| 81 | |
---|
| 82 | return 0; |
---|
[2450] | 83 | } |
---|
| 84 | |
---|
| 85 | int ssynth_free (void) { |
---|
| 86 | if ( !ssynth_conf.enable ) |
---|
| 87 | return 0; |
---|
| 88 | |
---|
[2452] | 89 | return streams_delete(g_ssynth.stream); |
---|
[2450] | 90 | } |
---|
| 91 | |
---|
[2457] | 92 | int ssynth_note_new(struct roar_note_octave * note, char vv) { |
---|
| 93 | int i; |
---|
| 94 | |
---|
| 95 | for (i = 0; i < SSYNTH_NOTES_MAX; i++) { |
---|
| 96 | if ( g_ssynth.notes[i].stage == SSYNTH_STAGE_UNUSED ) { |
---|
| 97 | // TODO: do some error checking here |
---|
| 98 | g_ssynth.notes[i].vv_down = vv; |
---|
| 99 | memcpy(&(g_ssynth.notes[i].note), note, sizeof(struct roar_note_octave)); |
---|
| 100 | roar_synth_init(&(g_ssynth.notes[i].synth), &(g_ssynth.notes[i].note), g_sa->rate); |
---|
| 101 | ssynth_note_set_stage(i, SSYNTH_STAGE_KEYSTROKE); |
---|
| 102 | return i; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | return -1; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | int ssynth_note_free(int id) { |
---|
| 110 | g_ssynth.notes[id].stage = SSYNTH_STAGE_UNUSED; |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | int ssynth_note_find(struct roar_note_octave * note) { |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | int ssynth_note_set_stage(int id, int stage) { |
---|
| 119 | int r = -1; |
---|
| 120 | |
---|
| 121 | switch (stage) { |
---|
| 122 | case SSYNTH_STAGE_UNUSED: |
---|
| 123 | r = ssynth_note_free(id); |
---|
| 124 | break; |
---|
| 125 | case SSYNTH_STAGE_KEYSTROKE: |
---|
| 126 | r = roar_fader_init(&(g_ssynth.notes[id].fader), ssynth_polys[SSYNTH_POLY_KEYDOWN], SSYNTH_POLY_COEFF); |
---|
| 127 | break; |
---|
| 128 | case SSYNTH_STAGE_MIDSECTION: |
---|
| 129 | r = 0; |
---|
| 130 | break; |
---|
| 131 | case SSYNTH_STAGE_KEYRELEASE: |
---|
| 132 | r = roar_fader_init(&(g_ssynth.notes[id].fader), ssynth_polys[SSYNTH_POLY_KEYUP], SSYNTH_POLY_COEFF); |
---|
| 133 | break; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if ( r == 0 ) |
---|
| 137 | g_ssynth.notes[id].stage = stage; |
---|
| 138 | |
---|
| 139 | return r; |
---|
| 140 | } |
---|
| 141 | |
---|
[2463] | 142 | int ssynth_note_on (struct roar_note_octave * note, char vv) { |
---|
[2464] | 143 | return ssynth_note_new(note, vv); |
---|
[2463] | 144 | } |
---|
| 145 | |
---|
| 146 | int ssynth_note_off (struct roar_note_octave * note, char vv) { |
---|
[2464] | 147 | int id; |
---|
| 148 | |
---|
| 149 | if ( (id = ssynth_note_find(note)) == -1 ) |
---|
| 150 | return -1; |
---|
| 151 | |
---|
| 152 | // add support to for keyups... |
---|
| 153 | |
---|
| 154 | return ssynth_note_free(id); |
---|
[2463] | 155 | } |
---|
| 156 | |
---|
[2460] | 157 | int ssynth_eval_message (struct midi_message * mes) { |
---|
[2463] | 158 | if ( !ssynth_conf.enable ) |
---|
| 159 | return -1; |
---|
| 160 | |
---|
| 161 | switch (mes->type) { |
---|
| 162 | case MIDI_TYPE_NOTE_OFF: |
---|
| 163 | return ssynth_note_off(&(mes->d.note), mes->vv); |
---|
| 164 | break; |
---|
| 165 | case MIDI_TYPE_NOTE_ON: |
---|
| 166 | return ssynth_note_off(&(mes->d.note), mes->vv); |
---|
| 167 | break; |
---|
| 168 | default: |
---|
| 169 | /* ignore all other events at the moment */ |
---|
| 170 | return 0; |
---|
| 171 | } |
---|
| 172 | |
---|
[2460] | 173 | return -1; |
---|
| 174 | } |
---|
| 175 | |
---|
[2448] | 176 | //ll |
---|