FMOD_TAG * bmx_FMOD_Sound_GetTag(FMOD_SOUND * sound, const char * name, int index) { FMOD_TAG * tag = new FMOD_TAG; FMOD_RESULT result = FMOD_Sound_GetTag(sound, name, index, tag); if (result) { return 0; } return tag; }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *cdsound; FMOD_CHANNEL *channel = 0; FMOD_RESULT result; int key; unsigned int numtracks, currenttrack = 0; unsigned int version; printf("==================================================================\n"); printf("CDPlayer Example. Copyright (c) Firelight Technologies 2004-2014.\n"); printf("==================================================================\n\n"); /* Create a System object and initialize. */ result = FMOD_System_Create(&system); ERRCHECK(result); result = FMOD_System_GetVersion(system, &version); ERRCHECK(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, NULL); ERRCHECK(result); /* Bump up the file buffer size a bit from the 16k default for CDDA, because it is a slower medium. */ result = FMOD_System_SetStreamBufferSize(system, 64*1024, FMOD_TIMEUNIT_RAWBYTES); ERRCHECK(result); /* Try a few drive letters. */ result = FMOD_System_CreateStream(system, "d:", FMOD_OPENONLY, 0, &cdsound); if (result != FMOD_OK) { result = FMOD_System_CreateStream(system, "e:", FMOD_OPENONLY, 0, &cdsound); if (result != FMOD_OK) { result = FMOD_System_CreateStream(system, "f:", FMOD_OPENONLY, 0, &cdsound); ERRCHECK(result); } } result = FMOD_Sound_GetNumSubSounds(cdsound, &numtracks); ERRCHECK(result); for (;;) { FMOD_TAG tag; if (FMOD_Sound_GetTag(cdsound, 0, -1, &tag) != FMOD_OK) { break; } if (tag.datatype == FMOD_TAGDATATYPE_CDTOC) { dump_cddb_query((FMOD_CDTOC *)tag.data); } } printf("\n========================================\n"); printf("Press SPACE to pause\n"); printf(" n to skip to next track\n"); printf(" < re-wind 10 seconds\n"); printf(" > fast-forward 10 seconds\n"); printf(" ESC to exit\n"); printf("========================================\n\n"); /* Print out length of entire CD. Did you know you can also play 'cdsound' and it will play the whole CD without gaps? */ { unsigned int lenms; result = FMOD_Sound_GetLength(cdsound, &lenms, FMOD_TIMEUNIT_MS); ERRCHECK(result); printf("Total CD length %02d:%02d\n\n", lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100); } /* Play whole CD */ result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, cdsound, FALSE, &channel); ERRCHECK(result); /* Main loop */ do { if (_kbhit()) { key = _getch(); switch (key) { case ' ' : { int paused; FMOD_Channel_GetPaused(channel, &paused); FMOD_Channel_SetPaused(channel, !paused); break; } case '<' : { unsigned int ms; FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_SENTENCE_MS); if (ms >= 10000) { ms -= 10000; } else { ms = 0; } FMOD_Channel_SetPosition(channel, ms, FMOD_TIMEUNIT_SENTENCE_MS); break; } case '>' : { unsigned int ms; FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_SENTENCE_MS); ms += 10000; FMOD_Channel_SetPosition(channel, ms, FMOD_TIMEUNIT_SENTENCE_MS); break; } case 'n' : { FMOD_Channel_GetPosition(channel, ¤ttrack, FMOD_TIMEUNIT_SENTENCE_SUBSOUND); currenttrack++; if (currenttrack >= numtracks) { currenttrack = 0; } FMOD_Channel_SetPosition(channel, currenttrack, FMOD_TIMEUNIT_SENTENCE_SUBSOUND); break; } } } FMOD_System_Update(system); if (channel) { unsigned int ms; unsigned int lenms; FMOD_BOOL playing; FMOD_BOOL paused; int busy; result = FMOD_Channel_GetPaused(channel, &paused); ERRCHECK(result); result = FMOD_Channel_IsPlaying(channel, &playing); ERRCHECK(result); result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_SENTENCE_MS); ERRCHECK(result); result = FMOD_Sound_GetLength(cdsound, &lenms, FMOD_TIMEUNIT_SENTENCE_MS); ERRCHECK(result); result = FMOD_File_GetDiskBusy(&busy); ERRCHECK(result); printf("Track %d/%d : %02d:%02d:%02d/%02d:%02d:%02d : %s (%s)\r", currenttrack + 1, numtracks, ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped", busy ? "*" : " "); } Sleep(50); } while (key != 27); printf("\n"); /* Shut down */ result = FMOD_Sound_Release(cdsound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *sound; FMOD_CHANNEL *channel = 0; FMOD_RESULT result; int key; unsigned int version; printf("===================================================================\n"); printf("NetStream Example. Copyright (c) Firelight Technologies 2004-2011.\n"); printf("===================================================================\n\n"); if (argc < 2) { printf("Usage: netstream <url>\n"); printf("Example: netstream http://www.fmod.org/stream.mp3\n\n"); return -1; } /* Create a System object and initialize. */ result = FMOD_System_Create(&system); ERRCHECK(result); result = FMOD_System_GetVersion(system,&version); ERRCHECK(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, 0); ERRCHECK(result); /* Bump up the file buffer size a little bit for netstreams (to account for lag). */ result = FMOD_System_SetStreamBufferSize(system, 64*1024, FMOD_TIMEUNIT_RAWBYTES); ERRCHECK(result); result = FMOD_System_CreateSound(system, argv[1], FMOD_HARDWARE | FMOD_2D | FMOD_CREATESTREAM | FMOD_NONBLOCKING, 0, &sound); ERRCHECK(result); printf("Press space to pause, Esc to quit\n\n"); /* Main loop */ do { unsigned int ms = 0, percent = 0; int playing = FALSE; int paused = FALSE; int starving = FALSE; FMOD_OPENSTATE openstate; if (!channel) { result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, FALSE, &channel); } if (_kbhit()) { key = _getch(); switch (key) { case ' ' : { if (channel) { int paused; FMOD_Channel_GetPaused(channel, &paused); FMOD_Channel_SetPaused(channel, !paused); } break; } } } FMOD_System_Update(system); for (;;) { FMOD_TAG tag; if (FMOD_Sound_GetTag(sound, 0, -1, &tag) != FMOD_OK) { break; } if (tag.datatype == FMOD_TAGDATATYPE_STRING) { printf("%s = %s (%d bytes) \n", tag.name, tag.data, tag.datalen); } else if (tag.type == FMOD_TAGTYPE_FMOD) { if (!strcmp(tag.name, "Sample Rate Change")) { FMOD_Channel_SetFrequency(channel, *((float *)tag.data)); } } } result = FMOD_Sound_GetOpenState(sound, &openstate, &percent, &starving, 0); ERRCHECK(result); if (channel) { result = FMOD_Channel_GetPaused(channel, &paused); ERRCHECK(result); result = FMOD_Channel_IsPlaying(channel, &playing); ERRCHECK(result); result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_MS); ERRCHECK(result); result = FMOD_Channel_SetMute(channel, starving); ERRCHECK(result); } printf("Time %02d:%02d:%02d : %s : (%3d%%%) %s \r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, openstate == FMOD_OPENSTATE_BUFFERING ? "Buffering..." : openstate == FMOD_OPENSTATE_CONNECTING ? "Connecting..." : paused ? "Paused " : playing ? "Playing " : "Stopped ", percent, starving ? "STARVING" : " "); Sleep(10); } while (key != 27); printf("\n"); printf("Shutting down.\n"); if (channel) { result = FMOD_Channel_Stop(channel); ERRCHECK(result); } /* If we pressed escape before it is ready, wait for it to finish opening before we release it. */ do { FMOD_OPENSTATE openstate; result = FMOD_Sound_GetOpenState(sound, &openstate, 0, 0, 0); ERRCHECK(result); if (openstate == FMOD_OPENSTATE_READY) { break; } printf("Waiting for sound to finish opening before trying to release it....\r"); Sleep(10); } while (1); /* Shut down */ result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *sound; FMOD_CHANNEL *channel = 0; FMOD_RESULT result; int key; unsigned int version; memset(gCurrentTrackArtist, 0, 256); memset(gCurrentTrackTitle, 0, 256); strcpy(gOutputFileName, "output.mp3"); /* Start off like this then rename if a title tag comes along */ printf("======================================================================\n"); printf("RipNetStream Example. Copyright (c) Firelight Technologies 2004-2011.\n"); printf("======================================================================\n\n"); if (argc < 2) { printf("Usage: ripnetstream <url>\n"); return -1; } /* Create a System object and initialize. */ result = FMOD_System_Create(&system); ERRCHECK(result); result = FMOD_System_GetVersion(system, &version); ERRCHECK(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } result = FMOD_System_Init(system, 100, FMOD_INIT_NORMAL, NULL); ERRCHECK(result); result = FMOD_System_SetStreamBufferSize(system, gFileBufferSize, FMOD_TIMEUNIT_RAWBYTES); ERRCHECK(result); result = FMOD_System_AttachFileSystem(system, myopen, myclose, myread, 0); ERRCHECK(result); printf("Buffering...\n\n"); result = FMOD_System_CreateSound(system, argv[1], FMOD_HARDWARE | FMOD_2D | FMOD_CREATESTREAM | FMOD_NONBLOCKING, 0, &sound); ERRCHECK(result); /* Main loop */ do { if (sound && !channel) { result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, FALSE, &channel); } if (kbhit()) { key = getch(); switch (key) { case ' ' : { if (channel) { int paused; FMOD_Channel_GetPaused(channel, &paused); FMOD_Channel_SetPaused(channel, !paused); } break; } case 'm' : case 'M' : { if (channel) { int mute; FMOD_Channel_GetMute(channel, &mute); FMOD_Channel_SetMute(channel, !mute); } break; } } } FMOD_System_Update(system); if (channel) { unsigned int ms = 0; int playing = FALSE; int paused = FALSE; int tagsupdated = 0; FMOD_Sound_GetNumTags(sound, 0, &tagsupdated); if (tagsupdated) { printf("\n"); printf("\n"); for (;;) { FMOD_TAG tag; if (FMOD_Sound_GetTag(sound, 0, -1, &tag) != FMOD_OK) { break; } if (tag.datatype == FMOD_TAGDATATYPE_STRING) { printf("[%-11s] %s (%d bytes)\n", tag.name, (char *)tag.data, tag.datalen); FMOD_Sound_GetFormat(sound, &gSoundType, 0, 0, 0); if (!strcmp(tag.name, "ARTIST")) { if (strncmp(gCurrentTrackArtist, (const char *)tag.data, 256)) { strncpy(gCurrentTrackArtist, (const char *)tag.data, 256); gUpdateFileName = TRUE; } } if (!strcmp(tag.name, "TITLE")) { if (strncmp(gCurrentTrackTitle, (const char *)tag.data, 256)) { strncpy(gCurrentTrackTitle, (const char *)tag.data, 256); gUpdateFileName = TRUE; } } } } printf("\n"); } result = FMOD_Channel_IsPlaying(channel, &playing); if (result != FMOD_OK || !playing) { FMOD_Sound_Release(sound); sound = 0; channel = 0; } else { result = FMOD_Channel_GetPaused(channel, &paused); result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_MS); printf("\rTime %02d:%02d:%02d : %s : Press SPACE to pause. 'm' to mute. ESC to quit.", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped"); fflush(stdout); } } if (sound) { FMOD_OPENSTATE openstate = FMOD_OPENSTATE_READY; FMOD_Sound_GetOpenState(sound, &openstate, 0, 0, 0); if (openstate == FMOD_OPENSTATE_ERROR) { FMOD_Sound_Release(sound); sound = 0; channel = 0; } } if (!sound) { printf("\n"); printf("Error occurred or stream ended. Restarting stream..\n"); result = FMOD_System_CreateSound(system, argv[1], FMOD_HARDWARE | FMOD_2D | FMOD_CREATESTREAM | FMOD_NONBLOCKING, 0, &sound); ERRCHECK(result); Sleep(1000); } Sleep(10); } while (key != 27); printf("\n"); /* Shut down */ result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *sound; FMOD_RESULT result; FMOD_TAG tag; int numtags, numtagsupdated, count; unsigned int version; printf("==================================================================\n"); printf("ReadTags Example. Copyright (c) Firelight Technologies 2004-2015.\n"); printf("==================================================================\n\n"); /* Create a System object and initialize. */ result = FMOD_System_Create(&system); ERRCHECK(result); result = FMOD_System_GetVersion(system, &version); ERRCHECK(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } result = FMOD_System_Init(system, 100, FMOD_INIT_NORMAL, NULL); ERRCHECK(result); /* Open the specified file. Use FMOD_CREATESTREAM and FMOD_OPENONLY so it opens quickly */ result = FMOD_System_CreateSound(system, "../media/wave.mp3", FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM | FMOD_OPENONLY, 0, &sound); ERRCHECK(result); /* Read and display all tags associated with this file */ for (;;) { /* An index of -1 means "get the first tag that's new or updated". If no tags are new or updated then getTag will return FMOD_ERR_TAGNOTFOUND. This is the first time we've read any tags so they'll all be new but after we've read them, they won't be new any more. */ if (FMOD_Sound_GetTag(sound, 0, -1, &tag) != FMOD_OK) { break; } if (tag.datatype == FMOD_TAGDATATYPE_STRING) { printf("%s = %s (%d bytes)\n", tag.name, tag.data, tag.datalen); } else { printf("%s = <binary> (%d bytes)\n", tag.name, tag.datalen); } } printf("\n"); /* Read all the tags regardless of whether they're updated or not. Also show the tag type. */ result = FMOD_Sound_GetNumTags(sound, &numtags, &numtagsupdated); ERRCHECK(result); for (count=0; count < numtags; count++) { result = FMOD_Sound_GetTag(sound, 0, count, &tag); ERRCHECK(result); switch (tag.type) { case FMOD_TAGTYPE_UNKNOWN : printf("FMOD_TAGTYPE_UNKNOWN "); break; case FMOD_TAGTYPE_ID3V1 : printf("FMOD_TAGTYPE_ID3V1 "); break; case FMOD_TAGTYPE_ID3V2 : printf("FMOD_TAGTYPE_ID3V2 "); break; case FMOD_TAGTYPE_VORBISCOMMENT : printf("FMOD_TAGTYPE_VORBISCOMMENT "); break; case FMOD_TAGTYPE_SHOUTCAST : printf("FMOD_TAGTYPE_SHOUTCAST "); break; case FMOD_TAGTYPE_ICECAST : printf("FMOD_TAGTYPE_ICECAST "); break; case FMOD_TAGTYPE_ASF : printf("FMOD_TAGTYPE_ASF "); break; case FMOD_TAGTYPE_FMOD : printf("FMOD_TAGTYPE_FMOD "); break; case FMOD_TAGTYPE_USER : printf("FMOD_TAGTYPE_USER "); break; } if (tag.datatype == FMOD_TAGDATATYPE_STRING) { printf("%s = %s (%d bytes)\n", tag.name, tag.data, tag.datalen); } else { printf("%s = ??? (%d bytes)\n", tag.name, tag.datalen); } } printf("\n"); /* Find a specific tag by name. Specify an index > 0 to get access to multiple tags of the same name. */ result = FMOD_Sound_GetTag(sound, "ARTIST", 0, &tag); ERRCHECK(result); printf("%s = %s (%d bytes)\n", tag.name, tag.data, tag.datalen); printf("\n"); /* Shut down */ result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *cdsound; FMOD_SOUND *sound; FMOD_CHANNEL *channel = 0; FMOD_RESULT result; int key, numtracks, currenttrack = 0; unsigned int version; if (argc < 2) { printf("Usage: cdplayer <volumename>\n"); printf("Example: cdplayer \"Audio CD\"\n"); exit(-1); } printf("==================================================================\n"); printf("CDPlayer Example. Copyright (c) Firelight Technologies 2004-2011.\n"); printf("==================================================================\n\n"); /* Create a System object and initialize. */ result = FMOD_System_Create(&system); ERRCHECK(result); result = FMOD_System_GetVersion(system, &version); ERRCHECK(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, NULL); ERRCHECK(result); /* Bump up the file buffer size a bit from the 16k default for CDDA, because it is a slower medium. */ result = FMOD_System_SetStreamBufferSize(system, 64*1024, FMOD_TIMEUNIT_RAWBYTES); ERRCHECK(result); result = FMOD_System_CreateStream(system, argv[1], FMOD_OPENONLY, 0, &cdsound); ERRCHECK(result); result = FMOD_Sound_GetNumSubSounds(cdsound, &numtracks); ERRCHECK(result); result = FMOD_Sound_GetSubSound(cdsound, currenttrack, &sound); ERRCHECK(result); for (;;) { FMOD_TAG tag; if (FMOD_Sound_GetTag(cdsound, 0, -1, &tag) != FMOD_OK) { break; } if (tag.datatype == FMOD_TAGDATATYPE_CDTOC) { dump_cddb_query((FMOD_CDTOC *)tag.data); } } printf("\n========================================\n"); printf("Press SPACE to pause\n"); printf(" n to skip to next track\n"); printf(" ESC to exit\n"); printf("========================================\n\n"); /* Print out length of entire CD. Did you know you can also play 'cdsound' and it will play the whole CD without gaps? */ { unsigned int lenms; result = FMOD_Sound_GetLength(cdsound, &lenms, FMOD_TIMEUNIT_MS); ERRCHECK(result); printf("Total CD length %02d:%02d\n\n", lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100); } /* Play a CD track */ result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, FALSE, &channel); ERRCHECK(result); /* Main loop */ do { if (kbhit()) { key = getch(); switch (key) { case ' ' : { int paused; FMOD_Channel_GetPaused(channel, &paused); FMOD_Channel_SetPaused(channel, !paused); break; } case 'n' : { currenttrack++; if (currenttrack >= numtracks) { currenttrack = 0; } result = FMOD_Sound_GetSubSound(cdsound, currenttrack, &sound); ERRCHECK(result); result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, FALSE, &channel); ERRCHECK(result); break; } } } FMOD_System_Update(system); if (channel) { unsigned int ms, lenms, percent = 0; FMOD_BOOL playing; FMOD_BOOL paused; result = FMOD_Channel_GetPaused(channel, &paused); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } result = FMOD_Channel_IsPlaying(channel, &playing); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_MS); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } result = FMOD_Sound_GetLength(sound, &lenms, FMOD_TIMEUNIT_MS); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } printf("\rTrack %d/%d : %02d:%02d:%02d/%02d:%02d:%02d : %s", currenttrack + 1, numtracks, ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped"); fflush(stdout); } Sleep(10); } while (key != 27); printf("\n"); /* Shut down */ result = FMOD_Sound_Release(cdsound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }
boolean I_StartDigSong(const char *musicname, boolean looping) { char *data; size_t len; FMOD_CREATESOUNDEXINFO fmt; lumpnum_t lumpnum = W_CheckNumForName(va("O_%s",musicname)); if (lumpnum == LUMPERROR) { lumpnum = W_CheckNumForName(va("D_%s",musicname)); if (lumpnum == LUMPERROR) return false; midimode = true; } else midimode = false; data = (char *)W_CacheLumpNum(lumpnum, PU_MUSIC); len = W_LumpLength(lumpnum); memset(&fmt, 0, sizeof(FMOD_CREATESOUNDEXINFO)); fmt.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); #ifdef HAVE_LIBGME if ((UINT8)data[0] == 0x1F && (UINT8)data[1] == 0x8B) { #ifdef HAVE_ZLIB UINT8 *inflatedData; size_t inflatedLen; z_stream stream; int zErr; // Somewhere to handle any error messages zlib tosses out memset(&stream, 0x00, sizeof (z_stream)); // Init zlib stream // Begin the inflation process inflatedLen = *(UINT32 *)(data + (len-4)); // Last 4 bytes are the decompressed size, typically inflatedData = (UINT8 *)Z_Calloc(inflatedLen, PU_MUSIC, NULL); // Make room for the decompressed data stream.total_in = stream.avail_in = len; stream.total_out = stream.avail_out = inflatedLen; stream.next_in = (UINT8 *)data; stream.next_out = inflatedData; zErr = inflateInit2(&stream, 32 + MAX_WBITS); if (zErr == Z_OK) // We're good to go { zErr = inflate(&stream, Z_FINISH); if (zErr == Z_STREAM_END) { // Run GME on new data if (!gme_open_data(inflatedData, inflatedLen, &gme, 44100)) { gme_equalizer_t gmeq = {GME_TREBLE, GME_BASS, 0,0,0,0,0,0,0,0}; Z_Free(inflatedData); // GME supposedly makes a copy for itself, so we don't need this lying around Z_Free(data); // We don't need this, either. gme_start_track(gme, 0); current_track = 0; gme_set_equalizer(gme,&gmeq); fmt.format = FMOD_SOUND_FORMAT_PCM16; fmt.defaultfrequency = 44100; fmt.numchannels = 2; fmt.length = -1; fmt.decodebuffersize = (44100 * 2) / 35; fmt.pcmreadcallback = GMEReadCallback; fmt.userdata = gme; FMR(FMOD_System_CreateStream(fsys, NULL, FMOD_OPENUSER | (looping ? FMOD_LOOP_NORMAL : 0), &fmt, &music_stream)); FMR(FMOD_System_PlaySound(fsys, FMOD_CHANNEL_FREE, music_stream, false, &music_channel)); FMR(FMOD_Channel_SetVolume(music_channel, music_volume / 31.0)); FMR(FMOD_Channel_SetPriority(music_channel, 0)); return true; } } else { const char *errorType; switch (zErr) { case Z_ERRNO: errorType = "Z_ERRNO"; break; case Z_STREAM_ERROR: errorType = "Z_STREAM_ERROR"; break; case Z_DATA_ERROR: errorType = "Z_DATA_ERROR"; break; case Z_MEM_ERROR: errorType = "Z_MEM_ERROR"; break; case Z_BUF_ERROR: errorType = "Z_BUF_ERROR"; break; case Z_VERSION_ERROR: errorType = "Z_VERSION_ERROR"; break; default: errorType = "unknown error"; } CONS_Alert(CONS_ERROR,"Encountered %s when running inflate: %s\n", errorType, stream.msg); } (void)inflateEnd(&stream); } else // Hold up, zlib's got a problem { const char *errorType; switch (zErr) { case Z_ERRNO: errorType = "Z_ERRNO"; break; case Z_STREAM_ERROR: errorType = "Z_STREAM_ERROR"; break; case Z_DATA_ERROR: errorType = "Z_DATA_ERROR"; break; case Z_MEM_ERROR: errorType = "Z_MEM_ERROR"; break; case Z_BUF_ERROR: errorType = "Z_BUF_ERROR"; break; case Z_VERSION_ERROR: errorType = "Z_VERSION_ERROR"; break; default: errorType = "unknown error"; } CONS_Alert(CONS_ERROR,"Encountered %s when running inflateInit: %s\n", errorType, stream.msg); } Z_Free(inflatedData); // GME didn't open jack, but don't let that stop us from freeing this up #else //CONS_Alert(CONS_ERROR,"Cannot decompress VGZ; no zlib support\n"); #endif } else if (!gme_open_data(data, len, &gme, 44100)) { gme_equalizer_t gmeq = {GME_TREBLE, GME_BASS, 0,0,0,0,0,0,0,0}; Z_Free(data); // We don't need this anymore. gme_start_track(gme, 0); current_track = 0; gme_set_equalizer(gme,&gmeq); fmt.format = FMOD_SOUND_FORMAT_PCM16; fmt.defaultfrequency = 44100; fmt.numchannels = 2; fmt.length = -1; fmt.decodebuffersize = (44100 * 2) / 35; fmt.pcmreadcallback = GMEReadCallback; fmt.userdata = gme; FMR(FMOD_System_CreateStream(fsys, NULL, FMOD_OPENUSER | (looping ? FMOD_LOOP_NORMAL : 0), &fmt, &music_stream)); FMR(FMOD_System_PlaySound(fsys, FMOD_CHANNEL_FREE, music_stream, false, &music_channel)); FMR(FMOD_Channel_SetVolume(music_channel, music_volume / 31.0)); FMR(FMOD_Channel_SetPriority(music_channel, 0)); return true; } #endif fmt.length = len; { FMOD_RESULT e = FMOD_System_CreateStream(fsys, data, FMOD_OPENMEMORY_POINT|(looping ? FMOD_LOOP_NORMAL : 0), &fmt, &music_stream); if (e != FMOD_OK) { if (e == FMOD_ERR_FORMAT) CONS_Alert(CONS_WARNING, "Failed to play music lump %s due to invalid format.\n", W_CheckNameForNum(lumpnum)); else FMR(e); return false; } } FMR(FMOD_System_PlaySound(fsys, FMOD_CHANNEL_FREE, music_stream, false, &music_channel)); if (midimode) FMR(FMOD_Channel_SetVolume(music_channel, midi_volume / 31.0)); else FMR(FMOD_Channel_SetVolume(music_channel, music_volume / 31.0)); FMR(FMOD_Channel_SetPriority(music_channel, 0)); current_track = 0; // Try to find a loop point in streaming music formats (ogg, mp3) if (looping) { FMOD_RESULT e; FMOD_TAG tag; unsigned int loopstart, loopend; // A proper LOOPPOINT is its own tag, stupid. e = FMOD_Sound_GetTag(music_stream, "LOOPPOINT", 0, &tag); if (e != FMOD_ERR_TAGNOTFOUND) { FMR(e); loopstart = atoi((char *)tag.data); // assumed to be a string data tag. FMR(FMOD_Sound_GetLoopPoints(music_stream, NULL, FMOD_TIMEUNIT_PCM, &loopend, FMOD_TIMEUNIT_PCM)); if (loopstart > 0) FMR(FMOD_Sound_SetLoopPoints(music_stream, loopstart, FMOD_TIMEUNIT_PCM, loopend, FMOD_TIMEUNIT_PCM)); return true; } // Use LOOPMS for time in miliseconds. e = FMOD_Sound_GetTag(music_stream, "LOOPMS", 0, &tag); if (e != FMOD_ERR_TAGNOTFOUND) { FMR(e); loopstart = atoi((char *)tag.data); // assumed to be a string data tag. FMR(FMOD_Sound_GetLoopPoints(music_stream, NULL, FMOD_TIMEUNIT_MS, &loopend, FMOD_TIMEUNIT_PCM)); if (loopstart > 0) FMR(FMOD_Sound_SetLoopPoints(music_stream, loopstart, FMOD_TIMEUNIT_MS, loopend, FMOD_TIMEUNIT_PCM)); return true; } // Try to fetch it from the COMMENT tag, like A.J. Freda e = FMOD_Sound_GetTag(music_stream, "COMMENT", 0, &tag); if (e != FMOD_ERR_TAGNOTFOUND) { char *loopText; // Handle any errors that arose, first FMR(e); // Figure out where the number starts loopText = strstr((char *)tag.data,"LOOPPOINT="); if (loopText != NULL) { // Skip the "LOOPPOINT=" part. loopText += 10; // Convert it to our looppoint // FMOD seems to ensure the tag is properly NULL-terminated. // atoi will stop when it reaches anything that's not a number. loopstart = atoi(loopText); // Now do the rest like above FMR(FMOD_Sound_GetLoopPoints(music_stream, NULL, FMOD_TIMEUNIT_PCM, &loopend, FMOD_TIMEUNIT_PCM)); if (loopstart > 0) FMR(FMOD_Sound_SetLoopPoints(music_stream, loopstart, FMOD_TIMEUNIT_PCM, loopend, FMOD_TIMEUNIT_PCM)); } return true; } } // No special loop point, but we're playing so it's all good. return true; }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system = 0; FMOD_SOUND *playlist = 0; FMOD_SOUND *sound = 0; FMOD_CHANNEL *channel = 0; FMOD_TAG tag; FMOD_RESULT result; FMOD_SOUND_TYPE soundtype; FMOD_BOOL isplaylist = 0; char *title = NULL; int count = 0; int key; unsigned int version; char file[128]; /* Create a System object and initialize. */ result = FMOD_System_Create(&system); ERRCHECK(result); result = FMOD_System_GetVersion(system, &version); ERRCHECK(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, 0); ERRCHECK(result); result = FMOD_System_CreateSound(system, "../media/playlist.m3u", FMOD_DEFAULT, 0, &playlist); ERRCHECK(result); result = FMOD_Sound_GetFormat(playlist, &soundtype, 0, 0, 0); ERRCHECK(result); isplaylist = (soundtype == FMOD_SOUND_TYPE_PLAYLIST); printf("===================================================================\n"); printf("PlayList Example. Copyright (c) Firelight Technologies 2004-2015.\n"); printf("===================================================================\n"); printf("\n"); printf("Press 'n' to play next sound in playlist\n"); printf("Press 'space' to pause/unpause current sound\n"); printf("Press 'Esc' to quit\n"); printf("\n"); if (isplaylist) { printf("PLAYLIST loaded.\n"); /* Get the first song in the playlist, create the sound and then play it. */ result = FMOD_Sound_GetTag(playlist, "FILE", count, &tag); ERRCHECK(result); sprintf(file, "../media/%s", (char *)tag.data); result = FMOD_System_CreateSound(system, file, FMOD_DEFAULT, 0, &sound); ERRCHECK(result); result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 0, &channel); ERRCHECK(result); FMOD_Sound_GetTag(playlist, "TITLE", count, &tag); title = (char *)tag.data; count++; } else { printf("SOUND loaded.\n"); /* This is just a normal sound, so just play it. */ sound = playlist; result = FMOD_Sound_SetMode(sound, FMOD_LOOP_NORMAL); ERRCHECK(result); result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 0, &channel); ERRCHECK(result); } printf("\n"); /* Main loop. */ do { FMOD_BOOL isplaying = 0; if (channel && isplaylist) { /* When sound has finished playing, play the next sound in the playlist */ FMOD_Channel_IsPlaying(channel, &isplaying); if (!isplaying) { if (sound) { FMOD_Sound_Release(sound); sound = NULL; } result = FMOD_Sound_GetTag(playlist, "FILE", count, &tag); if (result != FMOD_OK) { count = 0; } else { printf("playing next song in playlist...\n"); sprintf(file, "../media/%s", (char *)tag.data); result = FMOD_System_CreateSound(system, file, FMOD_DEFAULT, 0, &sound); ERRCHECK(result); result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 0, &channel); ERRCHECK(result); FMOD_Sound_GetTag(playlist, "TITLE", count, &tag); title = (char *)tag.data; count++; } } } if (_kbhit()) { key = _getch(); switch (key) { case 'n' : { /* Play the next song in the playlist */ if (channel && isplaylist) { FMOD_Channel_Stop(channel); } break; } case ' ' : { if (channel) { FMOD_BOOL paused; FMOD_Channel_GetPaused(channel, &paused); FMOD_Channel_SetPaused(channel, !paused); } } } } FMOD_System_Update(system); { unsigned int ms = 0; unsigned int lenms = 0; FMOD_BOOL paused = 0; if (channel) { if (sound) { result = FMOD_Sound_GetLength(sound, &lenms, FMOD_TIMEUNIT_MS); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } } result = FMOD_Channel_GetPaused(channel, &paused); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_MS); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } } printf("Time %02d:%02d:%02d/%02d:%02d:%02d : %s : %s\r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : "Playing ", title); } Sleep(10); } while (key != 27); printf("\n"); /* Shut down */ if (sound) { result = FMOD_Sound_Release(sound); ERRCHECK(result); } if (isplaylist) { result = FMOD_Sound_Release(playlist); ERRCHECK(result); } result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }