// This will return a MIDI/CMID/EMID/ECMI object from an open resource file // // INPUT: // theXSong is the SongResource structure // // OUTPUT: // pMusicName is a pascal string // pMusicType is the resource type // pMusicID is the resource ID // pReturnedSize is the resource size XPTR XGetMusicObjectFromSong(SongResource *theXSong, char *pMusicName, XResourceType *pMusicType, XLongResourceID *pMusicID, long *pReturnedSize) { long musicID; SongType songType; XPTR data; XResourceType midiTypes[] = {ID_MIDI, ID_MIDI_OLD, ID_CMID, ID_EMID, ID_ECMI}; short int count; data = NULL; if (pReturnedSize) { *pReturnedSize = 0; } if (pMusicName) { pMusicName[0] = 0; } if (theXSong) { *pMusicType = 0; *pMusicID = 0; musicID = XGetSongResourceObjectID(theXSong); songType = XGetSongResourceObjectType(theXSong); for (count = 0; count < (sizeof(midiTypes) / sizeof(long)); count++) { data = XGetAndDetachResource(midiTypes[count], musicID, pReturnedSize); if (data) { if (pMusicName) { XGetResourceName(midiTypes[count], musicID, pMusicName); XCtoPstr(pMusicName); } *pMusicType = midiTypes[count]; *pMusicID = musicID; break; } } } return data; }
GM_Song * GM_LoadSong(void *threadContext, void *context, XShortResourceID songID, void *theExternalSong, void *theExternalMidiData, INT32 midiSize, XShortResourceID *pInstrumentArray, XBOOL loadInstruments, XBOOL ignoreBadInstruments, OPErr *pErr) { GM_Song *pSong; XLongResourceID songObjectID; *pErr = MEMORY_ERR; pSong = NULL; if (theExternalSong) { songObjectID = (XLongResourceID)XGetSongResourceObjectID(theExternalSong); switch (XGetSongResourceObjectType(theExternalSong)) { case SONG_TYPE_SMS: pSong = PV_CreateSongFromMidi(songObjectID, theExternalMidiData, midiSize); break; case SONG_TYPE_RMF: if (theExternalMidiData == NULL) { // we only support midi data via the resource manager pSong = PV_CreateSongFromMidi(songObjectID, NULL, 0); } else { *pErr = PARAM_ERR; } break; case SONG_TYPE_BAD: case SONG_TYPE_RMF_LINEAR: /* satisfy compiler */ break; } } // load instruments if (pSong) { pSong->pSynths = NULL; pSong->context = context; GM_MergeExternalSong(theExternalSong, songID, pSong); pSong->ignoreBadInstruments = ignoreBadInstruments; *pErr = GM_LoadSongInstruments(pSong, pInstrumentArray, loadInstruments); if (*pErr) { GM_FreeSong(threadContext, pSong); // we ignore the error codes, because it should be ok to dispose pSong = NULL; } else { // song length not calculated pSong->songMidiTickLength = 0; pSong->songMicrosecondLength = 0; *pErr = NO_ERR; } } return pSong; }
XERR XCopySongMidiResources(XLongResourceID theSongID, XFILE readFileRef, XFILE writeFileRef, XBOOL protect, XBOOL copyNames) { XPTR pData; SongResource *theSong; long songSize; XResourceType theDataType; short int theID; char theName[256], theSongName[256]; unsigned long dataSize, newSize; XPTR newMidiData; XFileUseThisResourceFile(readFileRef); // from resource file theSong = (SongResource *)XGetFileResource(readFileRef, ID_SONG, theSongID, theSongName, &songSize); if (theSong) { theID = XGetSongResourceObjectID(theSong); if (protect) { pData = XGetMidiData(theID, (long *)&dataSize, &theDataType); if (pData) { newMidiData = XCompressAndEncrypt(pData, dataSize, &newSize); if (newMidiData) { theDataType = ID_ECMI; XDisposePtr(pData); pData = newMidiData; } else { theDataType = ID_MIDI; } } } else { theDataType = ID_CMID; pData = XGetFileResource(readFileRef, ID_CMID, theID, theName, (long *)&dataSize); if (pData == NULL) { theDataType = ID_ECMI; pData = XGetFileResource(readFileRef, ID_ECMI, theID, theName, (long *)&dataSize); } if (pData == NULL) { theDataType = ID_EMID; pData = XGetFileResource(readFileRef, ID_EMID, theID, theName, (long *)&dataSize); } if (pData == NULL) { theDataType = ID_MIDI; pData = XGetFileResource(readFileRef, ID_MIDI, theID, theName, (long *)&dataSize); } if (pData == NULL) { theDataType = ID_MIDI; // convert it to the new type pData = XGetFileResource(readFileRef, ID_MIDI_OLD, theID, theName, (long *)&dataSize); } } if (pData) { // write midi resource XFileUseThisResourceFile(writeFileRef); if (copyNames == FALSE) { theName[0] = 0; } XAddResource(theDataType, theID, theName, pData, (long)dataSize); XDisposePtr(pData); // write song resource XFileUseThisResourceFile(readFileRef); if (protect) { XSetSongLocked(theSong, TRUE); } XFileUseThisResourceFile(writeFileRef); XAddResource(ID_SONG, theID, theName, theSong, songSize); XDisposePtr(theSong); } } return 0; }