Repository navigation
fix: encode microphone recordings as valid PCM WAV - #3900
HaokaiDing wants to merge 1 commit into
Conversation
|
AI-assisted independent offline byte-level corroboration at
The core float/int8 correction is corroborated. Please add an explicit non-finite input policy (reject, replace or documented handling) and a test, rather than relying on cast behavior; clipping alone leaves NaN unresolved. The out-of-range finite control is also useful beyond the existing normalized fixtures. Scope: isolated helper and byte round-trip only; no recording, playback, sounddevice device access or native callback proof. Python3.14/Pydantic2.13.5; NumPy2.5.3 added to an isolated diagnostic directory, other dependencies shared. Archived sources are not a historical locked environment. Network denied during checks; no API or model activity. Standalone reproducerimport json,wave,warnings,numpy as np
from openai.helpers.microphone import Microphone
rows=[]
for name,values,dtype in [('float_edges',[-2,-1,0,1,2],np.float32),('float_nonfinite',[np.nan,np.inf,-np.inf,0],np.float32),('signed8',[-128,0,127],np.int8),('unsigned8',[0,128,255],np.uint8),('signed16',[-32768,0,32767],np.int16)]:
data=np.array(values,dtype=dtype);m=Microphone(channels=1,dtype=dtype)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always');name_,buffer,media=m._ndarray_to_wav(data)
with wave.open(buffer,'rb') as reader:
width=reader.getsampwidth();samples=np.frombuffer(reader.readframes(reader.getnframes()),dtype={1:'u1',2:'<i2',4:'<i4'}[width]).tolist();rows.append({'case':name,'sample_width':width,'decoded_pcm':samples,'warnings':[str(x.message) for x in w]})
print(json.dumps(rows)) |
Changes being requested
Microphone.record()writes every capture dtype directly into an integer PCM WAV file. Forfloat32, the IEEE float bytes are consequently interpreted as signed integers. Forint8, silence (0) is interpreted as the most negative sample because 8-bit WAV PCM is unsigned.Convert float32 recordings to 16-bit PCM and offset signed 8-bit recordings to unsigned PCM before writing the WAV, using the converted sample width. Preserve the existing int16, int32, and uint8 encodings and the original dtype/data when
return_ndarray=True.Additional context & links
Regression tests drive the public
record()method through a mocked input device and decode the resulting WAV. They cover all five supported capture dtypes, stereo metadata, and ndarray returns. The float32 and int8 WAV cases fail before the fix; the other eight cases pass.Validation on the latest main (3.16.1):
.venv/bin/python -m pytest -n 0 tests/lib/test_microphone.py tests/lib/test_audio.py -q: 14 passed.Format references: sounddevice sample types, WAV PCM sample ranges.