Skip to content

Commit

Permalink
Workaround for Google TTS skipping timepoints
Browse files Browse the repository at this point in the history
  • Loading branch information
met4citizen committed Jul 1, 2024
1 parent 63555dc commit 8c3b453
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions modules/talkinghead.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,7 @@ class TalkingHead {
// Classifiers
const dividersSentence = /[!\.\?\n\p{Extended_Pictographic}]/ug;
const dividersWord = /[ ]/ug;
const speakables = /[\p{L}\p{N},\.'!€\$\+\-%&\?]/ug;
const speakables = /[\p{L}\p{N},\.'!€\$\+\-–—%&\?]/ug;
const emojis = /[\p{Extended_Pictographic}]/ug;
const lipsyncLang = opt.lipsyncLang || this.avatar.lipsyncLang || this.opt.lipsyncLang;

Expand Down Expand Up @@ -2518,14 +2518,33 @@ class TalkingHead {
const audio = await this.audioCtx.decodeAudioData( buf );
this.speakWithHands();

// Workaround for Google TTS not providing all timepoints
const times = [ 0 ];
let markIndex = 0;
line.text.forEach( (x,i) => {
if ( i > 0 ) {
let ms = times[ times.length - 1 ];
if ( data.timepoints[markIndex] ) {
ms = data.timepoints[markIndex].timeSeconds * 1000;
if ( data.timepoints[markIndex].markName === ""+x.mark ) {
markIndex++;
}
}
times.push( ms );
}
});

console.log(times);

// Word-to-audio alignment
const timepoints = [ { mark: 0, time: 0 } ];
data.timepoints.forEach( (x,i) => {
const time = x.timeSeconds * 1000;
let prevDuration = time - timepoints[i].time;
if ( prevDuration > 150 ) prevDuration - 150; // Trim out leading space
timepoints[i].duration = prevDuration;
timepoints.push( { mark: parseInt(x.markName), time: time });
times.forEach( (x,i) => {
if ( i>0 ) {
let prevDuration = x - times[i-1];
if ( prevDuration > 150 ) prevDuration - 150; // Trim out leading space
timepoints[i-1].duration = prevDuration;
timepoints.push( { mark: i, time: x });
}
});
let d = 1000 * audio.duration; // Duration in ms
if ( d > this.opt.ttsTrimEnd ) d = d - this.opt.ttsTrimEnd; // Trim out silence at the end
Expand Down Expand Up @@ -3116,6 +3135,12 @@ class TalkingHead {
this.gestureTimeout = null;
}

// Stop talking hands animation
let ndx = this.animQueue.findIndex( y => y.template.name === "talkinghands" );
if ( ndx !== -1 ) {
this.animQueue[ndx].ts = this.animQueue[ndx].ts.map( x => 0 );
}

// Set gesture
this.gesture = this.propsToThreeObjects( g );
if ( mirror ) {
Expand Down

0 comments on commit 8c3b453

Please sign in to comment.