This project has moved and is read-only. For the latest updates, please go here.

AdsrSampleProvider Not Producing Adsr Effect

Jul 24, 2016 at 2:44 PM
Edited Jul 24, 2016 at 2:57 PM
Hi All,

_Edit: I changed the WaveOut object to NumberOfBuffers = 1 and it's now working. Now I have to figure out how to extend this class for stereo. Any advice is welcome._

I'm trying to get the AdsrSampleProvider to work. My audio plays fine through the speakers but it doesn't produce any Adsr effect to the sound. I have also tried changing the AttackSeconds and ReleaseSeconds properties with no effect. The test audio file is 16-bit mono. I've saved it to MP3 and WAV formats, but neither format produces the effect.

Here's my code. Does anyone see any obvious mistakes?
IWavePlayer waveOutDevice = null;
string fname = @"C:\TestAudio\MyAudioMono.wav";
WaveOut outputDeviceWaveOut = new WaveOut()
                {
                    DesiredLatency = 100,
                    NumberOfBuffers = 2,
                };
outputDeviceWaveOut.DeviceNumber = 0;
waveOutDevice = outputDeviceWaveOut;                
AudioFileReader arFile = new AudioFileReader(fname);  // sampler reader
AdsrSampleProvider adsrSamplr = new AdsrSampleProvider(arFile);
waveOutDevice.Init(adsrSamplr);
waveOutDevice.Play();                    
My ultimate goal is to generalize this sampler for stereo then create other samplers for effects like reverb, echo, etc. I'll be glad to share if I can get them working.
Jul 24, 2016 at 6:38 PM
The AdsrSampleProvider has four properties that control the effect:
        AttackSeconds
        SustainLevel
        DecayRate
        ReleaseSeconds
Is there a place where I can find valid value ranges (min and max value ) for each of these properties?
Jul 25, 2016 at 12:40 AM
Hi again Theseus,
I don't know why the ADSR didn't work with more than a single buffer, however I just tried to use it myself and ran into the "mono only" issue. All my media are likely to be stereo so it would be good to get this working. Are you working on it?
Sid
Jul 25, 2016 at 12:19 PM
Hi sidprice,
Yes, I'm still working with the ADSR code. I'm a novice with DSP and don't yet understand how the buffers get written with the sampler so this may take a while to get it working with stereo.
Jul 27, 2016 at 2:01 PM
Adapting AdsrSampleProvider for stereo was fairly simple. I just modified a few lines of code in the AdsrSampleProvider class Read method. This mod was based on the code in the FadeInOutSampleProvider class so I don't claim any points for original thinking.
       public int Read(float[] buffer, int offset, int count)
        {
            if (adsr.State == EnvelopeGenerator.EnvelopeState.Idle) return 0; // we've finished
            var samples = source.Read(buffer, offset, count);
            int sample = 0;
            int numChannels = source.WaveFormat.Channels;
            float adsrValue = 1.0f;

            while (sample < count)
            {
                adsrValue = adsr.Process();
                for (int ch = 0; ch < numChannels; ch++)
                {
                    buffer[offset + sample++] *= adsrValue; 
                }
            }
            return samples;
        }