Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="offset"></param>
        private void loadPcm(int offset, bool drum)
        {
            var sf2 = new SF2(openFileDialog.FileName);

            var spl = sf2.SoundChunk.SMPLSubChunk.Samples;
            int tn  = 0;
            int num = 0;

            foreach (var s in sf2.HydraChunk.SHDRSubChunk.Samples)
            {
                if (s.SampleType == SF2SampleLink.MonoSample ||
                    s.SampleType == SF2SampleLink.LeftSample)
                {
                    var tim = new C140Timbre();

                    double baseFreq = 440.0 * Math.Pow(2.0, ((double)s.OriginalKey - 69.0) / 12.0);
                    tim.BaseFreqency = baseFreq;
                    tim.SampleRate   = s.SampleRate;

                    uint start = s.Start;
                    uint end   = s.End;
                    if (s.LoopEnd < end && s.LoopStart < s.LoopEnd)
                    {
                        end = s.LoopEnd;
                    }

                    uint len = end - start + 1;
                    if (len > 65535)
                    {
                        len = 65535;
                    }
                    uint loopP = s.LoopStart - s.Start;
                    if (loopP > 65535)
                    {
                        loopP = 65535;
                    }

                    sbyte[] samples = new sbyte[len];
                    for (uint i = 0; i < len; i++)
                    {
                        samples[i] = (sbyte)(spl[start + i] >> 8);
                    }

                    tim.PcmData    = samples;
                    tim.LoopPoint  = (ushort)loopP;
                    tim.LoopEnable = s.LoopStart < s.LoopEnd;

                    if (s.LoopStart < s.LoopEnd)
                    {
                        tim.SDS.ADSR.Enable = true;
                        tim.SDS.ADSR.DR     = 80;
                        tim.SDS.ADSR.SL     = 127;
                    }
                    if (drum)
                    {
                        DrumTimbres[tn].TimbreNumber = (ProgramAssignmentNumber)(tn + offset);
                        DrumTimbres[tn].BaseNote     =
                            (NoteNames)(byte)Math.Round(MidiManager.CalcNoteNumberFromFrequency(tim.BaseFreqency));
                    }

                    Timbres[tn + offset] = tim;
                    num++;

                    var nidx = s.SampleName.IndexOf('\0');
                    if (nidx >= 0)
                    {
                        tim.TimbreName = s.SampleName.Substring(0, nidx);
                    }
                    else
                    {
                        tim.TimbreName = s.SampleName;
                    }

                    tn++;
                    if (tn == 128)
                    {
                        break;
                    }
                }
            }
        }