source: roaraudio/libroardsp/synth.c @ 2431:405e3c3d4815

Last change on this file since 2431:405e3c3d4815 was 2431:405e3c3d4815, checked in by phi, 15 years ago

added SYNF s2s

File size: 3.2 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
41 state->func = ROAR_SYNTH_SYNF_SINE;
42
43 return 0;
44}
45
46int roar_synth_set_offset(struct roar_synth_state * state, size_t offset) {
47 _CHECK_BASIC();
48
49 state->pcmoffset = offset;
50
51 return 0;
52}
53
54int roar_synth_set_func  (struct roar_synth_state * state, ROAR_SYNTH_FUNC_TYPE(func)) {
55 _CHECK_BASIC();
56
57 if ( func == NULL )
58  return -1;
59
60 state->func = func;
61
62 return 0;
63}
64
65int roar_synth_pcmout_i16n(struct roar_synth_state * state, int16_t * out, size_t frames, int channels) {
66 _CHECK_PCMOUT();
67
68 switch (channels) {
69  case 1: return roar_synth_pcmout_i161(state, out, frames);
70  default:
71    return -1;
72 }
73
74 return 0;
75}
76
77int roar_synth_pcmout_i161(struct roar_synth_state * state, int16_t * out, size_t frames) {
78 float t_step;
79 float t_cur;
80 float freq;
81 int i;
82
83 _CHECK_PCMOUT();
84
85 t_step = 1.0/state->rate;
86
87 t_cur  = t_step * state->pcmoffset;
88
89 freq   = state->note->freq;
90
91 for (i = 0; i < frames; i++, t_cur += t_step) {
92  out[i] = 32767.0*state->func(2.0*M_PI*freq*t_cur, state);
93 }
94
95 state->pcmoffset += frames;
96
97 return 0;
98}
99
100// basic SINFs:
101float roar_synth_synf_rect (float t, struct roar_synth_state * state) {
102 t /= 2*M_PI;
103 t -= (int)t;
104
105 if ( t < 0.5 )
106  return  1;
107 else
108  return -1;
109}
110
111float roar_synth_synf_saw  (float t, struct roar_synth_state * state) {
112 t /= 2*M_PI;
113 t -= (int)t;
114
115 return 2*t - 1;
116}
117
118float roar_synth_synf_tri  (float t, struct roar_synth_state * state) {
119 t /= 2*M_PI;
120 t -= (int)t;
121
122 if ( t < 0.5 )
123  return   4* t      - 1;
124 else
125  return  -4*(t-0.5) + 1;
126}
127
128float roar_synth_synf_trap (float t, struct roar_synth_state * state) {
129 t /= 2*M_PI;
130 t -= (int)t;
131
132 if ( t < 0.125 || t > 0.875 ) {
133  return -1;
134 } else if ( t < 0.625 && t > 0.375 ) {
135  return  1;
136 } else if ( t < 0.5 ) {
137  return  8*(t-0.375) + 1;
138 } else {
139  return -8*(t-0.625) + 1;
140 }
141}
142
143float roar_synth_synf_s2s  (float t, struct roar_synth_state * state) {
144 float sin2 = sinf(t/1.2);
145
146 return sin2*sin2 * sin(t*1.2);
147}
148
149//ll
Note: See TracBrowser for help on using the repository browser.