使用 scipy.wavfile 写入 numpy.ndarray 修改 .wav 音频文件没有属性 append

问题描述

从上一个问题开始工作。主要目标是读取 .wav 文件,特别是跳过 RIFF 和其他容器,并严格关注 .wav 文件数据部分内容。运行示例遇到以下错误

AttributeError: 'numpy.ndarray' object has no attribute 'append'

  1. 源自于:new_data = data.append(np.zeros(2 * sr))

但是,在更改以下文件以尝试解决此问题(例如 new_data.astype(np.int16))时,仍然遇到问题。

# ''' From Imports ''' #
from scipy.io.wavfile import write
# ''' Imports ''' #
import numpy as np

# ''' Filename IN '''
fn = 'example.wav'
# ''' Byte Length ''' #
bl = np.fromfile(fn,dtype=np.int32,count=1,offset=40)[0]
# ''' Offset ''' #
data = np.fromfile(fn,dtype=np.int16,count=bl // np.dtype(np.int16).itemsize,offset=44)
# ''' Sample Rate ''' #
sr = np.fromfile(fn,offset=24)[0]
# ''' New .WAV ''' #
with open('out.wav','a') as fout:
    # ''' Appending Two Seconds of 0.0's
    # >> AttributeError: 'numpy.ndarray' object has no attribute 'append'
    # ''' #
    new_data = data.append(np.zeros(2 * sr))
    write(fout,sr,new_data)

# ''' Close File ''' #
fout.close()
# ''' End ''' #

一种可能的解决方法是完全替换数据,这样是否正确?:

通过使用:write(fout,np.zeros(2 * sr).astype(np.int16))

有什么方法可以修复此解决方案?参考问题:https://stackoverflow.com/a/67455735/8813179

解决方法

ndarray 没有方法追加,使用 numpy.append(arr1,arr2)

numpy.append