source: roaraudio/libroardsp/synth.c @ 2428:0425d34a7bd3

Last change on this file since 2428:0425d34a7bd3 was 2428:0425d34a7bd3, checked in by phi, 15 years ago

correct return value :)

File size: 2.3 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 int i;
81
82 _CHECK_PCMOUT();
83
84 t_step = 1.0/state->rate;
85
86 t_cur  = t_step * state->pcmoffset;
87
88 for (i = 0; i < frames; i++, t_cur += t_step) {
89  out[i] = 32767.0*state->func(t_cur, state);
90 }
91
92 state->pcmoffset += frames;
93
94 return 0;
95}
96
97
98//ll
Note: See TracBrowser for help on using the repository browser.