MESH-Benchmark / code /dynamic_demo /continuous.py
Heiheihaha17's picture
Add files using upload-large-folder tool
6a16e42 verified
Raw History Blame Contribute Delete
2.94 kB
"""Replace split H01 turns with a frozen, original continuous utterance."""
from __future__ import annotations
import argparse
import copy
from pathlib import Path
import soundfile as sf
from .common import SR, read, sha, speech_activity, uid, write
from .motion import revised_recipe
REVISION='continuous_h01_v3'
def prepare(root):
recipe=read(root/'data/recipe.json');alignment=read(root/'assets/sources/H01_alignment.json')
asset='assets/sources/H01.flac';path=root/asset
if sha(path)!=alignment['source_sha256']:raise ValueError('Continuous source checksum mismatch')
wave,rate=sf.read(path,dtype='float32')
if rate!=SR or wave.ndim!=1:raise ValueError('Expected original 16 kHz mono speech')
words=alignment['words'];a=max(0,round((words[0]['start_s']-.03)*SR))
b=min(len(wave),round((words[-1]['end_s']+.04)*SR));start=round(.35*SR)
span=words[-1]['end_s']-words[0]['start_s']
max_gap=max(y['start_s']-x['end_s'] for x,y in zip(words,words[1:]))
if not span>=6. or max_gap>.3:raise ValueError('Speech is unsuitable for continuous motion inspection')
event=dict(event_id=uid('event','H01',REVISION),source_id='H01',audio_asset=asset,
original_source_sha256=alignment['source_sha256'],original_corpus_path=alignment['original_corpus_path'],
crop_samples=[a,b],start_sample=start,end_sample=start+b-a,text=' '.join(w['word'] for w in words),
activity_samples=speech_activity(wave[a:b],start),words=[dict(word=w['word'],
start_sample=start+round(w['start_s']*SR)-a,end_sample=start+round(w['end_s']*SR)-a,
alignment_score=w['mean_score']) for w in words])
gaps=[(y[0]-x[1])/SR for x,y in zip(event['activity_samples'],event['activity_samples'][1:])]
if max(gaps,default=0)>.35:raise ValueError('Original recording has too long an internal silence')
for scene in recipe['scenes']:
human=next(h for h in scene['humans'] if h['entity_id']=='H01')
if human['speaker_id']!=alignment['speaker_id']:raise ValueError('Speaker identity changed')
human['events']=[copy.deepcopy(event)];scene['speech_revision']=REVISION
recipe['speech_revision']=REVISION
recipe['speech_style']='H01: one original continuous LibriSpeech utterance; H02/robot: frozen assembled turns'
recipe['continuous_speech_check']=dict(source_id='H01',minimum_aligned_span_s=6.,
maximum_internal_silence_s=.35,maximum_word_gap_s=.3,source_asset_sha256=alignment['source_sha256'],
no_internal_splicing=True,no_time_stretching=True)
write(root/'data/recipe.json',revised_recipe(recipe))
print('Continuous H01:',round(start/SR,3),'to',round(event['end_sample']/SR,3),'s;',
'maximum original internal silence',max(gaps,default=0),'s',flush=True)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('--root',type=Path,required=True)
a=p.parse_args();prepare(a.root.resolve())