7

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.

Yamajac
  • 83
  • 1
  • 5
  • possible duplicate of [Programmatically switch audio devices on Windows 7](http://stackoverflow.com/questions/13054371/programmatically-switch-audio-devices-on-windows-7) – Jamie Bull Aug 21 '15 at 10:32
  • This is "not possible" by design, but there are reverse engineered API solutions on the thread I linked to above – Jamie Bull Aug 21 '15 at 10:35
  • @JamieBull. The linked question is different. In that question they are trying to programmatically change the *default* device through which any application might play sound. This question is trying to route sound through something other than the default device which is definitely possible and very common. No reverse engineered API required. – jaket Aug 21 '15 at 18:22
  • If you have headphones plugged in, the default is headphone by design. You can't play sounds without changing that. What you need to do is temporarily change the default device then change it back. At least that's how I'd approach it. – Jamie Bull Aug 21 '15 at 18:26
  • 1
    @JamieBull. You are correct if your talking about the laptop speakers vs the laptop headphone jack because they are exactly the same device and share the same DAC - this is internal routing with no sw control. The poster is referring to HDMI audio vs the laptop speakers which are *different* devices. All of the Windows APIs allow you to choose a device to route through. That's what allows one to route Windows alert sounds routed through PC speakers while simultaneously monitoring Audacity playback through a USB device, for example. Look at the params for `waveOutOpen`. – jaket Aug 21 '15 at 19:14

2 Answers2

6

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)

Community
  • 1
  • 1
Andreas
  • 622
  • 4
  • 11
  • This looks like a good way to start...and I would imagine that the internal speakers and HMDI port(s) would be different devices. – Steven Correia Aug 21 '15 at 12:29
  • This did it for me. I had a bit of trouble getting pyaudio installed though. Using pip was giving me some compilation errors with portaudio. I ended up using a wheel from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio and got it all sorted out though. For people that don't know how to install a .whl, you need to download the version for your version of pythin and architecture. IE: PyAudio‑0.2.8‑cp34‑none‑win32.whl Would be for python 3.4 and win32 installation of python. – Yamajac Aug 21 '15 at 20:28
  • pip install pyaudio worked fine for me just now - things must have improved since 2015 :) – Egor Kraev Jun 12 '18 at 12:47
1

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.

Matthias
  • 4,524
  • 2
  • 31
  • 50