Beispiel #1
0
        private void DecomposeSounds()
        {
            nbInstruments = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, 0x20, 4));

            int indexSoundPtr = 0;
            int soundLength   = 0;

            for (int i = 0; i < nbInstruments; i++)
            {
                Sound sound = new Sound();

                sound.start = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, indexSoundPtr + 0x30, 4));
                soundLength = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, indexSoundPtr + 0x34, 4));

                //grab sound data
                sound.rawData = CGeneric.GiveMeArray(waveTable, sound.start, soundLength);

                sound.loop.offset      = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, indexSoundPtr + 0x3C, 4));
                sound.predictor.offset = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, indexSoundPtr + 0x40, 4));

                //loop
                if (sound.loop.offset != 0)
                {
                    sound.loop.start = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, sound.loop.offset, 4));
                    sound.loop.end   = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, sound.loop.offset + 4, 4));
                    sound.loop.count = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, sound.loop.offset + 8, 4));
                    sound.loop.state = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, sound.loop.offset + 0xC, 0x20));
                }

                //predictors
                sound.predictor.order       = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, sound.predictor.offset, 4));
                sound.predictor.nPredictors = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(ptrTable, sound.predictor.offset + 4, 4));
                sound.predictor.predictors  = CGeneric.GiveMeArray(ptrTable, sound.predictor.offset + 8, sound.predictor.order * sound.predictor.nPredictors * 8 * 2);

                sound.predictor.predictorShort = new short[sound.predictor.predictors.Length / 2];
                for (int j = 0; j < sound.predictor.predictorShort.Length; j++)
                {
                    sound.predictor.predictorShort[j] = BitConverter.ToInt16(new[] { sound.predictor.predictors[j * 2 + 1], sound.predictor.predictors[j * 2] }, 0);
                }

                if (sound.loop.offset != 0)
                {
                    indexSoundPtr = sound.loop.offset;
                }
                else
                {
                    indexSoundPtr += 0xA0;
                }
                soundList.Add(sound);
            }
        }
Beispiel #2
0
        private int DecomposeSong()
        {
            int songTotalLength = 0;

            for (int i = 0; i < songDataAddress.Length; i += 8)
            {
                //starting address song0
                int songAddress = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(songDataAddress, i, 4));
                int songLength  = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(songDataAddress, i + 4, 4)) - songAddress;

                //grab the start address of song
                byte[] songRawData = CGeneric.GiveMeArray(rawData, songAddress - positionPtrStart, songLength);

                Song song = new Song(songRawData);
                songList.Add(song);
                songTotalLength += songLength;
            }
            return(songTotalLength);
        }
Beispiel #3
0
        public void WriteAllData(FileStream fs)
        {
            //set 16bit alignment for first soundbank
            int alignment = 16 - ((int)fs.Position % 16);

            for (int i = 0; i < alignment; i++)
            {
                fs.WriteByte(0);
            }

            //update address of midi (SoundBank 0) by making the difference between old position and new position
            this.finalIndexAudioStart = (int)fs.Position;


            //Array.Copy(rawData, RomLangAddress.GetMidiSongSoundBank0(), addressHeaderSb0, 0, addressHeaderSb0.Length);

            //set the good position of song, and write to rom
            int valueToStore1 = 0;
            int lengthData    = 0;
            int adder         = 0;

            fs.Position = RomLangAddress.GetMidiSongSoundBank0();
            for (int i = 0; i < soundBankList[0].songList.Count; i++)
            {
                //finalindexaudiostart + ptrlength+wavedatalength
                valueToStore1 = finalIndexAudioStart + soundBankList[0].ptrTable.Length + soundBankList[0].waveTable.Length + adder;
                lengthData    = soundBankList[0].songList[i].rawData.Length;
                fs.Write(CGeneric.ConvertIntToByteArray(valueToStore1), 0, 4);
                fs.Write(CGeneric.ConvertIntToByteArray(valueToStore1 + lengthData), 0, 4);
                adder += lengthData;
            }

            /*for (int i = 0; i < addressHeaderSb0.Length; i += 4)
             * {
             *  int tmpValue = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(addressHeaderSb0, i, 4));
             *  if (finalIndexAudioStart > initialIndexAudioStart)
             *      tmpValue += finalIndexAudioStart - initialIndexAudioStart;
             *  else
             *      tmpValue -= initialIndexAudioStart - finalIndexAudioStart;
             *  byte[] tmpBytesNew = CGeneric.ConvertIntToByteArray(tmpValue);
             *  for(int j = 0; j < tmpBytesNew.Length; j++)
             *      addressHeaderSb0[i + j] = tmpBytesNew[j];
             *
             * }*/

            //write rawData soundBank
            fs.Position = finalIndexAudioStart;

            //write ptrData, waveTable, midi and sfx
            for (int i = 0; i <= 0x15; i++)
            {
                //write ptrData and update position
                soundBankList[i].address.PtrTable = CGeneric.ConvertIntToByteArray((int)fs.Position);
                fs.Write(soundBankList[i].ptrTable, 0, soundBankList[i].ptrTable.Length);

                //write waveTable and update position
                soundBankList[i].address.WaveTable = CGeneric.ConvertIntToByteArray((int)fs.Position);
                fs.Write(soundBankList[i].waveTable, 0, soundBankList[i].waveTable.Length);

                //write midi song and update sfx position ! (will only change the position of soundbank 1 and 2, the others doesn't have midi)
                soundBankList[i].address.Sfx = CGeneric.ConvertIntToByteArray((int)fs.Position);
                fs.Write(soundBankList[i].GetRawSongs() /*midi*/, 0, soundBankList[i].GetRawSongs().Length /*midi.Length*/);

                //write sfx and update position
                fs.Write(soundBankList[i].sfx, 0, soundBankList[i].sfx.Length);

                //write end position
                soundBankList[i].address.end = CGeneric.ConvertIntToByteArray((int)fs.Position);
            }

            long savedFsPosition = fs.Position;

            //update address in the header (first part)
            for (int i = 0; i <= 0x15; i++)
            {
                fs.Position = RomLangAddress.GetSoundBankPtrTable()[i];
                fs.Write(soundBankList[i].address.PtrTable, 0, 4);

                fs.Write(soundBankList[i].address.WaveTable, 0, 4);
                if (i == 0 || i == 2)
                {
                    if (i == 2)
                    {
                        fs.Position -= 0x38;
                        fs.Write(soundBankList[i].address.Sfx, 0, 4); // in fact, it's the start of midi
                        fs.Write(soundBankList[i].address.end, 0, 4); // and it's end here (and not the sfx)
                        fs.Position += 0x30;
                    }
                    byte[] nullBytes = new byte[4];
                    fs.Write(nullBytes, 0, 4);
                    fs.Write(nullBytes, 0, 4);
                }
                else
                {
                    fs.Write(soundBankList[i].address.Sfx, 0, 4);

                    //small hack.. weird part in the rom...
                    if (i != 1)
                    {
                        fs.Write(soundBankList[i].address.end, 0, 4);
                    }
                    else
                    {
                        int    oldValue = CGeneric.ConvertByteArrayToInt(soundBankList[i].address.end);
                        byte[] newValue = CGeneric.ConvertIntToByteArray(oldValue - 0x470);
                        fs.Write(newValue, 0, 4);
                    }
                }
                fs.Write(soundBankList[i].address.WaveTable, 0, 4);
            }

            //update address in the header(second part)
            for (int i = 4; i <= 0x14; i++)
            {
                fs.Position = RomLangAddress.GetSoundBankPtrTable()[i] + 0x168;
                fs.Write(soundBankList[i].address.PtrTable, 0, 4);
                fs.Write(soundBankList[i].address.WaveTable, 0, 4);
                fs.Write(soundBankList[i].address.Sfx, 0, 4);
                fs.Write(soundBankList[i].address.end, 0, 4);
                fs.Write(soundBankList[i].address.WaveTable, 0, 4);
            }

            fs.Position = savedFsPosition;
        }
Beispiel #4
0
        //chunk RawData into PtrTable, WaveTable and Sfx
        private void ChunkRawData(int id)
        {
            //initialized to 16 to avoid first soundbank set to 0..
            int sizeOfPtrTable = 0;

            //searching waveTable (end of PtrTable) and write ptrTable rawData
            sizeOfPtrTable = CGeneric.SearchBytesInArray(rawData, CGeneric.patternN64WaveTable, 0, 16, 16);
            ptrTable       = CGeneric.GiveMeArray(rawData, 0, sizeOfPtrTable);

            //waveTable = CGeneric.GiveMeArray(rawData,ptrTable.Length,rawData.Length - ptrTable.Length);

            int sizeSfx  = 0;
            int sizeMidi = 0;

            //searching midi and sfx
            switch (id)
            {
            case 0:
                sizeMidi = DecomposeSong();
                break;

            //there is a midi song and sfx, so with a unknow wavetable length, we need to determine sfx and midi length..
            case 1:
                var startMidi = CGeneric.SearchBytesInArray(rawData, CGeneric.patternMidiSoundBank1, 0, 0, 16);
                sizeMidi = RomLangAddress.GetSizeMidiSoundBank1();
                sizeSfx  = rawData.Length - (startMidi + sizeMidi);
                break;

            //there is song but no sfx
            case 2:
                sizeMidi = RomLangAddress.GetSizeMidiSoundBank2();
                break;

            //standard computed sfx size and no midi file (dirty check, but seems working pretty well).
            default:
                int tmpSfxEqualValue;
                int tmpSfxEqualValue2;
                int tmpSfxEqualValue3;
                int tmpSfxNull;

                for (int i = ptrTable.Length; i < rawData.Length - 16; i += 16)
                {
                    //quick test, for avoiding cpu consuption
                    tmpSfxNull = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(rawData, i + 12, 4));
                    if (tmpSfxNull == 0)
                    {
                        tmpSfxEqualValue  = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(rawData, i, 4));
                        tmpSfxEqualValue2 = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(rawData, i + 4, 4));
                        tmpSfxEqualValue3 = CGeneric.ConvertByteArrayToInt(CGeneric.GiveMeArray(rawData, i + 8, 4));
                        var tmpSfxEqualValue4 = tmpSfxEqualValue3++;
                        if (tmpSfxEqualValue != 0 && tmpSfxEqualValue == tmpSfxEqualValue2 && (tmpSfxEqualValue2 == tmpSfxEqualValue4 || tmpSfxEqualValue2 == tmpSfxEqualValue3))
                        {
                            sizeSfx = rawData.Length - i;
                            break;
                        }
                    }
                }
                break;
            }

            //store date with size found previously
            waveTable = CGeneric.GiveMeArray(rawData, ptrTable.Length, rawData.Length - (ptrTable.Length + sizeMidi + sizeSfx));
            midi      = CGeneric.GiveMeArray(rawData, ptrTable.Length + waveTable.Length, sizeMidi);
            sfx       = CGeneric.GiveMeArray(rawData, ptrTable.Length + waveTable.Length + sizeMidi, sizeSfx);
        }