Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I play a sound through a non-default speaker with python on windows?

I have trouble waking up in the morning, so I want to make an alarm clock with python that'll force me to get up.

I currently use my computer with a pair of headphones plugged into an audio jack on my monitor, which is connected to my computer via HDMI.

I would like to play an audio file every morning through my laptop's speakers, rather than through my headphones. And I don't want to go through the hassle of unplugging my headphones every night.

Is this possible to do?

I'm not looking for being able to switch the default speakers. I just want to play a sound through a speaker that isn't the default speaker, which I KNOW is possible, because it happens from time to time with programs.

like image 565
Yamajac Avatar asked Aug 21 '15 10:08

Yamajac


2 Answers

I'm unsure if you can select what audio device to output to with winsound, but PyAudio is cross-platform and can output the sound to whichever device you want. PyAudio is available on pip through:

pip install --allow-external pyaudio --allow-unverified pyaudio pyaudio

In particular, the pyAudio.PyAudio().open() function takes an argument with the "input_device_index" – Index of Input Device to use. Unspecified (or None) uses default device. Ignored if input is False.

Docs and example: https://people.csail.mit.edu/hubert/pyaudio/docs/

Finding which device: List all audio devices with Python's pyaudio (portaudio binding)

like image 135
Andreas Avatar answered Oct 16 '22 11:10

Andreas


As an alternative to PyAudio you could try python-sounddevice, which also uses PortAudio (like PyAudio) but it is probably easier to install and use.

like image 1
Matthias Avatar answered Oct 16 '22 13:10

Matthias