⌈⌋ ⎇ branch:  Bitrhythm


Artifact Content

Artifact 852f709389f0d73719a36d7ea42f7b82e76af8eaa53842e280dbea0a59793ea1:

  • File public/sampler.js — part of check-in [941581eece] at 2022-02-15 23:20:56 on branch trunk — Cleanup post presentation Worklet code (user: dev size: 1078)


class Sampler extends AudioWorkletProcessor {
  files = []
  readIdx = {}
  loopStartIdx = []

  constructor(options) {
    super()
    this.port.onmessage = ({ data }) => {
      if (data.init) {
        this.files = data.init
        this.loopStartIdx = this.files.map(function (f) {
            return 0
        })
      }
      else if (data.noteOn) {
            this.readIdx[data.sample] = this.loopStartIdx[data.sample]
      }
      else if (data.noteOff) {
            delete this.readIdx[data.sample];
      }
    };
  }

  process(inputs, outputs) {
    var outLeft = outputs[0][0]
    var outRight = outputs[0][1]

    Object.keys(this.readIdx).map((sample) => {
        for (let i=0; i < outLeft.length; i++, this.readIdx[sample]++) {
            if (this.readIdx[sample] < this.files[sample].pcmLeft.length) {
                outLeft[i] += this.files[sample].pcmLeft[this.readIdx[sample]]
                outRight[i] += this.files[sample].pcmRight[this.readIdx[sample]]
            }
        }
    })

    return true
  }
}

registerProcessor('sampler', Sampler)