import NSSpeechSynthesizer # Used for Python to speak to us
import speech_recognition as sr # Used to translate human voice and pass to text
import webbrowser # Used to open web pages
import pywhatkit # Used to search on YouTube
import yfinance as yf # Used to search information in the stock market
import pyjokes # Used to tell jokes
import wikipedia # Used to search information on Wikipedia
import datetime # Used to get the current date and time
Listen to our microphone and return the audio as text
def transform_audio_into_text():
# Store the recognizer in a variable
r = sr.Recognizer()
# Configure the microphone
with sr.Microphone() as source:
# Waiting time
r.pause_threshold = 0.8
# Inform that the recording has started
print("You can start speaking now")
# Store the audio in a variable
audio = r.listen(source)
try:
# Search on Google what it has heard
request = r.recognize_google(audio, language='es-PE')
# Test that it was able to enter and transform
print("Your request: " + request)
# Return the request
return request
# In case it does not understand the audio
except sr.UnknownValueError:
# Test that it did not understand the audio
print("I didn't understand you")
# Return an error message
return "I'm still waiting..."
# In case it cannot resolve the request
except sr.RequestError:
# Test that it did not understand the audio
print("Service is not available")
# Return an error message
return "I'm still waiting..."
# Unexpected error
except:
# Test that it did not understand the audio
print("Something went wrong")
# Return an error message
return "I'm still waiting..."
Function for the assistant to be heard
def speak(message):
try:
synthesizer = NSSpeechSynthesizer.alloc().init()
synthesizer.setVoice_("com.apple.speech.synthesis.voice.Monica")
synthesizer.startSpeakingString_(message)
# Wait until it finishes speaking
while synthesizer.isSpeaking():
pass
except Exception as e:
print(f"Error using NSSpeechSynthesizer: {e}")
'''def speak(message):
try:
engine = pyttsx3.init(driverName='nsss')
engine.say(message)
engine.runAndWait()
except Exception as e:
print(f"Error using pyttsx3: {e}")'''
I have tried using Nsss and pyttsx3 on mac but it says there is an error:
NameError: name ‘objc’ is not defined. Did you mean: ‘object’?
If you look inside a ‘speak’ function I am using both nsss and pyttsx3, when I use nsss it works fine for me, but not with the other. I don’t know what to do, is it supposed to work or change something to be able to run the program using pyttsx3 on mac?
Yeferson CV is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.