0

从上一个问题开始工作。主要目标是读取 .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, 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))

解决此解决方案的任何方法?参考问题:https ://stackoverflow.com/a/67455735/8813179

4

1 回答 1

1

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

numpy.append

于 2021-05-13T02:29:00.960 回答