source: roaraudio/libroardsp/synth.c @ 3003:9d0935291fca

Last change on this file since 3003:9d0935291fca was 2483:758d9d062f97, checked in by phi, 15 years ago

added ROAR_SYNTH_SYNF_DEFAULT

File size: 4.0 KB
RevLine 
[2425]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
[2482]27#if !defined(ROAR_HAVE_LIBM) && !defined(M_PI)
28#define M_PI 3.141592
29#endif
30
[2426]31#define _CHECK_BASIC() if ( state == NULL ) return -1
[2427]32#define _CHECK_PCMOUT() _CHECK_BASIC(); if ( frames == 0 ) return 0; if ( out == NULL ) return -1
[2426]33
34int 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
[2432]42 state->note   = note; // NULL is valid here!
43 state->rate   = rate;
44 state->volume = 1;
[2426]45
[2483]46 state->func = ROAR_SYNTH_SYNF_DEFAULT;
[2426]47
48 return 0;
49}
50
51int 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
59int roar_synth_set_func  (struct roar_synth_state * state, ROAR_SYNTH_FUNC_TYPE(func)) {
60 _CHECK_BASIC();
61
[2427]62 if ( func == NULL )
63  return -1;
64
[2426]65 state->func = func;
66
67 return 0;
68}
69
[2432]70int 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
[2426]78int roar_synth_pcmout_i16n(struct roar_synth_state * state, int16_t * out, size_t frames, int channels) {
[2427]79 _CHECK_PCMOUT();
[2426]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
90int roar_synth_pcmout_i161(struct roar_synth_state * state, int16_t * out, size_t frames) {
[2427]91 float t_step;
92 float t_cur;
[2429]93 float freq;
[2475]94#ifdef DEBUG
95 float cv0, cv1;
96#endif
[2427]97 int i;
98
99 _CHECK_PCMOUT();
100
101 t_step = 1.0/state->rate;
102
[2474]103 ROAR_DBG("roar_synth_pcmout_i161(*): state->pcmoffset=%lu", (unsigned long)state->pcmoffset);
104
[2475]105 t_cur  = (float)state->pcmoffset/(float)state->rate;
[2427]106
[2429]107 freq   = state->note->freq;
108
[2475]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
[2427]114 for (i = 0; i < frames; i++, t_cur += t_step) {
[2432]115  out[i] = 32767.0*state->volume*state->func(2.0*M_PI*freq*t_cur, state);
[2427]116 }
[2475]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
[2427]124
125 state->pcmoffset += frames;
[2426]126
[2474]127 ROAR_DBG("roar_synth_pcmout_i161(*): state->pcmoffset=%lu", (unsigned long)state->pcmoffset);
128
[2428]129 return 0;
[2426]130}
131
[2430]132// basic SINFs:
133float 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
143float 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
150float 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
160float 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}
[2425]174
[2482]175#ifdef ROAR_HAVE_LIBM
[2431]176float 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}
[2482]181#endif
[2431]182
[2425]183//ll
Note: See TracBrowser for help on using the repository browser.