以下脚本可以播放原始文件.我尝试以明显的方式分离每个频道,但它不起作用.
import os
import wavio
import numpy
import pyglet
file_name = "guitarup_full.wav"
# I get the file !
File = wavio.read(file_name)
rate = File.rate
# it looks good
print File.data.shape
print rate
# and then the channels:
channel_1 = File.data[:,0]
channel_2 = File.data[:,1]
wavio.write("guitar_channel_1.wav", channel_1, rate )
wavio.write("guitar_channel_2.wav", channel_2, rate )
# now we try to play:
source = pyglet.resource.media(file_name, streaming=False)
source_ch1 = pyglet.resource.media("guitar_channel_1.wav", streaming=False)
source_ch2 = pyglet.resource.media("guitar_channel_2.wav", streaming=False)
#uncomment the one you want to listen.
source.play()
#source_ch1.play()
#source_ch2.play()
pyglet.app.run()
Run Code Online (Sandbox Code Playgroud)
第一个听起来像吉他,第二个和第三个听起来像高斯噪音.有人能告诉我它有什么问题吗?
我使用的音频文件是:https: //www.freesounds.info/global/wav.php?fileid = 11
数据的形状为:(88471,2),速率为:44100
另外,如果我在另一个播放器中播放该文件,我会得到相同的结果:高斯噪声.
注意: pyglet的使用对于这个问题来说是多余的.如果您使用它来调查此问题,请确保文件位于资源文件夹中注册的文件夹中.要做到这一点:
pyglet.resource.path.append("your_sounds_location")
pyglet.resource.reindex()
Run Code Online (Sandbox Code Playgroud)
mac*_*nic 10
我认为您正试图让它变得比需要的复杂得多。如果你不介意使用而scipy不是分离通道归结为:
from scipy.io import wavfile
fs, data = wavfile.read('guitarup_full.wav') # reading the file
wavfile.write('guitar_channel_1.wav', fs, data[:, 0]) # saving first column which corresponds to channel 1
wavfile.write('guitar_channel_2.wav', fs, data[:, 1]) # saving second column which corresponds to channel 2
Run Code Online (Sandbox Code Playgroud)
我不知道wavio代码有什么问题,但是这里是你如何使用标准Python模块wave(也被wavio使用)和numpy 分离WAV通道:
import wave
import numpy as np
def save_wav_channel(fn, wav, channel):
'''
Take Wave_read object as an input and save one of its
channels into a separate .wav file.
'''
# Read data
nch = wav.getnchannels()
depth = wav.getsampwidth()
wav.setpos(0)
sdata = wav.readframes(wav.getnframes())
# Extract channel data (24-bit data not supported)
typ = { 1: np.uint8, 2: np.uint16, 4: np.uint32 }.get(depth)
if not typ:
raise ValueError("sample width {} not supported".format(depth))
if channel >= nch:
raise ValueError("cannot extract channel {} out of {}".format(channel+1, nch))
print ("Extracting channel {} out of {} channels, {}-bit depth".format(channel+1, nch, depth*8))
data = np.fromstring(sdata, dtype=typ)
ch_data = data[channel::nch]
# Save channel to a separate file
outwav = wave.open(fn, 'w')
outwav.setparams(wav.getparams())
outwav.setnchannels(1)
outwav.writeframes(ch_data.tostring())
outwav.close()
wav = wave.open(WAV_FILENAME)
save_wav_channel('ch1.wav', wav, 0)
save_wav_channel('ch2.wav', wav, 1)
Run Code Online (Sandbox Code Playgroud)
在命令行上尝试ffmpeg:
将立体声输入中的每个声道输出到单独的单声道文件:
ffmpeg -i stereo.wav -map_channel 0.0.0 left.wav -map_channel 0.0.1 right.wav
Run Code Online (Sandbox Code Playgroud)
我在您的文件上尝试了它,它似乎起作用了。
如果您需要将其放入python程序中,请使用os.system(cmd)