~ubuntu-branches/ubuntu/utopic/freebirth/utopic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Freebirth
 * Copyright (C) 1999 topher lafata <topher@topher.com>,
 *		      Jake Donham <jake@bitmechanic.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program (see COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "oscillator.h"


#define RATE 44100

static sample sin_table[RATE];
static sample sqr_table[RATE]; 
static sample saw_table[RATE];

static int tables_calculated = 0;

#define SAW_ZERO 11025
#define SIN_ZERO 0
#define SQR_ZERO 0

#define MAX_FREQ_OFFSET 32768
static int current_zero;

/*--------------private stuff-------------------------*/


void _fill_buffer(osc *this)
{
  int i, phase_factor;
  event *e;
  sample *buf = this->buffer;
  sample *tbl = this->table;

  for( i = 0; i < TBASS_BUFF_SIZE;i++)
    {
      e = this->events[i];
      if(e){
	e->fire(e,(sample_producer*)this);

	this->events[i] = NULL;
      }
      phase_factor = this->phase_offset / this->freq;
      buf[i] = tbl[this->current_index + phase_factor];
      this->current_index += this->freq + 
	(this->freq * this->freq_offset / MAX_FREQ_OFFSET);

      if(this->current_index + phase_factor >= RATE )
	{
	  this->current_index  = this->current_index + phase_factor - RATE;
	}

    }
}


void _print_buffer(osc *this)
  {
    int i;
    sample *buf = this->buffer;
    for(i = 0; i < TBASS_BUFF_SIZE;i++)
      {
	printf("%d\n",*buf++);

      }


  }




/*------------------end private stuff ----------------------*/

/*funtions to fill in the tables */

void init_tables()
{

  int h = 0; /* harmonic number for sqr wave */
  int i = 0;
  double scale = (2.0 * M_PI) / RATE;

  /* for saw table was 16000 and -16000*/
  double step = (double)( (double)(32760* 2) / (RATE / 2) ); 
  double val  = -32760;

  /* init sine table with 1hz wave*/
  for(i = 0;i < RATE; i++)
    sin_table[i] = (sample)(32767 * sin(i*scale));

  /* init square table with 1hz wave consisting of first 9 partials*/ 
  for(h = 1; h <= 9; h += 2)
    {
      for(i = 0;i < RATE; i++)
	{
	  sqr_table[i] += (int) (32767 * (((double)1 / h) * sin((2.0 * M_PI) * h * i / RATE)));
	}
    }

  
      /*sample pulse = 8000;
      if(i > RATE/2)
	pulse = -pulse;
	sqr_table[i] = pulse; */



  /* init saw table with 1hz wave    */
  for(i = 0;i < RATE/2; i++, val += step)
    {
      saw_table[i] = (sample)val;
      saw_table[RATE - i] = (sample)val;

    }

  saw_table[RATE / 2] = 32760;
  
  tables_calculated = 1;
}


/*oscillator object functions */

void osc_set_type(osc * this,int type)
{
  switch(type)
    {
    case SIN:
      this->table = sin_table;
      current_zero = SIN_ZERO;
      break;
    case SQR:
      this->table = sqr_table;
      current_zero = SQR_ZERO;
      break;
    case SAW:
      this->table = saw_table;
      current_zero = SAW_ZERO;
      break;
    default:
      this->table = sin_table;

    }
}

void osc_set_frequency(osc *this, int freq)
{
  this->freq = freq;

}

void osc_trigger(osc *this)
{
  /*do nothing...maybe reset the phase */
  /*what does it mean for an osc to be triggered */
  /*  this->current_index = this->phase_offset;   */

}

void osc_next_buffer(osc *this) {
  this->next = 1;
}

sample *osc_get_buffer(osc * this)
{
  if (this->next) {
    this->next = 0;
    _fill_buffer(this);
  }
  return this->buffer;
}

void osc_set_phase_offset(osc *this, int offset)
{
  this->phase_offset = offset;

}

void osc_set_freq_offset(osc *this, int offset)
{
  this->freq_offset = offset;

}

void osc_delete(osc * this)
{
  free(this->buffer);
  free(this->events);
  free(this);
}


/* specific oscillator events */
typedef struct event_freq_change{
  int seq_handle;
  void(* fire)(event *this, sample_producer *target);
  int freq;
} event_freq_change;


void fire_freq_change(event_freq_change *this, osc *target) {
  osc_set_frequency(target,this->freq);
}

event *event_freq_change_new(int seq_handle,int freq)
{
  event_freq_change *out = (event_freq_change *)
    malloc(sizeof (event_freq_change));
  out->seq_handle = seq_handle;
  out->fire = (void (*)(event *, sample_producer *))fire_freq_change;
  out->freq = freq;
  return (event *)out;
}





static sample_producer **get_children(osc *this)
{
  static sample_producer *no_kids[] = { NULL };
  return no_kids;
}

static char **get_header(osc *this)
{
  static char *header[] = {
    "int $n_phase_offset = ((osc *)$t)->phase_offset;",
    "double $n_freq_offset = ((osc *)$t)->freq_offset;",
    "int $n_freq = ((osc *)$t)->freq;",
    "int $n_phase_factor = $n_phase_offset / $n_freq;",
    "sample *$n_table = ((osc *)$t)->table;",
    "int $n_i = ((osc *)$t)->current_index;",
    NULL
  };
  return header;
}

static char **get_code(osc *this)
{
  static char *code[] = {
    "$o = $n_table[$n_i + $n_phase_factor];",
    "$n_i += $n_freq + ($n_freq * $n_freq_offset) / MAX_FREQ_OFFSET;",
    "if ($n_i + $n_phase_factor >= RATE)",
    "  $n_i = $n_i + $n_phase_factor - RATE;",
    NULL
  };
  return code;
}

static char **get_footer(osc *this)
{
  static char *footer[] = {
    "((osc *)$t)->current_index = $n_i;",
    NULL
  };
  return footer;
}

osc *osc_new(int freq, int type)
{
  int i;

    
  osc *out  = (osc *)malloc(sizeof(osc));
  out->get_buffer = osc_get_buffer;
  out->next_buffer = osc_next_buffer;
  out->get_children	= get_children;
  out->get_header	= get_header;
  out->get_code		= get_code;
  out->get_footer	= get_footer;
  out->schedule   = sp_schedule_event;
  out->trigger    = ( void (*)(sample_producer *))osc_trigger;
  out->events = (event **)malloc(sizeof(event *) 
				 * TBASS_BUFF_SIZE);
  for(i = 0; i < TBASS_BUFF_SIZE; i++)
    out->events[i] = NULL;


  out->freq = freq;
  out->buffer = (sample *)malloc(sizeof(int) * TBASS_BUFF_SIZE);


  if(!tables_calculated)
    init_tables();

  switch(type)
    {
    case SIN:
      out->table = sin_table;
      current_zero = SIN_ZERO;
      break;
    case SQR:
      out->table = sqr_table;
      current_zero = SQR_ZERO;
      break;
    case SAW:
      out->table = saw_table;
      current_zero = SAW_ZERO;
      break;
    }

  out->current_index = 0;
  out->phase_offset  = 0;
  out->freq_offset   = 0;
  return out;
}

/*
  Local Variables:
  mode: font-lock
  End:
*/