source: roaraudio/libroardsp/synth.c @ 2474:ab15be66e611

Last change on this file since 2474:ab15be66e611 was 2474:ab15be66e611, checked in by phi, 15 years ago

debug lions and cast

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