| 1 | /* play.c - a simple utility to play WAV files (not part of tcpsound) |
| 2 | */ |
| 3 | |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
| 7 | //#include <mba/msgno.h> |
| 8 | #include <SDL/SDL.h> |
| 9 | #include <SDL/SDL_audio.h> |
| 10 | |
| 11 | #define NUM_SOUNDS 5 |
| 12 | |
| 13 | struct sample { |
| 14 | Uint8 *data; |
| 15 | Uint32 dpos; |
| 16 | Uint32 dlen; |
| 17 | } sounds[NUM_SOUNDS]; |
| 18 | |
| 19 | void |
| 20 | mixaudio(void *unused, Uint8 *stream, int len) |
| 21 | { |
| 22 | int i; |
| 23 | Uint32 amount; |
| 24 | for (i = 0; i < NUM_SOUNDS; i++) { |
| 25 | amount = (sounds[i].dlen-sounds[i].dpos); |
| 26 | if (amount > (Uint32)len) { |
| 27 | amount = len; |
| 28 | } |
| 29 | SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos], amount, SDL_MIX_MAXVOLUME); |
| 30 | sounds[i].dpos += amount; |
| 31 | } |
| 32 | (void)unused; |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | PlaySound(char *file) |
| 37 | { |
| 38 | int index; |
| 39 | SDL_AudioSpec wave; |
| 40 | Uint8 *data; |
| 41 | Uint32 dlen; |
| 42 | SDL_AudioCVT cvt; |
| 43 | |
| 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 ) { |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | if ( index == NUM_SOUNDS ) |
| 51 | return; |
| 52 | |
| 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()); |
| 56 | return; |
| 57 | } |
| 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); |
| 61 | cvt.len = dlen; |
| 62 | SDL_ConvertAudio(&cvt); |
| 63 | SDL_FreeWAV(data); |
| 64 | |
| 65 | /* Put the sound data in the slot (it starts playing immediately) */ |
| 66 | if (sounds[index].data) { |
| 67 | free(sounds[index].data); |
| 68 | } |
| 69 | SDL_LockAudio(); |
| 70 | sounds[index].data = cvt.buf; |
| 71 | sounds[index].dlen = cvt.len_cvt; |
| 72 | sounds[index].dpos = 0; |
| 73 | SDL_UnlockAudio(); |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | main(int argc, char *argv[]) |
| 78 | { |
| 79 | SDL_AudioSpec fmt; |
| 80 | int i; |
| 81 | |
| 82 | if (argc < 2) { |
| 83 | fprintf(stderr, "usage: %s <file.wav>\n", argv[0]); |
| 84 | return EXIT_FAILURE; |
| 85 | } |
| 86 | |
| 87 | /* Set 16-bit stereo audio at 22Khz */ |
| 88 | fmt.freq = 22050; |
| 89 | fmt.format = AUDIO_S16; |
| 90 | fmt.channels = 2; |
| 91 | fmt.samples = 512; /* A good value for games */ |
| 92 | fmt.callback = mixaudio; |
| 93 | fmt.userdata = NULL; |
| 94 | |
| 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()); |
| 98 | return EXIT_FAILURE; |
| 99 | } |
| 100 | |
| 101 | SDL_PauseAudio(0); |
| 102 | |
| 103 | for (i = 1; i < argc; i++) { |
| 104 | fprintf(stderr, "%s", argv[i]); |
| 105 | PlaySound(argv[i]); |
| 106 | /* fgetc(stdin); |
| 107 | */ |
| 108 | sleep(2); |
| 109 | } |
| 110 | |
| 111 | SDL_CloseAudio(); |
| 112 | |
| 113 | return EXIT_SUCCESS; |
| 114 | } |
| 115 | |