File: SphereFile.c

package info (click to toggle)
snack 2.2.10.20090624%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,868 kB
  • sloc: ansic: 32,602; sh: 8,558; tcl: 1,086; python: 761; makefile: 578
file content (354 lines) | stat: -rwxr-xr-x 8,301 bytes parent folder | download | duplicates (12)
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
/* 
 * Copyright (C) 1999-2001 Claude Barras and Kare Sjolander
 */

#include <tcl.h>
#include "snack.h"
#include <sp/sphere.h>

#if defined(__WIN32__)
#  define WIN32_LEAN_AND_MEAN
#  include <windows.h>
#  undef WIN32_LEAN_AND_MEAN
#  define EXPORT(a,b) __declspec(dllexport) a b
BOOL APIENTRY
DllMain(HINSTANCE hInst, DWORD reason, LPVOID reserved)
{
  return TRUE;
}
#else
#  define EXPORT(a,b) a b
#endif

#define fail(a) { \
   Tcl_AppendResult(interp, a, NULL); \
   if (ch) sp_close((SP_FILE *)ch); \
   return TCL_ERROR; \
}

#define NIST_STRING "NIST"
#define SPHERE_STRING "SPHERE"
#define SPHERE_HEADERSIZE 1024
#define SNACK_SPHERE_INT 17

static int
GetSphereHeader(Sound *s, Tcl_Interp *interp, Tcl_Channel ch, Tcl_Obj *obj,
	      char *buf);

static char * 
GuessSphereFile(char *buf, int len)
{
  if (len < (int) strlen(NIST_STRING)) return(QUE_STRING);
  if (strncasecmp(NIST_STRING, buf, strlen(NIST_STRING)) == 0) {
    return(SPHERE_STRING);
  }
  return(NULL);
}

char *
ExtSphereFile(char *s)
{
  int l1 = strlen(".sph");
  int l2 = strlen(s);

  if (strncasecmp(".sph", &s[l2 - l1], l1) == 0) {
    return(SPHERE_STRING);
  }
  return(NULL);
}

#define SPHERE_BUFFER_SIZE 100000

static int
OpenSphereFile(Sound *s, Tcl_Interp *interp, Tcl_Channel *ch, char *mode)
{
   /* open sphere file  */
  *ch = (Tcl_Channel) sp_open(Snack_GetSoundFilename(s), mode);
  if (*ch == NULL) {
    Tcl_AppendResult(interp, "SPHERE: unable to open file: ",
		     Snack_GetSoundFilename(s), NULL);
    return TCL_ERROR;
  }
  /*
    Hack for the pculaw format. Read header again because
    sp_set_data_mode() has to be called.
    */
  GetSphereHeader(s, interp, *ch, NULL, NULL);

  if (s->extHead != NULL && s->extHeadType != SNACK_SPHERE_INT) {
    Snack_FileFormat *ff;
    
    for (ff = Snack_GetFileFormats(); ff != NULL; ff = ff->nextPtr) {
      if (strcmp(s->fileType, ff->name) == 0) {
	if (ff->freeHeaderProc != NULL) {
	  (ff->freeHeaderProc)(s);
	}
      }
    }
  }
  
  if (s->extHead == NULL) {
    s->extHead = ckalloc(sizeof(short) * SPHERE_BUFFER_SIZE);
    s->extHeadType = SNACK_SPHERE_INT;
  }

  return TCL_OK;
}

static int
CloseSphereFile(Sound *s, Tcl_Interp *interp, Tcl_Channel *ch)
{
  /* close */
  if (sp_close((SP_FILE *)*ch))
    fail("SPHERE: error closing file");
  *ch = NULL;

  return TCL_OK;
}

static int
ReadSphereSamples(Sound *s, Tcl_Interp *interp, Tcl_Channel ch, char *ibuf,
		  float *obuf, int len)
{
  int tot = len / Snack_GetNumChannels(s);
  int i = 0, le = Snack_PlatformIsLittleEndian();
  unsigned char *q = (unsigned char *) s->extHead;
  char *sc  = (char *)  s->extHead;
  short *r  = (short *) s->extHead;
  int   *is = (int *)   s->extHead;
  float *fs = (float *) s->extHead;
  float *f  = obuf;
  int size = min(tot, SPHERE_BUFFER_SIZE / Snack_GetNumChannels(s));
  int read = sp_read_data(s->extHead, size, (SP_FILE *)ch);

  if (!(sp_error((SP_FILE *)ch) == 0 || sp_error((SP_FILE *)ch) == 101)) {
    return -1;
  }
  
  for (i = 0; i < read * Snack_GetNumChannels(s); i++) {
    switch (s->encoding) {
    case LIN16:
      if (s->swap) *r = Snack_SwapShort(*r);
      *f++ = (float) *r++;
      break;
    case LIN32:
      if (s->swap) *is = Snack_SwapLong(*is);
      *f++ = (float) *is++;
      break;
    case SNACK_FLOAT:
      if (s->swap) *fs = (float) Snack_SwapLong((int)*fs);
      *f++  = (float) *fs++;
      break;
    case ALAW:
      *f++ = (float) Snack_Alaw2Lin(*q++);
      break;
    case MULAW:
      *f++ = (float) Snack_Mulaw2Lin(*q++);
      break;
    case LIN8:
      *f++ = (float) *sc++;
      break;
    case LIN8OFFSET:
      *f++ = (float) *q++;
      break;
    case LIN24:
      {
	int ee;
	if (s->swap) {
	  if (le) {
	    ee = 0;
	  } else {
	    ee = 1;
	  }
	} else {
	  if (le) {
	    ee = 1;
	  } else {
	    ee = 0;
	  }		
	}
	if (ee) {
	  int t = *q++;
	  t |= *q++ << 8;
	  t |= *q++ << 16;
	  if (t & 0x00800000) {
	    t |= (unsigned int) 0xff000000;
	  }
	  *f++ = (float) t;
	} else {
	  int t = *q++ << 16;
	  t |= *q++ << 8;
	  t |= *q++;
	  if (t & 0x00800000) {
	    t |= (unsigned int) 0xff000000;
	  }
	  *f++ = (float) t;
	}
	break;
      }
    }
  }

  return(i);
}

static int
SeekSphereFile(Sound *s, Tcl_Interp *interp, Tcl_Channel ch, int pos)
{
  /* seek to pos */

  if (sp_seek((SP_FILE *)ch, pos, 0)) {
    return(-1);
  } else {
    return(pos);
  }
}

static int
GetSphereHeader(Sound *s, Tcl_Interp *interp, Tcl_Channel ch, Tcl_Obj *obj,
	      char *buf)
{
   /* SPHERE file and header fields */
   long sample_rate = 16000;
   long channel_count = 1;
   long sample_n_bytes = 2;
   long sample_cnt = 0;
   char *sample_coding = "";

   if (obj != NULL)
      fail("'data' subcommand forbidden for NIST/SPHERE format");

   if (Snack_GetDebugFlag(s) > 2) {
     Snack_WriteLog("    Reading NIST/SPHERE header\n");
   }

   /* sample_rate */
   if (sp_h_get_field((SP_FILE *)ch, "sample_rate", T_INTEGER,
		       (void *)&sample_rate) > 0)
      fail("SPHERE: unable to read sample_rate");

   Snack_SetSampleRate(s, sample_rate);
   if (Snack_GetDebugFlag(s) > 3) {
     Snack_WriteLogInt("      Setting rate", Snack_GetSampleRate(s));
   }

   /* sample_n_bytes */
   if (sp_h_get_field((SP_FILE *)ch, "sample_n_bytes", T_INTEGER,
		       (void *)&sample_n_bytes) > 0)
      fail("SPHERE: unable to read sample_n_bytes");
   Snack_SetBytesPerSample(s, sample_n_bytes);
   if (Snack_GetDebugFlag(s) > 3) {
     Snack_WriteLogInt("      Setting sampsize", Snack_GetBytesPerSample(s));
   }
   
   /* channel_count */
   if (sp_h_get_field((SP_FILE *)ch, "channel_count", T_INTEGER,
		       (void *)&channel_count) > 0)
      fail("SPHERE: unable to read channel_count");
   Snack_SetNumChannels(s, channel_count);
   if (Snack_GetDebugFlag(s) > 3) {
     Snack_WriteLogInt("      Setting channels", Snack_GetNumChannels(s));
   }

   /* sample_count */
   if (sp_h_get_field((SP_FILE *)ch, "sample_count", T_INTEGER,
		       (void *)&sample_cnt) > 0) {
      sample_cnt = 0;
   }
   if (Snack_GetDebugFlag(s) > 3) {
     Snack_WriteLogInt("      Setting length", sample_cnt);
   }

   /* sample_coding */
   if (sp_h_get_field((SP_FILE *)ch, "sample_coding", T_STRING,
                       (void *)&sample_coding) > 0) {
      sample_coding = "";
   }
   if (strncmp(sample_coding, "pculaw", 6) == 0) {
      sp_set_data_mode((SP_FILE *)ch, "SE-PCM-2");
      Snack_SetSampleEncoding(s, LIN16);
      Snack_SetBytesPerSample(s, 2);
   } else if (strncmp(sample_coding, "alaw", 4) == 0) {
      Snack_SetSampleEncoding(s, ALAW);
   } else if (strncmp(sample_coding, "ulaw", 4) == 0) {
      Snack_SetSampleEncoding(s, MULAW);
   } else if (strncmp(sample_coding, "pcm", 3) == 0 || sample_coding == "") {
      if (Snack_GetBytesPerSample(s) == 2) {
         Snack_SetSampleEncoding(s, LIN16);
      } else {
         Snack_SetSampleEncoding(s, LIN8);
      }
   }
   if (sample_coding != "") {
     free(sample_coding);
   }

   /* header size shouldn't be needed by user,
      so it is not given directly by SPHERE user interface */

   Snack_SetHeaderSize(s, SPHERE_HEADERSIZE);
   Snack_SetLength(s, sample_cnt);

   return TCL_OK;
}

void
FreeSphereHeader(Sound *s)
{
  if (s->extHead != NULL) {
    ckfree((char *) s->extHead);
    s->extHead = NULL;
    s->extHeadType = 0;
  }
}

#define SPHEREFILE_VERSION "1.2"

Snack_FileFormat snackSphFormat = {
  SPHERE_STRING,
  GuessSphereFile,
  GetSphereHeader,
  ExtSphereFile,
  NULL,
  OpenSphereFile,
  CloseSphereFile,
  ReadSphereSamples,
  NULL,
  SeekSphereFile,
  FreeSphereHeader,
  NULL,
  (Snack_FileFormat *) NULL
};

/* Called by "load libsnacksphere" */
EXPORT(int, Snacksphere_Init) _ANSI_ARGS_((Tcl_Interp *interp))
{
  int res;
  
#ifdef USE_TCL_STUBS
  if (Tcl_InitStubs(interp, "8", 0) == NULL) {
    return TCL_ERROR;
  }
#endif
  
#ifdef USE_SNACK_STUBS
  if (Snack_InitStubs(interp, "2", 0) == NULL) {
    return TCL_ERROR;
  }
#endif
  
  res = Tcl_PkgProvide(interp, "snacksphere", SPHEREFILE_VERSION);
  
  if (res != TCL_OK) return res;

  Tcl_SetVar(interp, "snack::snacksphere", SPHEREFILE_VERSION,TCL_GLOBAL_ONLY);

  Snack_CreateFileFormat(&snackSphFormat);

  return TCL_OK;
}

EXPORT(int, Snacksphere_SafeInit)(Tcl_Interp *interp)
{
  return Snacksphere_Init(interp);
}