@@ -56,23 +56,23 @@ public float[] Concatenate(List<ResamplerItem> resamplerItems, string tempPath,
56
56
segment . posMs = item . phone . positionMs - item . phone . leadingMs - ( phrase . positionMs - phrase . leadingMs ) ;
57
57
segment . posSamples = ( int ) Math . Round ( segment . posMs * 44100 / 1000 ) ;
58
58
segment . skipSamples = ( int ) Math . Round ( item . skipOver * 44100 / 1000 ) ;
59
- segment . envelope = EnvelopeMsToSamples ( item . phone . envelope , segment . skipSamples ) ;
60
-
61
- if ( ! phaseComp ) {
62
- continue ;
59
+ segment . envelope = item . EnvelopeMsToSamples ( ) ;
60
+
61
+ if ( phaseComp ) {
62
+ var headWindow = GetHeadWindow ( segment . samples , segment . envelope , out segment . headWindowStart ) ;
63
+ segment . headWindowF0 = GetF0AtSample ( phrase ,
64
+ segment . posSamples - segment . skipSamples + segment . headWindowStart + headWindow . Length / 2 ) ;
65
+ segment . headPhase = CalcPhase ( headWindow ,
66
+ segment . posSamples - segment . skipSamples + segment . headWindowStart , 44100 , segment . headWindowF0 ) ;
67
+
68
+ var tailWindow = GetTailWindow ( segment . samples , segment . envelope , out segment . tailWindowStart ) ;
69
+ segment . tailWindowF0 = GetF0AtSample ( phrase ,
70
+ segment . posSamples - segment . skipSamples + segment . tailWindowStart + tailWindow . Length / 2 ) ;
71
+ segment . tailPhase = CalcPhase ( tailWindow ,
72
+ segment . posSamples - segment . skipSamples + segment . tailWindowStart , 44100 , segment . tailWindowF0 ) ;
63
73
}
64
74
65
- var headWindow = GetHeadWindow ( segment . samples , segment . envelope , out segment . headWindowStart ) ;
66
- segment . headWindowF0 = GetF0AtSample ( phrase ,
67
- segment . posSamples - segment . skipSamples + segment . headWindowStart + headWindow . Length / 2 ) ;
68
- segment . headPhase = CalcPhase ( headWindow ,
69
- segment . posSamples - segment . skipSamples + segment . headWindowStart , 44100 , segment . headWindowF0 ) ;
70
-
71
- var tailWindow = GetTailWindow ( segment . samples , segment . envelope , out segment . tailWindowStart ) ;
72
- segment . tailWindowF0 = GetF0AtSample ( phrase ,
73
- segment . posSamples - segment . skipSamples + segment . tailWindowStart + tailWindow . Length / 2 ) ;
74
- segment . tailPhase = CalcPhase ( tailWindow ,
75
- segment . posSamples - segment . skipSamples + segment . tailWindowStart , 44100 , segment . tailWindowF0 ) ;
75
+ item . ApplyEnvelope ( segment . samples ) ;
76
76
}
77
77
78
78
if ( phaseComp ) {
@@ -100,50 +100,13 @@ public float[] Concatenate(List<ResamplerItem> resamplerItems, string tempPath,
100
100
var phraseSamples = new float [ 0 ] ;
101
101
foreach ( var segment in segments ) {
102
102
Array . Resize ( ref phraseSamples , segment . posSamples + segment . correction + segment . samples . Length - segment . skipSamples ) ;
103
- ApplyEnvelope ( segment . samples , segment . envelope ) ;
104
103
for ( int i = Math . Max ( 0 , - segment . skipSamples ) ; i < segment . samples . Length - segment . skipSamples ; i ++ ) {
105
104
phraseSamples [ segment . posSamples + segment . correction + i ] += segment . samples [ segment . skipSamples + i ] ;
106
105
}
107
106
}
108
107
return phraseSamples ;
109
108
}
110
109
111
- private static void ApplyEnvelope ( float [ ] data , IList < Vector2 > envelope ) {
112
- int nextPoint = 0 ;
113
- for ( int i = 0 ; i < data . Length ; ++ i ) {
114
- while ( nextPoint < envelope . Count && i > envelope [ nextPoint ] . X ) {
115
- nextPoint ++ ;
116
- }
117
- float gain ;
118
- if ( nextPoint == 0 ) {
119
- gain = envelope . First ( ) . Y ;
120
- } else if ( nextPoint >= envelope . Count ) {
121
- gain = envelope . Last ( ) . Y ;
122
- } else {
123
- var p0 = envelope [ nextPoint - 1 ] ;
124
- var p1 = envelope [ nextPoint ] ;
125
- if ( p0 . X >= p1 . X ) {
126
- gain = p0 . Y ;
127
- } else {
128
- gain = p0 . Y + ( p1 . Y - p0 . Y ) * ( i - p0 . X ) / ( p1 . X - p0 . X ) ;
129
- }
130
- }
131
- data [ i ] *= gain ;
132
- }
133
- }
134
-
135
- private static IList < Vector2 > EnvelopeMsToSamples ( IList < Vector2 > envelope , int skipOverSamples ) {
136
- envelope = new List < Vector2 > ( envelope ) ;
137
- double shift = - envelope [ 0 ] . X ;
138
- for ( var i = 0 ; i < envelope . Count ; i ++ ) {
139
- var point = envelope [ i ] ;
140
- point . X = ( float ) ( ( point . X + shift ) * 44100 / 1000 ) + skipOverSamples ;
141
- point . Y /= 100 ;
142
- envelope [ i ] = point ;
143
- }
144
- return envelope ;
145
- }
146
-
147
110
private float [ ] GetHeadWindow ( float [ ] samples , IList < Vector2 > envelope , out int windowStart ) {
148
111
var windowCenter = ( envelope [ 0 ] + envelope [ 1 ] ) * 0.5f ;
149
112
windowStart = Math . Max ( ( int ) windowCenter . X - 440 , 0 ) ;
0 commit comments