App-MHFS

 view release on metacpan or  search on metacpan

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

        }

        
        // commit that more data is available to read
        this._setwriteindex(writeindex);       
    }

    /*
    write_from_rb_reader(srcrb, count) {
        const srccount = srcrb.getcount();
        if(srccount <= count) throw("Not enough data to read");
        const destcount = this._getcount();
        if(destcount <= count) throw("not enough room to write");
        if(this._rb._subbuffercount !== src._rb._subbuffercount) throw("different subbuffer count between dest and src");
        
        // copy the first half
        let destwi = this._rb._writeindex();
        const writeleft = this._rb._size - destwi;
        const canwrite = Math.min(count, writeleft);
        src.read(this._rb._buffer, canwrite, destwi);       
        count -= canwrite;
        destwi = (destwi+canwrite) % this._rb._size;
    
        // copy the second half if needed
        if(count > 0) {            
            destwi = 0;
            src.read(this._rb._buffer, count, destwi);
            destwi += count;
        }              
       
        // commit that more data is available to read
        this._setwriteindex(destwi); 
    }
    */
}

class Float32AudioRingBuffer {       
    
    constructor(rb, samplerate, messagerb){        
        this._rb = rb;
        this._samplerate = samplerate;
        this._messages = messagerb;
        this._MSG = {
            'SKIP' : 1
        };
    }

    getcount() {
        return this._rb.getcount();
    }
    
    gettime() {
        const count = this.getcount();
        return  count / this._samplerate;         
    }

    getspace() {
        return this._rb.getspace();
    }
    
    static createpreq(framecount, numberofchannels) {
        return {
            'rb' : RingBuffer.create(Float32Array, framecount, numberofchannels),
            'messages' : RingBuffer.create(Uint32Array, 4095, 2)
        };
    }
}

class Float32AudioRingBufferReader extends Float32AudioRingBuffer  {
    constructor(rb, samplerate, messages){
        super(rb, samplerate, messages);
        this._msgreader =  new RingBufferReader(messages);               
        this._reader = new RingBufferReader(rb);        
        this._inmessage = [new Uint32Array(1), new Uint32Array(1)];
    }
    
    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());
    }
}

export {RingBuffer, Float32AudioRingBufferReader, Float32AudioRingBufferWriter};



( run in 1.299 second using v1.01-cache-2.11-cpan-e1769b4cff6 )