![](/img/trans.png)
[英]TypeError: Value passed to parameter 'input' has DataType bool not in list of allowed values: float32, float64, int32, uint8, int16, int8
[英]convert numpy int16 audio array to float32
我有原始二進制int16數據,正在使用將其轉換為numpy數組
audio = np.fromstring(raw_data, dtype=np.int16)
數據是音頻數據。 當我將數據轉換為float32時,音頻會失真:
audio = audio.astype(np.float32, order='C')
我將音頻保存到磁盤以使用SoundFile進行收聽:
soundfile.write('out.wav', audio, sample_rate)
如果我不執行astype
操作而將音頻直接寫入磁盤,則不會出現失真(即);
# no distortion
audio = np.fromstring(raw_data, dtype=np.int16)
soundfile.write('out.wav', audio, sample_rate)
# distortion
audio = np.fromstring(raw_data, dtype=np.int16)
audio = audio.astype(np.float32, order='C')
soundfile.write('out.wav', audio, sample_rate)
在這里轉換數據類型的正確方法是什么?
按照慣例,浮點音頻數據被歸一化為[-1.0,1.0]的范圍,您可以通過縮放來實現:
audio = audio.astype(np.float32, order='C') / 32768.0
這可能為您解決了該問題,但是您需要確保soundfile.write
寫入一個wav標頭,該標頭指示float32。 它可以根據數組的dtype自動執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.