When & why to convert events to streams

i noticed that in some examples events were converted to streams using a processor like:

processor EventToStream
{
  input event float eventIn;
  output stream float streamOut;

  float value;
  event eventIn(float newValue)
  {
    value = newValue;
  }

  void run()
  {
    loop
    {
      streamOut << value;
      advance();
    }
  }
}

what are the use-cases and advantages? is it just a convenience thing so that the value can be used directly from the input without having to use event change handlers to save an incoming event value to a processor state variable?

Yeah, it’s mainly about how you intend to use the values - if your use-case is that you’re sending new values which you need to use every time you process a sample, then a sparse stream makes more sense. If you have some sort of code that you need to run at the moment when a new value arrives, then that’s the use-case for an event.

If all you do in your event callback is to store the value and then read it later, then an event is probably the wrong pattern, but a lot of our example code was written before we had implemented sparse-streams, and probably needs a bit of spring cleaning!

that makes sense. are there any other performance considerations between streams and events?

Not really. Events and sparse streams should both be about the same performance, and clearly both work best when you send occasional changes rather than continuous streams.

1 Like