### Install alawmulaw with npm Source: https://rochars.github.io/alawmulaw/index.html Install the library using npm for use in Node.js projects. ```bash npm install alawmulaw ``` -------------------------------- ### Node.js Usage of alawmulaw Source: https://rochars.github.io/alawmulaw/index.html Require the 'alawmulaw' module in Node.js. This example demonstrates encoding an array of PCM samples into A-Law format. Only 16-bit samples are supported. ```javascript const alawmulaw = require('alawmulaw'); // Encode all the samples in a file // Only 16-bit samples are supported let aLawSamples = alawmulaw.alaw.encode(pcmSamples); ``` -------------------------------- ### Browser Usage of alawmulaw Source: https://rochars.github.io/alawmulaw/index.html Include the alawmulaw.js file or use a CDN to access the library in the browser. Examples show encoding and decoding of samples and arrays for both A-Law and mu-Law. ```html ``` ```html ``` ```html ``` -------------------------------- ### encodeSample Source: https://rochars.github.io/alawmulaw/module-alawmulaw_alaw.html Encodes a single 16-bit linear PCM sample into an 8-bit A-Law sample. ```APIDOC ## encodeSample(sample) ### Description Encodes a 16-bit linear PCM sample as 8-bit A-Law. ### Method static encodeSample ### Parameters #### Path Parameters - **sample** (number) - Required - A 16-bit PCM sample ### Returns: Type number ``` -------------------------------- ### encodeSample Source: https://rochars.github.io/alawmulaw/module-alawmulaw_mulaw.html Encodes a single 16-bit linear PCM sample into an 8-bit mu-Law sample. ```APIDOC ## encodeSample(sample) ### Description Encodes a 16-bit linear PCM sample as 8-bit mu-Law. ### Method `encodeSample(sample)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **sample** (number) - Required - A 16-bit PCM sample ### Request Example ```json { "example": "sample" } ``` ### Response #### Success Response (200) - **number** (number) - The 8-bit mu-Law encoded sample #### Response Example ```json { "example": "number" } ``` ``` -------------------------------- ### decodeSample Source: https://rochars.github.io/alawmulaw/module-alawmulaw_mulaw.html Decodes a single 8-bit mu-Law sample into a 16-bit PCM sample. ```APIDOC ## decodeSample(muLawSample) ### Description Decodes an 8-bit mu-Law sample as 16-bit PCM. ### Method `decodeSample(muLawSample)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **muLawSample** (number) - Required - The 8-bit mu-Law sample ### Request Example ```json { "example": "muLawSample" } ``` ### Response #### Success Response (200) - **number** (number) - The 16-bit PCM decoded sample #### Response Example ```json { "example": "number" } ``` ``` -------------------------------- ### Encode 16-bit PCM to 8-bit mu-Law sample Source: https://rochars.github.io/alawmulaw/lib_mulaw.js.html Encodes a single 16-bit linear PCM sample into an 8-bit mu-Law sample. Handles sign, bias, clipping, and conversion using lookup tables. ```javascript export function encodeSample(sample) { /** @type {number} */ let sign; /** @type {number} */ let exponent; /** @type {number} */ let mantissa; /** @type {number} */ let muLawSample; /** get the sample into sign-magnitude **/ sign = (sample >> 8) & 0x80; if (sign != 0) sample = -sample; /** convert from 16 bit linear to ulaw **/ sample = sample + BIAS; if (sample > CLIP) sample = CLIP; exponent = encodeTable[(sample>>7) & 0xFF]; mantissa = (sample >> (exponent+3)) & 0x0F; muLawSample = ~(sign | (exponent << 4) | mantissa); /** return the result **/ return muLawSample; } ``` -------------------------------- ### decodeSample Source: https://rochars.github.io/alawmulaw/lib_mulaw.js.html Decodes a single 8-bit mu-Law sample into a 16-bit linear PCM sample. ```APIDOC ## decodeSample ### Description Decodes an 8-bit mu-Law sample as 16-bit PCM. ### Method export function decodeSample(muLawSample) ### Parameters * **muLawSample** (number) - The 8-bit mu-Law sample ### Returns * (number) - The decoded 16-bit PCM sample ``` -------------------------------- ### decodeSample Source: https://rochars.github.io/alawmulaw/module-alawmulaw_alaw.html Decodes a single 8-bit A-Law sample into a 16-bit linear PCM sample. ```APIDOC ## decodeSample(aLawSample) ### Description Decode a 8-bit A-Law sample as 16-bit PCM. ### Method static decodeSample ### Parameters #### Path Parameters - **aLawSample** (number) - Required - The 8-bit A-Law sample ### Returns: Type number ``` -------------------------------- ### decode Source: https://rochars.github.io/alawmulaw/module-alawmulaw_mulaw.html Decodes an array of 8-bit mu-Law samples into an array of 16-bit PCM samples. ```APIDOC ## decode(samples) ### Description Decodes 8-bit mu-Law samples into 16-bit PCM samples. ### Method `decode(samples)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **samples** (Uint8Array) - Required - A array of 8-bit mu-Law samples. ### Request Example ```json { "example": "samples" } ``` ### Response #### Success Response (200) - **Int16Array** (Int16Array) - A array of 16-bit PCM samples. #### Response Example ```json { "example": "Int16Array" } ``` ``` -------------------------------- ### Batch encode 16-bit PCM to 8-bit mu-Law Source: https://rochars.github.io/alawmulaw/lib_mulaw.js.html Encodes an entire array of 16-bit linear PCM samples into an array of 8-bit mu-Law samples using the `encodeSample` function for each element. ```javascript export function encode(samples) { /** @type {!Uint8Array} */ let muLawSamples = new Uint8Array(samples.length); for (let i=0; i> 8) & 0x80; if (!sign) { sample = sample * -1; } if (sample > 32635) { sample = 32635; } if (sample >= 256) { /** @type {number} */ let exponent = LOG_TABLE[(sample >> 8) & 0x7F]; /** @type {number} */ let mantissa = (sample >> (exponent + 3) ) & 0x0F; compandedValue = ((exponent << 4) | mantissa); } else { compandedValue = sample >> 4; } return compandedValue ^ (sign ^ 0x55); } ``` -------------------------------- ### decode Source: https://rochars.github.io/alawmulaw/lib_mulaw.js.html Decodes an array of 8-bit mu-Law samples into an array of 16-bit linear PCM samples. ```APIDOC ## decode ### Description Decodes 8-bit mu-Law samples into 16-bit PCM samples. ### Method export function decode(samples) ### Parameters * **samples** (!Uint8Array) - A array of 8-bit mu-Law samples. ### Returns * (!Int16Array) - An array of 16-bit PCM samples. ``` -------------------------------- ### decode Source: https://rochars.github.io/alawmulaw/module-alawmulaw_alaw.html Decodes an array of 8-bit A-Law samples into an array of 16-bit linear PCM samples. ```APIDOC ## decode(samples) ### Description Decode 8-bit A-Law samples into 16-bit linear PCM samples. ### Method static decode ### Parameters #### Path Parameters - **samples** (Uint8Array) - Required - A array of 8-bit A-Law samples. ### Returns: Type Int16Array ``` -------------------------------- ### Decode 8-bit mu-Law to 16-bit PCM sample Source: https://rochars.github.io/alawmulaw/lib_mulaw.js.html Decodes a single 8-bit mu-Law sample back into a 16-bit linear PCM sample. Reverses the encoding process using lookup tables and sign/exponent/mantissa extraction. ```javascript export function decodeSample(muLawSample) { /** @type {number} */ let sign; /** @type {number} */ let exponent; /** @type {number} */ let mantissa; /** @type {number} */ let sample; muLawSample = ~muLawSample; sign = (muLawSample & 0x80); exponent = (muLawSample >> 4) & 0x07; mantissa = muLawSample & 0x0F; sample = decodeTable[exponent] + (mantissa << (exponent+3)); if (sign != 0) sample = -sample; return sample; } ``` -------------------------------- ### Batch decode 8-bit mu-Law to 16-bit PCM Source: https://rochars.github.io/alawmulaw/lib_mulaw.js.html Decodes an entire array of 8-bit mu-Law samples into an array of 16-bit linear PCM samples using the `decodeSample` function for each element. ```javascript export function decode(samples) { ``` -------------------------------- ### encode Source: https://rochars.github.io/alawmulaw/module-alawmulaw_alaw.html Encodes an array of 16-bit linear PCM samples into an array of 8-bit A-Law samples. ```APIDOC ## encode(samples) ### Description Encode 16-bit linear PCM samples as 8-bit A-Law samples. ### Method static encode ### Parameters #### Path Parameters - **samples** (Int16Array) - Required - A array of 16-bit PCM samples. ### Returns: Type Uint8Array ``` -------------------------------- ### Encode Array of 16-bit PCM Samples to A-Law Source: https://rochars.github.io/alawmulaw/lib_alaw.js.html Encodes an entire array of 16-bit linear PCM samples into an array of 8-bit A-Law samples. Iterates through each sample and applies the `encodeSample` function. ```javascript export function encode(samples) { /** @type {!Uint8Array} */ let aLawSamples = new Uint8Array(samples.length); for (let i=0; i> 4) + 4; /** @type {number} */ let decoded = 0; if (position != 4) { decoded = ((1 << position) | ((aLawSample & 0x0F) << (position - 4)) | (1 << (position - 5))); } else { decoded = (aLawSample << 1)|1; } decoded = (sign === 0) ? (decoded) : (-decoded); return (decoded * 8) * -1; } ``` -------------------------------- ### Decode Mu-law Samples Source: https://rochars.github.io/alawmulaw/lib_mulaw.js.html Decodes an array of Mu-law encoded samples into 16-bit PCM samples. Requires a `decodeSample` function to be defined elsewhere. ```javascript /** @type {!Int16Array} */ let pcmSamples = new Int16Array(samples.length); for (let i=0; i