Event types
Csound supports the following event types:Performance state events
play
Emitted when performance transitions from pause/stop to running state.pause
Emitted after a successfulcsound.pause() call.
stop
Emitted after performance ends or after a successfulcsound.stop() call.
Realtime performance events
realtimePerformanceStarted
Emitted at the start of realtime performance. Not emitted on resume or during offline rendering.realtimePerformancePaused
Emitted only whencsound.pause() is successfully called during realtime performance.
realtimePerformanceResumed
Emitted only whencsound.resume() is successfully called after a pause.
realtimePerformanceEnded
Emitted after realtime performance ends or after a successfulcsound.stop() call.
Render events
renderStarted
Emitted at the start of offline/non-realtime rendering.renderEnded
Emitted at the end of offline/non-realtime rendering.Audio node events
onAudioNodeCreated
Emitted when an AudioNode is created from the AudioContext or OfflineAudioContext before realtime performance. The event callback receives the AudioNode, which is needed ifautoConnect is set to false.
Message events
message
The main entry point to Csound’s messaging system (-m flag). A default event listener prints messages to the browser console. This default listener can be removed by the user.
Event methods
Csound provides standard EventEmitter methods for managing event listeners.on()
Adds a listener to the end of the listeners array for the specified event.once()
Adds a one-time listener for the specified event. The listener is removed and invoked the next time the event is triggered.off()
Removes a listener from the listener array. Alias forremoveListener().
addListener()
Alias foron().
removeListener()
Removes the specified listener from the listener array for the event.removeAllListeners()
Removes all listeners, or those of the specified event.eventNames()
Returns an array listing the events for which the emitter has registered listeners.listenerCount()
Returns the number of listeners listening to the specified event.listeners()
Returns a copy of the array of listeners for the specified event.Complete examples
Basic event handling
Performance monitoring
Custom message filtering
Audio node routing
Render progress tracking
State machine with events
Event best practices
- Clean up listeners - Always remove event listeners when components unmount or are destroyed
- Use
once()for one-time operations - Prevents memory leaks and unnecessary handler calls - Handle errors in listeners - Errors in event handlers can crash your application
- Don’t block the event loop - Keep event handlers fast and asynchronous
- Remove default listeners when needed - The default ‘message’ listener may not fit your use case