module import _ from "lodash" ; import { CompletionMsg , MsgType , OscType , OscValues } from "../osc-types" ; /**
 * Call and response is where an OSC command is sent to the
 * server which later responds with a message whose first items
 * match 'response', followed by the contents of the response.
 */ export interface CallAndResponse { call : MsgType ; response : MsgType ; } /**
 * Many scsynth OSC commands accept lists of params:
 *
 *    [\freq, 440, \pan, 0, \amp, 0.9]
 *
 * In supercollider.js these can be passed as objects:
 *
 *    {freq: 440, pan: 0, amp: 0.9}
 *
 * or as a list:
 *
 *    [['freq', 440], ['pan', 0], ['amp', 0.9]]
 */ type PairsType = Params | ParamList ; export interface Params { [ name ] : OscType ; } type Param = OscType [ ] ; type ParamList = Param [ ] ; /**
 * Flatten {} or [[ ], ...] to []
 */ function flattenPairs ( pairs ) { if ( _ . isPlainObject ( pairs ) ) { return _ . flatten ( _ . toPairs ( pairs ) ) ; } if ( _ . isArray ( pairs ) ) { return _ . flatten ( pairs ) ; } throw new TypeError ( `Received ${ typeof pairs } ${ JSON . stringify ( pairs ) }. Expected \{key: value, ...\} or [[key, value],...]` ) ; } memberof export enum AddActions { HEAD = 0 , TAIL = 1 , BEFORE = 2 , AFTER = 3 , REPLACE = 4 } /**
 * Tell server to exit
 *
 */ export function quit ( ) { return [ "/quit" ] ; } param { int } on export function notify ( on = 1 , clientId = 0 ) { return { call : [ "/notify" , on , clientId ] , response : [ "/done" , "/notify" ] , } ; } /**
 * Query for server status.
 * Server replies with `/status.reply`
 */ export function status ( ) { return { call : [ "/status" ] , response : [ "/status.reply" ] , } ; } /**
 * Execute a command defined by a UGen Plug-in
 */ export function cmd ( command , args = [ ] ) { return [ "/cmd" , command , ... args ] ; } param { int } code export function dumpOSC ( code = 1 ) { return [ "/dumpOSC" , code ] ; } param { int } id export function sync ( id ) { return { call : [ "/sync" , id ] , response : [ "/synced" , id ] , } ; } /**
 * Clear all scheduled bundles. Removes all bundles from the scheduling queue.
 */ export function clearSched ( ) { return [ "/clearSched" ] ; } param { int } on export function error ( on = 1 ) { return [ "/error" , on ] ; } /**** Synth Definition Commands **  */ param { Buffer } buffer param { Array } completionMsg export function defRecv ( buffer , completionMsg = null ) { return { call : [ "/d_recv" , buffer , completionMsg ] , response : [ "/done" , "/d_recv" ] , } ; } param { String } path param { Array } completionMsg export function defLoad ( path , completionMsg = null ) { return { call : [ "/d_load" , path , completionMsg ] , response : [ "/done" ] , } ; } param { String } path param { Array } completionMsg export function defLoadDir ( path , completionMsg = null ) { return { call : [ "/d_loadDir" , path , completionMsg ] , response : [ "/done" ] , } ; } param { String } defName export function defFree ( defName ) { return [ "/d_free" , defName ] ; } /******* Node Commands **********************  */ param { int } nodeID export function nodeFree ( nodeID ) { return [ "/n_free" , nodeID ] ; } param { int } nodeID param { int } on export function nodeRun ( nodeID , on = 1 ) { return [ "/n_run" , nodeID , on ] ; } example param { int } nodeID param { object | Array } pairs export function nodeSet ( nodeID , pairs ) { return [ "/n_set" , nodeID , ... flattenPairs ( pairs ) ] ; } param { int } nodeID param { Array } valueSets export function nodeSetn ( nodeID , valueSets ) { return [ "/n_setn" , nodeID , ... flattenPairs ( valueSets ) ] ; } param { int } nodeID param { Array } triples export function nodeFill ( nodeID , triples = [ ] ) { return [ "/n_fill" , nodeID , ... flattenPairs ( triples ) ] ; } param { int } nodeID param { Array | object } pairs export function nodeMap ( nodeID , pairs = [ ] ) { return [ "/n_map" , nodeID , ... flattenPairs ( pairs ) ] ; } param { int } nodeID param { Array } triples export function nodeMapn ( nodeID , triples = [ ] ) { return [ "/n_mapn" , nodeID , ... flattenPairs ( triples ) ] ; } param { int } nodeID param { Array } pairs export function nodeMapAudio ( nodeID , pairs ) { return [ "/n_mapa" , nodeID , ... flattenPairs ( pairs ) ] ; } param { int } nodeID param { Array } triples export function nodeMapAudion ( nodeID , triples = [ ] ) { return [ "/n_mapan" , nodeID , ... flattenPairs ( triples ) ] ; } param { int } moveNodeID param { int } beforeNodeID export function nodeBefore ( moveNodeID , beforeNodeID ) { return [ "/n_before" , moveNodeID , beforeNodeID ] ; } param { int } moveNodeID param { int } afterNodeID export function nodeAfter ( moveNodeID , afterNodeID ) { return [ "/n_after" , moveNodeID , afterNodeID ] ; } param { int } nodeID export function nodeQuery ( nodeID ) { return { call : [ "/n_query" , nodeID ] , response : [ "/n_info" , nodeID ] , } ; } param { int } nodeID export function nodeTrace ( nodeID ) { return [ "/n_trace" , nodeID ] ; } param { int } addAction param { int } targetID param { Array . < int > } nodeIDs export function nodeOrder ( addAction , targetID , nodeIDs ) { return [ "/n_order" , addAction , targetID , ... nodeIDs ] ; } /***** Synth Commands  **  */ param { object } args return export function synthNew ( defName , nodeID = - 1 , addAction = AddActions . TAIL , targetID = 0 , args = [ ] ) { return [ "/s_new" , defName , nodeID , addAction , targetID , ... flattenPairs ( args ) ] ; } param { int } synthID param { Array . < int | String > } controlNames export function synthGet ( synthID , controlNames ) { return { call : [ "/s_get" , synthID , ... controlNames ] , response : [ "/n_set" , synthID ] , } ; } param { int } synthID param { int | String } controlName param { int } n export function synthGetn ( synthID , controlName , n ) { return { call : [ "/s_getn" , synthID , controlName , n ] , response : [ "/n_setn" , synthID ] , } ; } param { Array } synthIDs export function synthNoid ( synthIDs ) { return [ "/s_noid" , ... synthIDs ] ; } /****** Group Commands ***  */ param { int } nodeID param { int } addAction param { int } targetID export function groupNew ( nodeID , addAction = AddActions . HEAD , targetID = 0 ) { return [ "/g_new" , nodeID , _ . isUndefined ( addAction ) ? AddActions . HEAD : addAction , targetID || 0 ] ; } param { int } groupID param { int } addAction param { int } targetID export function parallelGroupNew ( groupID , addAction = AddActions . HEAD , targetID = 0 ) { return [ "/p_new" , groupID , addAction , targetID ] ; } param { int } groupID param { int } nodeID param { ... int } rest export function groupHead ( groupID , nodeID , ... rest ) { return [ "/g_head" , groupID , nodeID , ... rest ] ; } param { int } groupID param { int } nodeID param { ... int } rest export function groupTail ( groupID , nodeID , ... rest ) { return [ "/g_tail" , groupID , nodeID , ... rest ] ; } param { int } groupID export function groupFreeAll ( groupID ) { return [ "/g_freeAll" , groupID ] ; } param { int } groupID export function groupDeepFree ( groupID ) { return [ "/g_deepFree" , groupID ] ; } param { int } groupID param { int } dumpControlValues export function groupDumpTree ( groupID , dumpControlValues = 0 ) { return [ "/g_dumpTree" , groupID , dumpControlValues ] ; } param { int } groupID param { int } dumpControlValues export function groupQueryTree ( groupID , dumpControlValues = 0 ) { return { call : [ "/g_queryTree" , groupID , dumpControlValues ] , response : [ "/g_queryTree.reply" , groupID ] , } ; } /***** Unit Generator Commands ***  */ param { int } nodeID param { int } uGenIndex param { String } command param { Array } args export function ugenCmd ( nodeID , uGenIndex , command , args = [ ] ) { return [ "/u_cmd" , nodeID , uGenIndex , command , ... args ] ; } /***** Buffer Commands ***  */ param { int } bufferID param { int } numFrames param { int } numChannels param { Array } completionMsg export function bufferAlloc ( bufferID , numFrames , numChannels , completionMsg = null ) { return { call : [ "/b_alloc" , bufferID , numFrames , numChannels , completionMsg ] , response : [ "/done" , "/b_alloc" , bufferID ] , } ; } param { int } bufferID param { String } path param { int } startFrame param { int } numFramesToRead param { Array } completionMsg export function bufferAllocRead ( bufferID , path , startFrame = 0 , numFramesToRead = - 1 , completionMsg = null ) { return { call : [ "/b_allocRead" , bufferID , path , startFrame , numFramesToRead , completionMsg ] , response : [ "/done" , "/b_allocRead" , bufferID ] , } ; } param { int } bufferID param { String } path param { int } startFrame param { int } numFramesToRead param { Array . < int > } channels param { Array } completionMsg export function bufferAllocReadChannel ( bufferID , path , startFrame , numFramesToRead , channels , completionMsg = null ) { const call = [ "/b_allocReadChannel" , bufferID , path , startFrame , numFramesToRead , ... channels ] ; if ( completionMsg ) { call . push ( completionMsg ) ; } return { call : call , response : [ "/done" , "/b_allocReadChannel" , bufferID ] , } ; } param { int } bufferID param { String } path param { int } startFrame param { int } numFramesToRead param { int } startFrameInBuffer param { int } leaveFileOpen param { Array } completionMsg export function bufferRead ( bufferID , path , startFrame = 0 , numFramesToRead = - 1 , startFrameInBuffer = 0 , leaveFileOpen = 0 , completionMsg = null ) { return { call : [ "/b_read" , bufferID , path , startFrame , numFramesToRead , startFrameInBuffer , leaveFileOpen , completionMsg ] , response : [ "/done" , "/b_read" , bufferID ] , } ; } param { int } bufferID param { String } path param { int } startFrame param { int } numFramesToRead param { int } startFrameInBuffer param { int } leaveFileOpen param { Array . < int > } channels param { Array } completionMsg export function bufferReadChannel ( bufferID , path , startFrame = 0 , numFramesToRead = - 1 , startFrameInBuffer = 0 , leaveFileOpen = 0 , channels = [ ] , completionMsg = null ) { const call = [ "/b_readChannel" , bufferID , path , startFrame , numFramesToRead , startFrameInBuffer , leaveFileOpen , ... channels , ] ; call . push ( completionMsg ) ; return { call : call , response : [ "/done" , "/b_readChannel" , bufferID ] , } ; } param { int } bufferID param { String } path param { String } headerFormat param { String } sampleFormat param { int } numFramesToWrite param { int } startFrameInBuffer param { int } leaveFileOpen param { Array } completionMsg export function bufferWrite ( bufferID , path , headerFormat = "aiff" , sampleFormat = "float" , numFramesToWrite = - 1 , startFrameInBuffer = 0 , leaveFileOpen = 0 , completionMsg = null ) { return { call : [ "/b_write" , bufferID , path , headerFormat , sampleFormat , numFramesToWrite , startFrameInBuffer , leaveFileOpen , completionMsg , ] , response : [ "/done" , "/b_write" , bufferID ] , } ; } param { int } bufferID param { Array } completionMsg export function bufferFree ( bufferID , completionMsg = null ) { return { call : [ "/b_free" , bufferID , completionMsg ] , response : [ "/done" , "/b_free" , bufferID ] , } ; } param { int } bufferID param { Array } completionMsg export function bufferZero ( bufferID , completionMsg = null ) { return { call : [ "/b_zero" , bufferID , completionMsg ] , response : [ "/done" , "/b_zero" , bufferID ] , } ; } param { int } bufferID param { Array } pairs export function bufferSet ( bufferID , pairs ) { return [ "/b_set" , bufferID , ... flattenPairs ( pairs ) ] ; } param { int } bufferID param { int } startFrame param { Array . < float > } values export function bufferSetn ( bufferID , startFrame , values = [ ] ) { return [ "/b_setn" , bufferID , startFrame , values . length , ... values ] ; } param { int } bufferID param { int } startFrame param { int } numFrames param { float } value export function bufferFill ( bufferID , startFrame , numFrames , value ) { return [ "/b_fill" , bufferID , startFrame , numFrames , value ] ; } param { int } bufferID param { String } command param { Array } args export function bufferGen ( bufferID , command , args = [ ] ) { return { call : [ "/b_gen" , bufferID , command , ... args ] , response : [ "/done" , "/b_gen" , bufferID ] , } ; } param { int } bufferID export function bufferClose ( bufferID ) { return { call : [ "/b_close" , bufferID ] , response : [ "/done" , "/b_close" , bufferID ] , } ; } param { int } bufferID export function bufferQuery ( bufferID ) { return { call : [ "/b_query" , bufferID ] , response : [ "/b_info" , bufferID ] , } ; } param { int } bufferID param { Array } framesArray export function bufferGet ( bufferID , framesArray ) { return { call : [ "/b_get" , bufferID , ... framesArray ] , response : [ "/b_set" , bufferID ] , } ; } param { int } bufferID param { int } startFrame param { int } numFrames export function bufferGetn ( bufferID , startFrame , numFrames ) { return { call : [ "/b_getn" , bufferID , startFrame , numFrames ] , response : [ "/b_setn" , bufferID ] , } ; } /***** Control Bus Commands ***  */ param { Array } pairs export function controlBusSet ( pairs ) { return [ "/c_set" , ... flattenPairs ( pairs ) ] ; } param { Array } triples export function controlBusSetn ( triples = [ ] ) { return [ "/c_setn" , ... flattenPairs ( triples ) ] ; } param { Array } triples export function controlBusFill ( triples = [ ] ) { return [ "/c_fill" , ... flattenPairs ( triples ) ] ; } param { Number } busID export function controlBusGet ( busID ) { return { call : [ "/c_get" , busID ] , response : [ "/c_set" , busID ] , } ; } param { int } startBusIndex param { int } numBusses export function controlBusGetn ( startBusIndex , numBusses ) { return { call : [ "/c_getn" , startBusIndex , numBusses ] , response : [ "/c_setn" , startBusIndex ] , } ; } /***** Non Real Time Mode Commands ***  */ /**
  End real time mode, close file.  Not yet implemented on server

  This message should be sent in a bundle in non real time mode.
  The bundle timestamp will establish the ending time of the file.
  This command will end non real time mode and close the sound file.
  Replies with `/done`.

  */ export function nonRealTimeEnd ( ) { return { call : [ "/nrt_end" ] , response : [ "/done" ] , } ; }