File size: 9,461 Bytes
c5c9abd
048101b
 
2defee0
53792d8
 
 
 
5c7029b
 
 
 
 
 
 
53792d8
f0fa26d
 
5c7029b
 
2defee0
5c7029b
86e56e5
cc7c120
53792d8
cc7c120
5c7029b
 
cc7c120
 
f0fa26d
d6846cc
366ecce
5c7029b
366ecce
53792d8
5c7029b
366ecce
 
53792d8
366ecce
06af375
 
f0fa26d
366ecce
 
 
 
 
 
5c7029b
 
f0fa26d
 
 
 
 
366ecce
f0fa26d
 
a894787
 
 
 
 
 
 
 
 
 
366ecce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a894787
5c7029b
 
366ecce
5c7029b
366ecce
 
 
 
 
5c7029b
 
 
 
366ecce
5c7029b
366ecce
5c7029b
366ecce
 
5c7029b
 
 
 
 
366ecce
 
5c7029b
366ecce
5c7029b
366ecce
cc178c4
366ecce
 
cc7c120
5c7029b
 
366ecce
 
 
 
 
5c7029b
 
2defee0
366ecce
2defee0
366ecce
 
 
 
 
5c7029b
 
 
 
366ecce
 
 
5c7029b
366ecce
f0fa26d
366ecce
 
 
 
5c7029b
 
366ecce
 
 
 
 
 
5c7029b
366ecce
 
 
 
 
 
 
 
 
5c7029b
f0fa26d
a4ed697
366ecce
 
 
 
 
5c7029b
 
 
366ecce
 
 
 
 
5c7029b
366ecce
 
5c7029b
366ecce
e2901c5
366ecce
8d1fcc3
366ecce
 
 
 
5c7029b
366ecce
 
c5c9abd
366ecce
c5c9abd
366ecce
 
 
 
 
 
 
 
 
 
 
 
5c7029b
 
 
 
53792d8
07c85d3
8d1fcc3
53792d8
 
07c85d3
53792d8
 
 
 
 
 
 
 
 
 
 
 
 
 
366ecce
 
53792d8
 
366ecce
 
53792d8
366ecce
 
 
53792d8
366ecce
 
53792d8
 
 
 
 
 
 
5c7029b
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os, unicodedata, string, random
from scripts.ctcalign import aligner, wav16m
from scripts.tapi import tiro
from scripts.reaper2pass import estimate_pitch, save_pitch
import scripts.clusterprosody as cl




# given a Sentence string,
# using a metadata file of SQ, // SQL1adult_metadata.tsv
# get every file from SQ of a L1 adult with that sentence
#  report how many, or if 0.


def run(sentence, voices, start_end_word_ix):
    #sentence = 'hvaða sjúkdómar geta fylgt óbeinum reykingum'
    #voices = ['Alfur','Dilja','Karl', 'Dora']
    # On tts.tiro.is speech marks are only available 
    # for the voices: Alfur, Dilja, Karl and Dora.
    # in practise, only for alfur and dilja.

    corpus_meta = '/home/user/app/human_data/SQL1adult10s_metadata.tsv'
    speech_dir = '/home/user/app/human_data/audio/squeries/'
    speech_aligns = '/home/user/app/human_data/align/squeries/'
    speech_f0 = '/home/user/app/human_data/f0/squeries/'
    align_model_path ="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-icelandic-ep10-1000h"

    tts_dir = '/home/user/app/tts_data/'

    
    norm_sentence = snorm(sentence)
    sentence = sentence.replace('\t', ' ')

    human_rec_ids = get_samromur_queries(norm_sentence, corpus_meta, speech_dir, speech_aligns, align_model_path, speech_f0)

    if voices:
        temp_tts_sample, tts_sent_dir = get_tts(sentence,voices,tts_dir,align_model_path)

        voices = [voices[0]] # TODO. now limit one voice at a time.
        score, tts_fig_p, mid_fig_p, bad_fig_p, tts_fig_e, fig_mid_e, fig_bad_e = cl.cluster(norm_sentence, sentence, human_rec_ids, speech_aligns, speech_f0, speech_dir, tts_sent_dir, voices, start_end_word_ix)

    # also stop forgetting duration.
        
    return temp_tts_sample, score, tts_fig_p, mid_fig_p, bad_fig_p, tts_fig_e, fig_mid_e, fig_bad_e







def snorm(s):
    s = ''.join([c.lower() for c in s if not unicodedata.category(c).startswith("P") ])
    while '  ' in s:
        s = s.replace('  ', ' ')
    return s
   
    

def create_temp_sent_list():
    corpusdb = '/home/user/app/human_data/SQL1adult10s_metadata.tsv'
    with open(corpusdb,'r') as handle:
        meta = handle.read().splitlines()
    meta = [l.split('\t')[3] for l in meta[1:]]
    meta = sorted(list(set(meta)))
    return meta
    
    
    
def align_file(wav_path, output_path, norm_sentence, word_aligner = None, model_path = "carlosdanielhernandezmena/wav2vec2-large-xlsr-53-icelandic-ep10-1000h"):

    model_word_sep = '|'
    model_blank_tk = '[PAD]'
    
    if not word_aligner:
        print('initiating forced alignment, can take some time...')
        word_aligner = aligner(model_path,model_word_sep,model_blank_tk)
  
    word_aln = word_aligner(wav16m(wav_path),norm_sentence,is_normed=True)
    word_aln = [[str(x) for x in l] for l in word_aln]
    
    with open(output_path,'w') as handle:
        handle.write(''.join(['\t'.join(l)+'\n' for l in word_aln]))
        
    return word_aligner
    
    


# find all the recordings of a given sentence
# listed in the corpus metadata.
# find or create their alignments and f0 tracking.
# sentence should be provided lowercase without punctuation
# TODO something not fatal to interface if <10 --
#   metadata file for SQ is already filtered.
# TODO handle audio that is not originally .wav
#   not an issue for SQ
def get_samromur_queries(sentence, corpusdb, speech_dir, align_dir, align_model_path, f0_dir, reaper_path = "REAPER/build/reaper"):
    with open(corpusdb,'r') as handle:
        meta = handle.read().splitlines()
    meta = [l.split('\t') for l in meta[1:]]
    
    
    # column index 4 of db is normalised sentence text
    meta = [l for l in meta if l[4] == sentence]
    
    if len(meta) < 10:
        if len(meta) < 1:
            print('This sentence does not exist in the corpus')
        else:
            print('Under 10 copies of the sentence: skipping.')
        return []
    else:
        print(f'{len(meta)} recordings of sentence <{sentence}>')
        
    
    word_aligner = None
    
    if not os.path.exists(align_dir):
            os.makedirs(align_dir)
    if not os.path.exists(f0_dir):
            os.makedirs(f0_dir)


    for rec in meta:
        wpath = f'{speech_dir}{rec[2]}'
        apath = align_dir + rec[2].replace('.wav','.tsv')
        if not os.path.exists(apath):
            word_aligner = align_file(wpath,apath, rec[4], word_aligner = word_aligner, model_path = align_model_path)
            
        fpath = f0_dir + rec[2].replace('.wav','.f0')
        if not os.path.exists(fpath):
            fpath = f0_dir + rec[2].replace('.wav','.f0')
            f0_data = estimate_pitch(wpath, reaper_path)
            save_pitch(f0_data,fpath)
      
    
    human_rec_ids = sorted([l[2].split('.wav')[0] for l in meta])
    return human_rec_ids
    




# check if the TTS wavs, alignments, f0 exist for this sentence
# if not, make them
def get_tts(sentence,voices,ttsdir,align_model_path,reaper_path = "REAPER/build/reaper"):

    dpath = setup_tts_sent(sentence,ttsdir)
    

    sample_paths = []

    word_aligner = None

    for v in voices:
        wpath = f'{dpath}/{v}.wav'
        apath = f'{dpath}/{v}.tsv'
        fpath = f'{dpath}/{v}.f0'
        
        if not os.path.exists(wpath):
            wf = tiro(sentence,v,save=f'{dpath}/')
            
        if not os.path.exists(apath):
            word_aligner = align_file(wpath, apath, snorm(sentence), word_aligner = word_aligner, model_path = align_model_path)


        if not os.path.exists(fpath):
            f0_data = estimate_pitch(wpath, reaper_path)
            save_pitch(f0_data,fpath)
        
        sample_paths.append(wpath)
        
        
        # TODO TEMP
        # return for single last voice
        temp_sample_path = wpath
        
    return temp_sample_path, dpath
    
    


# find if dir for this sentence exists yet
#   or make one, and record it.
# punctuation can affect synthesis
#   so index by original sentence, not normed text
def setup_tts_sent(sentence,ttsdir,meta_path = 'tts_meta.tsv'):
    
    if not os.path.exists(f'{ttsdir}'):
        os.makedirs(f'{ttsdir}')
    
    sentence = sentence.replace('\n',' ')
            
    with open(f'{ttsdir}{meta_path}','a+') as handle:
        handle.seek(0)
        tts_meta = handle.read().splitlines()
        tts_meta = [l.split('\t') for l in tts_meta]
            
        tts_meta = {sent:s_id for s_id,sent in tts_meta}
        
        if sentence not in tts_meta.keys():
            sent_id = sentence.replace(' ','_')[:33]
            rand_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
            while f'{sent_id}_{rand_id}' in tts_meta.values():
                rand_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
            sent_id = f'{sent_id}_{rand_id}'
            
            handle.write(f'{sent_id}\t{sentence}\n')
                
        else:
            sent_id = tts_meta[sentence]
        
    sent_dir = f'{ttsdir}{sent_id}'
    if not os.path.exists(f'{sent_dir}'):
        os.makedirs(f'{sent_dir}')
    return sent_dir





def localtest():
    sentence = 'En er hægt að taka orðalagið bókstaflega?'#'Ef svo er, hvað heita þau þá?'#'Var það ekki nóg?'
    voices = ['Alfur_v2'] #,'Dilja']
    # make for now the interface allows max one voice
    
    start_end_word_ix = '1-3'#'5-7'

    locl = '/home/caitlinr/work/peval/pce/'
    corpus_meta = locl+'human_data/SQL1adult10s_metadata.tsv'
    speech_dir = locl+'human_data/audio/squeries/'
    speech_aligns = locl+'human_data/align/squeries/'
    speech_f0 = locl+'human_data/f0/squeries/'
    align_model_path ="/home/caitlinr/work/models/LVL/wav2vec2-large-xlsr-53-icelandic-ep10-1000h"

    tts_dir = locl+'tts_data/'
    
    reaper_exc = '/home/caitlinr/work/notterra/REAPER/build/reaper'
    
    norm_sentence = snorm(sentence)
    
    
    human_rec_ids = get_samromur_queries(norm_sentence, corpus_meta, speech_dir, speech_aligns, align_model_path, speech_f0, reaper_path = reaper_exc)
    
    if voices:

        one_audio_sample, tts_sent_dir = get_tts(sentence,voices,tts_dir,align_model_path,reaper_path = reaper_exc)
        
        voices = [voices[0]] # TODO. now limit one voice at a time.

        score, tts_fig_p, mid_fig_p, bad_fig_p, tts_fig_e, fig_mid_e, fig_bad_e = cl.cluster(norm_sentence, sentence, human_rec_ids, speech_aligns, speech_f0, speech_dir, tts_sent_dir, voices, start_end_word_ix)
        





#localtest()
# torch matplotlib librosa sklearn_extra pydub
# env pclustr


# https://colab.research.google.com/drive/1RApnJEocx3-mqdQC2h5SH8vucDkSlQYt?authuser=1#scrollTo=410ecd91fa29bc73

# EVALUATION
# - of the tts
# - of the method: consistency? coherency / interpretability of 'best' voice across different features; alt. ability to recover good & problematic features from a combined method if that is chosen as the best? 
# - how similar are the results across different sentences? are any voices consistently good or bad; if multiple are good, are they good in the same way or good in different ways; do humans agree.
#  >> bc hey THAT could at least be an argument for the method, u might have to take time for human judgement once but then you can keep re using it free for new voices. or to select among alternative generations given you might know a context and know what you're going for in that context. etc.