rentbot / audio_utils.py
mgbam's picture
Create audio_utils.py
f694f68 verified
raw
history blame contribute delete
739 Bytes
# 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)