代码之家  ›  专栏  ›  技术社区  ›  ABC

使用scipy.wavfile write numpy.ndarray修改.wav音频文件没有属性append[duplicate]

  •  0
  • ABC  · 技术社区  · 5 年前

    里夫 数据 部分 文件内容。正在运行的示例遇到以下错误:

    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

    1 回复  |  直到 5 年前
        1
  •  1
  •   Phani Pavan    5 年前