mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 10:53:50 +02:00
Clean up PCM code
This commit is contained in:
+7
-23
@@ -1,4 +1,5 @@
|
||||
// https://github.com/dystopiancode/pcm-g711/blob/master/pcm-g711/g711.c
|
||||
|
||||
export const toUlaw = (s16: number) => {
|
||||
const MULAW_MAX = 0x1FFF;
|
||||
const MULAW_BIAS = 33;
|
||||
@@ -9,29 +10,24 @@ export const toUlaw = (s16: number) => {
|
||||
let position = 12;
|
||||
let lsb = 0;
|
||||
|
||||
// Handle negative numbers
|
||||
if (number < 0) {
|
||||
number = -number;
|
||||
sign = 0x80;
|
||||
}
|
||||
|
||||
// Add bias
|
||||
number += MULAW_BIAS;
|
||||
|
||||
// Clip to maximum
|
||||
if (number > MULAW_MAX) {
|
||||
number = MULAW_MAX;
|
||||
}
|
||||
|
||||
// Find position of first 1 in the number
|
||||
for (; ((number & mask) !== mask && position >= 5); mask >>= 1, position--) {
|
||||
// Empty loop body - all work done in condition
|
||||
while ((number & mask) !== mask && position >= 5) {
|
||||
mask >>= 1;
|
||||
position--;
|
||||
}
|
||||
|
||||
// Extract least significant bits
|
||||
lsb = (number >> (position - 4)) & 0x0f;
|
||||
|
||||
// Combine sign, position and lsb, then invert all bits
|
||||
return ~(sign | ((position - 5) << 4) | lsb) & 0xFF;
|
||||
};
|
||||
|
||||
@@ -40,19 +36,15 @@ export const fromUlaw = (u8: number) => {
|
||||
let sign = 0;
|
||||
let position = 0;
|
||||
|
||||
// Get byte and invert
|
||||
let number = ~u8;
|
||||
|
||||
// Handle sign
|
||||
if (number & 0x80) {
|
||||
number &= ~(1 << 7);
|
||||
sign = -1;
|
||||
}
|
||||
|
||||
// Calculate position
|
||||
position = ((number & 0xF0) >> 4) + 5;
|
||||
|
||||
// Reconstruct linear value
|
||||
const decoded = ((1 << position) | ((number & 0x0F) << (position - 4))
|
||||
| (1 << (position - 5))) - MULAW_BIAS;
|
||||
|
||||
@@ -68,26 +60,22 @@ export const toAlaw = (s16: number) => {
|
||||
|
||||
let number = s16;
|
||||
|
||||
// Handle negative numbers
|
||||
if (number < 0) {
|
||||
number = -number;
|
||||
sign = 0x80;
|
||||
}
|
||||
|
||||
// Clip to maximum
|
||||
if (number > ALAW_MAX) {
|
||||
number = ALAW_MAX;
|
||||
}
|
||||
|
||||
// Find position of first 1 in the number
|
||||
for (; ((number & mask) !== mask && position >= 5); mask >>= 1, position--) {
|
||||
// Empty loop body - all work done in condition
|
||||
while ((number & mask) !== mask && position >= 5) {
|
||||
mask >>= 1;
|
||||
position--;
|
||||
}
|
||||
|
||||
// Extract least significant bits
|
||||
lsb = (number >> ((position === 4) ? 1 : (position - 4))) & 0x0f;
|
||||
|
||||
// Combine sign, position and lsb, then XOR with 0x55
|
||||
return (sign | ((position - 4) << 4) | lsb) ^ 0x55;
|
||||
};
|
||||
|
||||
@@ -95,19 +83,15 @@ export const fromAlaw = (u8: number) => {
|
||||
let sign = 0x00;
|
||||
let position = 0;
|
||||
|
||||
// Get byte and XOR with 0x55
|
||||
let number = u8 ^ 0x55;
|
||||
|
||||
// Handle sign
|
||||
if (number & 0x80) {
|
||||
number &= ~(1 << 7);
|
||||
sign = -1;
|
||||
}
|
||||
|
||||
// Calculate position
|
||||
position = ((number & 0xF0) >> 4) + 4;
|
||||
|
||||
// Reconstruct linear value
|
||||
let decoded = 0;
|
||||
if (position !== 4) {
|
||||
decoded = ((1 << position) | ((number & 0x0F) << (position - 4))
|
||||
|
||||
Reference in New Issue
Block a user