microsoft / psi

Platform for Situated Intelligence
https://github.com/microsoft/psi/wiki
Other
538 stars 96 forks source link

How to Determine if a Message is the Last Message? #317

Closed KanaHayama closed 3 months ago

KanaHayama commented 3 months ago

Hi, I am writing a speech recognizer component that buffers audio and performs a one-time recognition of the buffered audio once the voice activity state becomes inactive. However, there is a problem when my audio is sourced from a wave file, where I cannot guarantee that the last message's voice activity property is inactive, thus the last buffered data cannot be posted to the downstream components.

I wonder if it is possible to know the final originating time of a Receiver. I know there is a Pipeline.ComponentCompleted event, but I guess I can't rely on it because my component does not know the name of the component which pipes to its Receiver, and there may be components with the same name. I also know I can get a final originating time if I implement ISourceComponent, but its semantic isn't what I want.

Could you tell me what is the best approach to know if a Receiver is receiving the last message, or to know the last message's originating time before the last message is received? Thank you.

danbohus commented 3 months ago

You should be able to attach a handler to a receiver's Unsubscribed event. So for instance, from the constructor of a component that has an In receiver, you can say:

this.In.Unsubscribed += closingTime => this.OnUnsubscribe(closingTime)

Then in the OnUnsubscribe method you can still post messages on your output stream. Note that the closingTime in this case will likely be the closing time of the pipeline.

A good example of this is the Processor component, which allows for an onClose handler.

KanaHayama commented 3 months ago

Thank you. The Unsubscribed event and the Processor class are both helpful.