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

Modifying .wav audio file using scipy.wavfile write numpy.ndarray has no attribute append

从上一个问题开始工作。主要的 objective 正在读取一个 .wav 文件并特别跳过 RIFF 和其他容器并严格关注 data 部分.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, dtype=np.int32, count=1, 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, sr, np.zeros(2 * sr).astype(np.int16)).

有什么方法可以解决这个问题?参考问题:

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

numpy.append