-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
857 lines (764 loc) · 22.1 KB
/
Program.cs
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using CevioCasts;
using LibSasara;
using LibSasara.Model;
using LibSasara.Model.FullContextLabel;
using LibSasara.VoiSona;
using LibSasara.VoiSona.Model.Talk;
using SharpOpenJTalk.Lang;
using WanaKanaNet;
ConsoleApp.Run<SongToTalk>(args);
[SuppressMessage("Usage", "CA1050")]
public partial class SongToTalk: ConsoleAppBase
{
private static OpenJTalkAPI? _jtalk;
[ThreadStatic]
private static Cast? _defs;
[RootCommand]
public async ValueTask<int> ExportAsync(
[Option("s", "path to song ccs file")]
string pathToSongCcs,
[Option("e", "path to a export tstprj file")]
string pathToPrj,
[Option("er", "emotion rate array")]
double[] emotions,
[Option("c", "singing cast name")]
string? castName = null,
[Option("split", "split note by threthold msec.")]
bool splitNoteByThrethold = true,
[Option("th", "threthold time (MessageProcessingHandler.) for note split")]
double splitThrethold = 250,
[Option("co", "offset seconds for consonant")]
decimal consonantOffset = 0.05m,
string? pathToWav = "",
string? pathToLab = ""
){
if(!File.Exists(pathToSongCcs)){
await Console.Error.WriteLineAsync($"file:{pathToSongCcs} is not found!")
.ConfigureAwait(false);
return 1;
}
//(あれば)wav/labを読み込む
var hasWav = pathToWav is not null
&& File.Exists(pathToWav);
var hasLab = pathToLab is not null
&& File.Exists(pathToLab);
//ccsを解析
var processed = await ProcessCcsAsync(pathToSongCcs)
.ConfigureAwait(false);
//あればlabファイル
if(hasLab){
processed.Label = await SasaraLabel
.LoadAsync(pathToLab!)
.ConfigureAwait(false);
}
//解析を元にtstprj作成
await GenerateFileAsync(
processed,
pathToPrj,
castName,
(splitNoteByThrethold, splitThrethold),
emotions,
consonantOffset
)
.ConfigureAwait(false);
_jtalk?.Dispose();
return default;
}
/// <summary>
/// songのccs/ccstデータを解析する
/// </summary>
/// <remarks>
/// 最初のSongトラックのみ
/// オプションで再現度合いを切り替え
/// 1. 楽譜データのみ
/// 2. 楽譜+調声データ
/// 3. 楽譜+フルPITCH(wav)+フルTMG(lab)
/// </remarks>
/// <param name="path"></param>
/// <returns></returns>
private static async ValueTask<SongData> ProcessCcsAsync(string path)
{
var ccs = await SasaraCcs.LoadAsync(path)
.ConfigureAwait(false);
var trackset = ccs
.GetTrackSets<SongUnit>()
.FirstOrDefault();
var song = trackset?
.Units
.FirstOrDefault();
if(trackset is null || song is null){
await Console.Error.WriteLineAsync($"Error!: ソングデータがありません: { path }")
.ConfigureAwait(false);
return new SongData();
}
//中間データに解析・変換
var songData = new SongData()
{
//トラック名
SongTrackName = trackset.Name,
//楽譜のテンポ・ビート
TempoList = song.Tempos,
BeatList = song.Beat,
//1note=1uttranceは重いはず
//なので休符で区切ったphrase単位に
PhraseList = SplitByPhrase(song, 0),
//TODO:tmgやf0を渡す
};
return songData;
}
/// <summary>
/// ノートをフレーズ単位で分割
/// </summary>
/// <param name="song"></param>
/// <param name="threthold">同じフレーズとみなすしきい値(tick; 960=1/4)</param>
/// <returns></returns>
private static List<List<Note>> SplitByPhrase(
SongUnit song,
int threthold = 0
)
{
//返すよう
List<List<Note>> list = [];
//念の為ソート
var notes = song
.Notes
.OrderBy(n => n.Clock);
var phrase = Enumerable.Empty<Note>().ToList();
foreach (var note in notes)
{
if(phrase.Count != 0)
{
var last = phrase[^1];
var isOver = IsOverThrethold(threthold, note, last);
bool isOverZero = phrase.Count > 0;
if (
//しきい値以下は同じフレーズとみなす
isOverZero && isOver
//またはブレス指定があればしきい値以下でも別フレーズ
||
isOverZero && !isOver && last.Breath
)
{
list.Add(phrase);
phrase = Enumerable
.Empty<Note>()
.ToList();
}
}
phrase.Add(note);
}
list.Add(phrase);
return list;
static bool IsOverThrethold(int threthold, Note note, Note last)
{
return Math.Abs(note.Clock - (last.Clock + last.Duration)) > threthold;
}
}
private async ValueTask GenerateFileAsync(
SongData processed,
string exportPath,
string? castName = null,
(bool isSplit, double threthold)? splitNote = null,
double[]? emotions = null,
decimal consonantOffset = 0.0m
)
{
var path = Path.Combine(
System.AppDomain.CurrentDomain.BaseDirectory,
"file/template.tstprj"
);
var TemplateTalk = await LibVoiSona
.LoadAsync<TstPrj>(path)
.ConfigureAwait(false);
await InitOpenJTalkAsync()
.ConfigureAwait(false);
var rates = await CulcEmoRatesAsync(castName, emotions)
.ConfigureAwait(false);
processed.TempoList ??= new() { { 0, 120 } };
var sw = new Stopwatch();
sw.Start();
var us = processed.PhraseList?
.AsParallel().AsOrdered()
.Select(ToUtterance(processed, rates, splitNote, consonantOffset))
.ToImmutableList()
;
if (us is null)
{
await Console.Error.WriteLineAsync("解析に失敗しました。。。")
.ConfigureAwait(false);
return;
}
var tstprj = TemplateTalk
.ReplaceAllUtterancesAsPrj(us);
sw.Stop();
Debug.WriteLine($"★processed: {sw.ElapsedMilliseconds} msec.");
if (castName is not null)
{
var voice = await GetVoiceByCastNameAsync(castName)
.ConfigureAwait(false);
tstprj = tstprj
.ReplaceVoiceAsPrj(voice);
}
await LibVoiSona
.SaveAsync(exportPath, tstprj.Data.ToArray())
.ConfigureAwait(false);
}
private async Task<double[]?> CulcEmoRatesAsync(string? castName, double[]? emotions)
{
double[]? rates = null;
if (castName is not null && emotions is null)
{
//感情数を調べる
var cast = await GetCastDefAsync(castName)
.ConfigureAwait(false);
rates = cast
.Emotions
.Select(e => 0.00)
.ToArray();
//とりあえず最初の感情だけMAX
rates[0] = 1.00;
}
if (emotions is not null)
{
//感情比率設定可能に
if (emotions.Length == 0)
{
await Console.Error.WriteLineAsync($"emotion {emotions} is length 0.")
.ConfigureAwait(false);
}
rates = emotions;
}
return rates;
}
private async ValueTask<Voice> GetVoiceByCastNameAsync(string castName)
{
var cast = await GetCastDefAsync(castName)
.ConfigureAwait(false);
return new Voice(
Array.Find(cast.Names, n => n.Lang == Lang.English)?.Display ?? "error",
cast.Cname,
cast.Versions[^1]);
}
private async ValueTask<Cast> GetCastDefAsync(string castName)
{
if (_defs is not null) return _defs;
var path = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"lib/data.json"
);
var jsonString = await File
.ReadAllTextAsync(path)
.ConfigureAwait(false);
var defs = Definitions.FromJson(jsonString);
if(defs is null)
{
await Console.Error.WriteLineAsync($"invalid cast definitions data: {path}")
.ConfigureAwait(false);
ThrowInvalidException(path);
}
_defs = Array.Find(defs.Casts,
c => c.Product == Product.VoiSona
&& c.Category == CevioCasts.Category.TextVocal
&& c.Names.Any(n => string.Equals(n.Display, castName, StringComparison.OrdinalIgnoreCase))
)
?? throw new ArgumentException(
$"cast name {castName} is not found in cast data. please check https://github.com/InuInu2022/cevio-casts/ ",
nameof(castName));
return _defs;
// use polyfill for netstandard 2.0
[DoesNotReturn]
static void ThrowInvalidException(string path)
{
throw new InvalidDataException($"invalid cast definitions data: {path}");
}
}
private async ValueTask InitOpenJTalkAsync()
{
var path = Path.Combine(
System.AppDomain.CurrentDomain.BaseDirectory,
"lib/open_jtalk_dic_utf_8-1.11/");
var result = await Task
.Run(()=> {
_jtalk = new OpenJTalkAPI();
return _jtalk.Initialize(path);
})
.ConfigureAwait(false);
}
private Func<List<Note>, Utterance>
ToUtterance(
SongData data,
double[]? emotionRates = null,
(bool isSplit, double threthold)? noteSplit = null,
decimal consonantOffset = 0.0m
)
{
return p =>
{
//フレーズをセリフ化
var text = GetPhraseText(p);
//分割
if (noteSplit?.isSplit is true){
p = SplitNoteIfSetOption(data, noteSplit, p);
}
var fcLabel = GetFullContext(p);
//フレーズの音素
var phoneme = GetPhonemeLabel(fcLabel, GetPhonemeMode.Note);
//読み
var pronounce = GetPronounce(phoneme);
//アクセントの高低
//TODO: ノートの高低に合わせる
//とりあえず数だけ合わせる
var accent = MakeAccText(fcLabel);
//調整後LEN
//TODO:ccsやlabから割当
var timing = "";
//PIT
//楽譜データだけならnote高さから計算
//TODO:ccsやwavがあるなら解析して割当
var pitch = GetPitches(p, data);
//フレーズ最初が子音の時のオフセット
var offset = 0.0m;
var firstPh = phoneme.Split('|')[0].Split(',')[0];
if(PhonemeUtil.IsConsonant(firstPh)){
//とりあえず 固定値
offset = consonantOffset;
}
var nu = new Utterance(
text: text,
//tsmlを無理やり生成
tsml: GetTsml(text, pronounce, phoneme, accent),
//開始時刻
start: GetStartTimeString(data, p, offset),
//書き出しファイル名、とりあえずセリフ
exportName: $"{text}"
)
{
//感情比率
//感情数に合わせる
RawFrameStyle = emotionRates is null
? "0:1:1.000:0.000:0.000:0.000:0.000"
: $"0:1:{string.Join(':', emotionRates)}",
//調整前LEN
PhonemeOriginalDuration = GetSplittedTiming(p, data, consonantOffset),
};
//timing
if (!string.IsNullOrEmpty(timing))
{
nu.PhonemeDuration = nu.PhonemeOriginalDuration;
}
//pitch
if (!string.IsNullOrEmpty(pitch))
{
nu.RawFrameLogF0 = pitch;
}
Console.WriteLine($"u[{text}], start:{nu.Start}");
return nu;
};
}
private List<Note> SplitNoteIfSetOption(
SongData data,
(bool isSplit, double threthold)? noteSplit,
List<Note> p)
{
//「ー」対応
ManageLongVowelSymbols(p);
return p.ConvertAll(n =>
{
var dur = LibSasaraUtil
.ClockToTimeSpan(
n.Duration,
data.TempoList ?? new() { { 0, 120 } })
.TotalMilliseconds;
var th = noteSplit?.threthold ?? 100000;
//th = th < 100 ? 100 : th;
th = Math.Max(th, 100);
if (dur < th) return n;
var spCount = (int)Math.Floor(dur / th) + 1;
var ph = GetPhonemeLabel(GetFullContext(new List<Note> { n }), GetPhonemeMode.Note);
var sph = ph.Split('|');
var add = spCount - sph.Length;
if (add <= 0) return n;
sph = sph[^1].Split(',')[^1] switch
{
//ん
"N" => sph
.Concat(
Enumerable
.Repeat("u", add > 0 ? add - 1 : 0)
.Append("N"))
.ToArray(),
//無効
"xx" => sph,
//それ以外(母音)
string s => sph
.Concat(Enumerable.Repeat(s, add))
.ToArray()
};
n.Phonetic = string
.Join(',', sph);
n.Lyric = GetPronounce(string.Join('|', sph));
return n;
})
;
}
/// <summary>
/// 歌詞中の「ー」対応
/// </summary>
/// <param name="p"></param>
private void ManageLongVowelSymbols(List<Note> p)
{
for (var i = 1; i < p.Count; i++)
{
var lyric = p[i].Lyric;
if (lyric is null) continue;
if (!lyric.Contains('ー', StringComparison.InvariantCulture))
{
continue;
}
//「ー」の時は前のノートの母音歌詞に置換
var prev = p[i - 1];
var ph = GetPhonemeLabel(GetFullContext(new List<Note> { prev }), GetPhonemeMode.Note).Split('|');
var last = ph.Last() ?? "a";
p[i].Lyric = lyric
.Replace(
"ー",
GetPronounce(last),
StringComparison.InvariantCulture);
}
}
private static readonly WanaKanaOptions kanaOption = new()
{
CustomKanaMapping = new Dictionary<string, string>(StringComparer.Ordinal)
{
{"cl","ッ"},
{"di","ディ"},
},
};
private static string GetPronounce(string phonemes)
{
var sb = new StringBuilder(phonemes);
sb.Replace('|', ' ');
sb.Replace(",", string.Empty);
//読みを変えたフレーズ
var yomi = WanaKana.ToKatakana(sb.ToString(), kanaOption);
return yomi.Replace(" ", string.Empty, StringComparison.InvariantCulture);
}
private string GetPitches(
List<Note> notes,
SongData data)
{
var tempo = data.TempoList ?? new() { { 0, 120 } };
List<(TimeSpan start, TimeSpan end, double logF0, int counts)> d = notes
.ConvertAll(n =>
(
start: LibSasaraUtil
.ClockToTimeSpan(
tempo,
n.Clock
),
end: LibSasaraUtil
.ClockToTimeSpan(
tempo,
n.Clock + n.Duration
),
logF0: Math.Log(LibSasaraUtil
.OctaveStepToFreq(n.PitchOctave, n.PitchStep)),
counts: CountPhonemes(n)
))
;
var pitches = Enumerable.Empty<(double ph, double logF0)>().ToList();
var offset = d[0].start.TotalMilliseconds;
var total = 1; //冒頭sil分offset
foreach (var (start, end, logF0, counts) in d)
{
//TODO:時間を見て分割数を決める?
//一旦固定分割数で
const int split = 20;
var length = split * counts;
const double add = 1.0 / split;
for (var i = 0; i <= length; i++)
{
var t = total + i * add;
pitches.Add((t, logF0));
}
total += counts;
}
var sb = new StringBuilder(100000);
for (int i = 0; i < pitches.Count; i++)
{
var sf = pitches[i].ph
.ToString("F2", CultureInfo.InvariantCulture);
var logF0 = pitches[i].logF0;
sb.Append(
CultureInfo.InvariantCulture,
$"{sf}:{logF0}");
if(i<pitches.Count-1)sb.Append(',');
}
return sb.ToString();
}
private static string MakeAccText(FullContextLab fcLabel)
{
var mCount = fcLabel
.Lines
.Cast<FCLabLineJa>()
.First()?
.UtteranceInfo?
.Mora
?? 0
;
var ac = Enumerable
.Range(0, mCount > 1 ? mCount - 1 : 0)
.Select(s => "h")
.ToArray();
return $"l{string.Concat(ac)}";
}
private static string GetPhonemeLabel(
FullContextLab fcLabel,
GetPhonemeMode mode
)
{
var moras = fcLabel.Lines
.Cast<FCLabLineJa>();
var splited = FullContextLabUtil
.SplitByMora(moras)
.Select(s => s
.Select(s2 => s2.Phoneme)
.Where(s2 => s2 != "sil"))
.Select(s => string.Join(',', s))
.Where(s => !string.IsNullOrEmpty(s))
;
return string.Join('|', splited);
}
private FullContextLab GetFullContext(IEnumerable<Note> notes)
{
_jtalk ??= new OpenJTalkAPI();
var lyrics = GetPhraseText(notes);
if(fcLabelCache
.TryGetValue(lyrics, out var cachedLabel))
{
//キャッシュがあればキャッシュを返す
return cachedLabel;
}
var text = Enumerable.Empty<string>();
lock(_jtalk){
text = _jtalk.GetLabels(lyrics);
}
if (text is null)
{
return new FullContextLab(string.Empty);
}
return new FullContextLab(string.Join('\n', text));
}
private static IEnumerable<string> ConvertSimpleLabel(IEnumerable<string> fullLabel)
{
//pL-pC+pR
return fullLabel
.AsParallel().AsSequential()
.Select(s => s.Split('/')[0])
.Select(s => FullContextLabelRegex().Match(s).Groups[1].Value)
;
}
/// <summary>
/// 音素数で等分分割した時間を求める
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
private string GetSplittedTiming(
List<Note> notes,
SongData song,
decimal offset = 0.0m
)
{
var tempo = song.TempoList ?? new() { { 0, 120 } };
var timings = notes
.AsParallel().AsSequential()
.Select((n,i) =>
{
//オフセット準備
var is1stNote = i is 0;
var isConso1stPh = Check1stConsoPhoneme(n);
var isConsoNextPh = CheckNextConsoPhoneme(notes, i);
//音素数を数える
//OpenJTalkで正確に数える
int count = CountPhonemes(n);
//開始時間
var start = LibSasaraUtil.ClockToTimeSpan(
tempo,
n.Clock
).TotalMilliseconds;
if (isConso1stPh)
{
start -= (double)(offset * 1000);
}
//終了時間
var end = LibSasaraUtil.ClockToTimeSpan(
tempo,
n.Clock + n.Duration
).TotalMilliseconds;
if (isConsoNextPh)
{
end -= (double)(offset * 1000);
}
var repeat = count > 1 && isConso1stPh && offset > 0 ? count - 1 : count;
//ノートあたりの長さを音素数で等分
var nLen = (decimal)(end - start);
nLen = isConso1stPh ? nLen - (offset * 1000) : nLen;
var sub = nLen / repeat;
var len = Enumerable
.Range(0, repeat)
.Select(_ => sub / 1000m);
len = isConso1stPh && offset > 0 ? [offset, .. len] : len;
var str = len.Select(v => v.ToString("N3", CultureInfo.InvariantCulture));
return string.Join(',', str);
})
;
var s = string
.Join(',', timings);
return $"0.005,{s},0.125";
}
private bool CheckNextConsoPhoneme(List<Note> p, int i)
{
var isConsoNext = false;
if ((i + 1) < p.Count)
{
var next = p[i + 1];
isConsoNext = Check1stConsoPhoneme(next);
}
return isConsoNext;
}
private bool Check1stConsoPhoneme(Note n)
{
var result = GetPhonemeLabel(
GetFullContext(new List<Note>() { n }),
GetPhonemeMode.Note
).Split('|', StringSplitOptions.None)[0].Split(',')[0];
return PhonemeUtil.IsConsonant(result) && result != "N";
}
/// <summary>
/// フルコンテクストラベルのキャッシュ
/// </summary>
private static Dictionary<string, FullContextLab> fcLabelCache = [];
private int CountPhonemes(Note n)
{
//ノート歌詞が「ー」の時はOpenJTalkでエラーになるので解析しない
if(string.Equals(n.Lyric, "ー", StringComparison.Ordinal))
{
//母音音素一つになるので1
return 1;
}
if(n.Lyric is null){
return 1;
}
var isCached = fcLabelCache
.TryGetValue(n.Lyric, out var fullContextLab);
var fcLabel = isCached
? fullContextLab!
: GetFullContext(new List<Note> { n });
return fcLabel
.Lines
.Cast<FCLabLineJa>()
.Select(s => s.Phoneme)
//前後sil除外
.Count(s => !string.Equals(s, "sil", StringComparison.Ordinal));
}
/// <summary>
/// ソング用の特殊ラベルを消してフレーズのセリフを得る
/// </summary>
/// <param name="p"></param>
/// <remarks>
/// - 「’」(全角アポストロフィ)はトークにもあるが意味が異なるので除外
/// - 「※$$」(ファルセット)指定はALPで擬似的に再現できるが将来TODO
/// </remarks>
/// <returns></returns>
private static string GetPhraseText(IEnumerable<Note> p)
{
var concated = string
.Concat(p.Select(n => n.Lyric));
//TODO: ユーザー辞書対応
concated = concated
.Replace(
"クヮ", "クワ", StringComparison.InvariantCulture);
if(string.IsNullOrEmpty(concated)){
concated = "ラ";
}
return SpecialLabelRegex()
.Replace(concated, string.Empty);
}
[GeneratedRegex(
"[’※$$@@%%^^__==]",
RegexOptions.None,
matchTimeoutMilliseconds: 1000)]
private static partial Regex SpecialLabelRegex();
private static string GetTsml(
string text,
string pronounce,
string phoneme,
string accent)
{
var bytes = Encoding.UTF8.GetByteCount(pronounce);
return $"""<acoustic_phrase><word begin_byte_index="0" chain="0" end_byte_index="{bytes}" hl="{accent}" original="{text}" phoneme="{phoneme}" pos="感動詞" pronunciation="{pronounce}">{text}</word></acoustic_phrase>""";
}
// とりあえずnoteから算出
// 本当は最初の子音分、前にはみ出したほうがいい
private static string GetStartTimeString(
SongData data,
List<Note> p,
decimal offset = 0.0m
){
var time = LibSasaraUtil
.ClockToTimeSpan(
data.TempoList!,
p[0].Clock
);
var seconds = ((decimal)time.TotalMilliseconds / 1000.0m) - offset;
Debug.WriteLine($"+ clock:{p[0].Clock}, time:{time.TotalMilliseconds}, seconds:{seconds}");
return seconds
.ToString("N3", CultureInfo.InvariantCulture);
}
[GeneratedRegex(
"-([a-zAIUEON]+)+",
RegexOptions.ExplicitCapture,
matchTimeoutMilliseconds: 1000)]
private static partial Regex FullContextLabelRegex();
[GeneratedRegex(@"\|,",
RegexOptions.None,
matchTimeoutMilliseconds: 1000)]
private static partial Regex GetPhonemeRegex();
}
/// <summary>
/// トーク変換用中間ソングデータ
/// </summary>
internal sealed record SongData{
public string? SongTrackName { get; set; }
//フレーズ単位で分割されたNoteのリスト
//フレーズ単位でセリフ化する
public IEnumerable<List<Note>>? PhraseList { get; set; }
//ピッチ調声データ(LogF0)のリスト
public IEnumerable<List<decimal>>? PitchList { get; set; }
//タイミング調声データのリスト
//ccsに記録されたものであれば細かい中間タイミングデータが取れる
//なにもない時はでっち上げ
//labファイルがある時は子音と母音だけなので中間タイミングデータをでっち上げ
//TODO:public IEnumerable<List<>>? TimingList { get; set; }
public Lab? Label { get; set; }
public SortedDictionary<int, decimal>? TempoList { get; set; }
public SortedDictionary<int, (int Beats, int BeatType)>? BeatList { get; set; }
//TODO:将来的に対応するパラメータ
//g Alpha
//g PitchShift => Pitch?
//g PitchTune => Into.
//g Husky
//Volume => VOL
//Alpha => ALP
}
internal enum GetPhonemeMode
{
Phrase = 0,
Note = 1
}