speech-scoring/tts-wav-gen.py

131 lines
5.1 KiB
Python

import objc
from AppKit import NSSpeechSynthesizer,NSSpeechInputModeProperty,NSSpeechModePhoneme
from Foundation import NSURL,NSError,NSObject
import json
import random
import os
import re
import subprocess
OUTPUT_NAME = 'audio'
def create_output_dir():
direc = os.path.abspath('.')+'/outputs/'+OUTPUT_NAME+'/'
if not os.path.exists(direc):
os.mkdir(direc)
create_output_dir()
dest_filename = lambda n,v,r,t: '{}-{}-{}-{}-'.format(n,v,r,t)+str(random.randint(0,10000))+'.aiff'
dest_path = lambda n: os.path.abspath('.')+'/outputs/'+OUTPUT_NAME+'/'+n
dest_url = lambda p: NSURL.fileURLWithPath_(p)
def cli_gen_audio(word,rate,voice,out_path):
subprocess.call(['say','-v',voice,'-r',str(rate),'-o',out_path,word])
class SynthFile(object):
"""docstring for SynthFile."""
def __init__(self,word, filename,voice,rate,operation):
super(SynthFile, self).__init__()
self.word = word
self.filename = filename
self.voice = voice
self.rate = rate
self.operation = operation
def get_json(self):
return {'filename':self.filename,'voice':self.voice,
'rate':self.rate,'operation':self.operation}
def get_csv(self):
return '{},{},{},{},{}\n'.format(self.word,self.voice,self.rate,self.operation,self.filename)
class SynthVariant(object):
"""docstring for SynthVariant."""
def __init__(self,identifier,rate,op):
super(SynthVariant, self).__init__()
self.synth = NSSpeechSynthesizer.alloc().initWithVoice_(identifier)
self.synth.setVolume_(100)
self.synth.setRate_(rate)
self.phone_synth = NSSpeechSynthesizer.alloc().initWithVoice_(identifier)
self.phone_synth.setVolume_(100)
self.phone_synth.setRate_(rate)
self.phone_synth.setObject_forProperty_error_(NSSpeechModePhoneme,NSSpeechInputModeProperty,None)
self.identifier = identifier
self.rate = rate
self.name = identifier.split('.')[-1]
self.operation = op
def __repr__(self):
return 'Synthesizer[{} - {}]({})'.format(self.name,self.rate,self.operation)
def generate_audio(self,word):
fname = dest_filename(word,self.name,self.rate,self.operation)
d_path = dest_path(fname)
d_url = dest_url(d_path)
if self.operation == 'normal':
self.synth.startSpeakingString_toURL_(word,d_url)
# cli_gen_audio(word,self.rate,self.name,d_path)
else:
orig_phon = self.synth.phonemesFromText_(word)
phon = '[[inpt PHON]] '+re.sub('[0-9]','',orig_phon)
# phon = re.sub('[0-9]','',orig_phon)
cli_gen_audio(phon,self.rate,self.name,d_path)
# if phon != '':
# self.phone_synth.startSpeakingString_toURL_(phon,d_url)
# else:
# self.synth.startSpeakingString_toURL_(word,d_url)
return SynthFile(word,fname,self.name,self.rate,self.operation)
def synth_generator():
voices_installed = NSSpeechSynthesizer.availableVoices()
voice_attrs = [NSSpeechSynthesizer.attributesForVoice_(v) for v in voices_installed]
us_voices_ids = [v['VoiceIdentifier'] for v in voice_attrs if v['VoiceLanguage'] == 'en-US' and v['VoiceIdentifier'].split('.')[-1][0].isupper()]
# us_voices_ids = ['com.apple.speech.synthesis.voice.Fred','com.apple.speech.synthesis.voice.Alex',
# 'com.apple.speech.synthesis.voice.Victoria']
# voice_rates = list(range(150,221,(220-180)//4))
voice_rates = [150,180,210]
voice_synths = []
variants = ['normal','phoneme']
for v in us_voices_ids:
for r in voice_rates:
for o in variants:
voice_synths.append(SynthVariant(v,r,o))
def synth_for_words(words):
all_synths = []
for w in words:
for s in voice_synths:
all_synths.append(s.generate_audio(w))
return all_synths
return synth_for_words
def write_synths(synth_list,fname,csv=False):
f = open(fname,'w')
if csv:
for s in synth_list:
f.write(s.get_csv())
else:
json.dump([s.get_json() for s in synth_list],f)
f.close()
def generate_audio_for_stories():
stories_data = json.load(open('./inputs/all_stories_hs.json'))
word_list = [t[0] for i in stories_data.values() for t in i]
words_audio_synth = synth_generator()
return words_audio_synth(word_list)
# words_audio_synth = synth_generator()
# synth = NSSpeechSynthesizer.alloc().init()
# voices_installed = NSSpeechSynthesizer.availableVoices()
# voice_attrs = [NSSpeechSynthesizer.attributesForVoice_(v) for v in voices_installed]
# us_voices_ids = [v['VoiceIdentifier'] for v in voice_attrs if v['VoiceLanguage'] == 'en-US' and v['VoiceIdentifier'].split('.')[-1][0].isupper()]
# synth.setVoice_(us_voices_ids[2])
# synth.startSpeakingString_('your')
# fname = dest_filename(word,self.name,self.rate,self.operation)
# d_path = dest_path(fname)
# d_url = dest_url(d_path)
synths = synth_generator()([OUTPUT_NAME])
# synths = generate_audio_for_stories()
write_synths(synths,'./outputs/'+OUTPUT_NAME+'.csv',True)
# write_synths(synths,'./outputs/synths.json')