// // I_ReadDataBlock() // // Returns a handle to the internal storage of a MEMFILE and moves the read // position of the MEMFILE forward to the end of the block. // static byte* I_ReadDataBlock(MEMFILE *mf, size_t length) { if (!mf) return NULL; size_t memfileoffset = mem_ftell(mf); if (mem_fsize(mf) < memfileoffset + length) return NULL; byte* data = (byte*)mem_fgetbuf(mf) + memfileoffset; mem_fseek(mf, length, MEM_SEEK_CUR); return data; }
int I_RegisterSong (char *data, size_t musicLen) { if(!music_initialized) return 0; // input mus memory file and midi MEMFILE *mus = mem_fopen_read(data, musicLen); MEMFILE *midi = mem_fopen_write(); I_UnRegisterSong(0); int result = mus2mid(mus, midi); switch(result) { case 1: Printf(PRINT_HIGH, "MUS is not valid\n"); break; case 0: case 2: { #ifdef OSX if (NewMusicSequence(&sequence) != noErr) return 0; cfd = CFDataCreate(NULL, (const Uint8 *)mem_fgetbuf(midi), mem_fsize(midi)); if(!cfd) { DisposeMusicSequence(sequence); return 0; } if (MusicSequenceLoadSMFData(sequence, (CFDataRef)cfd) != noErr) { DisposeMusicSequence(sequence); CFRelease(cfd); return 0; } registered_tracks[0].Track = (Mix_Music*)1; #else Mix_Music *music = 0; // older versions of sdl-mixer require a physical midi file to be read, 1.2.7+ can read from memory #ifndef TEMP_MIDI // SDL >= 1.2.7 if (result == 0) // it is a midi { registered_tracks[0].Data = SDL_RWFromMem(mem_fgetbuf(midi), mem_fsize(midi)); } else // it is another format { registered_tracks[0].Data = SDL_RWFromMem(data, musicLen); } if (!registered_tracks[0].Data) { Printf(PRINT_HIGH, "SDL_RWFromMem: %s\n", SDL_GetError()); break; } music = Mix_LoadMUS_RW(registered_tracks[0].Data); if(!music) { Printf(PRINT_HIGH, "Mix_LoadMUS_RW: %s\n", Mix_GetError()); SDL_FreeRW(registered_tracks[0].Data); registered_tracks[0].Data = NULL; break; } #else // SDL <= 1.2.6 - Create a file so it can load the midi FILE *fp = fopen(TEMP_MIDI, "wb+"); if(!fp) { Printf(PRINT_HIGH, "Could not open temporary music file %s, not playing track\n", TEMP_MIDI); break; } for(int i = 0; i < mem_fsize(midi); i++) fputc(mem_fgetbuf(midi)[i], fp); fclose(fp); music = Mix_LoadMUS(TEMP_MIDI); if(!music) { Printf(PRINT_HIGH, "Mix_LoadMUS: %s\n", Mix_GetError()); break; } #endif registered_tracks[0].Track = music; #endif // OSX break; } // case 2 } mem_fclose(mus); mem_fclose(midi); return 1; }