1 | //synth.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
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 | |
---|
25 | #include "libroardsp.h" |
---|
26 | |
---|
27 | #if !defined(ROAR_HAVE_LIBM) && !defined(M_PI) |
---|
28 | #define M_PI 3.141592 |
---|
29 | #endif |
---|
30 | |
---|
31 | #define _CHECK_BASIC() if ( state == NULL ) return -1 |
---|
32 | #define _CHECK_PCMOUT() _CHECK_BASIC(); if ( frames == 0 ) return 0; if ( out == NULL ) return -1 |
---|
33 | |
---|
34 | int roar_synth_init(struct roar_synth_state * state, struct roar_note_octave * note, int rate) { |
---|
35 | _CHECK_BASIC(); |
---|
36 | |
---|
37 | if ( rate <= 0 ) |
---|
38 | return -1; |
---|
39 | |
---|
40 | memset(state, 0, sizeof(struct roar_synth_state)); |
---|
41 | |
---|
42 | state->note = note; // NULL is valid here! |
---|
43 | state->rate = rate; |
---|
44 | state->volume = 1; |
---|
45 | |
---|
46 | state->func = ROAR_SYNTH_SYNF_DEFAULT; |
---|
47 | |
---|
48 | return 0; |
---|
49 | } |
---|
50 | |
---|
51 | int roar_synth_set_offset(struct roar_synth_state * state, size_t offset) { |
---|
52 | _CHECK_BASIC(); |
---|
53 | |
---|
54 | state->pcmoffset = offset; |
---|
55 | |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | int roar_synth_set_func (struct roar_synth_state * state, ROAR_SYNTH_FUNC_TYPE(func)) { |
---|
60 | _CHECK_BASIC(); |
---|
61 | |
---|
62 | if ( func == NULL ) |
---|
63 | return -1; |
---|
64 | |
---|
65 | state->func = func; |
---|
66 | |
---|
67 | return 0; |
---|
68 | } |
---|
69 | |
---|
70 | int roar_synth_set_volume(struct roar_synth_state * state, float volume) { |
---|
71 | _CHECK_BASIC(); |
---|
72 | |
---|
73 | state->volume = volume; |
---|
74 | |
---|
75 | return 0; |
---|
76 | } |
---|
77 | |
---|
78 | int roar_synth_pcmout_i16n(struct roar_synth_state * state, int16_t * out, size_t frames, int channels) { |
---|
79 | _CHECK_PCMOUT(); |
---|
80 | |
---|
81 | switch (channels) { |
---|
82 | case 1: return roar_synth_pcmout_i161(state, out, frames); |
---|
83 | default: |
---|
84 | return -1; |
---|
85 | } |
---|
86 | |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | int roar_synth_pcmout_i161(struct roar_synth_state * state, int16_t * out, size_t frames) { |
---|
91 | float t_step; |
---|
92 | float t_cur; |
---|
93 | float freq; |
---|
94 | #ifdef DEBUG |
---|
95 | float cv0, cv1; |
---|
96 | #endif |
---|
97 | int i; |
---|
98 | |
---|
99 | _CHECK_PCMOUT(); |
---|
100 | |
---|
101 | t_step = 1.0/state->rate; |
---|
102 | |
---|
103 | ROAR_DBG("roar_synth_pcmout_i161(*): state->pcmoffset=%lu", (unsigned long)state->pcmoffset); |
---|
104 | |
---|
105 | t_cur = (float)state->pcmoffset/(float)state->rate; |
---|
106 | |
---|
107 | freq = state->note->freq; |
---|
108 | |
---|
109 | ROAR_DBG("roar_synth_pcmout_i161(*): t_cur=%f, freq=%f", t_cur, freq); |
---|
110 | |
---|
111 | #ifdef DEBUG |
---|
112 | cv0 = 2.0*M_PI*freq*t_cur; |
---|
113 | #endif |
---|
114 | for (i = 0; i < frames; i++, t_cur += t_step) { |
---|
115 | out[i] = 32767.0*state->volume*state->func(2.0*M_PI*freq*t_cur, state); |
---|
116 | } |
---|
117 | #ifdef DEBUG |
---|
118 | cv1 = 2.0*M_PI*freq*t_cur; |
---|
119 | #endif |
---|
120 | |
---|
121 | #ifdef DEBUG |
---|
122 | ROAR_DBG("roar_synth_pcmout_i161(*): cv0=%f, cv1=%f, cv1-cv0=%f", cv0, cv1, cv1-cv0); |
---|
123 | #endif |
---|
124 | |
---|
125 | state->pcmoffset += frames; |
---|
126 | |
---|
127 | ROAR_DBG("roar_synth_pcmout_i161(*): state->pcmoffset=%lu", (unsigned long)state->pcmoffset); |
---|
128 | |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | // basic SINFs: |
---|
133 | float roar_synth_synf_rect (float t, struct roar_synth_state * state) { |
---|
134 | t /= 2*M_PI; |
---|
135 | t -= (int)t; |
---|
136 | |
---|
137 | if ( t < 0.5 ) |
---|
138 | return 1; |
---|
139 | else |
---|
140 | return -1; |
---|
141 | } |
---|
142 | |
---|
143 | float roar_synth_synf_saw (float t, struct roar_synth_state * state) { |
---|
144 | t /= 2*M_PI; |
---|
145 | t -= (int)t; |
---|
146 | |
---|
147 | return 2*t - 1; |
---|
148 | } |
---|
149 | |
---|
150 | float roar_synth_synf_tri (float t, struct roar_synth_state * state) { |
---|
151 | t /= 2*M_PI; |
---|
152 | t -= (int)t; |
---|
153 | |
---|
154 | if ( t < 0.5 ) |
---|
155 | return 4* t - 1; |
---|
156 | else |
---|
157 | return -4*(t-0.5) + 1; |
---|
158 | } |
---|
159 | |
---|
160 | float roar_synth_synf_trap (float t, struct roar_synth_state * state) { |
---|
161 | t /= 2*M_PI; |
---|
162 | t -= (int)t; |
---|
163 | |
---|
164 | if ( t < 0.125 || t > 0.875 ) { |
---|
165 | return -1; |
---|
166 | } else if ( t < 0.625 && t > 0.375 ) { |
---|
167 | return 1; |
---|
168 | } else if ( t < 0.5 ) { |
---|
169 | return 8*(t-0.375) + 1; |
---|
170 | } else { |
---|
171 | return -8*(t-0.625) + 1; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | #ifdef ROAR_HAVE_LIBM |
---|
176 | float roar_synth_synf_s2s (float t, struct roar_synth_state * state) { |
---|
177 | float sin2 = sinf(t/1.2); |
---|
178 | |
---|
179 | return sin2*sin2 * sin(t*1.2); |
---|
180 | } |
---|
181 | #endif |
---|
182 | |
---|
183 | //ll |
---|