Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stagas/mono-worklet

mono lang AudioWorkletNode
https://github.com/stagas/mono-worklet

Last synced: 13 days ago
JSON representation

mono lang AudioWorkletNode

Awesome Lists containing this project

README

        


mono-worklet

mono lang AudioWorkletNode



npm i mono-worklet

pnpm add mono-worklet

yarn add mono-worklet

## Examples

# web

    # view source example/web.tsx

    ```tsx
    /** @jsxImportSource sigl */
    import $ from 'sigl'

    import { CodeEditElement } from 'code-edit'
    import { SchedulerEventGroupNode, SchedulerNode } from 'scheduler-node'

    import { MonoNode } from 'mono-worklet' // <- we use the dist/ files for the worklet to work

    const sampleRate = 44100

    const code = `\
    #:1,2;
    write_note(x)=(
    #=(t,note_to_hz(x));
    0
    );
    midi_in(op=0,x=0,y=0)=(
    op==144 && write_note(x);drop;
    0
    );
    f()=((nt,y)=#(-1);saw(y)*env(nt))

    `
    // a=300.0;
    // play(x=300.0)=(
    // a=x;
    // a
    // );

    // midi_in(x=1,y=1,z=1)=
    // (a=note_to_hz(y);0);

    // f()=sine(a)
    // `

    const ctx = new AudioContext({ sampleRate, latencyHint: 0.06 })

    const main = async () => {
    const mainSchedulerNode = await SchedulerNode.create(ctx)
    const midiEvent = new MIDIMessageEvent('midimessage', {
    data: new Uint8Array([0x90, 40, 127]),
    }) as WebMidi.MIDIMessageEvent
    midiEvent.receivedTime = 0
    const schedulerGroupNode = new SchedulerEventGroupNode(mainSchedulerNode)
    schedulerGroupNode.eventGroup.replaceAllWithNotes([[0, 40, 127, .1]])
    schedulerGroupNode.eventGroup.loopEnd = 1
    schedulerGroupNode.eventGroup.loop = true

    const monoNode = await MonoNode.create(ctx, {
    numberOfInputs: 0,
    numberOfOutputs: 1,
    processorOptions: {
    metrics: 0,
    },
    })
    console.log(monoNode)

    schedulerGroupNode.connect(monoNode)

    // monoNode.setCode(`f()=sin(pi2*440.0*t)`)
    await monoNode.setCode(code)
    monoNode.connect(ctx.destination)
    mainSchedulerNode.start()

    // setInterval(() => {
    // const midiEvent = new MIDIMessageEvent('midimessage', { data: new Uint8Array([0x90, 40, 127]) })
    // midiEvent.receivedTime = ctx.currentTime * 1000
    // monoNode.processMidiEvent(midiEvent)
    // }, 1000)

    // setTimeout(() => {
    // monoProcessor.setCode(`f(x[1mono-worklet100]=52.4, y[1mono-worklet400]=158.1,
    // z[1mono-worklet500]=56, c[40mono-worklet400]=147.7,
    // r[0.001mono-worklet4]=1.32, p[0.1mono-worklet100]=24.5)=
    // lp(sin(pi2*(x+(exp((-t%0.5)*z)*y))*(t%0.5))
    // * exp(-t%0.5*p), c, r)
    // `)
    // }, 2000)

    const CodeEdit = $.element(CodeEditElement)

    $.render(
    ,
    document.body
    )
    }

    main()
    ```


## API

# MonoNode

# schedulerTarget

    SchedulerTarget

# state  =  'running'

    "disabled" | "suspended" | "running"

# vmParams # vmParamsMap  =  ... # worklet # registeredContexts  =  ...

    Set<BaseAudioContext>

# createVM()


    createVM()  =>



      Promise<undefined | {

      # inputChannels  =  res.inputChannels


        number

      # outputChannels  =  res.outputChannels

        number

      # params  =  ...

      }>

# disable()


    disable()  =>


      void
# init()


    init()  =>



      Promise<void>
# processMidiEvent(midiEvent)

    # midiEvent


      MIDIMessageEvent


    processMidiEvent(midiEvent)  =>


      void
# restartMem()


    restartMem()  =>


      void
# resume()


    resume()  =>


      void
# setClockBuffer(clockBuffer)

    # clockBuffer


      Float64Array


    setClockBuffer(clockBuffer)  =>


      void
# setCode(code, reset)

    # code


      string

    # reset  =  false

      boolean


    setCode(code, reset)  =>



      Promise<{

      # inputChannels


        number

      # outputChannels

        number

      }>
# setSampleBuffer(index, buffer, range)

    # index


      number

    # buffer

      Float32Array []

    # range

      [ number, number ]


    setSampleBuffer(index, buffer, range)  =>



      Promise<void>
# setSampleBufferRange(index, range)

    # index


      number

    # range

      [ number, number ]


    setSampleBufferRange(index, range)  =>



      Promise<void>
# suspend()


    suspend()  =>


      void
# test(frame, length, params)

    # frame


      number

    # length

      number

    # params

      any []


    test(frame, length, params)  =>



      Promise<Float32Array>
# create(context, options) # register(context)

    # context


      BaseAudioContext


    register(context)  =>



      Promise<void>
# MonoParam

    # constructor(data)

    # code

      string

    # defaultValue

      number

    # fnId

      Token & string

    # id

      Token & string

    # maxValue

      number

    # minValue

      number

    # name

      string

    # normalValue

      number

    # paramId

      Token & string

    # scaleValue

      number

    # source

      {


      # arg


        string

      # default

        string

      # id

        string

      # range

        string

      }

# sourceIndex

    number

# normalize(value)

    # value


      number


    normalize(value)  =>


      number
# scale(normal)

    # normal


      number


    scale(normal)  =>


      number
# MonoNodeOptions

    AudioWorkletNodeOptions & {


    # channelCount


      number

    # numberOfInputs

      number

    # numberOfOutputs

      number

    # outputChannelCount

      number []

    }

## Credits
- [alice-bob](https://npmjs.org/package/alice-bob) by [stagas](https://github.com/stagas) – transport agnostic strongly typed duplex rpc interfaces
- [better-console-time](https://npmjs.org/package/better-console-time) by [stagas](https://github.com/stagas) – console.time that optionally warns when above a threshold.
- [monolang](https://npmjs.org/package/monolang) by [stagas](https://github.com/stagas) – mono is a low level language for audio expressions that compiles to wasm
- [scheduler-node](https://npmjs.org/package/scheduler-node) by [stagas](https://github.com/stagas) – Sample perfect Audioworklet MIDI Scheduler Node

## Contributing

[Fork](https://github.com/stagas/mono-worklet/fork) or [edit](https://github.dev/stagas/mono-worklet) and submit a PR.

All contributions are welcome!

## License

MIT © 2023 [stagas](https://github.com/stagas)