File: raw_wave.c

package info (click to toggle)
freebirth 0.3.2-9.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 3,632 kB
  • ctags: 640
  • sloc: ansic: 4,724; makefile: 89; sh: 1
file content (287 lines) | stat: -rw-r--r-- 6,903 bytes parent folder | download | duplicates (2)
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
/* 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 "raw_wave.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <endian.h>

/* borrowed from glib2 */
#define SHORT_SWAP_LE_BE(val)  ((short) ( \
    (short) ((short) (val) >> 8) |      \
    (short) ((short) (val) << 8)))
static void swap_endian(short *data, int length)
{
       int i;
       for (i = 0; i < length; i += 1, data++)
               *data = SHORT_SWAP_LE_BE(*data);
}

static char *get_full_path(char *fn)
{
  char *full_fn;
  char *fb_samples;

  fb_samples = getenv("FB_SAMPLES");
  if (fb_samples == NULL) fb_samples = FB_SAMPLES;
  full_fn = (char *)malloc(strlen(fb_samples) + 1 + strlen(fn) + 1);
   
  if (fn == NULL || fn[0] == '/') { /* path is valid already */
     sprintf(full_fn, "%s", fn); 
  } else {
     sprintf(full_fn, "%s/%s", fb_samples, fn); /* add path */
  }
   
  return full_fn;
}

void _fill_table(raw_wave *this)
{
  /*There is now a memory leak here i think because I am
    not freeing the old table before replacing it with the
    new one */ /* FIXED !! i believe */
  FILE *in;
  int i;
  short *tmp;
  int block_size = 8192;
  int current_block = 0;
  int count         = 0;
  
  in = fopen(this->filename,"r");
  /* PATCH BY: adam sjogren asjo@diku.dk */
  if( in==NULL )
    {
      fprintf(stderr, "Couldn't open sample: %s\n", this->filename);
      this->length=0;
      return;
    }
  if( this->table != NULL)
    {
      free(this->table);
    }
  this->length = 0; /*in the event that we are repopulating the table */
  this->current_index = 0;

  tmp = (short *)malloc(sizeof(short) * block_size);
  while((count = fread(tmp + (block_size * current_block),
		       sizeof(short),
		       block_size,
		       in))
	== block_size) {
    this->length += count;
    tmp  = realloc(tmp, sizeof(short) * 
		   block_size * (current_block + 2));
    current_block++;

  }
  this->length += count;
#if __BYTE_ORDER == __BIG_ENDIAN
  swap_endian(tmp, this->length);
#endif
  this->table = (sample *)malloc(sizeof(sample) * this->length);
  for(i = 0; i < this->length;i++) {
    this->table[i] = (sample)tmp[i];
  }
  fclose(in);
  free(tmp);
}


void raw_wave_next_buffer(raw_wave *this) {
  this->next = 1;
}

sample *raw_wave_get_buffer(raw_wave *this)
{
    int i;
    event *e;
 
    if (this->next) {
      this->next = 0;
      for(i = 0;i < TBASS_BUFF_SIZE; i++)
	{
	  e = this->events[i];
	  if(e){
	    e->fire(e,(sample_producer*) this);
	    this->events[i] = NULL;
	  }

	  if(this->current_index >= this->length)
	    this->buffer[i] = 0;
	  else
	    {
	      /*this is a collapse of the whole interpolate
		and decimate pitch changing scheme */
	      sample s1,s2;
	      double    ci_w;
	      double    ci_f;
	      ci_f = modf(this->current_index, &ci_w);

	      s1 = this->table[(int)ci_w];
	      s2 = this->table[(int)ci_w + 1];
	      this->buffer[i] = (s2 - s1)*ci_f + s1;
	      this->current_index += this->pitch;

	
	    }

	
	}
    }

  return this->buffer;  
}
  
 void raw_wave_trigger(raw_wave *this)
{

  this->current_index = 0;

}

void raw_wave_set_pitch(raw_wave *this, double p)
{
  this->pitch = p;

}

void raw_wave_set_sample_file(raw_wave* this, char *filename)
{
  FILE *in;
  if (this->filename != NULL) free(this->filename);
  this->filename = get_full_path(filename);
  in = fopen(this->filename,"r");
  if( in==NULL )
    {
      fprintf(stderr, "Couldn't open sample: %s\n", filename);
      return;
    } 
  _fill_table(this); 
}

typedef struct event_pitch_change{
  int seq_handle;
  void(* fire)(event *this, sample_producer *target);
  double pitch;
} event_pitch_change;

void fire_pitch_change(event_pitch_change *this, raw_wave *target)
{
  raw_wave_set_pitch(target,this->pitch);
}

event *event_pitch_change_new(int seq_handle, double pitch)
{
  event_pitch_change  *out = (event_pitch_change *)
    malloc(sizeof(event_pitch_change));
  out->seq_handle = seq_handle;
  out->fire = (void (*)(event *, sample_producer *))fire_pitch_change;
  out->pitch = pitch;
  return (event *)out;
}

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

static char **get_header(raw_wave *this)
{
  static char *header[] = {
    "sample *$n_table = ((raw_wave *)$t)->table;",
    "int $n_length = ((raw_wave *)$t)->length;",
    "sample $n_s1, $n_s2;",
    "double $n_ci_w, $n_ci_f, $n_pitch = ((raw_wave *)$t)->pitch;",
    NULL
  };
  return header;
}

static char **get_code(raw_wave *this)
{
  static char *code[] = {
    "if (((raw_wave *)$t)->current_index >= $n_length)",
    "  $o = 0;",
    "else {",
    "  $n_ci_f = modf(((raw_wave *)$t)->current_index, &$n_ci_w);",
    "  $n_s1 = $n_table[(int)$n_ci_w];",
    "  $n_s2 = $n_table[(int)$n_ci_w +1];",
    "  ((raw_wave *)$t)->current_index += $n_pitch;",
    "  $o = ($n_s2 - $n_s1) * $n_ci_f + $n_s1;",
    "}",
    NULL
  };
  return code;
}

static char **get_footer(raw_wave *this)
{
  static char *footer[] = {
    NULL
  };
  return footer;
}

raw_wave * raw_wave_new(char *filename)
{
  int i;
  raw_wave *out = (raw_wave *)malloc(sizeof(raw_wave));

  /* begin sample_producer interface */
  out->get_buffer = raw_wave_get_buffer;
  out->next_buffer = raw_wave_next_buffer;
  out->get_children	= get_children;
  out->get_header	= get_header;
  out->get_code		= get_code;
  out->get_footer	= get_footer;
  out->next = 0;
  out->schedule   = sp_schedule_event;
  out->trigger    = (void (*)(sample_producer *))raw_wave_trigger;
  out->events = (event **)malloc(sizeof(event *) 
				 * TBASS_BUFF_SIZE);
  for(i = 0; i < TBASS_BUFF_SIZE; i++)
    out->events[i] = NULL; 
  /* end sample_producer interface */
  /* set table to zero here check if it is zero in */
  /* _fill_table */
  out->table = 0;
  out->filename = NULL;
  raw_wave_set_sample_file(out, filename);

  out->buffer   = (sample *)malloc(sizeof(sample) * TBASS_BUFF_SIZE);
  out->current_index = out->length;
  out->pitch = 1;

  return out;
}



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