App-MHFS

 view release on metacpan or  search on metacpan

share/public_html/static/music_worklet_inprogress/player/worklet_processor_ff.js  view on Meta::CPAN

    }
    
    static create(framecount, numberofchannels, samplerate) {
        const prereq = super.createpreq(framecount, numberofchannels); 
        return new Float32AudioRingBufferReader(prereq.rb, samplerate, prereq.messages);
    }
    
    static from(obj) {
        const rb = new RingBuffer(Float32Array, obj._rb._capacity, obj._rb._subbuffercount, obj._rb._sharedvarssab, obj._rb._sab);
        const messages = new RingBuffer(Uint32Array, obj._messages._capacity, obj._messages._subbuffercount, obj._messages._sharedvarssab, obj._messages._sab);
        return new Float32AudioRingBufferReader(rb, obj._samplerate, messages);
    }

    // (READER ONLY) reduces the amount of data to read. never move it farther than writehead 
    _setreadhead(index) {
        this._reader._setreadindex(index);
    }    
    
    // (READER ONLY)
    read(destarrs, max, destoffset) {
        return this._reader.read(destarrs, max, destoffset);       
    }

    // (READER ONLY) on the reader process messages from the writer
    processmessages() {
       while(this._msgreader.read(this._inmessage, 1) > 0) {
           if(this._inmessage[0][0] === this._MSG.SKIP) {
               this._setreadhead(this._inmessage[1][0]);
           }
       }
    }    
}

class Float32AudioRingBufferWriter extends Float32AudioRingBuffer{
    constructor(rb, samplerate, messages){        
        super(rb, samplerate, messages);
        this._msgwriter = new RingBufferWriter(messages);
        this._writer = new RingBufferWriter(rb);
        this._outmessage = [new Uint32Array(1), new Uint32Array(1)];
    }

    static create(framecount, numberofchannels, samplerate) {
        const prereq = super.createpreq(framecount, numberofchannels); 
        return new Float32AudioRingBufferWriter(prereq.rb, samplerate, prereq.messages);
    }
    
    to() {
        return {
            '_rb' : this._rb.to(),
            '_messages' : this._messages.to(),
            '_samplerate' : this._samplerate        
        };
    }

    // (WRITER ONLY)
    write(arrs, max) {
        return this._writer.write(arrs, max);
    }

    // (WRITER) ONLY)send message from the writer
    sendmessage(msgid, data) {
        this._outmessage[0][0] = msgid;
        this._outmessage[1][0] = data;
        this._msgwriter.write(this._outmessage);
    }   

    // (WRITER ONLY) empty the read buffer
    reset() {        
        this.sendmessage(this._MSG.SKIP, this._rb._writeindex());
    }
}



class MusicProcessor extends AudioWorkletProcessor {
  constructor() {
    super();
    this._initialized = false;
    this.port.onmessage = (e) => {
          console.log(e.data);
          if(e.data.message == 'init') {
                this._audioreader = Float32AudioRingBufferReader.from(e.data.audiobuffer);
                this._initialized = true;
          }
    };
  }

    process (inputs, outputs, parameters) {
      if(!this._initialized) {
        //this._lasttime = currentTime;   
        return true;
      }
      
      /*
      const newtime = currentTime;
      const delta = newtime - this._lasttime;
      if(delta > 0.00291) {
          console.error("ACTUAL XRUN " + delta);
      }
      this._lasttime = newtime;
      */

       // possibly adjust the readindex
      this._audioreader.processmessages();
      
      // fill the buffer with audio
      this._audioreader.read(outputs[0]);            
      return true
    }
  }
  
  registerProcessor('MusicProcessor', MusicProcessor);

  



( run in 0.854 second using v1.01-cache-2.11-cpan-5735350b133 )