Esempio n. 1
0
 /// <summary>
 /// Read the generator.
 /// </summary>
 /// <param name="r">The reader.</param>
 public void Read(FileReader r)
 {
     Gen           = (SF2Generators)r.ReadUInt16();
     Amount        = new SF2GeneratorAmount();
     Amount.Amount = r.ReadInt16();
 }
Esempio n. 2
0
        /// <summary>
        /// Create a sound font from a downloadable sounds file.
        /// </summary>
        /// <param name="dls">A DLS file.</param>
        public SoundFont(DownloadableSounds dls)
        {
            //Get samples.
            Dictionary <int, int> waveLink;

            dls.AssignLoops();
            CreateSampleTable(dls.Waves, out waveLink);

            //Get instruments.
            foreach (var i in dls.Instruments)
            {
                Instrument inst = new Instrument();
                inst.Name = i.Name;
                foreach (var r in i.Regions)
                {
                    //New zone.
                    Zone z = new Zone();

                    //Key range.
                    if (r.NoteHigh != 127 || r.NoteLow != 0)
                    {
                        z.Generators.Add(new Generator()
                        {
                            Gen = SF2Generators.KeyRange, Amount = new SF2GeneratorAmount()
                            {
                                LowByte = (byte)r.NoteLow, HighByte = (byte)r.NoteHigh
                            }
                        });
                    }

                    //Velocity range.
                    if (r.VelocityHigh != 127 || r.VelocityLow != 0)
                    {
                        z.Generators.Add(new Generator()
                        {
                            Gen = SF2Generators.VelRange, Amount = new SF2GeneratorAmount()
                            {
                                LowByte = (byte)r.VelocityLow, HighByte = (byte)r.VelocityHigh
                            }
                        });
                    }

                    //Root key.
                    z.Generators.Add(new Generator()
                    {
                        Gen = SF2Generators.OverridingRootKey, Amount = new SF2GeneratorAmount()
                        {
                            UAmount = r.RootNote
                        }
                    });

                    //Pitch correction.
                    Samples[waveLink[(int)r.WaveId]].PitchCorrection = (sbyte)(r.Tuning / 65536);

                    //Sample Id.
                    z.Generators.Add(new Generator()
                    {
                        Gen = SF2Generators.SampleID, Amount = new SF2GeneratorAmount()
                        {
                            UAmount = (ushort)waveLink[(int)r.WaveId]
                        }
                    });
                    if (dls.Waves[(int)r.WaveId].Loops)
                    {
                        z.Generators.Add(new Generator()
                        {
                            Gen = SF2Generators.SampleModes, Amount = new SF2GeneratorAmount()
                            {
                                Amount = 1
                            }
                        });
                    }

                    //Articulators.
                    foreach (var a in r.Articulators)
                    {
                        foreach (var c in a.Connections)
                        {
                            //Generator.
                            SF2Generators      gen    = (SF2Generators)100;
                            SF2GeneratorAmount amount = new SF2GeneratorAmount();

                            //Switch connection type.
                            switch (c.DestinationConnection)
                            {
                            case DestinationConnection.Chorus:
                                gen           = SF2Generators.ChorusEffectsSend;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.EG1AttackTime:
                                gen           = SF2Generators.AttackVolEnv;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.EG1DecayTime:
                                gen           = SF2Generators.DecayVolEnv;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.EG1DelayTime:
                                gen           = SF2Generators.DelayVolEnv;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.EG1HoldTime:
                                gen           = SF2Generators.HoldVolEnv;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.EG1ReleaseTime:
                                gen           = SF2Generators.ReleaseVolEnv;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.EG1SustainLevel:
                                gen           = SF2Generators.SustainVolEnv;
                                amount.Amount = (short)((1 - c.Scale / 65536d / 1000) * 1000);
                                break;

                            case DestinationConnection.KeyNumber:
                                gen           = SF2Generators.Keynum;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.Pan:
                                gen           = SF2Generators.Pan;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.LFOFrequency:
                                gen           = SF2Generators.FreqModLFO;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;

                            case DestinationConnection.LFOStartDelayTime:
                                gen           = SF2Generators.DelayModLFO;
                                amount.Amount = (short)(c.Scale / 65536);
                                break;
                            }

                            //Add generator.
                            if ((int)gen != 100)
                            {
                                z.Generators.Add(new Generator()
                                {
                                    Gen = gen, Amount = amount
                                });
                            }

                            //Modulator used.
                            if (c.TransformConnection != TransformConnection.None)
                            {
                                //Nah, I'm lazy.
                            }
                        }
                    }

                    //Add zone.
                    inst.Zones.Add(z);
                }
                Instruments.Add(inst);
            }

            //Set presets.
            ushort instNum = 0;

            foreach (var i in dls.Instruments)
            {
                Presets.Add(new Preset()
                {
                    Bank = (ushort)i.BankId, Name = i.Name, PresetNumber = (ushort)i.InstrumentId, Zones = new List <Zone>()
                    {
                        new Zone()
                        {
                            Generators = new List <Generator>()
                            {
                                new Generator()
                                {
                                    Gen = SF2Generators.Instrument, Amount = new SF2GeneratorAmount()
                                    {
                                        UAmount = instNum++
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }