python wave模块可以接受StringIO对象吗

发布于 2024-09-05 11:42:50 字数 1522 浏览 3 评论 0原文

我正在尝试使用 wave 模块在 python 中读取 wav 文件。

我的应用程序的不典型之处在于,我使用文件或文件名来读取 wav 文件,而是将 wav 文件放在缓冲区中。

这就是我正在做的事情

import StringIO

buffer = StringIO.StringIO()
buffer.output(wav_buffer)

file = wave.open(buffer, 'r')

,但是当我运行它时,我收到了 EOFError...

  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 493, in open
return Wave_read(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 163, in __init__
self.initfp(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 128, in initfp
self._file = Chunk(file, bigendian = 0)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/chunk.py", line 63, in __init__
raise EOFError

我知道 StringIO 的东西可以用于创建 wav 文件,我尝试了以下并且有效

import StringIO


buffer = StringIO.StringIO()
audio_out = wave.open(buffer, 'w')
audio_out.setframerate(m.getRate())
audio_out.setsampwidth(2)
audio_out.setcomptype('NONE', 'not compressed')
audio_out.setnchannels(1)

audio_out.writeframes(raw_audio)
audio_out.close()
buffer.flush()

# these lines do not work...
# buffer.output(wav_buffer)
# file = wave.open(buffer, 'r')

# this file plays out fine in VLC
file = open(FILE_NAME + ".wav", 'w')
file.write(buffer.getvalue())
file.close()
buffer.close()

i'm trying to use the wave module to read wav files in python.

whats not typical of my applications is that I'm NOT using a file or a filename to read the wav file, but instead i have the wav file in a buffer.

And here's what i'm doing

import StringIO

buffer = StringIO.StringIO()
buffer.output(wav_buffer)

file = wave.open(buffer, 'r')

but i'm getting a EOFError when i run it...

  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 493, in open
return Wave_read(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 163, in __init__
self.initfp(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 128, in initfp
self._file = Chunk(file, bigendian = 0)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/chunk.py", line 63, in __init__
raise EOFError

i know the StringIO stuff works for creation of wav file and i tried the following and it works

import StringIO


buffer = StringIO.StringIO()
audio_out = wave.open(buffer, 'w')
audio_out.setframerate(m.getRate())
audio_out.setsampwidth(2)
audio_out.setcomptype('NONE', 'not compressed')
audio_out.setnchannels(1)

audio_out.writeframes(raw_audio)
audio_out.close()
buffer.flush()

# these lines do not work...
# buffer.output(wav_buffer)
# file = wave.open(buffer, 'r')

# this file plays out fine in VLC
file = open(FILE_NAME + ".wav", 'w')
file.write(buffer.getvalue())
file.close()
buffer.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-09-12 11:42:50

试试这个:

import StringIO

buffer = StringIO.StringIO(wav_buffer)
file = wave.open(buffer, 'r')

try this:

import StringIO

buffer = StringIO.StringIO(wav_buffer)
file = wave.open(buffer, 'r')
灯角 2024-09-12 11:42:50
buffer = StringIO.StringIO()
buffer.output(wav_buffer)

StringIO 不是这样工作的。它不是一个连接到自身的管道:当您read()时,您不会收到之前传递给write()的数据。 (不用介意 output() ,我猜这是一个错误,因为没有这样的方法。)

相反,它充当单独的读管道和写管道。 read() 将返回的内容通过构造函数传入:

buffer = StringIO.StringIO(wav_buffer)
file = wave.open(buffer, 'rb')

并且从 write() 收集的任何内容都可以通过 getvalue() 读取。

(我使用了二进制读取模式,因为这就是正在发生的情况,尽管 wave 模块无论如何都会默默地将 r 模式转换为 rb。)

buffer = StringIO.StringIO()
buffer.output(wav_buffer)

A StringIO doesn't work like that. It's not a pipe that's connected to itself: when you read(), you don't receive data that you previously passed to write(). (Never mind output() which I'm guessing is a mispaste as there is no such method.)

Instead it acts as a separate read-pipe and write-pipe. The content that read() will return is passed in with the constructor:

buffer = StringIO.StringIO(wav_buffer)
file = wave.open(buffer, 'rb')

And any content collected from write() is readable through getvalue().

(I used binary read mode because this is what's happening, though the wave module will silently convert r mode to rb anyway.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文