aboutsummaryrefslogtreecommitdiff
path: root/iemlib1/src/soundfile_info.c
blob: f018370a69b53879df9fd6347b6509ed754b1e62 (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iemlib1 written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2010 */

#include "m_pd.h"
#include "iemlib.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#define SFI_HEADER_SAMPLERATE 0
#define SFI_HEADER_FILENAME 1
#define SFI_HEADER_MULTICHANNEL_FILE_LENGTH 2
#define SFI_HEADER_HEADERBYTES 3
#define SFI_HEADER_CHANNELS 4
#define SFI_HEADER_BYTES_PER_SAMPLE 5
#define SFI_HEADER_ENDINESS 6
#define SFI_HEADER_FORMAT_CODE 7

#define SFI_HEADER_SIZE 8
#define SFI_HEADER_CHUNK_SIZE_ESTIMATION 10000



/* --------------------------- soundfile_info -------------------------------- */
/* -- reads only header of a wave-file and outputs the important parameters -- */

static t_class *soundfile_info_class;

typedef struct _soundfile_info
{
  t_object  x_obj;
  long      *x_begmem;
  int       x_mem_size;
  t_atom    x_at_header[SFI_HEADER_SIZE];
  t_canvas  *x_canvas;
  void      *x_list_out;
} t_soundfile_info;

static short soundfile_info_string_to_int16(char *cvec)
{
  short ss=0;
  unsigned char *uc=(unsigned char *)cvec;
  
  ss += (short)(*uc);
  ss += (short)(*(uc+1)*256);
  return(ss);
}

static unsigned long soundfile_info_string_to_uint32(char *cvec)
{
  unsigned long ul=0;
  unsigned char *uc=(unsigned char *)cvec;
  
  ul += (unsigned long)(*uc);
  ul += (unsigned long)(*(uc+1)*256);
  ul += (unsigned long)(*(uc+2)*65536);
  ul += (unsigned long)(*(uc+3)*16777216);
  return(ul);
}

static void soundfile_info_uint32_to_string(char *cvec, unsigned long ul)
{
  unsigned char *uc=(unsigned char *)cvec;
  
  *uc = (unsigned char)(0x000000ff & ul);
  *(uc+1) = (unsigned char)(0x000000ff & (ul/256));
  *(uc+2) = (unsigned char)(0x000000ff & (ul/65536));
  *(uc+3) = (unsigned char)(0x000000ff & (ul/16777216));
  return;
}

static void soundfile_info_overwrite_sr(t_soundfile_info *x, t_symbol *filename, t_floatarg new_sr)
{
  char completefilename[400];
  int i, n, n2, n4, filesize, read_chars, header_size=0, ch, bytesperframe, sr, n_frames;
  FILE *fh;
  t_atom *at;
  char *cvec;
  unsigned long ul_chunk_size, ul_sr;
  short ss_format, ss_ch, ss_bytesperframe;
  
  if(filename->s_name[0] == '/')/*make complete path + filename*/
  {
    strcpy(completefilename, filename->s_name);
  }
  else if(((filename->s_name[0] >= 'A')&&(filename->s_name[0] <= 'Z')||
           (filename->s_name[0] >= 'a')&&(filename->s_name[0] <= 'z'))&&
          (filename->s_name[1] == ':')&&(filename->s_name[2] == '/'))
  {
    strcpy(completefilename, filename->s_name);
  }
  else
  {
    strcpy(completefilename, canvas_getdir(x->x_canvas)->s_name);
    strcat(completefilename, "/");
    strcat(completefilename, filename->s_name);
  }
  
  fh = fopen(completefilename,"r+b");
  if(!fh)
  {
    post("soundfile_info_read: cannot open %s !!\n", completefilename);
  }
  else
  {
    n = x->x_mem_size; // 10000 bytes
    n2 = sizeof(short) * x->x_mem_size;
    n4 = sizeof(long) * x->x_mem_size;
    fseek(fh, 0, SEEK_END);
    filesize = ftell(fh);
    fseek(fh,0,SEEK_SET);
    read_chars = (int)fread(x->x_begmem, sizeof(char), n4, fh) / 2;
    //    post("read chars = %d", read_chars);
    cvec = (char *)x->x_begmem;
    if(read_chars > 4)
    {
      if(strncmp(cvec, "RIFF", 4))
      {
        post("soundfile_info_read-error:  %s is no RIFF-WAVE-file", completefilename);
        goto sr_soundfile_info_end;
      }
      header_size += 8; // jump over RIFF chunk size
      cvec += 8;
      if(strncmp(cvec, "WAVE", 4))
      {
        post("soundfile_info_read-error:  %s is no RIFF-WAVE-file", completefilename);
        goto sr_soundfile_info_end;
      }
      header_size += 4;
      cvec += 4;
      
      for(i=header_size/2; i<read_chars; i++)
      {
        if(!strncmp(cvec, "fmt ", 4))
        {
          header_size += 4;
          cvec += 4;
          goto sr_soundfile_info_fmt;
        }
        header_size += 2;
        cvec += 2;
      }
      post("soundfile_info_read-error:  %s has at begin no format-chunk", completefilename);
      goto sr_soundfile_info_end;
      
    sr_soundfile_info_fmt:
      ul_chunk_size = soundfile_info_string_to_uint32(cvec);
      if(ul_chunk_size < 16)
      {
        post("soundfile_info_read-error:  %s has a format-chunk less than 16", completefilename);
        goto sr_soundfile_info_end;
      }
      header_size += 4;
      cvec += 4;
      
      ss_format = soundfile_info_string_to_int16(cvec);
      if((ss_format != 1) && (ss_format != 3) && (ss_format != 6) && (ss_format != 7) && (ss_format != -2)) /* PCM = 1 ; IEEE-FLOAT = 3 ; ALAW = 6 ; MULAW = 7 ; WAVE_EX = -2 */
      {
        post("soundfile_info_read-error:  %s has unknown format code", completefilename);
        goto sr_soundfile_info_end;
      }
      header_size += 2;
      cvec += 2;
      
      ss_ch = soundfile_info_string_to_int16(cvec); /* channels */
      if((ss_ch < 1) || (ss_ch > 32000))
      {
        post("soundfile_info_read-error:  %s has no common channel-number", completefilename);
        goto sr_soundfile_info_end;
      }
      SETFLOAT(x->x_at_header+SFI_HEADER_CHANNELS, (t_float)ss_ch);
      ch = (int)ss_ch;
      header_size += 2;
      cvec += 2;
      
      ul_sr = soundfile_info_string_to_uint32(cvec); /* samplerate */
      if((ul_sr > 2000000000) || (ul_sr < 1))
      {
        post("soundfile_info_read-error:  %s has no common samplerate", completefilename);
        goto sr_soundfile_info_end;
      }
      ul_sr = (unsigned long)new_sr;
      soundfile_info_uint32_to_string(cvec, ul_sr);
      
      fseek(fh,0,SEEK_SET);
      read_chars = (int)fwrite (x->x_begmem, sizeof(char), n4, fh);
      fclose(fh);
      post("written");
      
      sr = (int)ul_sr;
      header_size += 4;
      cvec += 4;
      
      header_size += 4; /* jump over bytes_per_sec */
      cvec += 4;
      
      ss_bytesperframe = soundfile_info_string_to_int16(cvec); /* bytes_per_frame */
      if((ss_bytesperframe < 1) || (ss_bytesperframe > 32000))
      {
        post("soundfile_info_read-error:  %s has no common number of bytes per frame", completefilename);
        goto sr_soundfile_info_end;
      }
      
      bytesperframe = (int)ss_bytesperframe;
      header_size += 2;
      cvec += 2;
      
      header_size += 2; /* jump over bits_per_sample */
      cvec += 2;
      
      for(i=header_size/2; i<read_chars; i++) // looking for data chunk
      {
        if(!strncmp(cvec, "data", 4))
          goto sr_soundfile_info_data;
        header_size += 2;
        cvec += 2;
      }
      post("soundfile_info_read-error:  %s has at begin no data-chunk", completefilename);
      goto sr_soundfile_info_end;
      
    sr_soundfile_info_data:
      header_size += 8; // ignore data chunk size
      cvec += 8;
      
      
      
      /*      post("ch = %d", ch);
       post("sr = %d", sr);
       post("bpf = %d", bytesperframe/ch);
       post("head = %d", header_size);
       post("len = %d", n_frames);*/
      
      
    sr_soundfile_info_end:
      
      ;
    }
  }
}

static void soundfile_info_read(t_soundfile_info *x, t_symbol *filename)
{
  char completefilename[400];
  int i, n, n2, n4, filesize, read_chars, header_size=0, ch, bytesperframe, sr, n_frames;
  FILE *fh;
  t_atom *at;
  char *cvec;
  unsigned long ul_chunk_size, ul_sr;
  short ss_format, ss_ch, ss_bytesperframe;
  
  if(filename->s_name[0] == '/')/*make complete path + filename*/
  {
    strcpy(completefilename, filename->s_name);
  }
  else if(((filename->s_name[0] >= 'A')&&(filename->s_name[0] <= 'Z')||
    (filename->s_name[0] >= 'a')&&(filename->s_name[0] <= 'z'))&&
    (filename->s_name[1] == ':')&&(filename->s_name[2] == '/'))
  {
    strcpy(completefilename, filename->s_name);
  }
  else
  {
    strcpy(completefilename, canvas_getdir(x->x_canvas)->s_name);
    strcat(completefilename, "/");
    strcat(completefilename, filename->s_name);
  }
  
  fh = fopen(completefilename,"rb");
  if(!fh)
  {
    post("soundfile_info_read: cannot open %s !!\n", completefilename);
  }
  else
  {
    n = x->x_mem_size; // 10000 bytes
    n2 = sizeof(short) * x->x_mem_size;
    n4 = sizeof(long) * x->x_mem_size;
    fseek(fh, 0, SEEK_END);
    filesize = ftell(fh);
    fseek(fh,0,SEEK_SET);
    read_chars = (int)fread(x->x_begmem, sizeof(char), n4, fh) / 2;
    fclose(fh);
    //    post("read chars = %d", read_chars);
    cvec = (char *)x->x_begmem;
    if(read_chars > 4)
    {
      if(strncmp(cvec, "RIFF", 4))
      {
        post("soundfile_info_read-error:  %s is no RIFF-WAVE-file", completefilename);
        goto soundfile_info_end;
      }
      header_size += 8; // jump over RIFF chunk size
      cvec += 8;
      if(strncmp(cvec, "WAVE", 4))
      {
        post("soundfile_info_read-error:  %s is no RIFF-WAVE-file", completefilename);
        goto soundfile_info_end;
      }
      header_size += 4;
      cvec += 4;
      
      for(i=header_size/2; i<read_chars; i++)
      {
        if(!strncmp(cvec, "fmt ", 4))
        {
          header_size += 4;
          cvec += 4;
          goto soundfile_info_fmt;
        }
        header_size += 2;
        cvec += 2;
      }
      post("soundfile_info_read-error:  %s has at begin no format-chunk", completefilename);
      goto soundfile_info_end;
      
soundfile_info_fmt:
      ul_chunk_size = soundfile_info_string_to_uint32(cvec);
      if(ul_chunk_size < 16)
      {
        post("soundfile_info_read-error:  %s has a format-chunk less than 16", completefilename);
        goto soundfile_info_end;
      }
      header_size += 4;
      cvec += 4;

      ss_format = soundfile_info_string_to_int16(cvec);
      if((ss_format != 1) && (ss_format != 3) && (ss_format != 6) && (ss_format != 7) && (ss_format != -2)) /* PCM = 1 ; IEEE-FLOAT = 3 ; ALAW = 6 ; MULAW = 7 ; WAVE_EX = -2 */
      {
        post("soundfile_info_read-error:  %s has unknown format code", completefilename);
        goto soundfile_info_end;
      }
      SETFLOAT(x->x_at_header+SFI_HEADER_FORMAT_CODE, (t_float)ss_format);
      header_size += 2;
      cvec += 2;

      ss_ch = soundfile_info_string_to_int16(cvec); /* channels */
      if((ss_ch < 1) || (ss_ch > 32000))
      {
        post("soundfile_info_read-error:  %s has no common channel-number", completefilename);
        goto soundfile_info_end;
      }
      SETFLOAT(x->x_at_header+SFI_HEADER_CHANNELS, (t_float)ss_ch);
      ch = (int)ss_ch;
      header_size += 2;
      cvec += 2;

      ul_sr = soundfile_info_string_to_uint32(cvec); /* samplerate */
      if((ul_sr > 2000000000) || (ul_sr < 1))
      {
        post("soundfile_info_read-error:  %s has no common samplerate", completefilename);
        goto soundfile_info_end;
      }
      SETFLOAT(x->x_at_header+SFI_HEADER_SAMPLERATE, (t_float)ul_sr);
      sr = (int)ul_sr;
      header_size += 4;
      cvec += 4;
      
      header_size += 4; /* jump over bytes_per_sec */
      cvec += 4;

      ss_bytesperframe = soundfile_info_string_to_int16(cvec); /* bytes_per_frame */
      if((ss_bytesperframe < 1) || (ss_bytesperframe > 32000))
      {
        post("soundfile_info_read-error:  %s has no common number of bytes per frame", completefilename);
        goto soundfile_info_end;
      }
      SETFLOAT(x->x_at_header+SFI_HEADER_BYTES_PER_SAMPLE, (t_float)(ss_bytesperframe / ss_ch));
      bytesperframe = (int)ss_bytesperframe;
      header_size += 2;
      cvec += 2;
      
      header_size += 2; /* jump over bits_per_sample */
      cvec += 2;
      
      for(i=header_size/2; i<read_chars; i++) // looking for data chunk
      {
        if(!strncmp(cvec, "data", 4))
          goto soundfile_info_data;
        header_size += 2;
        cvec += 2;
      }
      post("soundfile_info_read-error:  %s has at begin no data-chunk", completefilename);
      goto soundfile_info_end;
      
soundfile_info_data:
      header_size += 8; // ignore data chunk size
      cvec += 8;
      
      SETFLOAT(x->x_at_header+SFI_HEADER_HEADERBYTES, (t_float)header_size);
      
      n_frames = (filesize - header_size) / bytesperframe;
      SETFLOAT(x->x_at_header+SFI_HEADER_MULTICHANNEL_FILE_LENGTH, (t_float)n_frames);
      SETSYMBOL(x->x_at_header+SFI_HEADER_ENDINESS, gensym("l"));
      SETSYMBOL(x->x_at_header+SFI_HEADER_FILENAME, gensym(completefilename));
      
      /*      post("ch = %d", ch);
      post("sr = %d", sr);
      post("bpf = %d", bytesperframe/ch);
      post("head = %d", header_size);
      post("len = %d", n_frames);*/
      
      outlet_list(x->x_list_out, &s_list, SFI_HEADER_SIZE, x->x_at_header);
      
      
soundfile_info_end:
      
      ;
    }
  }
}

static void soundfile_info_free(t_soundfile_info *x)
{
  freebytes(x->x_begmem, x->x_mem_size * sizeof(long));
}

static void *soundfile_info_new(void)
{
  t_soundfile_info *x = (t_soundfile_info *)pd_new(soundfile_info_class);
  
  x->x_mem_size = SFI_HEADER_CHUNK_SIZE_ESTIMATION; /* try to read the first 10000 bytes of the soundfile */
  x->x_begmem = (long *)getbytes(x->x_mem_size * sizeof(long));
  x->x_list_out = outlet_new(&x->x_obj, &s_list);
  x->x_canvas = canvas_getcurrent();
  return (x);
}

/* ---------------- global setup function -------------------- */

void soundfile_info_setup(void)
{
  soundfile_info_class = class_new(gensym("soundfile_info"), (t_newmethod)soundfile_info_new,
    (t_method)soundfile_info_free, sizeof(t_soundfile_info), 0, 0);
  class_addmethod(soundfile_info_class, (t_method)soundfile_info_read, gensym("read"), A_SYMBOL, 0);
  class_addmethod(soundfile_info_class, (t_method)soundfile_info_overwrite_sr, gensym("overwrite_sr"), A_SYMBOL, A_FLOAT, 0);
}