예제 #1
0
        /// <summary>
        /// Initializes a new synthesizer using a specified SoundFont and settings.
        /// </summary>
        /// <param name="soundFont">The SoundFont instance.</param>
        /// <param name="settings">The settings for synthesis.</param>
        public Synthesizer(SoundFont soundFont, SynthesizerSettings settings)
        {
            if (soundFont == null)
            {
                throw new ArgumentNullException(nameof(soundFont));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            this.soundFont             = soundFont;
            this.sampleRate            = settings.SampleRate;
            this.blockSize             = settings.BlockSize;
            this.maximumPolyphony      = settings.MaximumPolyphony;
            this.enableReverbAndChorus = settings.EnableReverbAndChorus;

            minimumVoiceDuration = sampleRate / 500;

            presetLookup = new Dictionary <int, Preset>();

            var minPresetId = int.MaxValue;

            foreach (var preset in soundFont.PresetArray)
            {
                var presetId = (preset.BankNumber << 16) | preset.PatchNumber;
                presetLookup.TryAdd(presetId, preset);

                // The preset with the minimum ID number will be default.
                // If the SoundFont is GM compatible, the piano will be chosen.
                if (presetId < minPresetId)
                {
                    defaultPreset = preset;
                    minPresetId   = presetId;
                }
            }

            channels = new Channel[channelCount];
            for (var i = 0; i < channels.Length; i++)
            {
                channels[i] = new Channel(this, i == percussionChannel);
            }

            voices = new VoiceCollection(this, maximumPolyphony);

            blockLeft  = new float[blockSize];
            blockRight = new float[blockSize];

            inverseBlockSize = 1F / blockSize;

            blockRead = blockSize;

            masterVolume = 0.5F;

            if (enableReverbAndChorus)
            {
                reverb            = new Reverb(sampleRate);
                reverbInput       = new float[blockSize];
                reverbOutputLeft  = new float[blockSize];
                reverbOutputRight = new float[blockSize];

                chorus            = new Chorus(sampleRate, 0.002, 0.0019, 0.4);
                chorusInputLeft   = new float[blockSize];
                chorusInputRight  = new float[blockSize];
                chorusOutputLeft  = new float[blockSize];
                chorusOutputRight = new float[blockSize];
            }
        }
예제 #2
0
 /// <summary>
 /// Initializes a new synthesizer using a specified SoundFont and sample rate.
 /// </summary>
 /// <param name="soundFont">The SoundFont instance.</param>
 /// <param name="sampleRate">The sample rate for synthesis.</param>
 public Synthesizer(SoundFont soundFont, int sampleRate) : this(soundFont, new SynthesizerSettings(sampleRate))
 {
 }