Exemple #1
0
        private static Dictionary <int, (WaveSampleChunk, int)> AddSamples(Config config, DLS dls)
        {
            ListChunk waves      = dls.WavePool;
            var       sampleDict = new Dictionary <int, (WaveSampleChunk, int)>((int)config.SampleTableSize);

            for (int i = 0; i < config.SampleTableSize; i++)
            {
                int ofs = config.Reader.ReadInt32(config.SampleTableOffset + (i * 4));
                if (ofs == 0)
                {
                    continue; // Skip null samples
                }

                ofs += config.SampleTableOffset;
                SampleHeader sh = config.Reader.ReadObject <SampleHeader>(ofs);

                // Create format chunk
                var fmt = new FormatChunk(WaveFormat.PCM);
                fmt.WaveInfo.Channels        = 1;
                fmt.WaveInfo.SamplesPerSec   = (uint)(sh.SampleRate >> 10);
                fmt.WaveInfo.AvgBytesPerSec  = fmt.WaveInfo.SamplesPerSec;
                fmt.WaveInfo.BlockAlign      = 1;
                fmt.FormatInfo.BitsPerSample = 8;
                // Create wave sample chunk and add loop if there is one
                var wsmp = new WaveSampleChunk
                {
                    UnityNote = 60,
                    Options   = WaveSampleOptions.NoTruncation | WaveSampleOptions.NoCompression
                };
                if (sh.DoesLoop == 0x40000000)
                {
                    wsmp.Loop = new WaveSampleLoop
                    {
                        LoopStart  = (uint)sh.LoopOffset,
                        LoopLength = (uint)(sh.Length - sh.LoopOffset),
                        LoopType   = LoopType.Forward
                    };
                }
                // Get PCM sample
                byte[] pcm = new byte[sh.Length];
                Array.Copy(config.ROM, ofs + 0x10, pcm, 0, sh.Length);

                // Add
                int dlsIndex = waves.Count;
                waves.Add(new ListChunk("wave")
                {
                    fmt,
                    wsmp,
                    new DataChunk(pcm),
                    new ListChunk("INFO")
                    {
                        new InfoSubChunk("INAM", $"Sample {i}")
                    }
                });
                sampleDict.Add(i, (wsmp, dlsIndex));
            }
            return(sampleDict);
        }
Exemple #2
0
 public void Init(byte key, ADSR adsr, int sampleOffset, bool bFixed)
 {
     _velocity     = adsr.A;
     State         = EnvelopeState.Attack;
     _pos          = 0; _interPos = 0;
     Key           = key;
     _adsr         = adsr;
     _sampleHeader = _mixer.Config.Reader.ReadObject <SampleHeader>(sampleOffset);
     _sampleOffset = sampleOffset + 0x10;
     _bFixed       = bFixed;
     Stopped       = false;
 }
Exemple #3
0
        private static Dictionary <int, (SampleHeader, int)> AddSamples(Config config, SF2 sf2)
        {
            var sampleDict = new Dictionary <int, (SampleHeader, int)>((int)config.SampleTableSize);

            for (int i = 0; i < config.SampleTableSize; i++)
            {
                int ofs = config.Reader.ReadInt32(config.SampleTableOffset + (i * 4));
                if (ofs == 0)
                {
                    continue;
                }

                ofs += config.SampleTableOffset;
                SampleHeader sh = config.Reader.ReadObject <SampleHeader>(ofs);

                short[] pcm16    = SampleUtils.PCMU8ToPCM16(config.ROM, ofs + 0x10, sh.Length);
                int     sf2Index = (int)sf2.AddSample(pcm16, $"Sample {i}", sh.DoesLoop == 0x40000000, (uint)sh.LoopOffset, (uint)(sh.SampleRate >> 10), 60, 0);
                sampleDict.Add(i, (sh, sf2Index));
            }
            return(sampleDict);
        }