public DescriptorList(StreamReader reader)
 {
     List<EnvelopeDescriptor> envList = new List<EnvelopeDescriptor>();
     List<FilterDescriptor> fltrList = new List<FilterDescriptor>();
     List<LfoDescriptor> lfoList = new List<LfoDescriptor>();
     List<GeneratorDescriptor> genList = new List<GeneratorDescriptor>();
     List<CustomDescriptor> cList = new List<CustomDescriptor>();
     List<string> descList = new List<string>();
     while (!reader.EndOfStream)
     {
         string tag = ReadNextTag(reader, descList); 
         switch (tag)
         {
             case "envelope":
             {
                 EnvelopeDescriptor env = new EnvelopeDescriptor();
                 env.Read(descList.ToArray());
                 envList.Add(env);
                 break;
             }
             case "generator":
             {
                 GeneratorDescriptor gen = new GeneratorDescriptor();
                 gen.Read(descList.ToArray());
                 genList.Add(gen);
                 break;
             }
             case "filter":
             {
                 FilterDescriptor fltr = new FilterDescriptor();
                 fltr.Read(descList.ToArray());
                 fltrList.Add(fltr);
                 break;
             }
             case "lfo":
             {
                 LfoDescriptor lfo = new LfoDescriptor();
                 lfo.Read(descList.ToArray());
                 lfoList.Add(lfo);
                 break;
             }
             default:
                 if (!tag.Equals(string.Empty))
                 {
                     CustomDescriptor cus = new CustomDescriptor(tag, 0);
                     cus.Read(descList.ToArray());
                     cList.Add(cus);
                 }
                 break;
         }
         descList.Clear();
     }
     EnvelopeDescriptions = envList.ToArray();
     FilterDescriptions = fltrList.ToArray();
     LfoDescriptions = lfoList.ToArray();
     GenDescriptions = genList.ToArray();
     CustomDescriptions = cList.ToArray();
 }
 public void QuickSetup(int sampleRate, float velocity, EnvelopeDescriptor envelopeInfo)
 {
     depth = envelopeInfo.Depth + velocity * envelopeInfo.Vel2Depth;
     //Delay
     stages[0].offset = 0;
     stages[0].scale = 0;
     stages[0].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.DelayTime + envelopeInfo.Vel2Delay * velocity)));
     //Attack
     stages[1].offset = envelopeInfo.StartLevel;
     stages[1].scale = envelopeInfo.PeakLevel - envelopeInfo.StartLevel;
     stages[1].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.AttackTime + envelopeInfo.Vel2Attack * velocity)));
     stages[1].graph = Tables.EnvelopeTables[envelopeInfo.AttackGraph];
     //Hold
     stages[2].offset = 0;
     stages[2].scale = envelopeInfo.PeakLevel;
     stages[2].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.HoldTime + envelopeInfo.Vel2Hold * velocity)));
     //Decay
     stages[3].offset = envelopeInfo.SustainLevel;
     stages[3].scale = envelopeInfo.PeakLevel - envelopeInfo.SustainLevel;
     stages[3].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.DecayTime + envelopeInfo.Vel2Decay * velocity)));
     stages[3].graph = Tables.EnvelopeTables[envelopeInfo.DecayGraph];
     //Sustain
     stages[4].offset = 0;
     stages[4].scale = envelopeInfo.SustainLevel + envelopeInfo.Vel2Sustain * velocity;
     stages[4].time = (int)(sampleRate * envelopeInfo.SustainTime);
     //Release
     stages[5].offset = 0;
     stages[5].scale = (stages[3].time == 0 && stages[4].time == 0) ? envelopeInfo.PeakLevel : stages[4].scale;
     stages[5].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.ReleaseTime + envelopeInfo.Vel2Release * velocity)));
     stages[5].graph = Tables.EnvelopeTables[envelopeInfo.ReleaseGraph];
     //None
     stages[6].scale = 0;
     //Reset value, index, and starting state
     index = 0; value = 0;
     envState = EnvelopeStateEnum.Delay;
     while (stages[(int)envState].time == 0)
     {
         envState++;
     }
     stage = stages[(int)envState];
 }
 public override void Load(DescriptorList description, AssetManager assets)
 {
     gen = description.GenDescriptions[0].ToGenerator(assets);
     env = description.EnvelopeDescriptions[0];
     lfo = description.LfoDescriptions[0];
 }
 public DescriptorList(BinaryReader reader)
 {
     List<EnvelopeDescriptor> envList = new List<EnvelopeDescriptor>();
     List<FilterDescriptor> fltrList = new List<FilterDescriptor>();
     List<LfoDescriptor> lfoList = new List<LfoDescriptor>();
     List<GeneratorDescriptor> genList = new List<GeneratorDescriptor>();
     List<CustomDescriptor> cList = new List<CustomDescriptor>();
     int count = reader.ReadInt16();
     for (int x = 0; x < count; x++)
     {
         string id = new string(IOHelper.Read8BitChars(reader, 4));
         int size = reader.ReadInt32();
         switch (id.ToLower())
         {
             case EnvelopeDescriptor.ID:
             {
                 EnvelopeDescriptor env = new EnvelopeDescriptor();
                 env.Read(reader);
                 envList.Add(env);
                 break;
             }
             case GeneratorDescriptor.ID:
             {
                 GeneratorDescriptor gen = new GeneratorDescriptor();
                 gen.Read(reader);
                 genList.Add(gen);
                 break;
             }
             case FilterDescriptor.ID:
             {
                 FilterDescriptor fltr = new FilterDescriptor();
                 fltr.Read(reader);
                 fltrList.Add(fltr);
                 break;
             }
             case LfoDescriptor.ID:
             {
                 LfoDescriptor lfo = new LfoDescriptor();
                 lfo.Read(reader);
                 lfoList.Add(lfo);
                 break;
             }
             default:
             {
                 CustomDescriptor cus = new CustomDescriptor(id, size);
                 cus.Read(reader);
                 cList.Add(cus);
                 break;
             }
         }
     }
     EnvelopeDescriptions = envList.ToArray();
     FilterDescriptions = fltrList.ToArray();
     LfoDescriptions = lfoList.ToArray();
     GenDescriptions = genList.ToArray();
     CustomDescriptions = cList.ToArray();
 }
 private void LoadSfzEnvelopes(SfzRegion region)
 {
     EnvelopeDescriptions = new EnvelopeDescriptor[3];
     EnvelopeDescriptions[0] = new EnvelopeDescriptor();
     EnvelopeDescriptions[0].DelayTime = region.pitchEGDelay;
     EnvelopeDescriptions[0].AttackTime = region.pitchEGAttack;
     EnvelopeDescriptions[0].HoldTime = region.pitchEGHold;
     EnvelopeDescriptions[0].DecayTime = region.pitchEGDecay;
     EnvelopeDescriptions[0].SustainLevel = region.pitchEGSustain / 100f;
     EnvelopeDescriptions[0].ReleaseTime = region.pitchEGRelease;
     EnvelopeDescriptions[0].StartLevel = region.pitchEGStart / 100f;
     EnvelopeDescriptions[0].Depth = region.pitchEGDepth;
     EnvelopeDescriptions[0].Vel2Delay = region.pitchEGVel2Delay;
     EnvelopeDescriptions[0].Vel2Attack = region.pitchEGVel2Attack;
     EnvelopeDescriptions[0].Vel2Hold = region.pitchEGVel2Hold;
     EnvelopeDescriptions[0].Vel2Decay = region.pitchEGVel2Decay;
     EnvelopeDescriptions[0].Vel2Sustain = region.pitchEGVel2Sustain;
     EnvelopeDescriptions[0].Vel2Release = region.pitchEGVel2Release;
     EnvelopeDescriptions[0].Vel2Depth = region.pitchEGVel2Depth;
     EnvelopeDescriptions[1] = new EnvelopeDescriptor();
     EnvelopeDescriptions[1].DelayTime = region.filterEGDelay;
     EnvelopeDescriptions[1].AttackTime = region.filterEGAttack;
     EnvelopeDescriptions[1].HoldTime = region.filterEGHold;
     EnvelopeDescriptions[1].DecayTime = region.filterEGDecay;
     EnvelopeDescriptions[1].SustainLevel = region.filterEGSustain / 100f;
     EnvelopeDescriptions[1].ReleaseTime = region.filterEGRelease;
     EnvelopeDescriptions[1].StartLevel = region.filterEGStart / 100f;
     EnvelopeDescriptions[1].Depth = region.filterEGDepth;
     EnvelopeDescriptions[1].Vel2Delay = region.filterEGVel2Delay;
     EnvelopeDescriptions[1].Vel2Attack = region.filterEGVel2Attack;
     EnvelopeDescriptions[1].Vel2Hold = region.filterEGVel2Hold;
     EnvelopeDescriptions[1].Vel2Decay = region.filterEGVel2Decay;
     EnvelopeDescriptions[1].Vel2Sustain = region.filterEGVel2Sustain;
     EnvelopeDescriptions[1].Vel2Release = region.filterEGVel2Release;
     EnvelopeDescriptions[1].Vel2Depth = region.filterEGVel2Depth;
     EnvelopeDescriptions[2] = new EnvelopeDescriptor();
     EnvelopeDescriptions[2].DelayTime = region.ampEGDelay;
     EnvelopeDescriptions[2].AttackTime = region.ampEGAttack;
     EnvelopeDescriptions[2].HoldTime = region.ampEGHold;
     EnvelopeDescriptions[2].DecayTime = region.ampEGDecay;
     EnvelopeDescriptions[2].SustainLevel = region.ampEGSustain / 100f;
     EnvelopeDescriptions[2].ReleaseTime = region.ampEGRelease;
     EnvelopeDescriptions[2].StartLevel = region.ampEGStart / 100f;
     EnvelopeDescriptions[2].Depth = 1f;
     EnvelopeDescriptions[2].Vel2Delay = region.ampEGVel2Delay;
     EnvelopeDescriptions[2].Vel2Attack = region.ampEGVel2Attack;
     EnvelopeDescriptions[2].Vel2Hold = region.ampEGVel2Hold;
     EnvelopeDescriptions[2].Vel2Decay = region.ampEGVel2Decay;
     EnvelopeDescriptions[2].Vel2Sustain = region.ampEGVel2Sustain;
     EnvelopeDescriptions[2].Vel2Release = region.ampEGVel2Release;
     EnvelopeDescriptions[2].Vel2Depth = 0f;
 }
 public void QuickSetupSf2(int sampleRate, int note, short keyNumToHold, short keyNumToDecay, bool isVolumeEnvelope, EnvelopeDescriptor envelopeInfo)
 {
     depth = envelopeInfo.Depth;
     //Delay
     stages[0].offset = 0;
     stages[0].scale = 0;
     stages[0].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.DelayTime)));
     //Attack
     stages[1].offset = envelopeInfo.StartLevel;
     stages[1].scale = envelopeInfo.PeakLevel - envelopeInfo.StartLevel;
     stages[1].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.AttackTime)));
     stages[1].graph = Tables.EnvelopeTables[envelopeInfo.AttackGraph];
     //Hold
     stages[2].offset = 0;
     stages[2].scale = envelopeInfo.PeakLevel;
     stages[2].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.HoldTime) * Math.Pow(2, ((60 - note) * keyNumToHold) / 1200.0)));
     //Decay
     stages[3].offset = envelopeInfo.SustainLevel;
     stages[3].scale = envelopeInfo.PeakLevel - envelopeInfo.SustainLevel;
     if (envelopeInfo.SustainLevel == envelopeInfo.PeakLevel)
         stages[3].time = 0;
     else
         stages[3].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.DecayTime) * Math.Pow(2, ((60 - note) * keyNumToDecay) / 1200.0)));
     stages[3].graph = Tables.EnvelopeTables[envelopeInfo.DecayGraph];
     //Sustain
     stages[4].offset = 0;
     stages[4].scale = envelopeInfo.SustainLevel;
     stages[4].time = (int)(sampleRate * envelopeInfo.SustainTime);
     //Release
     stages[5].scale = stages[3].time == 0 && stages[4].time == 0 ? envelopeInfo.PeakLevel : stages[4].scale;
     if (isVolumeEnvelope)
     {
         stages[5].offset = -100;
         stages[5].scale += 100;
         stages[6].scale = -100;
     }
     else
     {
         stages[5].offset = 0;
         stages[6].scale = 0;
     }
     stages[5].time = Math.Max(0, (int)(sampleRate * (envelopeInfo.ReleaseTime)));
     stages[5].graph = Tables.EnvelopeTables[envelopeInfo.ReleaseGraph];
     //Reset value, index, and starting state
     index = 0; value = 0;
     envState = EnvelopeStateEnum.Delay;
     while (stages[(int)envState].time == 0)
     {
         envState++;
     }
     stage = stages[(int)envState];
 }
 private void LoadEnvelopes(Sf2Region region)
 {
     //mod env
     mod_env = new EnvelopeDescriptor();
     mod_env.AttackTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.AttackModulationEnvelope] / 1200.0);
     mod_env.AttackGraph = 3;
     mod_env.DecayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DecayModulationEnvelope] / 1200.0);
     mod_env.DelayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DelayModulationEnvelope] / 1200.0);
     mod_env.HoldTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.HoldModulationEnvelope] / 1200.0);
     mod_env.PeakLevel = 1;
     mod_env.ReleaseTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.ReleaseModulationEnvelope] / 1200.0);
     mod_env.StartLevel = 0;
     mod_env.SustainLevel = 1f - SynthHelper.Clamp(region.Generators[(int)GeneratorEnum.SustainModulationEnvelope], (short)0, (short)1000) / 1000f;
     //checks
     if (mod_env.AttackTime < 0.001f)
         mod_env.AttackTime = 0.001f;
     else if (mod_env.AttackTime > 100f)
         mod_env.AttackTime = 100f;
     if (mod_env.DecayTime < 0.001f)
         mod_env.DecayTime = 0;
     else if (mod_env.DecayTime > 100f)
         mod_env.DecayTime = 100f;
     if (mod_env.DelayTime < 0.001f)
         mod_env.DelayTime = 0;
     else if (mod_env.DelayTime > 20f)
         mod_env.DelayTime = 20f;
     if (mod_env.HoldTime < 0.001f)
         mod_env.HoldTime = 0;
     else if (mod_env.HoldTime > 20f)
         mod_env.HoldTime = 20f;
     if (mod_env.ReleaseTime < 0.001f)
         mod_env.ReleaseTime = 0.001f;
     else if (mod_env.ReleaseTime > 100f)
         mod_env.ReleaseTime = 100f;
     //volume env
     vel_env = new EnvelopeDescriptor();
     vel_env.AttackTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.AttackVolumeEnvelope] / 1200.0);
     vel_env.AttackGraph = 3;
     vel_env.DecayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DecayVolumeEnvelope] / 1200.0);
     vel_env.DelayTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.DelayVolumeEnvelope] / 1200.0);
     vel_env.HoldTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.HoldVolumeEnvelope] / 1200.0);
     vel_env.PeakLevel = 0;
     vel_env.ReleaseTime = (float)Math.Pow(2, region.Generators[(int)GeneratorEnum.ReleaseVolumeEnvelope] / 1200.0);
     vel_env.StartLevel = -100;
     vel_env.SustainLevel = SynthHelper.Clamp(region.Generators[(int)GeneratorEnum.SustainVolumeEnvelope], (short)0, (short)1000) / -10f;
     //checks
     if (vel_env.AttackTime < 0.001f)
         vel_env.AttackTime = 0.001f;
     else if (vel_env.AttackTime > 100f)
         vel_env.AttackTime = 100f;
     if (vel_env.DecayTime < 0.001f)
         vel_env.DecayTime = 0;
     else if (vel_env.DecayTime > 100f)
         vel_env.DecayTime = 100f;
     if (vel_env.DelayTime < 0.001f)
         vel_env.DelayTime = 0;
     else if (vel_env.DelayTime > 20f)
         vel_env.DelayTime = 20f;
     if (vel_env.HoldTime < 0.001f)
         vel_env.HoldTime = 0;
     else if (vel_env.HoldTime > 20f)
         vel_env.HoldTime = 20f;
     if (vel_env.ReleaseTime < 0.001f)
         vel_env.ReleaseTime = 0.001f;
     else if (vel_env.ReleaseTime > 100f)
         vel_env.ReleaseTime = 100f;
 }
 public override void Load(DescriptorList description, AssetManager assets)
 {
     //read in sfz params
     CustomDescriptor sfzConfig = description.FindCustomDescriptor("sfzi");
     exTarget = (int)sfzConfig.Objects[0];
     exGroup = (int)sfzConfig.Objects[1];
     sfzVolume = (float)sfzConfig.Objects[2];
     sfzPan = new PanComponent((float)sfzConfig.Objects[3], PanFormulaEnum.Neg3dBCenter);
     ampKeyTrack = (float)sfzConfig.Objects[4];
     ampRootKey = (byte)sfzConfig.Objects[5];
     ampVelTrack = (float)sfzConfig.Objects[6];
     //read in the generator info
     GeneratorDescriptor gdes = description.GenDescriptions[0];
     if (gdes.SamplerType != WaveformEnum.SampleData)
         throw new Exception("Sfz can only support sample data generators.");
     gen = gdes.ToGenerator(assets);
     //read in the envelope info
     ptch_env = description.EnvelopeDescriptions[0];
     fltr_env = description.EnvelopeDescriptions[1];
     amp_env = description.EnvelopeDescriptions[2];
     //read in the lfo info
     ptch_lfo = description.LfoDescriptions[0];
     fltr_lfo = description.LfoDescriptions[1];
     amp_lfo = description.LfoDescriptions[2];
     //read in the filter info
     fltr = description.FilterDescriptions[0];
 }
 public override void Load(DescriptorList description, AssetManager assets)
 {
     CustomDescriptor fmConfig = description.FindCustomDescriptor("fm2c");
     cIndex = (double)fmConfig.Objects[0];
     mIndex = (double)fmConfig.Objects[1];
     feedBack = (double)fmConfig.Objects[2];
     sync = GetSyncModeFromString((string)fmConfig.Objects[3]);
     if (description.GenDescriptions[0].LoopMethod != LoopModeEnum.Continuous || description.GenDescriptions[1].LoopMethod != LoopModeEnum.Continuous)
         throw new Exception("Fm2 patches must have continuous generators with wrapping bounds.");
     cGen = description.GenDescriptions[0].ToGenerator(assets);
     mGen = description.GenDescriptions[1].ToGenerator(assets);
     cEnv = description.EnvelopeDescriptions[0];
     mEnv = description.EnvelopeDescriptions[1];
     lfo = description.LfoDescriptions[0];
 }