Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Envelope timings incorrect #185

Open
fhoenig opened this issue Jun 6, 2023 · 1 comment
Open

Envelope timings incorrect #185

fhoenig opened this issue Jun 6, 2023 · 1 comment

Comments

@fhoenig
Copy link

fhoenig commented Jun 6, 2023

Looks like attack is in seconds but decay and release are off by a factor of ~5 w.r.t. them being seconds.
Is this a know issue / intended?

#include <daisysp.h>
#include <chrono>
#include <thread>
 

daisysp::Oscillator osc;
daisysp::Metro      tick;
daisysp::Adsr       env;
bool gate = false;

void testaudio(void *userData, Uint8 *buffer, int length)
{
    memset(buffer, 0, length);
    int numToWrite = length / (sizeof(float) * 2);
    float *sampleBuffer = (float*)buffer;
    float osc_out, env_out;
    for (int sample = 0; sample < numToWrite; sample++)
    {
        // When the metro ticks, trigger the envelope to start.
        //if (tick.Process())
        //{
        //    gate = !gate;
        //}

        // Use envelope to control the amplitude of the oscillator.
        env_out = env.Process(true);
        osc.SetAmp(env_out);
        osc_out = osc.Process();


        *sampleBuffer++ = osc_out; // Left channel value
        *sampleBuffer++ = osc_out; // Right channel value
    }
}

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_AUDIO);
    SDL_AudioSpec requested = {};
    requested.freq = 48000;
    requested.samples = 2048;
    requested.format = AUDIO_F32;
    requested.channels = 2;
    requested.callback = testaudio;

    SDL_AudioSpec obtained = {};
    int deviceId = SDL_OpenAudioDevice(nullptr, 0, &requested, &obtained, 0);
    
    osc.Init(obtained.freq);
    osc.SetFreq(440);
    tick.Init(1.0f, obtained.freq);
    env.Init(obtained.freq);
    env.SetTime(daisysp::ADSR_SEG_ATTACK, 0.0);
    env.SetTime(daisysp::ADSR_SEG_DECAY, 1.0);
    env.SetTime(daisysp::ADSR_SEG_RELEASE, 1.0);
    env.SetSustainLevel(0);
    
    SDL_PauseAudioDevice(deviceId, false);
    while (true)
    {
        std::this_thread::sleep_for(std::chrono::microseconds(100));
    }
    return 0;
}
@takumi-ogata
Copy link

Hi Florian,

The ADSR is ported from here (https://paulbatchelor.github.io/res/soundpipe/docs/adsr.html).
Here's a note about it on that page:

NOTE: The attack, decay, and release parameters are "fuzzy" values that don't exactly correspond to duration in seconds. More accurately, they are special tau constant units that feed into the filter used to generate the envelope. The attack value specificly undergoes some "creative" modificiations in order to create snappier attack times. It is highly recommend to tune parameters by ear rather than to read the values literally.

For more reference:
https://ccrma.stanford.edu/~jos/mdft/Exponentials.html
https://ccrma.stanford.edu/~jos/mdft/Audio_Decay_Time_T60.html

We could clarify all of this in a documentation, comments in the example code, and etc.
Thank you for bringing this topic up. We can look into this more and have a discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants