我正在尝试绘制.wav文件的频谱图。 下面的代码表现方式的奇怪之处在于它适用于某些.wav文件而在其他文件上失败。我怀疑这是因为一些.wav文件与其他文件相比具有不同数量的通道。但是我不知道如何确定.wav文件包含多少个通道。在发布我的问题之前,我已经查看了这个堆栈溢出帖子:What is a channel in a .wav file format?Do all channels play simultaneaously when a wav file is played?
我已粘贴下面的一个方法,尝试将文件路径(myAudio)转换为带有filepath(fileNameToSaveTo)的jpg。
def individualWavToSpectrogram(myAudio, fileNameToSaveTo):
print(myAudio)
#Read file and get sampling freq [ usually 44100 Hz ] and sound object
samplingFreq, mySound = wavfile.read(myAudio)
#Check if wave file is 16bit or 32 bit. 24bit is not supported
mySoundDataType = mySound.dtype
#We can convert our sound array to floating point values ranging from -1 to 1 as follows
mySound = mySound / (2.**15)
#Check sample points and sound channel for duel channel(5060, 2) or (5060, ) for mono channel
mySoundShape = mySound.shape
samplePoints = float(mySound.shape[0])
#Get duration of sound file
signalDuration = mySound.shape[0] / samplingFreq
#If two channels, then select only one channel
mySoundOneChannel = mySound[:,0]
#Plotting the tone
# We can represent sound by plotting the pressure values against time axis.
#Create an array of sample point in one dimension
timeArray = numpy.arange(0, samplePoints, 1)
#
timeArray = timeArray / samplingFreq
#Scale to milliSeconds
timeArray = timeArray * 1000
#Plot the tone
plt.plot(timeArray, mySoundOneChannel, color='Black')
#plt.xlabel('Time (ms)')
#plt.ylabel('Amplitude')
print("trying to save")
plt.savefig('/Users/billybobjoe/Desktop/SavedSpecs' + fileNameToSaveTo + '.jpg')
print("saved")
plt.show()
这会在我的某些.wav文件中产生以下错误 第57行,在个别的WWTTpectrogram中 mySoundOneChannel = mySound [:,0] IndexError:数组
的索引太多失败的代码行是
mySoundOneChannel = mySound[:,0]
如何查看.wav文件的通道数,以及如何相应地设置mySoundOneChannel?
答案 0 :(得分:0)
据我所知,如果有多个频道,数据数组mySound
将具有形状(nSamples, nChannels)
。如果有一个频道,则mySound
的形状为(nSamples,)
。
此处,您的音频文件必须只有一个频道,因此您无法将其编入索引,就像它是2D数组一样。
因此,您应该能够替换
mySoundOneChannel = mySound[:,0]
类似
if len(mySound.shape) > 1:
mySoundOneChannel = mySound[:,0]
else:
mySoundOneChannel = mySound
要获得频道数量,您应该可以:
if len(mySound.shape) > 1:
nChannels = mySound.shape[1]
else:
nChannels = 1