1 /* play.c - a simple utility to play WAV files (not part of tcpsound)
7 //#include <mba/msgno.h>
9 #include <SDL/SDL_audio.h>
20 mixaudio(void *unused
, Uint8
*stream
, int len
)
24 for (i
= 0; i
< NUM_SOUNDS
; i
++) {
25 amount
= (sounds
[i
].dlen
-sounds
[i
].dpos
);
26 if (amount
> (Uint32
)len
) {
29 SDL_MixAudio(stream
, &sounds
[i
].data
[sounds
[i
].dpos
], amount
, SDL_MIX_MAXVOLUME
);
30 sounds
[i
].dpos
+= amount
;
44 /* Look for an empty (or finished) sound slot */
45 for ( index
=0; index
<NUM_SOUNDS
; ++index
) {
46 if ( sounds
[index
].dpos
== sounds
[index
].dlen
) {
50 if ( index
== NUM_SOUNDS
)
53 /* Load the sound file and convert it to 16-bit stereo at 22kHz */
54 if ( SDL_LoadWAV(file
, &wave
, &data
, &dlen
) == NULL
) {
55 fprintf(stderr
, "Couldn't load %s: %s\n", file
, SDL_GetError());
58 SDL_BuildAudioCVT(&cvt
, wave
.format
, wave
.channels
, wave
.freq
, AUDIO_S16
, 2, 22050);
59 cvt
.buf
= malloc(dlen
*cvt
.len_mult
);
60 memcpy(cvt
.buf
, data
, dlen
);
62 SDL_ConvertAudio(&cvt
);
65 /* Put the sound data in the slot (it starts playing immediately) */
66 if (sounds
[index
].data
) {
67 free(sounds
[index
].data
);
70 sounds
[index
].data
= cvt
.buf
;
71 sounds
[index
].dlen
= cvt
.len_cvt
;
72 sounds
[index
].dpos
= 0;
77 main(int argc
, char *argv
[])
83 fprintf(stderr
, "usage: %s <file.wav>\n", argv
[0]);
87 /* Set 16-bit stereo audio at 22Khz */
89 fmt
.format
= AUDIO_S16
;
91 fmt
.samples
= 512; /* A good value for games */
92 fmt
.callback
= mixaudio
;
95 /* Open the audio device and start playing sound! */
96 if (SDL_OpenAudio(&fmt
, NULL
) < 0) {
97 printf("Unable to open audio: %s\n", SDL_GetError());
103 for (i
= 1; i
< argc
; i
++) {
104 fprintf(stderr
, "%s", argv
[i
]);
This page took 0.557582 seconds and 4 git commands to generate.