python wave模块可以接受StringIO对象

我正在尝试使用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()

解决方法:

试试这个:

import StringIO

buffer = StringIO.StringIO(wav_buffer)
file = wave.open(buffer, 'r')
上一篇:[Usaco2009 Oct]Heat Wave 热浪(裸最短路径)


下一篇:java – 在Android上合并两个WAVE文件(连接)