soul-lang / SOUL

The SOUL programming language and API
Other
1.71k stars 95 forks source link

[FR] Shorthand for mapping event input values #47

Closed agdsat134 closed 3 years ago

agdsat134 commented 3 years ago

I wasn't sure if this is the right place to put a feature request, if not let me know.

Since processors can potentially have a lot of event inputs, I thought there may be a way we could reduce boilerplate. From my understanding this is the current best practice for adding an input and mapping it to a value:

processor Synth
{
    input event float inputOne;

    float inputOneValue = 0.0f;

    event inputOne(float v)
    {
        inputOneValue = v;
    }
}

When you add a lot of different inputs, this requires a lot more code and reduces clarity. What if we could map the value in two lines:

float inputOneValue;
input event float inputOne => inputOneValue;

Or add a getter function (.getValue()) to the event identifier so you don't have to create a separate variable:

processor Gain
{
    input stream float in;
    output stream float out;
    input event float gain = 1.0f; // initial value

    void run()
    {
        loop
        {
            out << in * gain.getValue();

            advance();
        }
    }
}
julianstorer commented 3 years ago

Thanks - this is a good idea, and we'd been thinking along the same lines!

It's on the to-do-list.. My current thoughts are that we can implement it without any new syntax: if you don't provide an event function, then any uses of the endpoint name will simply return the last value that was received.

(And if you do provide a callback function, then it'll work the same as it does now, and won't allowed the name to be used as an expression)

Good point about it needing an initial value - the syntax you suggested is probably the best for that (and I guess that if you do provide an initial value, it means you're not allowed to also provide an event function)

Cheers - will implement this soon!