Send audio to specific sound device without libraries or complicated code

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
thegame2010
Posts: 29
Joined: 04 Feb 2016, 18:14
Location: 'Murica

Send audio to specific sound device without libraries or complicated code

15 Feb 2021, 10:03

This solution is simple and wonderful (and complete) for anyone hoping to easily make a soundboard-type script. It has less to do with AutoHotkey specifically and more to do with using Windows 10 and the Virtual Audio Cable driver to simplify what other people accomplished with VLC commands and mplayer and libraries and real, difficult coding stuff.

My task was to have a series of shortcuts like Windows + F1 to play a sound through a microphone as though I said it. This would be good for trolling in multiplayer games (classic Duke Nukem soundboard stuff) or in my case for saying common things while I work as a helpdesk technician "Thanks for calling, how may I assist you today?" I like to sound less robotic to the people who monitor my calls for quality assurance purposes, so I produced a few versions of each phrase I wanted to use and exported them as .wav files using Audacity and cleaning up the audio with noise reduction. That's right, I used a robot to sound less robotic.

My code is pretty simple. I used lists to pick one of my recordings at random each time I use the hotkey. In fact, this is the first and it became the final version of the code.

Code: Select all

#persistent

list1= aGreeting1.wav, aGreeting2.wav, aGreeting3.wav
listA:=strSplit(list1, ",") ; A = Array

list2= zFarewell1.wav, zFarewell2.wav, zFarewell3.wav, zFarewell4.wav
listB:=strSplit(list2, ",") ; B = Array

#F1::
	random, choice, 1, % listA.maxIndex() ; random between 1 and the total item count.
	response:=trim(listA[choice])
	SoundPlay, %response%
return
#F2::SoundPlay, cancel.wav
#Space::SoundPlay, cancel.wav
#F3::
	random, choice, 1, % listB.maxIndex() ; random between 1 and the total item count.
	response:=trim(listB[choice])
	SoundPlay, %response%
return
So in the same folder as my script are the various wav files. There isn't one called cancel.wav so that I can interrupt any of my scripts if I have to for any reason. I can modify this to include lots of other things as well by making a list 3, etc.

But now comes the part where I hope I'm writing in enough search engine optimization. I beat my head against the wall to find out how to make these sounds play as though through my USB headset microphone. I wanted to be able to talk to a customer after my computer greeted them, and have the customer have no idea that it was any different (except that my prerecorded response had all the enthusiasm that my QM team demands and would listen for).

I stumbled upon Virtual Audio Cable and downloaded some free trial, which didn't seem right. Then I found this video and in its description I found this link. I'm not confident that either of those links will persist as long as this post will, so here's the text, except that I updated it as of 6/15/2021 (original post mid-February 2021):
If you have Lync, Jabber, Skype or any other conferencing system in which you want to use more than one microphone, this tutorial is for you.

https://www.youtube.com/watch?v=TQHP1PZBPvc

Download Virtual Audio Cable from this page (most likely you want the 64 bit version)
Install Virtual Audio Cable – do NOT touch the software after the install
Go to your Windows RECORDING DEVICES – do NOT touch your PLAYBACK DEVICES
Double click on each Microphone you want to use, click the SOUNDS TAB, click LISTEN TO THIS DEVICE and set the drop down to "CABLE-Input (VB-Audio Virtual Cable)"
In RECORDING DEVICES, right click on "CABLE-Input (VB-Audio Virtual Cable)" and set it to DEFAULT
Now you are done, other than to make sure that the software in question (Skype, Lync…) is set to use either your Windows Default Recording Device OR "CABLE-Input (VB-Audio Virtual Cable)"
Do exactly that. My work's virtual phone program is playing into my USB headset and listening from Virtual Audio Cable. But that isn't right still. In order for the computer to basically work like normal except AutoHotkey sends noises to Virtual Audio Cable, I had to use Windows 10's Sound Settings (right click on the volume icon in the system tray and Open Sound Settings - scroll down and click App volume and device preferences). AutoHotkey didn't show up in this list for me, so while I had that window open I ran my code and played a file. Then "AutoHotkey Unicode 64-bit" showed up in the list and I could set its default output and input. I don't do anything with input into AutoHotkey, but I still set the output as CABLE Input and the input as CABLE Output. I'm sure there's an easier way to say that.

I tested this by using my virtual phone system to echo a test call back to me. I didn't have to tweak the volume any to get it to sound right, but you may have to do that. If you want to test this, try using Skype or something similar and use its echo test call feature.
The only thing I'd like to add to this is some kind of notification that shows while a sound file is being played. My very bogus workaround for this is to open the recording devices window (right-click the sound icon in your system tray and select "Sounds" then go to the recording tab) and to have this sitting somewhere barely visible on my screen so I can see the meter rise and fall as my robot talks.

Troubleshooting:
When I go back into Sound Settings, AutoHotkey isn't in there anymore and suddenly it doesn't work. I have to play a sound, get it in the list, and then change the playback device to something else and then back to whatever I want it to be (headphones in my case. See below for where I mean).
Image

Once in a while a Windows or driver update may stop the bit where the headset microphone is being "listened" to by the VAC. Just test it out before using it. I noticed that if I speak into the microphone and both bars don't rise in the recording window, then the listening isn't happening.
Image

Edits: This post was edited twice: once to update links and then immediately again to fix formatting and combine my previous replies into the main post. I've since deleted the replies.
Last edited by thegame2010 on 15 Jun 2021, 09:49, edited 2 times in total.
Godfthr
Posts: 13
Joined: 28 Jul 2021, 11:43

Re: Send audio to specific sound device without libraries or complicated code

24 Nov 2021, 02:28

Hey, I just want to thank you for this. I have to send a ton of voicemails for work, implementing this has been great. Super lightweight and straight forward.

I did find a fix (or rather, a work around) for needing to switch the playback device. I was motivated to find this because not only was this happening for me as well for the playback device, but when I restart my PC, the recording default devices switches back to my microphone and so the functionality stops working, so I have to do this manually which is annoying.

Regarding switching the playback device, I've found I've had to do that every time I run the script. Therefore, what I did was download Soundswitch: https://github.com/Belphemur/SoundSwitch and utilized the hotkey I assigned to it to activate twice at the beginning of my script, doing it automatically. Soundswitch essentially allows for seamless transitioning between playback devices and recording devices with a hotkey.


Cheers :thumbup:
User avatar
geekyadam
Posts: 45
Joined: 01 Aug 2016, 17:11

Re: Send audio to specific sound device without libraries or complicated code

11 Mar 2022, 18:01

Yes I want to echo thanks for this. I've been using Voicemod to alter voice output and use a soundboard for awhile now, and I had a gut feeling that I should be able to get AHK to play sounds via the Voicemod audio device that gets created, and this is the solution. I believe the Voicemod audio device that gets installed when Voicemod is installed is similar or identical to the results of that Virtual Audio Cable driver. Voicemod has more features, but I was interested in putting a bunch of sound bites in folders for organization, so using AHK would be easier as I could loop through the sound folders to populate the list of sound bites, and then use hotkeys to play one at random as I want to. When using Voicemod, I would need to load in each and every sound clip one at a time, then send a random hotkey to play a random one. Using this solution, I should be able to just put a new sound clip in a folder and reload the script.

Thanks again!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 129 guests