int main(int argc, char **argv) { StreamPlayer *player; int i; /* Print out usage if no file was specified */ if(argc < 2) { fprintf(stderr, "Usage: %s <filenames...>\n", argv[0]); return 1; } if(InitAL() != 0) return 1; if(alIsExtensionPresent("AL_SOFT_buffer_samples")) { printf("AL_SOFT_buffer_samples supported!\n"); alBufferSamplesSOFT = alGetProcAddress("alBufferSamplesSOFT"); alIsBufferFormatSupportedSOFT = alGetProcAddress("alIsBufferFormatSupportedSOFT"); } else printf("AL_SOFT_buffer_samples not supported\n"); player = NewPlayer(); /* Play each file listed on the command line */ for(i = 1;i < argc;i++) { if(!OpenPlayerFile(player, argv[i])) continue; printf("Playing %s (%s, %s, %dhz)\n", argv[i], TypeName(player->type), ChannelsName(player->channels), player->rate); fflush(stdout); if(!StartPlayer(player)) { ClosePlayerFile(player); continue; } while(UpdatePlayer(player)) Sleep(10); /* All done with this file. Close it and go to the next */ ClosePlayerFile(player); } printf("Done.\n"); /* All files done. Delete the player, and close OpenAL */ DeletePlayer(player); player = NULL; CloseAL(); return 0; }
/* Destroys a player object, deleting the source and buffers. No error handling * since these calls shouldn't fail with a properly-made player object. */ static void DeletePlayer(StreamPlayer *player) { ClosePlayerFile(player); alDeleteSources(1, &player->source); alDeleteBuffers(NUM_BUFFERS, player->buffers); if(alGetError() != AL_NO_ERROR) fprintf(stderr, "Failed to delete object IDs\n"); memset(player, 0, sizeof(*player)); free(player); }
/* Opens the first audio stream of the named file. If a file is already open, * it will be closed first. */ static int OpenPlayerFile(StreamPlayer *player, const char *filename) { ClosePlayerFile(player); /* Open the file and get the first stream from it */ player->file = openAVFile(filename); player->stream = getAVAudioStream(player->file, 0); if(!player->stream) { fprintf(stderr, "Could not open audio in %s\n", filename); goto error; } /* Get the stream format, and figure out the OpenAL format */ if(getAVAudioInfo(player->stream, &player->rate, &player->channels, &player->type) != 0) { fprintf(stderr, "Error getting audio info for %s\n", filename); goto error; } player->format = GetFormat(player->channels, player->type, alIsBufferFormatSupportedSOFT); if(player->format == 0) { fprintf(stderr, "Unsupported format (%s, %s) for %s\n", ChannelsName(player->channels), TypeName(player->type), filename); goto error; } /* Allocate enough space for the temp buffer, given the format */ player->datasize = FramesToBytes(BUFFER_SIZE, player->channels, player->type); player->data = malloc(player->datasize); if(player->data == NULL) { fprintf(stderr, "Error allocating %d bytes\n", player->datasize); goto error; } return 1; error: closeAVFile(player->file); player->file = NULL; player->stream = NULL; player->datasize = 0; return 0; }