【问题标题】:Append data to a wave soundfile without loading its currrent content将数据附加到波形声音文件而不加载其当前内容
【发布时间】:2019-05-09 12:01:44
【问题描述】:

我正在尝试在不加载其内容的情况下将数据附加到声音文件(因为它可能有千兆字节的数据),我目前正在使用 pysoundfile 库,我已经找到了一种用于 wave64 的方法,但是在 wav 中,由于某种原因它会引发错误。

根据 pysoundfile 文档,当使用文件描述符打开 SoundFile 时,它​​应该在不截断的情况下写入,这就是我目前正在做的事情

    fd = open('foo.wav',mode='ab')
    with sf.SoundFile(fd, mode = 'w', samplerate = self._samplerate,channels = self._channels, format = 'wav') as wfile:
        wfile.seek(0,sf.SEEK_END)
        wfile.write(self._samples)
        wfile.close()
    fd.close()

当我使用 wave 文件类型时,会出现以下错误:

RuntimeError: Error opening <_io.BufferedWriter name='../datasets/emddf_clean/qcoisa.wav'>: Unspecified internal error.

但是使用 w64 格式的文件,它会以某种方式工作...... 如果有人能照亮我,那将是惊人的, 提前致谢!

【问题讨论】:

    标签: python audio wave


    【解决方案1】:

    我设法完全按照我的意愿行事,而没有明确使用文件描述符:

        with sf.SoundFile(path['full_path'], mode = 'r+') as wfile:
            wfile.seek(0,sf.SEEK_END)
            wfile.write(self._samples)
    

    如果文件是r+模式(读/写),它支持查找,这意味着我们可以指向文件的末尾允许追加。 唯一的问题是如果文件不存在,它会抛出错误,但您可以通过以下方式轻松修复它:

        if(self.mode == my_utils.APPEND and os.path.isfile(path['full_path'])):
            with sf.SoundFile(path['full_path'], mode = 'r+', samplerate = samplerate) as wfile:
                wfile.seek(0,sf.SEEK_END)
                wfile.write(self.file.getSamples())
        else:
            sf.write(path['full_path'], self.file.getSamples(), samplerate,format=path['extension']) # writes to the new file 
        return
    

    希望我清楚并帮助某人!

    【讨论】:

    • 救命稻草,非常感谢!仍然像魅力一样工作
    猜你喜欢
    • 2021-10-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    相关资源
    最近更新 更多