Python: 将单声道 wav 文件转换为立体声
Python: Convert mono wave file to stereo
我希望在 Python 中将波形文件从单声道转换为立体声。最后应该有左右两个相同的通道具有单声道信息。我的代码不起作用。我只得到带有输入信息的左通道,右通道是空的。有什么建议吗?
import struct, wave
import numpy as np
def make_stereo(file1, output):
def everyOther (v, offset=0):
return [v[i] for i in range(offset, len(v), 2)]
ifile = wave.open(file1)
print ifile.getparams()
# (1, 2, 44100, 2013900, 'NONE', 'not compressed')
(nchannels, sampwidth, framerate, nframes, comptype, compname) = ifile.getparams()
frames = ifile.readframes(nframes * nchannels)
ifile.close()
out = struct.unpack_from("%dh" % nframes * nchannels, frames)
# Convert 2 channels to numpy arrays
if nchannels == 2:
left = np.array(list(everyOther(out, 0)))
right = np.array(list(everyOther(out, 1)))
else:
left = np.array(out)
right = left
ofile = wave.open(output, 'w')
ofile.setparams((2, sampwidth, framerate, nframes, comptype, compname))
ofile.writeframes(left.tostring())
# ofile.writeframes(right.tostring())
ofile.close()
make_stereo("Input.wav", "Output.wav")
使用 numpy
因为这是众所周知的大象枪打苍蝇,除非,也许,如果你有性能问题。
array
很容易推理和使用。我不在我的音频计算机旁,但是你的程序使用数组的翻译应该是这样的:
import wave, array
def make_stereo(file1, output):
ifile = wave.open(file1)
print ifile.getparams()
# (1, 2, 44100, 2013900, 'NONE', 'not compressed')
(nchannels, sampwidth, framerate, nframes, comptype, compname) = ifile.getparams()
assert comptype == 'NONE' # Compressed not supported yet
array_type = {1:'B', 2: 'h', 4: 'l'}[sampwidth]
left_channel = array.array(array_type, ifile.readframes(nframes))[::nchannels]
ifile.close()
stereo = 2 * left_channel
stereo[0::2] = stereo[1::2] = left_channel
ofile = wave.open(output, 'w')
ofile.setparams((2, sampwidth, framerate, nframes, comptype, compname))
ofile.writeframes(stereo.tostring())
ofile.close()
make_stereo("Input.wav", "Output.wav")
我希望在 Python 中将波形文件从单声道转换为立体声。最后应该有左右两个相同的通道具有单声道信息。我的代码不起作用。我只得到带有输入信息的左通道,右通道是空的。有什么建议吗?
import struct, wave
import numpy as np
def make_stereo(file1, output):
def everyOther (v, offset=0):
return [v[i] for i in range(offset, len(v), 2)]
ifile = wave.open(file1)
print ifile.getparams()
# (1, 2, 44100, 2013900, 'NONE', 'not compressed')
(nchannels, sampwidth, framerate, nframes, comptype, compname) = ifile.getparams()
frames = ifile.readframes(nframes * nchannels)
ifile.close()
out = struct.unpack_from("%dh" % nframes * nchannels, frames)
# Convert 2 channels to numpy arrays
if nchannels == 2:
left = np.array(list(everyOther(out, 0)))
right = np.array(list(everyOther(out, 1)))
else:
left = np.array(out)
right = left
ofile = wave.open(output, 'w')
ofile.setparams((2, sampwidth, framerate, nframes, comptype, compname))
ofile.writeframes(left.tostring())
# ofile.writeframes(right.tostring())
ofile.close()
make_stereo("Input.wav", "Output.wav")
使用 numpy
因为这是众所周知的大象枪打苍蝇,除非,也许,如果你有性能问题。
array
很容易推理和使用。我不在我的音频计算机旁,但是你的程序使用数组的翻译应该是这样的:
import wave, array
def make_stereo(file1, output):
ifile = wave.open(file1)
print ifile.getparams()
# (1, 2, 44100, 2013900, 'NONE', 'not compressed')
(nchannels, sampwidth, framerate, nframes, comptype, compname) = ifile.getparams()
assert comptype == 'NONE' # Compressed not supported yet
array_type = {1:'B', 2: 'h', 4: 'l'}[sampwidth]
left_channel = array.array(array_type, ifile.readframes(nframes))[::nchannels]
ifile.close()
stereo = 2 * left_channel
stereo[0::2] = stereo[1::2] = left_channel
ofile = wave.open(output, 'w')
ofile.setparams((2, sampwidth, framerate, nframes, comptype, compname))
ofile.writeframes(stereo.tostring())
ofile.close()
make_stereo("Input.wav", "Output.wav")