source: roaraudio/libroardsp/synth.c @ 2475:6bace8656151

Last change on this file since 2475:6bace8656151 was 2475:6bace8656151, checked in by phi, 15 years ago

some debug infos

File size: 3.9 KB
Line 
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
30int 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
47int 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
55int 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
66int 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
74int 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
86int 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#ifdef DEBUG
91 float cv0, cv1;
92#endif
93 int i;
94
95 _CHECK_PCMOUT();
96
97 t_step = 1.0/state->rate;
98
99 ROAR_DBG("roar_synth_pcmout_i161(*): state->pcmoffset=%lu", (unsigned long)state->pcmoffset);
100
101 t_cur  = (float)state->pcmoffset/(float)state->rate;
102
103 freq   = state->note->freq;
104
105 ROAR_DBG("roar_synth_pcmout_i161(*): t_cur=%f, freq=%f", t_cur, freq);
106
107#ifdef DEBUG
108 cv0 = 2.0*M_PI*freq*t_cur;
109#endif
110 for (i = 0; i < frames; i++, t_cur += t_step) {
111  out[i] = 32767.0*state->volume*state->func(2.0*M_PI*freq*t_cur, state);
112 }
113#ifdef DEBUG
114 cv1 = 2.0*M_PI*freq*t_cur;
115#endif
116
117#ifdef DEBUG
118 ROAR_DBG("roar_synth_pcmout_i161(*): cv0=%f, cv1=%f, cv1-cv0=%f", cv0, cv1, cv1-cv0);
119#endif
120
121 state->pcmoffset += frames;
122
123 ROAR_DBG("roar_synth_pcmout_i161(*): state->pcmoffset=%lu", (unsigned long)state->pcmoffset);
124
125 return 0;
126}
127
128// basic SINFs:
129float roar_synth_synf_rect (float t, struct roar_synth_state * state) {
130 t /= 2*M_PI;
131 t -= (int)t;
132
133 if ( t < 0.5 )
134  return  1;
135 else
136  return -1;
137}
138
139float roar_synth_synf_saw  (float t, struct roar_synth_state * state) {
140 t /= 2*M_PI;
141 t -= (int)t;
142
143 return 2*t - 1;
144}
145
146float roar_synth_synf_tri  (float t, struct roar_synth_state * state) {
147 t /= 2*M_PI;
148 t -= (int)t;
149
150 if ( t < 0.5 )
151  return   4* t      - 1;
152 else
153  return  -4*(t-0.5) + 1;
154}
155
156float roar_synth_synf_trap (float t, struct roar_synth_state * state) {
157 t /= 2*M_PI;
158 t -= (int)t;
159
160 if ( t < 0.125 || t > 0.875 ) {
161  return -1;
162 } else if ( t < 0.625 && t > 0.375 ) {
163  return  1;
164 } else if ( t < 0.5 ) {
165  return  8*(t-0.375) + 1;
166 } else {
167  return -8*(t-0.625) + 1;
168 }
169}
170
171float roar_synth_synf_s2s  (float t, struct roar_synth_state * state) {
172 float sin2 = sinf(t/1.2);
173
174 return sin2*sin2 * sin(t*1.2);
175}
176
177//ll
Note: See TracBrowser for help on using the repository browser.