276
279
static EsdSample_ptr
277
280
sound_esd_load_sample(char *file)
279
AFfilehandle in_file;
282
int in_format, in_width, in_channels, frame_count;
283
int bytes_per_frame, frames_read;
285
char *origfile = strdup( file ? file : "" );
286
char fullfile[MAXPATHLEN];
289
(void) fprintf(stderr, "sound_esd_load_sample: %s\n", origfile);
293
while ( s && strcmp( file, s->file ) )
295
if ( s && !strcmp( file, s->file ) )
299
(void) fprintf(stderr, "sound_esd_load_sample: sample not loaded: loading ...\n", origfile);
304
(void) sprintf( fullfile, "%s/%s", DEFAULT_SOUND_DIR, file );
307
if (stat(file, &stbuf) < 0)
309
(void) fprintf( stderr, "Can not find the sound file %s\n", file);
312
if ( !( in_file = afOpenFile(file, "r", NULL) ) )
314
(void) fprintf( stderr, "Can not open sound sample, bad format?\n" );
320
while ( s && s->next )
322
s->next = (EsdSample_t *) malloc(sizeof(EsdSample_t));
325
(void) fprintf( stderr, "Can not allocate sample data.\n" );
326
afCloseFile(in_file);
334
s = (EsdSample_t *) malloc(sizeof(EsdSample_t));
337
(void) fprintf( stderr, "Can not allocate sample data.\n" );
338
afCloseFile(in_file);
345
frame_count = afGetFrameCount(in_file, AF_DEFAULT_TRACK);
346
in_channels = afGetChannels(in_file, AF_DEFAULT_TRACK);
347
in_rate = afGetRate(in_file, AF_DEFAULT_TRACK);
348
afGetSampleFormat(in_file, AF_DEFAULT_TRACK, &in_format, &in_width);
349
afSetVirtualByteOrder(in_file, AF_DEFAULT_TRACK, (bigendian()) ?
350
AF_BYTEORDER_BIGENDIAN : AF_BYTEORDER_LITTLEENDIAN);
351
s->file = strdup(origfile);
353
s->format = ESD_STREAM | ESD_PLAY;
359
s->format |= ESD_BITS8;
360
else if (in_width == 16)
361
s->format |= ESD_BITS16;
362
bytes_per_frame = (in_width * in_channels) / 8;
363
if (in_channels == 1)
364
s->format |= ESD_MONO;
365
else if (in_channels == 2)
366
s->format |= ESD_STEREO;
367
s->rate = (int)in_rate;
369
s->samples = frame_count * bytes_per_frame;
370
s->data = (unsigned char *) malloc(frame_count * bytes_per_frame);
373
(void) fprintf( stderr, "Can not allocate memory for sample.\n" );
374
afCloseFile(in_file);
377
frames_read = afReadFrames(in_file, AF_DEFAULT_TRACK, s->data, frame_count);
378
afCloseFile(in_file);
282
AFfilehandle in_file;
285
int in_format, in_width, in_channels, frame_count;
286
int bytes_per_frame, frames_read;
288
char *origfile = strdup(file ? file : "");
289
char fullfile[MAXPATHLEN];
292
(void) fprintf(stderr, "sound_esd_load_sample: %s\n", origfile);
296
while (s && strcmp(file, s->file))
298
if (s && !strcmp(file, s->file))
302
(void) fprintf(stderr,
303
"sound_esd_load_sample: sample not loaded: loading ...\n",
307
if (file[0] != '/') {
308
(void) sprintf(fullfile, "%s/%s", DEFAULT_SOUND_DIR, file);
311
if (stat(file, &stbuf) < 0) {
312
(void) fprintf(stderr,
313
"Can not find the sound file %s\n", file);
316
if (!(in_file = afOpenFile(file, "r", NULL))) {
317
(void) fprintf(stderr,
318
"Can not open sound sample, bad format?\n");
325
s->next = (EsdSample_t *) malloc(sizeof(EsdSample_t));
327
(void) fprintf(stderr,
328
"Can not allocate sample data.\n");
329
afCloseFile(in_file);
335
s = (EsdSample_t *) malloc(sizeof(EsdSample_t));
337
(void) fprintf(stderr,
338
"Can not allocate sample data.\n");
339
afCloseFile(in_file);
345
frame_count = afGetFrameCount(in_file, AF_DEFAULT_TRACK);
346
in_channels = afGetChannels(in_file, AF_DEFAULT_TRACK);
347
in_rate = afGetRate(in_file, AF_DEFAULT_TRACK);
348
afGetSampleFormat(in_file, AF_DEFAULT_TRACK, &in_format, &in_width);
349
afSetVirtualByteOrder(in_file, AF_DEFAULT_TRACK, (bigendian()) ?
350
AF_BYTEORDER_BIGENDIAN : AF_BYTEORDER_LITTLEENDIAN);
351
s->file = strdup(origfile);
353
s->format = ESD_STREAM | ESD_PLAY;
358
s->format |= ESD_BITS8;
359
else if (in_width == 16)
360
s->format |= ESD_BITS16;
361
bytes_per_frame = (in_width * in_channels) / 8;
362
if (in_channels == 1)
363
s->format |= ESD_MONO;
364
else if (in_channels == 2)
365
s->format |= ESD_STEREO;
366
s->rate = (int)in_rate;
367
s->samples = frame_count * bytes_per_frame;
368
s->data = (unsigned char *) malloc(frame_count * bytes_per_frame);
370
(void) fprintf(stderr,
371
"Can not allocate memory for sample.\n");
372
afCloseFile(in_file);
375
frames_read = afReadFrames(in_file, AF_DEFAULT_TRACK, s->data,
377
afCloseFile(in_file);
384
383
sound_esd_play(EsdSample_ptr s)
386
int size, confirm = 0;
385
int size, confirm = 0;
389
(void) fprintf( stderr, "sound_esd_play\n" );
388
(void) fprintf(stderr, "sound_esd_play\n");
392
if ((sound_fd < 0) || (!s))
401
s->id = esd_sample_getid(sound_fd, s->file);
404
s->id = esd_sample_cache(sound_fd, s->format, s->rate, size, s->file);
405
write(sound_fd, s->data, size);
406
confirm = esd_confirm_sample_cache(sound_fd);
407
if (confirm != s->id)
416
esd_sample_play(sound_fd, s->id);
391
if ((sound_fd < 0) || (!s))
397
s->id = esd_sample_getid(sound_fd, s->file);
399
s->id = esd_sample_cache(sound_fd,
402
write(sound_fd, s->data, size);
403
confirm = esd_confirm_sample_cache(
405
if (confirm != s->id)
414
esd_sample_play(sound_fd, s->id);
421
419
sound_esd_destroy_sample(EsdSample_ptr s)
424
(void) fprintf( stderr, "sound_esd_destroy_sample\n" );
422
(void) fprintf(stderr, "sound_esd_destroy_sample\n");
427
if ((s->id) && (sound_fd >= 0))
428
esd_sample_free(sound_fd, s->id);
425
if ((s->id) && (sound_fd >= 0))
426
esd_sample_free(sound_fd, s->id);
437
434
sound_esd_init(void)
442
(void) fprintf(stderr, "sound_esd_init\n");
439
(void) fprintf(stderr, "sound_esd_init\n");
446
fd = esd_open_sound(NULL);
451
(void) fprintf(stderr, "Error initialising sound.\n");
443
fd = esd_open_sound(NULL);
447
(void) fprintf(stderr, "Error initializing sound.\n");
458
454
sound_esd_shutdown(void)
461
(void) fprintf(stderr, "sound_esd_shutdown\n" );
457
(void) fprintf(stderr, "sound_esd_shutdown\n");
465
EsdSample_ptr s = EsdSamplesList;
460
EsdSample_ptr s = EsdSamplesList;
469
EsdSamplesList = s->next;
470
sound_esd_destroy_sample( s );
463
EsdSamplesList = s->next;
464
sound_esd_destroy_sample(s);