@csound/browser package provides Csound as a WebAssembly module for web browsers, with full Web Audio API integration.
Initialization
Csound()
Initializes and returns a new Csound instance.Parameters
audioContext- Optional AudioContext or OfflineAudioContext to use. If not provided, an AudioContext will be created automatically.inputChannelCount- Input channel count for AudioNode. Defaults to 2.outputChannelCount- Output channel count for AudioNode. Defaults to 2.autoConnect- Automatically connect to audioContext.destination. Default:true.withPlugins- Array of WebAssembly plugin libraries to load.useWorker- Run Csound in a Web Worker separate from the audio callback. Default:false.useSAB- Use SharedArrayBuffers for Web Worker communication if supported. Default:true.
Returns
Promise<CsoundObj | undefined> - A Csound instance or undefined if initialization fails.
Example
CsoundObj interface
The main Csound object returned byCsound() initialization.
Web Audio integration
getAudioContext()
Returns the AudioContext or OfflineAudioContext used by Csound.getNode()
Returns the AudioNode used for Csound processing.Instance information
getSr()
Returns the sample rate.getKr()
Returns the control rate.getKsmps()
Returns the ksmps value (samples per control period).getNchnls()
Returns the number of output channels.getNchnlsInput()
Returns the number of input channels.get0dBFS()
Returns the 0dBFS reference value.getA4()
Returns the A4 frequency reference.getCurrentTimeSamples()
Returns the current performance time in samples.getSizeOfMYFLT()
Returns the size of MYFLT in bytes.Configuration
setOption()
Sets a single Csound command-line option.setParams()
Configures Csound with a CSOUND_PARAMS structure.getParams()
Gets the current CSOUND_PARAMS structure.Compilation
compileCSD()
Compiles a CSD file or text but does not perform it.mode- 0 for file path, 1 for CSD text. Default: 0
compileOrc()
Compiles orchestra code.parseOrc()
Parses orchestra code and returns an AST.compileTree()
Compiles an AST tree.evalCode()
Compiles and evaluates orchestra code.Performance control
start()
Prepares Csound for performance.perform()
Performs audio until the end is reached.performKsmps()
Performs one ksmps worth of samples.performBuffer()
Performs one buffer worth of audio.pause()
Pauses performance.resume()
Resumes paused performance.stop()
Stops performance.cleanup()
Prints performance info and closes audio/MIDI devices.reset()
Resets Csound to initial state.Channels
setControlChannel()
Sets a control channel value.getControlChannel()
Gets a control channel value.setStringChannel()
Sets a string channel value.getStringChannel()
Gets a string channel value.Score events
inputMessage()
Inputs an immediate score event.inputMessageAsync()
Inputs an immediate score event asynchronously.readScore()
Reads and loads a score from a string.getScoreTime()
Returns the current score time in seconds.setScoreOffsetSeconds()
Sets the score offset time.getScoreOffsetSeconds()
Gets the score offset time.rewindScore()
Rewinds the score to the offset time.isScorePending()
Checks if score events are pending.setScorePending()
Sets whether score events are performed.Tables
tableLength()
Returns the length of a table (excluding guard point).tableGet()
Gets a value from a table.tableSet()
Sets a value in a table.tableCopyIn()
Copies an array into a table.tableCopyOut()
Copies a table to a Float64Array.getTable()
Alias for tableCopyOut().getTableArgs()
Gets table arguments.isNamedGEN()
Checks if a GEN number is a named GEN.getNamedGEN()
Gets the GEN name for a table.MIDI
midiMessage()
Sends a MIDI message.getMIDIDevList()
Gets a list of MIDI devices.getMidiOutFileName()
Gets the MIDI output filename.getRtMidiName()
Gets the real-time MIDI name.setMidiCallbacks()
Sets MIDI callbacks.Audio I/O
enableAudioInput()
Enables audio input (microphone).getInputBufferSize()
Returns the input buffer size in samples.getOutputBufferSize()
Returns the output buffer size in samples.getInputBuffer()
Returns the address of the input buffer.getOutputBuffer()
Returns the address of the output buffer.getSpin()
Returns the address of the input working buffer.getSpout()
Returns the address of the output working buffer.System
getInputName()
Returns the audio input name.getOutputName()
Returns the audio output name.getAPIVersion()
Returns the API version.getVersion()
Returns the Csound version.getDebug()
Checks if debug mode is enabled.setDebug()
Sets debug mode.initialize()
Initializes Csound with specific flags.destroy()
Destroys the Csound instance and frees memory.terminateInstance()
Terminates the instance and all workers.appendEnv()
Appends an environment variable.Filesystem
See thefs property for filesystem operations.