In my code, I am using Tkinter, PyAudio and the SpeechRecognition library. The speech recognition library uses PyAudio for interfacing with the PC's microphones (im on Win 10), and allows you to select the microphone input to use through an index value. This index refers to a list of microphones the system has available. This list contains multiple dictionaries, each dictionary containing information on each audio device connected to the system. Each of these dictionaries contains a field called "index", which stores an integer value representing where that dictionary lays in the overall list. This list is fetched by the SpeechRecognition library through the PyAudio library.
I can use the same method the speech recognition library does with the PyAudio library to generate my own list of microphones using
def getMicrophones(self):
import pyaudio
p = pyaudio.PyAudio()
temp = []
for i in range(p.get_device_count()):
if p.get_device_info_by_index(i).get("maxInputChannels") > 0: #if maxInputChannels > 0, then device is a microphone
temp.append(p.get_device_info_by_index(i).get("name"))
return temp
This function fetches each dictionary separately from this list, and, if the maxInputChannels field has a value > 0, the value of the name
field is stored in a separate list.
I can then display this list in a tkinter OptionMenu with the below code:
self.micOptions = self.getMicrophones()
self.selectedMicrophone = tk.StringVar() #Tkinter variable needs to be made so that the dropdown can hold a value
self.selectedMicrophone.set(self.micOptions[0]) #Set default OptionMenu value
self.micDropDown = tk.OptionMenu(self, self.selectedMicrophone, *self.micOptions)
Here's the problem:
These microphone names are taken from a dictionary that is returned by p.get_device_info_by_index(i)
using .get("name"), as seen above. The names are then placed into a list to be used, as explained.
Overall, my question would be, how can i use the selected string (name) of the microphone from the OptionMenu in order to get back to the original dictionary it was taken from, so that i can then access its index
field to use later on.
Any help is much appreciated, as i feel like there would definitely be some best practices for this.