我可以获取python列表的使用索引并将其应用于另一个列表吗?

时间:2019-05-26 00:46:16

标签: python python-3.x matplotlib pyaudio librosa

这里有我的代码...

from boot import talk
from playsound import playsound
from dtw import dtw

import librosa
import matplotlib.pyplot as plt
import os
import wave
import pyaudio

FORMAT=pyaudio.paInt16
CHANNELS=2
RATE=44100
CHUNK=1024
RECORD_SECONDS=5
FILE_NAME="voiceTemp.wav"
audio=pyaudio.PyAudio()

def run(runfile):
   with open(runfile,"r") as rnf:
       exec(rnf.read())

def voiceAuth():
    talk("Please say your voice identification phrase after the beep.")
    time.sleep(2)
    playsound("include/effects/beep.mp3")

stream=audio.open(format=FORMAT,channels=CHANNELS,
              rate=RATE,
              input=True,
              frames_per_buffer=CHUNK)

#starting recording
frames=[]

for i in range(0,int(RATE/CHUNK*RECORD_SECONDS)):
    data=stream.read(CHUNK)
    data_chunk=array('h',data)
    vol=max(data_chunk)
    if(vol>=500):
        print("something said")
        frames.append(data)
    else:
        print("nothing")
    print("\n")


#end of recording
stream.stop_stream()
stream.close()
audio.terminate()
#writing to file
wavfile=wave.open(FILE_NAME,'wb')
wavfile.setnchannels(CHANNELS)
wavfile.setsampwidth(audio.get_sample_size(FORMAT))
wavfile.setframerate(RATE)
wavfile.writeframes(b''.join(frames))#append frames recorded to file
wavfile.close()

preFiles = [
    "Name_1",
    "Name_2",
    "Name_3",
    "Name_4",
]

names = [
    "1",
    "2",
    "3",
    "4"
]

talk("Analizing files now")

#Loading audio files
y1, sr1 = librosa.load(preFiles+".wav")
y2, sr2 = librosa.load('voiceTemp.wav')

#Showing multiple plots using subplot
plt.subplot(1, 2, 1)
mfcc1 = librosa.feature.mfcc(y1,sr1)   #Computing MFCC values
librosa.display.specshow(mfcc1)

plt.subplot(1, 2, 2)
mfcc2 = librosa.feature.mfcc(y2, sr2)
librosa.display.specshow(mfcc2)

dist, cost, path = dtw(mfcc1.T, mfcc2.T)
print("The normalized distance between the two : ",dist)   # 0 for similar audios

if dist is 0:
    ###########################################

#plt.imshow(cost.T, origin='lower', cmap=plt.get_cmap('gray'), interpolation='nearest')
#plt.plot(path[0], path[1], 'w')   #creating plot for DTW
#plt.show() #To display the plots graphically

录音文件系统正常工作,所有的talk和playsound功能都正常工作,但是我想要的是一次waveFile.close()之后的脚本,将第一个列表中的文件与voiceTemp.wav进行比较如果其中之一匹配,则获取其列表索引以用于第二个列表。 现在,我可以为每个名称创建一个try函数,并分别进行操作,但是我希望能够随时在两个列表中添加名称,而不必更改任何其他代码即可使用。 另一件事是我为此尝试了audiodiff软件包,但无法安装。它一直给我一个命令错误。这个脚本可以吗?

0 个答案:

没有答案