~ubuntu-branches/ubuntu/karmic/xpuzzles/karmic

« back to all changes in this revision

Viewing changes to xbarrel/sound.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-22 16:29:42 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050822162942-tdtf178zkpd3xxdg
Tags: 7.1.3-1ubuntu1
Update {build-,}depends for xorg -> mesa transition.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
static void
44
44
play_sound(char *fileName)
45
45
{
46
 
        int         rplay_fd = rplay_open_default();
 
46
        int rplay_fd = rplay_open_default();
47
47
 
48
48
        if (rplay_fd >= 0) {
49
49
                rplay_sound(rplay_fd, fileName);
68
68
static void
69
69
play_sound(char *fileName)
70
70
{
71
 
        char       *auservername = DisplayString(dsp);
72
 
        char       *fname = fileName;
73
 
        AuServer   *aud;        /* audio server connection */
 
71
        char *auservername = DisplayString(dsp);
 
72
        char *fname = fileName;
 
73
        AuServer *aud; /* audio server connection */
74
74
 
75
75
        if (!(aud = AuOpenServer(auservername, 0, NULL, 0, NULL, NULL)))
76
76
                return;         /*cannot connect - no server? */
84
84
#endif
85
85
 
86
86
#ifdef USE_XAUDIO
87
 
  /* Anybody ever get this working? XAudio.007 */
 
87
/* Anybody ever get this working? XAudio.007 */
88
88
#endif
89
89
 
90
90
#ifdef HAS_MMOV
110
110
static int
111
111
play_sound_so(char *fileName)
112
112
{
113
 
        int         i, j, status;
114
 
        char        buffer[2048];
115
 
        int         volume = 65;        /* volume is default to 65% */
 
113
        int i, j, status;
 
114
        char buffer[2048];
 
115
        int volume = 65; /* volume is default to 65% */
116
116
 
117
117
        /* use the internal speaker(s) */
118
 
        int         speaker = SO_INTERNAL /*SO_EXTERNAL */ ;
119
 
        int         fp;
 
118
        int speaker = SO_INTERNAL /*SO_EXTERNAL */ ;
 
119
        int fp;
120
120
 
121
121
        status = AmdInitialize("SO:", volume);  /* Initialize access to AMD */
122
 
        if ( status !=0 ) return status;
 
122
        if (status != 0) return status;
123
123
 
124
124
        AmdSelect(speaker);     /* Select which speaker */
125
125
        fp = open(fileName, O_RDONLY, 0777);    /* Open the file */
134
134
                }
135
135
        }
136
136
        (void) close(fp);
137
 
   return 0;
 
137
        return 0;
138
138
}
139
139
 
140
140
#endif
141
141
 
142
 
#if defined( HAS_MMOV ) || defined( USE_VMSPLAY )
 
142
#if defined(HAS_MMOV) || defined(USE_VMSPLAY)
143
143
static void
144
144
play_sound(char* fileName)
145
145
{
146
146
#ifdef USE_VMSPLAY
147
 
   if (play_sound_so(fileName) != 0 )
 
147
        if (play_sound_so(fileName) != 0)
148
148
#endif
149
 
     {
 
149
        {
150
150
#ifdef HAS_MMOV
151
 
        int pr_status , privs[2] , SYS$SETPRV();
152
 
 
153
 
        privs[1] = 0;
154
 
        privs[0] = PRV$M_SYSNAM;
155
 
        pr_status = SYS$SETPRV ( 1, privs, 0, 0 );
156
 
 
157
 
        if ( pr_status == SS$_NORMAL ) PLAY_SOUND_MMOV(fileName);
 
151
                int pr_status , privs[2] , SYS$SETPRV();
 
152
 
 
153
                privs[1] = 0;
 
154
                privs[0] = PRV$M_SYSNAM;
 
155
                pr_status = SYS$SETPRV (1, privs, 0, 0);
 
156
 
 
157
                if (pr_status == SS$_NORMAL)
 
158
                        PLAY_SOUND_MMOV(fileName);
158
159
#endif
159
 
     }
 
160
        }
160
161
}
161
162
#endif
162
163
 
164
165
static void
165
166
play_sound(char *fileName)
166
167
{
167
 
        char        *progrun = '\0';
 
168
        char *progrun = '\0';
168
169
 
169
170
        if ((progrun = (char *) malloc(strlen(DEF_PLAY) + strlen(fileName) + 11)) != NULL) {
170
171
                (void) sprintf(progrun, "( %s %s ) 2>&1", DEF_PLAY, fileName);
182
183
 
183
184
#ifndef DEFAULT_SOUND_DIR
184
185
#if 1
 
186
#define DEFAULT_SOUND_DIR "/usr/local/share/games/xpuzzles"
 
187
#else
185
188
#define DEFAULT_SOUND_DIR "/usr/share/games/xpuzzles"
186
 
#else
187
 
#define DEFAULT_SOUND_DIR "@PREFIX@/share/games/xpuzzles"
188
189
#endif
189
190
#endif
190
191
 
201
202
 
202
203
typedef struct _esd_sample
203
204
{
204
 
    char               *file;
205
 
    int                 rate;
206
 
    int                 format;
207
 
    int                 samples;
208
 
    unsigned char      *data;
209
 
    int                 id;
210
 
    struct _esd_sample *next;
 
205
        char *file;
 
206
        int rate;
 
207
        int format;
 
208
        int samples;
 
209
        unsigned char *data;
 
210
        int id;
 
211
        struct _esd_sample *next;
211
212
} EsdSample_t;
212
213
 
213
 
typedef EsdSample_t    *EsdSample_ptr;
214
 
 
215
 
static EsdSample_ptr    EsdSamplesList =(EsdSample_ptr)NULL;
216
 
static int              sound_fd = -1;
217
 
 
218
 
static EsdSample_ptr    sound_esd_load_sample(char *file);
219
 
static void             sound_esd_play(EsdSample_ptr s);
220
 
static void             sound_esd_destroy_sample(EsdSample_ptr s);
221
 
static int              sound_esd_init(void);
222
 
static void             sound_esd_shutdown(void);
 
214
typedef EsdSample_t *EsdSample_ptr;
 
215
 
 
216
static EsdSample_ptr EsdSamplesList =(EsdSample_ptr)NULL;
 
217
static int sound_fd = -1;
 
218
 
 
219
static EsdSample_ptr sound_esd_load_sample(char *file);
 
220
static void sound_esd_play(EsdSample_ptr s);
 
221
static void sound_esd_destroy_sample(EsdSample_ptr s);
 
222
static int sound_esd_init(void);
 
223
static void sound_esd_shutdown(void);
223
224
 
224
225
 
225
226
/*
227
228
 * =============================
228
229
 */
229
230
 
230
 
static void
231
 
play_sound(char *fileName)
232
 
{
233
 
#ifdef DEBUG
234
 
    (void) fprintf( stderr, "play_sound %s\n", fileName );
235
 
#endif
236
 
    if ( fileName && *fileName )
237
 
      sound_esd_play( sound_esd_load_sample( fileName ) );
238
 
}
239
 
 
240
231
int init_sound(void)
241
232
{
242
 
    return( sound_esd_init() );
 
233
        return (sound_esd_init());
243
234
}
244
235
 
245
236
void shutdown_sound(void)
246
237
{
247
 
    sound_esd_shutdown();
 
238
        sound_esd_shutdown();
 
239
}
 
240
 
 
241
static void
 
242
play_sound(char *fileName)
 
243
{
 
244
        init_sound();
 
245
#ifdef DEBUG
 
246
        (void) fprintf(stderr, "play_sound %s\n", fileName);
 
247
#endif
 
248
        if (fileName && *fileName)
 
249
                sound_esd_play(sound_esd_load_sample(fileName));
 
250
        shutdown_sound();
248
251
}
249
252
 
250
253
/* Found this snippet from modes/glx/xpm-image.c by way of
252
255
static int
253
256
bigendian (void)
254
257
{
255
 
  union { int i; char c[sizeof(int)]; } u;
256
 
  u.i = 1;
257
 
  return !u.c[0];
 
258
        union { int i; char c[sizeof(int)]; } u;
 
259
        u.i = 1;
 
260
        return !u.c[0];
258
261
}
259
262
 
260
263
 
276
279
static EsdSample_ptr
277
280
sound_esd_load_sample(char *file)
278
281
{
279
 
   AFfilehandle        in_file;
280
 
   struct stat         stbuf;
281
 
   EsdSample_ptr       s;
282
 
   int                 in_format, in_width, in_channels, frame_count;
283
 
   int                 bytes_per_frame, frames_read;
284
 
   double              in_rate;
285
 
   char               *origfile = strdup( file ? file : "" );
286
 
   char                fullfile[MAXPATHLEN];
287
 
 
288
 
#ifdef DEBUG
289
 
   (void) fprintf(stderr, "sound_esd_load_sample: %s\n", origfile);
290
 
#endif
291
 
 
292
 
   s = EsdSamplesList;
293
 
   while ( s && strcmp( file, s->file ) )
294
 
     s = s->next;
295
 
   if ( s && !strcmp( file, s->file ) )
296
 
     return s;
297
 
 
298
 
#ifdef DEBUG
299
 
   (void) fprintf(stderr, "sound_esd_load_sample: sample not loaded: loading ...\n", origfile);
300
 
#endif
301
 
 
302
 
   if (file[0] != '/')
303
 
   {
304
 
       (void) sprintf( fullfile, "%s/%s", DEFAULT_SOUND_DIR, file );
305
 
       file = fullfile;
306
 
   }
307
 
   if (stat(file, &stbuf) < 0)
308
 
   {
309
 
       (void) fprintf( stderr, "Can not find the sound file %s\n", file);
310
 
       return NULL;
311
 
   }
312
 
   if ( !( in_file = afOpenFile(file, "r", NULL) ) )
313
 
   {
314
 
       (void) fprintf( stderr, "Can not open sound sample, bad format?\n" );
315
 
       return(NULL);
316
 
   }
317
 
   s = EsdSamplesList;
318
 
   if ( s )
319
 
   {
320
 
       while ( s && s->next )
321
 
         s = s->next;
322
 
       s->next = (EsdSample_t *) malloc(sizeof(EsdSample_t));
323
 
       if ( !s->next )
324
 
       {
325
 
           (void) fprintf( stderr, "Can not allocate sample data.\n" );
326
 
           afCloseFile(in_file);
327
 
           return NULL;
328
 
       }
329
 
       s = s->next;
330
 
       s->next = NULL;
331
 
   }
332
 
   else
333
 
   {
334
 
       s = (EsdSample_t *) malloc(sizeof(EsdSample_t));
335
 
       if ( !s )
336
 
       {
337
 
           (void) fprintf( stderr, "Can not allocate sample data.\n" );
338
 
           afCloseFile(in_file);
339
 
           return NULL;
340
 
       }
341
 
       EsdSamplesList = s;
342
 
       s->next = NULL;
343
 
   }
344
 
 
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);
352
 
   s->rate = 44100;
353
 
   s->format = ESD_STREAM | ESD_PLAY;
354
 
   s->samples = 0;
355
 
   s->data = NULL;
356
 
   s->id = 0;
357
 
 
358
 
   if (in_width == 8)
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;
368
 
 
369
 
   s->samples = frame_count * bytes_per_frame;
370
 
   s->data = (unsigned char *) malloc(frame_count * bytes_per_frame);
371
 
   if ( !s->data )
372
 
   {
373
 
       (void) fprintf( stderr, "Can not allocate memory for sample.\n" );
374
 
       afCloseFile(in_file);
375
 
       return NULL;
376
 
   }
377
 
   frames_read = afReadFrames(in_file, AF_DEFAULT_TRACK, s->data, frame_count);
378
 
   afCloseFile(in_file);
379
 
   return s;
 
282
        AFfilehandle in_file;
 
283
        struct stat stbuf;
 
284
        EsdSample_ptr s;
 
285
        int in_format, in_width, in_channels, frame_count;
 
286
        int bytes_per_frame, frames_read;
 
287
        double in_rate;
 
288
        char *origfile = strdup(file ? file : "");
 
289
        char fullfile[MAXPATHLEN];
 
290
 
 
291
#ifdef DEBUG
 
292
        (void) fprintf(stderr, "sound_esd_load_sample: %s\n", origfile);
 
293
#endif
 
294
 
 
295
        s = EsdSamplesList;
 
296
        while (s && strcmp(file, s->file))
 
297
                s = s->next;
 
298
        if (s && !strcmp(file, s->file))
 
299
                return s;
 
300
 
 
301
#ifdef DEBUG
 
302
        (void) fprintf(stderr,
 
303
                "sound_esd_load_sample: sample not loaded: loading ...\n",
 
304
                origfile);
 
305
#endif
 
306
 
 
307
        if (file[0] != '/') {
 
308
                (void) sprintf(fullfile, "%s/%s", DEFAULT_SOUND_DIR, file);
 
309
                file = fullfile;
 
310
        }
 
311
        if (stat(file, &stbuf) < 0) {
 
312
                (void) fprintf(stderr,
 
313
                        "Can not find the sound file %s\n", file);
 
314
                return NULL;
 
315
        }
 
316
        if (!(in_file = afOpenFile(file, "r", NULL))) {
 
317
                (void) fprintf(stderr,
 
318
                        "Can not open sound sample, bad format?\n");
 
319
                return(NULL);
 
320
        }
 
321
        s = EsdSamplesList;
 
322
        if (s) {
 
323
                while (s && s->next)
 
324
                        s = s->next;
 
325
                s->next = (EsdSample_t *) malloc(sizeof(EsdSample_t));
 
326
                if (!s->next) {
 
327
                        (void) fprintf(stderr,
 
328
                                "Can not allocate sample data.\n");
 
329
                        afCloseFile(in_file);
 
330
                        return NULL;
 
331
                }
 
332
                s = s->next;
 
333
                s->next = NULL;
 
334
        } else {
 
335
                s = (EsdSample_t *) malloc(sizeof(EsdSample_t));
 
336
                if (!s) {
 
337
                        (void) fprintf(stderr,
 
338
                                "Can not allocate sample data.\n");
 
339
                        afCloseFile(in_file);
 
340
                        return NULL;
 
341
                }
 
342
                EsdSamplesList = s;
 
343
                s->next = NULL;
 
344
        }
 
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);
 
352
        s->rate = 44100;
 
353
        s->format = ESD_STREAM | ESD_PLAY;
 
354
        s->samples = 0;
 
355
        s->data = NULL;
 
356
        s->id = 0;
 
357
        if (in_width == 8)
 
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);
 
369
        if (!s->data) {
 
370
                (void) fprintf(stderr,
 
371
                        "Can not allocate memory for sample.\n");
 
372
                afCloseFile(in_file);
 
373
                return NULL;
 
374
        }
 
375
        frames_read = afReadFrames(in_file, AF_DEFAULT_TRACK, s->data,
 
376
                frame_count);
 
377
        afCloseFile(in_file);
 
378
        return s;
380
379
}
381
380
 
382
381
static
383
382
void
384
383
sound_esd_play(EsdSample_ptr s)
385
384
{
386
 
   int                 size, confirm = 0;
 
385
        int size, confirm = 0;
387
386
 
388
387
#ifdef DEBUG
389
 
   (void) fprintf( stderr, "sound_esd_play\n" );
 
388
        (void) fprintf(stderr, "sound_esd_play\n");
390
389
#endif
391
390
 
392
 
   if ((sound_fd < 0) || (!s))
393
 
     return;
394
 
   if (!s->id)
395
 
     {
396
 
        if (sound_fd >= 0)
397
 
          {
398
 
             if (s->data)
399
 
               {
400
 
                  size = s->samples;
401
 
                  s->id = esd_sample_getid(sound_fd, s->file);
402
 
                  if (s->id < 0)
403
 
                    {
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)
408
 
                         s->id = 0;
409
 
                    }
410
 
                  free(s->data);
411
 
                  s->data = NULL;
412
 
               }
413
 
          }
414
 
     }
415
 
   if (s->id > 0)
416
 
     esd_sample_play(sound_fd, s->id);
417
 
   return;
 
391
        if ((sound_fd < 0) || (!s))
 
392
                return;
 
393
        if (!s->id) {
 
394
                if (sound_fd >= 0) {
 
395
                        if (s->data) {
 
396
                                size = s->samples;
 
397
                                s->id = esd_sample_getid(sound_fd, s->file);
 
398
                                if (s->id < 0) {
 
399
                                        s->id = esd_sample_cache(sound_fd,
 
400
                                                s->format, s->rate,
 
401
                                                size, s->file);
 
402
                                        write(sound_fd, s->data, size);
 
403
                                        confirm = esd_confirm_sample_cache(
 
404
                                                sound_fd);
 
405
                                        if (confirm != s->id)
 
406
                                                s->id = 0;
 
407
                                }
 
408
                                free(s->data);
 
409
                                s->data = NULL;
 
410
                        }
 
411
                }
 
412
        }
 
413
        if (s->id > 0)
 
414
                esd_sample_play(sound_fd, s->id);
 
415
        return;
418
416
}
419
417
 
420
418
static void
421
419
sound_esd_destroy_sample(EsdSample_ptr s)
422
420
{
423
421
#ifdef DEBUG
424
 
    (void) fprintf( stderr, "sound_esd_destroy_sample\n" );
 
422
        (void) fprintf(stderr, "sound_esd_destroy_sample\n");
425
423
#endif
426
424
 
427
 
    if ((s->id) && (sound_fd >= 0))
428
 
      esd_sample_free(sound_fd, s->id);
429
 
 
430
 
    if (s->data)
431
 
      free(s->data);
432
 
    if (s->file)
433
 
      free(s->file);
 
425
        if ((s->id) && (sound_fd >= 0))
 
426
                esd_sample_free(sound_fd, s->id);
 
427
        if (s->data)
 
428
                free(s->data);
 
429
        if (s->file)
 
430
                free(s->file);
434
431
}
435
432
 
436
433
static int
437
434
sound_esd_init(void)
438
435
{
439
 
    int                 fd;
 
436
        int fd;
440
437
 
441
438
#ifdef DEBUG
442
 
    (void) fprintf(stderr, "sound_esd_init\n");
 
439
        (void) fprintf(stderr, "sound_esd_init\n");
443
440
#endif
444
 
    if (sound_fd != -1)
445
 
      return 0;
446
 
    fd = esd_open_sound(NULL);
447
 
    if (fd >= 0)
448
 
      sound_fd = fd;
449
 
    else
450
 
    {
451
 
        (void) fprintf(stderr, "Error initialising sound.\n");
452
 
        return -1;
453
 
     }
454
 
    return 0;
 
441
        if (sound_fd != -1)
 
442
                return 0;
 
443
        fd = esd_open_sound(NULL);
 
444
        if (fd >= 0)
 
445
                sound_fd = fd;
 
446
        else {
 
447
                (void) fprintf(stderr, "Error initializing sound.\n");
 
448
                return -1;
 
449
        }
 
450
        return 0;
455
451
}
456
452
 
457
453
static void
458
454
sound_esd_shutdown(void)
459
455
{
460
456
#ifdef DEBUG
461
 
    (void) fprintf(stderr, "sound_esd_shutdown\n" );
 
457
        (void) fprintf(stderr, "sound_esd_shutdown\n");
462
458
#endif
463
 
    if (sound_fd >= 0)
464
 
    {
465
 
        EsdSample_ptr   s = EsdSamplesList;
 
459
        if (sound_fd >= 0) {
 
460
                EsdSample_ptr s = EsdSamplesList;
466
461
 
467
 
        while ( s )
468
 
        {
469
 
            EsdSamplesList = s->next;
470
 
            sound_esd_destroy_sample( s );
471
 
            free( s );
472
 
            s = EsdSamplesList;
473
 
        }
474
 
        close(sound_fd);
475
 
        sound_fd = -1;
476
 
    }
 
462
                while (s) {
 
463
                        EsdSamplesList = s->next;
 
464
                        sound_esd_destroy_sample(s);
 
465
                        free(s);
 
466
                        s = EsdSamplesList;
 
467
                }
 
468
                close(sound_fd);
 
469
                sound_fd = -1;
 
470
        }
477
471
}
478
472
 
479
473
#endif
488
482
        (void) printf("sound file %s\n", temp);
489
483
#endif
490
484
        if (temp && *temp) {
491
 
                int ret = 1;
492
 
                ret = init_sound();
493
 
                if (ret == 0) {
494
 
                        play_sound(temp);
495
 
                        shutdown_sound();
496
 
                }
 
485
                play_sound(temp);
497
486
        }
498
487
}
499
488
#endif