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 | #define _CHECK_BASIC() if ( state == NULL ) return -1 |
---|
28 | #define _CHECK_PCMOUT() _CHECK_BASIC(); if ( frames == 0 ) return 0; if ( out == NULL ) return -1 |
---|
29 | |
---|
30 | int roar_synth_init(struct roar_synth_state * state, struct roar_note_octave * note, int rate) { |
---|
31 | _CHECK_BASIC(); |
---|
32 | |
---|
33 | if ( rate <= 0 ) |
---|
34 | return -1; |
---|
35 | |
---|
36 | memset(state, 0, sizeof(struct roar_synth_state)); |
---|
37 | |
---|
38 | state->note = note; // NULL is valid here! |
---|
39 | state->rate = rate; |
---|
40 | state->volume = 1; |
---|
41 | |
---|
42 | state->func = ROAR_SYNTH_SYNF_SIN; |
---|
43 | |
---|
44 | return 0; |
---|
45 | } |
---|
46 | |
---|
47 | int roar_synth_set_offset(struct roar_synth_state * state, size_t offset) { |
---|
48 | _CHECK_BASIC(); |
---|
49 | |
---|
50 | state->pcmoffset = offset; |
---|
51 | |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | int roar_synth_set_func (struct roar_synth_state * state, ROAR_SYNTH_FUNC_TYPE(func)) { |
---|
56 | _CHECK_BASIC(); |
---|
57 | |
---|
58 | if ( func == NULL ) |
---|
59 | return -1; |
---|
60 | |
---|
61 | state->func = func; |
---|
62 | |
---|
63 | return 0; |
---|
64 | } |
---|
65 | |
---|
66 | int roar_synth_set_volume(struct roar_synth_state * state, float volume) { |
---|
67 | _CHECK_BASIC(); |
---|
68 | |
---|
69 | state->volume = volume; |
---|
70 | |
---|
71 | return 0; |
---|
72 | } |
---|
73 | |
---|
74 | int roar_synth_pcmout_i16n(struct roar_synth_state * state, int16_t * out, size_t frames, int channels) { |
---|
75 | _CHECK_PCMOUT(); |
---|
76 | |
---|
77 | switch (channels) { |
---|
78 | case 1: return roar_synth_pcmout_i161(state, out, frames); |
---|
79 | default: |
---|
80 | return -1; |
---|
81 | } |
---|
82 | |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | int roar_synth_pcmout_i161(struct roar_synth_state * state, int16_t * out, size_t frames) { |
---|
87 | float t_step; |
---|
88 | float t_cur; |
---|
89 | float freq; |
---|
90 | int i; |
---|
91 | |
---|
92 | _CHECK_PCMOUT(); |
---|
93 | |
---|
94 | t_step = 1.0/state->rate; |
---|
95 | |
---|
96 | t_cur = t_step * state->pcmoffset; |
---|
97 | |
---|
98 | freq = state->note->freq; |
---|
99 | |
---|
100 | for (i = 0; i < frames; i++, t_cur += t_step) { |
---|
101 | out[i] = 32767.0*state->volume*state->func(2.0*M_PI*freq*t_cur, state); |
---|
102 | } |
---|
103 | |
---|
104 | state->pcmoffset += frames; |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | // basic SINFs: |
---|
110 | float roar_synth_synf_rect (float t, struct roar_synth_state * state) { |
---|
111 | t /= 2*M_PI; |
---|
112 | t -= (int)t; |
---|
113 | |
---|
114 | if ( t < 0.5 ) |
---|
115 | return 1; |
---|
116 | else |
---|
117 | return -1; |
---|
118 | } |
---|
119 | |
---|
120 | float roar_synth_synf_saw (float t, struct roar_synth_state * state) { |
---|
121 | t /= 2*M_PI; |
---|
122 | t -= (int)t; |
---|
123 | |
---|
124 | return 2*t - 1; |
---|
125 | } |
---|
126 | |
---|
127 | float roar_synth_synf_tri (float t, struct roar_synth_state * state) { |
---|
128 | t /= 2*M_PI; |
---|
129 | t -= (int)t; |
---|
130 | |
---|
131 | if ( t < 0.5 ) |
---|
132 | return 4* t - 1; |
---|
133 | else |
---|
134 | return -4*(t-0.5) + 1; |
---|
135 | } |
---|
136 | |
---|
137 | float roar_synth_synf_trap (float t, struct roar_synth_state * state) { |
---|
138 | t /= 2*M_PI; |
---|
139 | t -= (int)t; |
---|
140 | |
---|
141 | if ( t < 0.125 || t > 0.875 ) { |
---|
142 | return -1; |
---|
143 | } else if ( t < 0.625 && t > 0.375 ) { |
---|
144 | return 1; |
---|
145 | } else if ( t < 0.5 ) { |
---|
146 | return 8*(t-0.375) + 1; |
---|
147 | } else { |
---|
148 | return -8*(t-0.625) + 1; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | float roar_synth_synf_s2s (float t, struct roar_synth_state * state) { |
---|
153 | float sin2 = sinf(t/1.2); |
---|
154 | |
---|
155 | return sin2*sin2 * sin(t*1.2); |
---|
156 | } |
---|
157 | |
---|
158 | //ll |
---|