Пример #1
0
        private byte[] GetWavHeader(int soundBank, int instrument, int lengthWave)
        {
            byte[] wavHeader = CGeneric.patternHeaderWavFile;

            int loopHeader = 0;

            if (loop.count > 0)
            {
                loopHeader = 0x18;
            }

            byte[] fileSize = CGeneric.ConvertIntToByteArray(0x4C + lengthWave + loopHeader);
            wavHeader[0x04] = fileSize[3];
            wavHeader[0x05] = fileSize[2];
            wavHeader[0x06] = fileSize[1];
            wavHeader[0x07] = fileSize[0];

            if (IsHalfSamplingRate(soundBank, instrument))
            {
                wavHeader[0x18] = 0x11;
                wavHeader[0x19] = 0x2B;
                wavHeader[0x1C] = 0x22;
                wavHeader[0x1D] = 0x56;
            }
            else
            {
                wavHeader[0x18] = 0x22;
                wavHeader[0x19] = 0x56;
                wavHeader[0x1C] = 0x44;
                wavHeader[0x1D] = 0xAC;
            }

            byte[] lengthFile = CGeneric.ConvertIntToByteArray(lengthWave);
            wavHeader[0x28] = lengthFile[3];
            wavHeader[0x29] = lengthFile[2];
            wavHeader[0x2A] = lengthFile[1];
            wavHeader[0x2B] = lengthFile[0];

            return(wavHeader);
        }
Пример #2
0
        private byte[] GetWavBottom()
        {
            byte[] wavBottom = CGeneric.patternBottomWavFile;
            if (loop.count > 0)
            {
                wavBottom[0x4]  = 0x3C;
                wavBottom[0x24] = 0x01;

                var loopStartByte = CGeneric.ConvertIntToByteArray(loop.start);
                var loopEndByte   = CGeneric.ConvertIntToByteArray(loop.end);
                wavBottom[0x34] = loopStartByte[3];
                wavBottom[0x35] = loopStartByte[2];
                wavBottom[0x36] = loopStartByte[1];
                wavBottom[0x37] = loopStartByte[0];
                wavBottom[0x38] = loopEndByte[3];
                wavBottom[0x39] = loopEndByte[2];
                wavBottom[0x3A] = loopEndByte[1];
                wavBottom[0x3B] = loopEndByte[0];

                if (loop.count != -1)
                {
                    var loopCountByte = CGeneric.ConvertIntToByteArray(loop.count);
                    wavBottom[0x40] = loopCountByte[3];
                    wavBottom[0x41] = loopCountByte[2];
                    wavBottom[0x42] = loopCountByte[1];
                    wavBottom[0x43] = loopCountByte[0];
                }
            }
            else
            {
                wavBottom[0x4] = 0x24;
            }
            //keybase to add
            wavBottom[0x14] = 0x3C;



            return(wavBottom);
        }
Пример #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;
        }