Digital Farmer
Digital Farmer

Reputation: 2107

How to use the audio created by pyttsx3 in the microphone output or choose audio output driver?

Usage need:

I'm going to do a draw during an audio conference, for that I'm generating a random number in Python and making pyttsx3 read the text created.

from num2words import num2words
from random import randint
import pyttsx3

engine = pyttsx3.init()
engine.setProperty('voice','HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\MSTTS_V110_ptBR_DanielM')

text = num2words(randint(1,10), lang='pt_BR')
engine.say(text=f'The number drawn was {text}')
engine.runAndWait()

But this speech is coming out of my speaker, I would like this speech to come out as my microphone, so that people who are in the conference call hear the result of the draw as if I were speaking.

If Visual Studio Code or Python appears in the Windows sound apps list, I could set the output to CABLE Input and use it as a microphone on my digital soundboard as I did with Google Chrome:

enter image description here

But as they do not appear, I would like to know if there is any way to select which audio output driver pyttsx3 will use or if there is some other module that can work with it to make this choice.

Upvotes: 1

Views: 1876

Answers (1)

JM Elbambo
JM Elbambo

Reputation: 26

I've been able to play the speech audio generated by pyttsx3 using pygame module and an external software, VB-CABLE.

Just install VB-CABLE (https://vb-audio.com/Cable/) and install the pygame module.

pip install pygame

Here is a sample code:

import pyttsx3
from pygame import mixer, _sdl2 as devices

# Get available output devices
mixer.init()
print("Outputs:", devices.audio.get_audio_device_names(False))
mixer.quit()

# Initialize mixer with the correct device
# Set the parameter devicename to use the VB-CABLE name from the outputs printed previously.
mixer.init(devicename = "CABLE Input (VB-Audio Virtual Cable)")

# Initialize text to speech
engine = pyttsx3.Engine()
text = "The quick brown fox jumped over the lazy dog."

# Save speech as audio file
engine.save_to_file(text, "speech.wav")
engine.runAndWait()

# Play the saved audio file
mixer.music.load("speech.wav")
mixer.music.play()

Don't forget to delete the audio files created afterwards.

Upvotes: 1

Related Questions