import { IRatio } from '../type' ; export type StringEncoding = 'ascii' | 'utf8' | 'utf16le' | 'ucs2' | 'base64url' | 'latin1' | 'hex' ; export function getBit ( buf , off , bit ) { return ( buf [ off ] & ( 1 << bit ) ) !== 0 ; } param uint8Array param start param end param encoding return export function findZero ( uint8Array , start , end , encoding ? ) { let i = start ; if ( encoding === 'utf16le' ) { while ( uint8Array [ i ] !== 0 || uint8Array [ i + 1 ] !== 0 ) { if ( i >= end ) return end ; i += 2 ; } return i ; } else { while ( uint8Array [ i ] !== 0 ) { if ( i >= end ) return end ; i ++ ; } return i ; } } export function trimRightNull ( x ) { const pos0 = x . indexOf ( '\0' ) ; return pos0 === - 1 ? x : x . substr ( 0 , pos0 ) ; } function swapBytes < T extends Uint8Array > ( uint8Array ) { const l = uint8Array . length ; if ( ( l & 1 ) !== 0 ) throw new Error ( 'Buffer length must be even' ) ; for ( let i = 0 ; i < l ; i += 2 ) { const a = uint8Array [ i ] ; uint8Array [ i ] = uint8Array [ i + 1 ] ; uint8Array [ i + 1 ] = a ; } return uint8Array ; } param buffer param encoding return { string } export function decodeString ( buffer , encoding ) { let offset = 0 ; if ( buffer [ 0 ] === 0xFF && buffer [ 1 ] === 0xFE ) { if ( encoding === 'utf16le' ) { offset = 2 ; } else if ( buffer [ 2 ] === 0xFE && buffer [ 3 ] === 0xFF ) { offset = 2 ; } } else if ( encoding === 'utf16le' && buffer [ 0 ] === 0xFE && buffer [ 1 ] === 0xFF ) { return decodeString ( swapBytes ( buffer ) , encoding ) ; } return buffer . toString ( encoding , offset ) ; } export function stripNulls ( str ) { str = str . replace ( /^\x00+/g , '' ) ; str = str . replace ( /\x00+$/g , '' ) ; return str ; } param buf param byteOffset param bitOffset param len return { number } export function getBitAllignedNumber ( buf , byteOffset , bitOffset , len ) { const byteOff = byteOffset + ~ ~ ( bitOffset / 8 ) ; const bitOff = bitOffset % 8 ; let value = buf [ byteOff ] ; value &= 0xff >> bitOff ; const bitsRead = 8 - bitOff ; const bitsLeft = len - bitsRead ; if ( bitsLeft < 0 ) { value >>= ( 8 - bitOff - len ) ; } else if ( bitsLeft > 0 ) { value <<= bitsLeft ; value |= getBitAllignedNumber ( buf , byteOffset , bitOffset + bitsRead , bitsLeft ) ; } return value ; } param buf param byteOffset param bitOffset return { number } export function isBitSet ( buf , byteOffset , bitOffset ) { return getBitAllignedNumber ( buf , byteOffset , bitOffset , 1 ) === 1 ; } export function a2hex ( str ) { const arr = [ ] ; for ( let i = 0 , l = str . length ; i < l ; i ++ ) { const hex = Number ( str . charCodeAt ( i ) ) . toString ( 16 ) ; arr . push ( hex . length === 1 ? '0' + hex : hex ) ; } return arr . join ( ' ' ) ; } /**
 * Convert power ratio to DB
 * ratio: [0..1]
 */ export function ratioToDb ( ratio ) { return 10 * Math . log10 ( ratio ) ; } /**
 * Convert dB to ratio
 * db Decibels
 */ export function dbToRatio ( dB ) { return Math . pow ( 10 , dB / 10 ) ; } param value export function toRatio ( value ) { const ps = value . split ( ' ' ) . map ( p => p . trim ( ) . toLowerCase ( ) ) ; if ( ps . length >= 1 ) { const v = parseFloat ( ps [ 0 ] ) ; if ( ps . length === 2 && ps [ 1 ] === 'db' ) { return { dB : v , ratio : dbToRatio ( v ) } ; } else { return { dB : ratioToDb ( v ) , ratio : v } ; } } }