import objc from AppKit import * from Foundation import NSURL from PyObjCTools import AppHelper from time import time apple_phonemes = [ '%', '@', 'AE', 'EY', 'AO', 'AX', 'IY', 'EH', 'IH', 'AY', 'IX', 'AA', 'UW', 'UH', 'UX', 'OW', 'AW', 'OY', 'b', 'C', 'd', 'D', 'f', 'g', 'h', 'J', 'k', 'l', 'm', 'n', 'N', 'p', 'r', 's', 'S', 't', 'T', 'v', 'w', 'y', 'z', 'Z' ] len(apple_phonemes) speech_phoneme_data = [] class SpeechDelegate (NSObject): def speechSynthesizer_willSpeakWord_ofString_(self, sender, word, text): '''Called automatically when the application has launched''' print("Speaking word {} in sentence {}".format(word,text)) def speechSynthesizer_willSpeakPhoneme_(self,sender,phoneme): phon_ch = apple_phonemes[phoneme] # print('first',speech_phoneme_data) # prev_time = speech_phoneme_data[-1][1] # print('prev_time',prev_time) speech_phoneme_data.append((phon_ch,time())) print("phoneme boundary for {} time {}".format(phon_ch,time())) # NSApp().terminate_(self) def speechSynthesizer_didFinishSpeaking_(self,synth,didFinishSpeaking): speech_phoneme_data.append(('%',time())) print("finished speaking time {}".format(time())) diff_time = [] for i in range(len(speech_phoneme_data)-1): dur = speech_phoneme_data[i+1][1] - speech_phoneme_data[i][1] diff_time.append((speech_phoneme_data[i][0],dur)) print(diff_time) # del SpeechDelegate class Delegate (NSObject): def applicationDidFinishLaunching_(self, aNotification): '''Called automatically when the application has launched''' print("Window, World!") def windowWillClose_(self, aNotification): '''Called automatically when the window is closed''' print("Window has been closed") # Terminate the application NSApp().terminate_(self) def main(): speech_delg = SpeechDelegate.alloc().init() speech_delg.speechSynthesizer_didFinishSpeaking_('t',True) voices = NSSpeechSynthesizer.availableVoices() identifier = voices[2] time() alex_voice = NSSpeechSynthesizer.alloc().initWithVoice_(identifier) alex_voice.setDelegate_(speech_delg) alex_voice.startSpeakingString_("This is a test for speech synthesis generation") # Create a new application instance ... a=NSApplication.sharedApplication() # ... and create its delgate. Note the use of the # Objective C constructors below, because Delegate # is a subcalss of an Objective C class, NSObject delegate = Delegate.alloc().init() # Tell the application which delegate object to use. a.setDelegate_(delegate) AppHelper.runEventLoop() if __name__ == '__main__': main()