acl-js/src-js/encoder.js
// Compresses a TrackArray into a CompressedTracks instance.
Compresses a TrackArray into a CompressedTracks instance.
Compresses a TrackArray into a CompressedTracks instance .
compress(tracks) {
    if (!wasmModule) {
      throw new Error('WASM module not ready')
    }
    if (!tracks || !(tracks instanceof TrackArray) || !tracks.isValid()) {
      throw new TypeError("'tracks' must be a valid TrackArray")
    }
    const rawDataBufferSize = tracks._rawData.byteLength + (64 * 1024)
    const metadataBufferSize = tracks._metadata.byteLength + 64
    const totalBufferSize = rawDataBufferSize + metadataBufferSize
    const buffer = wasmInstance.exports.malloc(totalBufferSize)
    const metadataBuffer = buffer
    const rawDataBuffer = buffer + metadataBufferSize   
    wasmHeap.set(new Uint8Array(tracks._metadata.buffer), metadataBuffer)
    wasmHeap.set(new Uint8Array(tracks._rawData.buffer), rawDataBuffer)
    const compressedTracksSize = wasmInstance.exports.compress(metadataBuffer, metadataBufferSize, rawDataBuffer, rawDataBufferSize)
    let compressedTracks = null
    if (compressedTracksSize <= 0) {
      wasmInstance.exports.free(buffer)
      throw new Error(`Failed to compress: ${compressedTracksSize}`)
    }
    else {
      const compressedBuffer = new Uint8Array(compressedTracksSize)
      compressedBuffer.set(wasmHeap.subarray(rawDataBuffer, rawDataBuffer + compressedTracksSize))
      const errorDataSize = 8 * 3 
      const errorData = new Float64Array(wasmHeap.buffer, metadataBuffer, 3)
      const trackError = new TrackError(errorData[0], errorData[1], errorData[2])
      compressedTracks = new CompressedTracks(compressedBuffer, trackError)
    }
    wasmInstance.exports.free(buffer)
    return compressedTracks
  }
compress ( tracks ) { if ( ! wasmModule ) { throw new Error ( 'WASM module not ready' ) } if ( ! tracks || ! ( tracks instanceof TrackArray ) || ! tracks . isValid ( ) ) { throw new TypeError ( "'tracks' must be a valid TrackArray" ) } const rawDataBufferSize = tracks . _rawData . byteLength + ( 64 * 1024 ) const metadataBufferSize = tracks . _metadata . byteLength + 64 const totalBufferSize = rawDataBufferSize + metadataBufferSize const buffer = wasmInstance . exports . malloc ( totalBufferSize ) const metadataBuffer = buffer const rawDataBuffer = buffer + metadataBufferSize wasmHeap . set ( new Uint8Array ( tracks . _metadata . buffer ) , metadataBuffer ) wasmHeap . set ( new Uint8Array ( tracks . _rawData . buffer ) , rawDataBuffer ) const compressedTracksSize = wasmInstance . exports . compress ( metadataBuffer , metadataBufferSize , rawDataBuffer , rawDataBufferSize ) let compressedTracks = null if ( compressedTracksSize <= 0 ) { wasmInstance . exports . free ( buffer ) throw new Error ( ` ${ compressedTracksSize } ` ) } else { const compressedBuffer = new Uint8Array ( compressedTracksSize ) compressedBuffer . set ( wasmHeap . subarray ( rawDataBuffer , rawDataBuffer + compressedTracksSize ) ) const errorDataSize = 8 * 3 const errorData = new Float64Array ( wasmHeap . buffer , metadataBuffer , 3 ) const trackError = new TrackError ( errorData [ 0 ] , errorData [ 1 ] , errorData [ 2 ] ) compressedTracks = new CompressedTracks ( compressedBuffer , trackError ) } wasmInstance . exports . free ( buffer ) return compressedTracks }
Compresses a TrackArray into a CompressedTracks instance.
Compresses a TrackArray into a CompressedTracks instance .
// Add a bit of padding to account for our header, etc // We'll use the raw data buffer for our output as well // Allocate a single buffer for efficiency // Alignment is fine, both double arrays //console.log(`Writing metadata to WASM heap offset ${metadataBuffer}, ${metadataBufferSize} bytes`) //console.log(`Writing raw data to WASM heap offset ${rawDataBuffer}, ${rawDataBufferSize} bytes`) // Our compressed data will overwrite our raw data with the new size returned //console.log(`Compression result: ${compressedTracksSize}`) //console.log(`Reading compressed clip from WASM heap offset ${rawDataBuffer}, ${compressedTracksSize} bytes`) // 3x double values