-
Notifications
You must be signed in to change notification settings - Fork 0
/
TotalLipSync.asc
751 lines (683 loc) · 23.8 KB
/
TotalLipSync.asc
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// TOTAL LIP SYNC MODULE - Script
// by Gunnar Harboe (Snarky), v0.6
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#define TLS_PHONEMES_LINE_MAX 500 // How long a lip-sync animation can be, in number of phonemes (=frames)
#define TLS_PHONEMES_MAP_MAX 100 // How many different phoneme-to-frame mappings we can define
// Where the module should look for sync files by default
// If AGS supports the $DATA$ token to bundle data directories in the package (v3.6.0 and above), use that;
// Otherwise, use a folder in the install directory
#ifdef SCRIPT_API_v360
#define TLS_DEFAULT_DIRECTORY "$DATA$/sync"
#endif
#ifndef SCRIPT_API_v360
#define TLS_DEFAULT_DIRECTORY "$INSTALLDIR$/sync"
#endif
#region Data types
// A frame in a lip-sync'ed animation
struct _SyncFrame {
int time;
bool played;
String phoneme;
};
// A mapping from a phoneme to an animation frame
struct _PhonemeFrameMap {
String phoneme;
int frame;
};
#endregion
#region Configuration variables
String _dataDirectory;
String _fileExtension;
Casing _fileCasing;
TotalLipSyncFileFormat _lipSyncFormat;
int _sierraDummyView = -1;
int _frameRate = 24;
int _defaultFrame = 0;
#endregion
#region State variables
bool _initialized=false; // Has the module been initialized?
DynamicSprite* _sierraFrame; // Used to assign a flipped sprite to the "dummy" speech view we display for non-LucasArts lip sync
// The data from the phoneme mapping and sync file
_SyncFrame _syncFrames[TLS_PHONEMES_LINE_MAX]; // The lip-sync data (phonemes and timing) for a line of speech, read/parsed from a lip-sync file
_PhonemeFrameMap _phonemeFrameMaps[TLS_PHONEMES_MAP_MAX]; // The set of mappings from phonemes to animation frames
int _phonemeFrameMapCount=0; // How many phoneme mappings are defined so far
// Sync playback
String _currentPhoneme; // The phoneme at this position in the sync playback
int _currentFrame = -1; // The frame at this position in the sync playback
Character* _syncChar; // The character we're lip-sync'ing
float _speechTimer = 0.0; // _speechTimer counts in milliseconds
int _nextTime=-1; // Time of the next frame, in milliseconds
int _nextFrame=-1; // The next lip sync frame
bool _doLipSync=false; // Whether we're actually doing lip sync (only true if message starts with speech clip prefix and there is a matching data file)
int _realSpeechView = -1; // Used to backup the real speech view for non-LucasArts lip sync
//int _dummyFramebkup; // Backup of the dummy frame sprite slot (reset in order to avoid crash)
#endregion
#region General helper functions
// Splits a string into sections separated by the divider, and returns an array of the sections. (Last entry is a null)
String[] _Split(this String*, String divider)
{
int arrayLength = 2; // We always need at least two entries, to store original string and null
// First, count how large an array we need
String remainder = this;
if(!String.IsNullOrEmpty(divider))
{
int splitIndex = remainder.IndexOf(divider);
while(splitIndex >= 0)
{
arrayLength++;
remainder = remainder.Substring(splitIndex + divider.Length, remainder.Length - splitIndex - divider.Length);
splitIndex = remainder.IndexOf(divider);
}
}
String list[] = new String[arrayLength];
// Now put the segments into the array
if(arrayLength > 2)
{
int i=0;
remainder = this;
int splitIndex = remainder.IndexOf(divider);
while(splitIndex >= 0)
{
list[i] = remainder.Substring(0, splitIndex);
remainder = remainder.Substring(splitIndex + divider.Length, remainder.Length - splitIndex - divider.Length);
splitIndex = remainder.IndexOf(divider);
i++;
}
list[i] = remainder;
}
else
{
list[0] = this;
}
list[arrayLength-1] = null;
return list;
}
#endregion
#region Phoneme mappings
// Add a mapping from a phoneme to an animation frame
static void TotalLipSync::AddPhonemeMapping(String phoneme, int frame)
{
if(_phonemeFrameMapCount < TLS_PHONEMES_MAP_MAX)
{
_phonemeFrameMaps[_phonemeFrameMapCount].phoneme = phoneme.LowerCase();
_phonemeFrameMaps[_phonemeFrameMapCount].frame = frame;
_phonemeFrameMapCount++;
}
else
AbortGame(String.Format("TotalLipSync.AddPhonemeMapping is limited to %d mappings. Overflow at '%s'.", TLS_PHONEMES_MAP_MAX, phoneme));
}
// Add mappings from multiple phonemes (separated by '/') to an animation frame
static void TotalLipSync::AddPhonemeMappings(String phonemes, int frame)
{
String phones[] = phonemes._Split("/");
int i=0;
while(phones[i] != null)
{
TotalLipSync.AddPhonemeMapping(phones[i], frame);
i++;
}
}
// Delete all phoneme mappings
static void TotalLipSync::ClearPhonemeMappings()
{
_phonemeFrameMapCount = 0;
}
// Define a default set of mappings for Pamela files (that distinguishes stressed and unstressed vowels)
void _autoMapPhonemesPamelaStressed()
{
// DO NOT EDIT THIS FUNCTION!
// If you wish to change the mapping, set up a manual mapping
// where you initialize the module. You can copy this mapping there and edit it.
// Phoneme list from Pamela Help: http://users.monash.edu.au/~myless/catnap/pamela/
TotalLipSync.AddPhonemeMapping("None",0);
TotalLipSync.AddPhonemeMappings("M/B/P",1);
TotalLipSync.AddPhonemeMappings("K/S/T/D/G/DH/TH/R/HH/CH/Y/N/NG/SH/Z/ZH/JH",2);
TotalLipSync.AddPhonemeMappings("IH0/IH1/IH2/IY0/IY1/IY2/EH0/EH1/EH2/AH0/AH1/AH2/EY0/EY1/EY2/AW0/AW1/AW2/ER0/ER1/ER2",3);
TotalLipSync.AddPhonemeMappings("AA0/AA1/AA2/AE0/AE1/AE2/AY0/AY1/AY2",4);
TotalLipSync.AddPhonemeMappings("AO0/AA1/AA2/OW0/OW1/OW2",5);
TotalLipSync.AddPhonemeMappings("UW0/UW1/UW2/OY0/OY1/OY2/UH0/UH1/UH2",6);
TotalLipSync.AddPhonemeMapping("W",7);
TotalLipSync.AddPhonemeMappings("F/V",8);
TotalLipSync.AddPhonemeMappings("L",9);
/*
TotalLipSync.AddPhonemeMappings("None",0);
TotalLipSync.AddPhonemeMappings("B/M/P",1);
TotalLipSync.AddPhonemeMappings("S/Z/IH0/IH1/IH2/IY0/IY1/IY2/SH/T/TH/D/DH/JH/N/NG/ZH",2);
TotalLipSync.AddPhonemeMappings("EH0/EH1/EH2/CH/ER0/ER1/ER2/EY0/EY1/EY2/G/K/R/Y/HH",3);
TotalLipSync.AddPhonemeMappings("AY0/AY1/AY2/AA0/AA1/AA2/AH0/AH1/AH2/AE0/AE1/AE2",4);
TotalLipSync.AddPhonemeMappings("AO0/AO1/AO2/AW0/AW1/AW2/UH0/UH1/UH2",5);
TotalLipSync.AddPhonemeMappings("W/OW0/OW1/OW2/OY0/OY1/OY2/UW0/UW1/UW2",6);
// Frame 7 unassigned to match Moho mapping
TotalLipSync.AddPhonemeMappings("F/V",8);
TotalLipSync.AddPhonemeMappings("L",9);
*/
}
// Define a default set of mappings for Pamela files (that does not distinguish stressed and unstressed vowels)
void _autoMapPhonemesPamelaIgnoreStress()
{
// DO NOT EDIT THIS FUNCTION!
// If you wish to change the mapping, set up a manual mapping
// where you initialize the module. You can copy this mapping there and edit it.
// Phoneme list from Pamela Help: http://users.monash.edu.au/~myless/catnap/pamela/
TotalLipSync.AddPhonemeMapping("None",0);
TotalLipSync.AddPhonemeMappings("M/B/P",1);
TotalLipSync.AddPhonemeMappings("K/S/T/D/G/DH/TH/R/HH/CH/Y/N/NG/SH/Z/ZH/JH",2);
TotalLipSync.AddPhonemeMappings("IH/IY/EH/AH/EY/AW/ER",3);
TotalLipSync.AddPhonemeMappings("AA/AE/AY",4);
TotalLipSync.AddPhonemeMappings("AO/OW",5);
TotalLipSync.AddPhonemeMappings("UW/OY/UH",6);
TotalLipSync.AddPhonemeMapping("W",7);
TotalLipSync.AddPhonemeMappings("F/V",8);
TotalLipSync.AddPhonemeMappings("L",9);
/*
TotalLipSync.AddPhonemeMappings("None",0);
TotalLipSync.AddPhonemeMappings("B/M/P",1);
TotalLipSync.AddPhonemeMappings("S/Z/IH/IY/SH/T/TH/D/DH/JH/N/NG/ZH",2);
TotalLipSync.AddPhonemeMappings("EH/CH/ER/EY/G/K/R/Y/HH",3);
TotalLipSync.AddPhonemeMappings("AY/AA/AH/AE",4);
TotalLipSync.AddPhonemeMappings("AO/AW/UH",5);
TotalLipSync.AddPhonemeMappings("W/OW/OY/UW",6);
// Frame 7 unassigned to match Moho mapping
TotalLipSync.AddPhonemeMappings("F/V",8);
TotalLipSync.AddPhonemeMappings("L",9);
*/
}
void _autoMapPhonemesMoho()
{
// DO NOT EDIT THIS FUNCTION!
// If you wish to change the mapping, set up a manual mapping
// where you initialize the module. You can copy this mapping there and edit it.
// http://www.k-3d.org/wiki/PapagayoLipsyncReader
TotalLipSync.AddPhonemeMapping("rest",0);
TotalLipSync.AddPhonemeMapping("MBP",1);
TotalLipSync.AddPhonemeMapping("etc",2);
TotalLipSync.AddPhonemeMapping("E",3);
TotalLipSync.AddPhonemeMapping("AI",4);
TotalLipSync.AddPhonemeMapping("O",5);
TotalLipSync.AddPhonemeMapping("U",6);
TotalLipSync.AddPhonemeMapping("WQ",7);
TotalLipSync.AddPhonemeMapping("FV",8);
TotalLipSync.AddPhonemeMapping("L",9);
}
void _autoMapPhonemesAnno()
{
// DO NOT EDIT THIS FUNCTION!
// If you wish to change the mapping, set up a manual mapping
// where you initialize the module. You can copy this mapping there and edit it.
// http://www.annosoft.com/sapi_lipsync/docs/group__anno40.html
// http://www.adventuregamestudio.co.uk/forums/index.php?topic=34516.msg451624#msg451624
TotalLipSync.AddPhonemeMapping("x",0);
TotalLipSync.AddPhonemeMappings("m/b/p",1);
TotalLipSync.AddPhonemeMappings("k/s/t/d/g/DH/TH/r/h/CH/y/n/NG/SH/z/ZH/j/JH",2); // Sources differ on whether Anno uses j or JH
TotalLipSync.AddPhonemeMappings("IH/IY/EH/AH/EY/AW/ER",3);
TotalLipSync.AddPhonemeMappings("AA/AE/AY",4);
TotalLipSync.AddPhonemeMappings("AO/OW",5);
TotalLipSync.AddPhonemeMappings("UW/OY/UH",6);
TotalLipSync.AddPhonemeMapping("w",7);
TotalLipSync.AddPhonemeMappings("f/v",8);
TotalLipSync.AddPhonemeMappings("l",9);
}
// Define a default set of mappings for Rhubarb files
void _autoMapPhonemesRhubarb()
{
// DO NOT EDIT THIS FUNCTION!
// If you wish to change the mapping, set up a manual mapping
// where you initialize the module. You can copy this mapping there and edit it.
// https://github.com/DanielSWolf/rhubarb-lip-sync#mouth-shapes
TotalLipSync.AddPhonemeMapping("X",0);
TotalLipSync.AddPhonemeMapping("A",1); // mbp
TotalLipSync.AddPhonemeMapping("B",2); // other consonants
TotalLipSync.AddPhonemeMapping("C",3); // EH/AH/EY etc. (bed, hut, bait)
TotalLipSync.AddPhonemeMapping("D",4); // AA/AE/AY (father, bat, like)
TotalLipSync.AddPhonemeMapping("E",5); // AO/OW (thaw, slow)
TotalLipSync.AddPhonemeMapping("F",6); // UW/OY/UH/OW (you, toy, poor)
// Frame 7 unassigned to match Moho mapping
TotalLipSync.AddPhonemeMapping("G",8); // F/V (fine, very)
TotalLipSync.AddPhonemeMapping("H",9); // L (letter)
}
static void TotalLipSync::AutoMapPhonemes()
{
if(_initialized)
{
TotalLipSync.ClearPhonemeMappings();
if(_lipSyncFormat == eLipSyncPamelaStressed)
_autoMapPhonemesPamelaStressed();
else if(_lipSyncFormat == eLipSyncPamelaIgnoreStress)
_autoMapPhonemesPamelaIgnoreStress();
else if(_lipSyncFormat == eLipSyncMoho)
_autoMapPhonemesMoho();
else if(_lipSyncFormat == eLipSyncAnno)
_autoMapPhonemesAnno();
else if(_lipSyncFormat == eLipSyncRhubarb)
_autoMapPhonemesRhubarb();
}
else AbortGame("Calling TotalLipSync.AutoMapPhonemes() when TotalLipSync has not been initialized");
}
#endregion
#region Setup and Configuration
static void TotalLipSync::Init(TotalLipSyncFileFormat lipSyncFormat, String extension)
{
_lipSyncFormat = lipSyncFormat;
if(lipSyncFormat == eLipSyncPamelaStressed || lipSyncFormat == eLipSyncPamelaIgnoreStress)
_fileExtension = "pam";
else if(lipSyncFormat == eLipSyncMoho)
_fileExtension = "dat";
else if(lipSyncFormat == eLipSyncAnno)
_fileExtension = "anno";
else if(lipSyncFormat == eLipSyncRhubarb)
_fileExtension = "tsv";
if(extension != null)
_fileExtension = extension;
_initialized = true;
}
static void TotalLipSync::SetDataDirectory(String dataDirectory)
{
// Strip trailing slashes
while(dataDirectory.Length>0 && (dataDirectory.Chars[dataDirectory.Length-1]=='/')) // || dataDirectory.Chars[dataDirectory.Length-1] == '\\'))
dataDirectory = dataDirectory.Truncate(dataDirectory.Length-1);
_dataDirectory = dataDirectory;
}
static void TotalLipSync::SetFileExtension(String fileExtension)
{
_fileExtension = fileExtension;
}
static void TotalLipSync::SetFileCasing(Casing fileCasing)
{
_fileCasing = fileCasing;
}
static void TotalLipSync::SetDataFileFrameRate(int frameRate)
{
_frameRate = frameRate;
}
static void TotalLipSync::SetSierraDummyView(int viewNumber)
{
_sierraDummyView = viewNumber;
}
static void TotalLipSync::SetDefaultFrame(int frameNumber)
{
_defaultFrame = frameNumber;
}
#endregion
#region Get current state
static Character* TotalLipSync::GetCurrentLipSyncingCharacter()
{
return _syncChar;
}
static String TotalLipSync::GetCurrentPhoneme()
{
return _currentPhoneme;
}
static int TotalLipSync::GetCurrentFrame()
{
return _currentFrame;
}
#endregion
#region Parse sync file
bool _parsePamela(String filepath)
{
File* pamFile = File.Open(filepath, eFileRead);
if (pamFile != null)
{
bool processing;
int index = 0;
while(!pamFile.EOF)
{
String line = pamFile.ReadRawLineBack();
if (processing && !line.StartsWith("//"))
{
int colon = line.IndexOf(":");
if (colon > 0)
{
String strtime = line.Substring(0, colon);
_syncFrames[index].time = ((strtime.AsInt * 1000) / (15*_frameRate)); // Convert from Pamela XPOS to milliseconds
_syncFrames[index].phoneme = line.Substring(colon + 1, line.Length - colon - 1);
index ++;
// If we're ignoring stress, discard stress information on vowels (a number at the end of the phoneme code)
if(_lipSyncFormat == eLipSyncPamelaIgnoreStress)
{
String phone = _syncFrames[index].phoneme;
if(phone.Length>1)
{
char x = phone.Chars[phone.Length-1];
if(x >= '0' && x <= '9')
_syncFrames[index].phoneme = phone.Truncate(phone.Length-1);
}
}
//Display("%d;%s",SyncFrames[index].time, SyncFrames[index].phoneme);
}
}
// We only process the [Speech] section
if (line == "[Speech]")
processing = true;
else if(line.StartsWith("[",false))
processing = false;
}
pamFile.Close();
return true;
}
else
return false;
}
bool _parseMoho(String filepath)
{
File* datFile = File.Open(filepath, eFileRead);
if(datFile != null)
{
bool processing=false;
int i=0;
while(!datFile.EOF)
{
String line = datFile.ReadRawLineBack();
if(processing)
{
int space = line.IndexOf(" ");
if(space > 0)
{
String strFrame = line.Substring(0, space);
_syncFrames[i].time = (strFrame.AsInt * 1000) / _frameRate; // Convert from frame count to milliseconds
_syncFrames[i].phoneme = line.Substring(space + 1, line.Length - space - 1);
i++;
}
}
if(line == "MohoSwitch1")
processing=true;
}
datFile.Close();
return true;
}
else
return false;
}
bool _parseAnno(String filepath)
{
File* annoFile = File.Open(filepath, eFileRead);
if(annoFile != null)
{
bool processing = true;
int i=0;
while(!annoFile.EOF)
{
String line = annoFile.ReadRawLineBack();
if(processing && line.IndexOf(" ") > 0)
{
String segment[] = line._Split(" ");
if(segment[0] == "phn")
{
_syncFrames[i].time = segment[1].AsInt;
_syncFrames[i].phoneme = segment[4];
i++;
}
}
if(line == "%%-begin-anno-text-%% ")
processing = false;
else if(line == "%%-end-anno-text-%%")
processing = true;
}
annoFile.Close();
return true;
}
else
return false;
}
bool _parseRhubarb(String filepath)
{
File* tsvFile = File.Open(filepath, eFileRead);
if(tsvFile != null)
{
int i=0;
while(!tsvFile.EOF)
{
String line = tsvFile.ReadRawLineBack();
int tab = line.IndexOf(" "); // tab
if(tab > 0)
{
String strSec = line.Substring(0, tab);
_syncFrames[i].time = FloatToInt(strSec.AsFloat * 1000.0);
_syncFrames[i].phoneme = line.Substring(tab + 1, line.Length - tab - 1);
i++;
}
}
tsvFile.Close();
return true;
}
else
return false;
}
#endregion
#region Start and stop sync
// Reset all the lip-sync data (for a new line of speech)
void _resetSyncFrames()
{
int i = 0;
while (i < TLS_PHONEMES_LINE_MAX)
{
_syncFrames[i].played = false;
_syncFrames[i].time = -1;
_syncFrames[i].phoneme = "";
i++;
}
}
// Map from phoneme to animation frame
int _getFrame(String phoneme)
{
phoneme = phoneme.LowerCase();
int i=0;
while(i < _phonemeFrameMapCount)
{
if(phoneme == _phonemeFrameMaps[i].phoneme)
return _phonemeFrameMaps[i].frame;
i++;
}
// Return 0 if not found
return 0;
}
// Because we can't control the frame display during Sierra-style speech, we instead set the view to a single-frame loop, and overwrite its .Graphic sprite
void _setDummyFrame(int realView, int realLoop, int realFrame)
{
ViewFrame* vfReal = Game.GetViewFrame(realView, realLoop, realFrame);
ViewFrame* vfDummy = Game.GetViewFrame(_sierraDummyView, 0, 0);
if(vfReal.Flipped)
{
if(_sierraFrame != null) _sierraFrame.Delete();
_sierraFrame = DynamicSprite.CreateFromExistingSprite(vfReal.Graphic, true);
_sierraFrame.Flip(eFlipLeftToRight);
vfDummy.Graphic = _sierraFrame.Graphic;
}
else
vfDummy.Graphic = vfReal.Graphic;
}
void _stopLipSync()
{
// Make sure our dummy view isn't set to a dynamic sprite about to be deleted
if(_sierraDummyView != -1)
{
ViewFrame* vfDummy = Game.GetViewFrame(_sierraDummyView, 0, 0);
vfDummy.Graphic = 0;
}
if(_syncChar != null)
{
if(Speech.Style != eSpeechLucasarts && _realSpeechView != -1)
{
_syncChar.SpeechView = _realSpeechView;
_realSpeechView = -1;
}
_syncChar.UnlockView();
_syncChar = null;
}
if(_sierraFrame != null)
{
_sierraFrame.Delete();
_sierraFrame = null;
}
_speechTimer = 0.0;
_nextFrame = -1;
_nextTime = -1;
_doLipSync = false;
_currentFrame = -1;
_currentPhoneme = null;
}
void _sync(Character* c, String message)
{
// Make sure to stop any already running lip sync animations
_stopLipSync();
_resetSyncFrames();
// We only sync if the line starts with a speech clip prefix (e.g. "&111 Blah blah blah.")
if (message.StartsWith("&",false))
{
// Generate the filename for the matching lip sync data file:
// -the first four letters of the character name (without the initial c)...
String filename = String.Format("%s",c.scrname);
filename = filename.Substring(1, 4);
// -... followed by the speech clip number
int firstspace = message.IndexOf(" ");
filename = filename.Append(message.Substring(1, firstspace - 1));
// Apply casing to filename if desired (for case-sensitive file systems)
if(_fileCasing == eCasingLowerCase)
filename = filename.LowerCase();
else if(_fileCasing == eCasingUpperCase)
filename = filename.UpperCase();
String filepath = String.Format("%s/%s.%s", _dataDirectory, filename, _fileExtension);
if(_lipSyncFormat == eLipSyncPamelaStressed || _lipSyncFormat == eLipSyncPamelaIgnoreStress)
_doLipSync = _parsePamela(filepath);
else if(_lipSyncFormat == eLipSyncMoho)
_doLipSync = _parseMoho(filepath);
else if(_lipSyncFormat == eLipSyncAnno)
_doLipSync = _parseAnno(filepath);
else if(_lipSyncFormat == eLipSyncRhubarb)
_doLipSync = _parseRhubarb(filepath);
#ifdef SCRIPT_API_v360
if(!_doLipSync)
System.Log(eLogWarn, "TotalLipSync: Sync failed, could not load sync file %s", filepath);
#endif
}
_syncChar = c;
if(Speech.Style != eSpeechLucasarts)
{
_realSpeechView = _syncChar.SpeechView;
_syncChar.SpeechView = _sierraDummyView;
_setDummyFrame(_realSpeechView, c.Loop, 0);
}
}
#endregion
void SaySync(this Character*, String message)
{
if(!Game.SkippingCutscene)
{
_sync(this, message);
this.Say(message);
_stopLipSync();
}
}
void SayAtSync(this Character*, int x, int y, int width, String message)
{
if(!Game.SkippingCutscene)
{
_sync(this, message);
this.SayAt(x, y, width, message);
_stopLipSync();
}
}
#region Update sync
int _getFrameNumber(int millis, bool next)
{
// Because the frames aren't necessarily in order, we have to scan through all of them to see which one is current
int i = 0;
int closestTime = -1;
int closestFrame = -1;
while (i < TLS_PHONEMES_LINE_MAX)
{
if ( (next && (!_syncFrames[i].played && (closestTime < 0 || _syncFrames[i].time < closestTime) && _syncFrames[i].time > millis)) // Searches for next frame
||(!next && (!_syncFrames[i].played && (closestTime < 0 || _syncFrames[i].time > closestTime) && _syncFrames[i].time <= millis))) // Searches for current frame
{
closestTime = _syncFrames[i].time;
closestFrame = i;
}
i++;
}
return closestFrame;
}
void _updateNextFrame(int millis)
{
_nextFrame = _getFrameNumber(millis, true);
if(_nextFrame == -1)
_nextTime = -1;
else
_nextTime = _syncFrames[_nextFrame].time;
}
void _playFrame(int frame)
{
// Look up the frame based on phoneme, unless argument is -1, in which case use _defaultFrame
if(frame == -1)
{
_currentPhoneme = "";
_currentFrame = _defaultFrame;
}
else
{
_currentPhoneme = _syncFrames[frame].phoneme;
_currentFrame = _getFrame(_currentPhoneme);
}
if(Speech.Style == eSpeechLucasarts)
_syncChar.LockViewFrame(_syncChar.SpeechView, _syncChar.Loop, _currentFrame);
else
_setDummyFrame(_realSpeechView, _syncChar.Loop, _currentFrame);
if(frame != -1)
_syncFrames[frame].played = true;
}
// Update the animation of lip-synced speaking characters
void _updateLipSync()
{
if (_syncChar != null)
{
if(_syncChar.Speaking)
{
// Start animation. If the first phoneme defined in the animation isn't at the very beginning, use frame 0 for now
if(_speechTimer == 0.0)
{
if(_doLipSync)
{
int frame = _getFrameNumber(0, false);
_playFrame(frame);
_updateNextFrame(0);
}
else
{
_playFrame(-1);
}
}
if(_doLipSync)
{
// If it's time to play the next frame, do so (if there is one) and update the next frame to the one after that
int millis;
AudioChannel* ac = System.AudioChannels[0];
if(ac.IsPlaying)
millis = ac.PositionMs;
else
millis= FloatToInt(_speechTimer, eRoundNearest);
if(millis >= _nextTime && _nextFrame != -1)
{
_playFrame(_nextFrame);
_updateNextFrame(millis);
}
_speechTimer += 1000.0 / IntToFloat(GetGameSpeed());
}
}
else
{
_stopLipSync();
}
}
}
#endregion
function game_start()
{
_dataDirectory = TLS_DEFAULT_DIRECTORY;
#ifdef TLS_DUMMY
_sierraDummyView = TLS_DUMMY;
#endif
}
function repeatedly_execute_always()
{
_updateLipSync();
}