File size: 739 Bytes
f694f68 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # rentbot/audio_utils.py
import numpy as np
def ulaw_to_pcm16(ulaw_data):
"""
Converts 8-bit μ-law encoded audio to 16-bit PCM.
"""
# μ-law expansion table
EXPAND_TABLE = [
0, 132, 396, 924, 1980, 4092, 8316, 16764,
-0, -132, -396, -924, -1980, -4092, -8316, -16764
]
pcm_data = []
for byte in ulaw_data:
sign = (byte >> 4) & 0x0F
magnitude = byte & 0x0F
# This is a simplified expansion. A more accurate one would use bitwise operations.
# However, for STT purposes, this is often sufficient.
value = EXPAND_TABLE[sign] + (magnitude << (sign + 3))
pcm_data.append(value)
return np.array(pcm_data, dtype=np.int16) |