You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
eSpeak has been dead for me on Windows and Mac. In my current fork I've fixed it for mac There were two things stopping it on Mac. 1 Building it from scratch the normal dll file cant be found so I updated that. (I also refactored that dll finding logic as it was annoying me) and 2. It had no way to play the temp file. aplay is a lunux only thing and the ffmpeg line was never called. So we needed to output to the OS native alternative (afplay on Mac).
Now on Windows its still broken. And I cant figure out why. This code below works (its standalone). But why when I integrate the onSynth logic into the main code it hangs I haven't figured out yet.
importosimportplatformimportwavefromtempfileimportNamedTemporaryFilefromctypesimportcdll, CFUNCTYPE, POINTER, Structure, c_char_p, c_int, c_short, c_uint, c_void_p, cast# Conditional import for winsound on Windowsifplatform.system() =='Windows':
importwinsounddll=Nonedefload_library():
globaldllpaths= [
# macOS paths'/usr/local/lib/libespeak-ng.1.dylib',
'/usr/local/lib/libespeak.dylib',
# Linux paths'libespeak-ng.so.1',
'/usr/local/lib/libespeak-ng.so.1',
'libespeak.so.1',
# Windows pathsr'C:\Program Files\eSpeak NG\libespeak-ng.dll',
r'C:\Program Files (x86)\eSpeak NG\libespeak-ng.dll'
]
forpathinpaths:
try:
dll=cdll.LoadLibrary(path)
print(f"Successfully loaded: {path}")
returnTrueexceptExceptionase:
print(f"Failed to load: {path}, Exception: {str(e)}")
returnFalseclassESPEAK_EVENT(Structure):
_fields_= [
("type", c_int),
("unique_identifier", c_uint),
("text_position", c_int),
("length", c_int),
("audio_position", c_int),
("sample", c_int),
("user_data", c_void_p),
]
# Define the synthesis callback functiondefsynth_callback(wav, numsamples, events):
print("Synthesis callback called")
ifnotwavornumsamples<=0:
print("No samples to process")
return0# Return 0 to indicate successstream=NamedTemporaryFile(delete=False, suffix='.wav')
try:
withwave.open(stream, 'wb') asf:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(22050.0)
# Convert the wav data to bytesaudio_data=cast(wav, POINTER(c_short*numsamples)).contentsbyte_data=bytearray()
forsampleinaudio_data:
byte_data.extend(sample.to_bytes(2, byteorder='little', signed=True))
f.writeframes(byte_data)
stream.close()
print(f"Temporary WAV file created at: {stream.name}")
ifplatform.system() =='Darwin': # macOSos.system(f'afplay {stream.name}')
elifplatform.system() =='Linux':
os.system(f'aplay {stream.name} -q')
elifplatform.system() =='Windows':
print(f"Playing sound on Windows... {stream.name}")
winsound.PlaySound(stream.name, winsound.SND_FILENAME)
else:
raiseRuntimeError("Unsupported operating system for audio playback")
exceptExceptionase:
print(f"Error during playback: {e}")
finally:
try:
os.remove(stream.name)
print(f"Temporary WAV file deleted: {stream.name}")
exceptExceptionase:
print(f"Error deleting temporary WAV file: {e}")
return0# Return 0 to indicate successdefmain():
ifnotload_library():
raiseRuntimeError("This means you probably do not have eSpeak or eSpeak-ng installed!")
# Initialize eSpeakdll.espeak_Initialize.restype=c_intifdll.espeak_Initialize(c_int(1), c_int(22050), c_void_p(0), c_int(0)) ==-1:
raiseRuntimeError("Failed to initialize eSpeak")
print("eSpeak initialized")
# Set the voicedll.espeak_SetVoiceByName.restype=c_intifdll.espeak_SetVoiceByName(c_char_p(b'en')) !=0:
raiseRuntimeError("Failed to set voice")
print("Voice set")
# Define the synthesis callbackCALLBACK=CFUNCTYPE(c_int, POINTER(c_short), c_int, POINTER(ESPEAK_EVENT))
callback=CALLBACK(synth_callback)
# Set the callback functiondll.espeak_SetSynthCallback(callback)
print("Synthesis callback set")
# Send text to eSpeaktext="Hello World, this is a test."dll.espeak_Synth.restype=c_intifdll.espeak_Synth(c_char_p(text.encode('utf-8')), c_int(len(text)), c_int(0), c_int(0), c_int(0), c_uint(0), c_void_p(0), c_void_p(0)) !=0:
raiseRuntimeError("Failed to synthesize text")
print("Text synthesized")
# Wait for synthesis to completedll.espeak_Synchronize.restype=c_void_pdll.espeak_Synchronize()
print("Synthesis synchronized")
if__name__=="__main__":
try:
main()
exceptExceptionasexp:
print("Exception: "+str(exp) +"\n")
raise
The text was updated successfully, but these errors were encountered:
PS: Apologies for the vast amount of commits on my fork. I'm having difficulties testing the library on my windows VM and my only option seems to be to do a pip install and use my git repo.. (if you were wondering..) (pip install -e is failing for me I should say. Advice welcome!)
eSpeak has been dead for me on Windows and Mac. In my current fork I've fixed it for mac There were two things stopping it on Mac. 1 Building it from scratch the normal dll file cant be found so I updated that. (I also refactored that dll finding logic as it was annoying me) and 2. It had no way to play the temp file. aplay is a lunux only thing and the ffmpeg line was never called. So we needed to output to the OS native alternative (afplay on Mac).
Now on Windows its still broken. And I cant figure out why. This code below works (its standalone). But why when I integrate the onSynth logic into the main code it hangs I haven't figured out yet.
The text was updated successfully, but these errors were encountered: