index.js
// to be sync-api compatible, it creates worklet lazily
to be sync-api compatible, it creates worklet lazily
to be sync - api compatible it creates worklet lazily
function createWriter (dest) {
  if (!dest) dest = (new AudioContext).destination
  const context = dest.context
  let listeners
  let init = context.audioWorklet.addModule(workletURL).then(() => {
    init = null
    write.node = new AudioWorkletNode(context, 'writer-worklet', {
      outputChannelCount: [dest.channelCount]
    })
    listeners = []
    write.node.port.onmessage = e => {
      listeners.shift()(e)
      if (e.data === null) write.node.closed = true
    }
    write.node.connect(dest)
    write.node.write = write
  })
  function write (data) {
    if (write.then) write.then = null
    return init ? init.then(() => push(data)) : push(data)
  }
  write.then = (fn) => {
    write.then = null
    init.then(() => fn(write))
  }
  function push(data) {
    if (write.node.closed) throw Error('Writer is closed')
    let arr = data
    if (data) {
      if (data.getChannelData) {
        arr = []
        for (let c = 0; c < dest.channelCount; c++) {
          arr[c] = c < data.numberOfChannels ? data.getChannelData(c) : new Float32Array()
        }
      }
      else if (typeof data[0] === 'number') {
        arr = []
        let len = Math.floor(data.length / dest.channelCount)
        for (let c = 0; c < dest.channelCount; c++) {
          arr[c] = data.subarray ? data.subarray(c * len, c * len + len) : new Float32Array(data.slice(c * len, c * len + len))
        }
      }
    }
    return new Promise(resolve => {
      listeners.push(resolve)
      write.node.port.postMessage(arr)
    })
  }
  return write
}
function createWriter ( dest ) { if ( ! dest ) dest = ( new AudioContext ) . destination const context = dest . context let listeners let init = context . audioWorklet . addModule ( workletURL ) . then ( ( ) => { init = null write . node = new AudioWorkletNode ( context , 'writer-worklet' , { outputChannelCount : [ dest . channelCount ] } ) listeners = [ ] write . node . port . onmessage = e => { listeners . shift ( ) ( e ) if ( e . data === null ) write . node . closed = true } write . node . connect ( dest ) write . node . write = write } ) function write ( data ) { if ( write . then ) write . then = null return init ? init . then ( ( ) => push ( data ) ) : push ( data ) } write . then = ( fn ) => { write . then = null init . then ( ( ) => fn ( write ) ) } function push ( data ) { if ( write . node . closed ) throw Error ( 'Writer is closed' ) let arr = data if ( data ) { if ( data . getChannelData ) { arr = [ ] for ( let c = 0 ; c < dest . channelCount ; c ++ ) { arr [ c ] = c < data . numberOfChannels ? data . getChannelData ( c ) : new Float32Array ( ) } } else if ( typeof data [ 0 ] === 'number' ) { arr = [ ] let len = Math . floor ( data . length / dest . channelCount ) for ( let c = 0 ; c < dest . channelCount ; c ++ ) { arr [ c ] = data . subarray ? data . subarray ( c * len , c * len + len ) : new Float32Array ( data . slice ( c * len , c * len + len ) ) } } } return new Promise ( resolve => { listeners . push ( resolve ) write . node . port . postMessage ( arr ) } ) } return write }
to be sync-api compatible, it creates worklet lazily
to be sync - api compatible it creates worklet lazily
// to make API sync, first consuming awaits for init