| 1 |
/* Manipulation.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2012,2014-2023 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "Manipulation.h" |
| 20 |
#include "Sound_to_Pitch.h" |
| 21 |
#include "Pitch_to_PitchTier.h" |
| 22 |
#include "Pitch_AnyTier_to_PitchTier.h" |
| 23 |
#include "PitchTier_to_PointProcess.h" |
| 24 |
#include "Pitch_to_PointProcess.h" |
| 25 |
#include "PointProcess_and_Sound.h" |
| 26 |
#include "Sound_and_LPC.h" |
| 27 |
|
| 28 |
#define MAX_T 0.02000000001 /* Maximum interval between two voice pulses (otherwise voiceless). */ |
| 29 |
|
| 30 |
#include "oo_DESTROY.h" |
| 31 |
#include "Manipulation_def.h" |
| 32 |
#include "oo_COPY.h" |
| 33 |
#include "Manipulation_def.h" |
| 34 |
#include "oo_EQUAL.h" |
| 35 |
#include "Manipulation_def.h" |
| 36 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 37 |
#include "Manipulation_def.h" |
| 38 |
#include "oo_WRITE_TEXT.h" |
| 39 |
#include "Manipulation_def.h" |
| 40 |
#include "oo_READ_TEXT.h" |
| 41 |
#include "Manipulation_def.h" |
| 42 |
#include "oo_WRITE_BINARY.h" |
| 43 |
#include "Manipulation_def.h" |
| 44 |
#include "oo_READ_BINARY.h" |
| 45 |
#include "Manipulation_def.h" |
| 46 |
#include "oo_DESCRIPTION.h" |
| 47 |
#include "Manipulation_def.h" |
| 48 |
|
| 49 |
Thing_implement (Manipulation, Function, 5); |
| 50 |
|
| 51 |
void structManipulation :: v_shiftX (double xfrom, double xto) { |
| 52 |
Manipulation_Parent :: v_shiftX (xfrom, xto); |
| 53 |
if (our sound ) Function_shiftXTo (our sound.get(), xfrom, xto); |
| 54 |
if (our pulses ) Function_shiftXTo (our pulses.get(), xfrom, xto); |
| 55 |
if (our pitch ) Function_shiftXTo (our pitch.get(), xfrom, xto); |
| 56 |
if (our duration) Function_shiftXTo (our duration.get(), xfrom, xto); |
| 57 |
if (our lpc ) Function_shiftXTo (our lpc.get(), xfrom, xto); |
| 58 |
} |
| 59 |
|
| 60 |
void structManipulation :: v_scaleX (double xminfrom, double xmaxfrom, double xminto, double xmaxto) { |
| 61 |
Manipulation_Parent :: v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto); |
| 62 |
if (our sound ) our sound -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto); |
| 63 |
if (our pulses ) our pulses -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto); |
| 64 |
if (our pitch ) our pitch -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto); |
| 65 |
if (our duration) our duration -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto); |
| 66 |
if (our lpc ) our lpc -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto); |
| 67 |
} |
| 68 |
|
| 69 |
autoManipulation Manipulation_create (double tmin, double tmax) { |
| 70 |
try { |
| 71 |
autoManipulation me = Thing_new (Manipulation); |
| 72 |
my xmin = tmin, my xmax = tmax; |
| 73 |
my duration = DurationTier_create (tmin, tmax); |
| 74 |
return me; |
| 75 |
} catch (MelderError) { |
| 76 |
Melder_throw (U"Manipulation object not created."); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void Manipulation_replaceOriginalSound (Manipulation me, Sound sound) { |
| 81 |
try { |
| 82 |
my sound = Sound_convertToMono (sound); |
| 83 |
Vector_subtractMean (my sound.get()); |
| 84 |
my lpc = autoLPC(); |
| 85 |
} catch (MelderError) { |
| 86 |
Melder_throw (me, U": original Sound not replaced with ", sound, U"."); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void Manipulation_replacePulses (Manipulation me, PointProcess pulses) { |
| 91 |
try { |
| 92 |
my pulses = Data_copy (pulses); |
| 93 |
} catch (MelderError) { |
| 94 |
Melder_throw (me, U": pulses not replaced with ", pulses, U"."); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void Manipulation_replacePitchTier (Manipulation me, PitchTier pitch) { |
| 99 |
try { |
| 100 |
my pitch = Data_copy (pitch); |
| 101 |
} catch (MelderError) { |
| 102 |
Melder_throw (me, U": pitch tier not replaced with ", pitch, U"."); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void Manipulation_replaceDurationTier (Manipulation me, DurationTier duration) { |
| 107 |
try { |
| 108 |
my duration = Data_copy (duration); |
| 109 |
} catch (MelderError) { |
| 110 |
Melder_throw (me, U": duration tier not replaced with ", duration, U"."); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
autoManipulation Sound_to_Manipulation (Sound me, double timeStep, double pitchFloor, double pitchCeiling) { |
| 115 |
try { |
| 116 |
autoManipulation thee = Manipulation_create (my xmin, my xmax); |
| 117 |
thy sound = Sound_convertToMono (me); |
| 118 |
Vector_subtractMean (thy sound.get()); |
| 119 |
autoPitch pitch = Sound_to_Pitch (thy sound.get(), timeStep, pitchFloor, pitchCeiling); |
| 120 |
thy pulses = Sound_Pitch_to_PointProcess_cc (thy sound.get(), pitch.get()); |
| 121 |
thy pitch = Pitch_to_PitchTier (pitch.get()); |
| 122 |
/* (DurationTier has been done at creation time) */ |
| 123 |
return thee; |
| 124 |
} catch (MelderError) { |
| 125 |
Melder_throw (me, U": not converted to Manipulation."); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
autoManipulation Sound_Pitch_to_Manipulation (Sound sound, Pitch pitch) { |
| 130 |
try { |
| 131 |
autoManipulation me = Manipulation_create (sound -> xmin, sound -> xmax); |
| 132 |
my sound = Sound_convertToMono (sound); |
| 133 |
Vector_subtractMean (my sound.get()); |
| 134 |
my pulses = Sound_Pitch_to_PointProcess_cc (my sound.get(), pitch); |
| 135 |
my pitch = Pitch_to_PitchTier (pitch); |
| 136 |
return me; |
| 137 |
} catch (MelderError) { |
| 138 |
Melder_throw (sound, U" & ", pitch, U": not converted to Manipulation."); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
autoManipulation Sound_PointProcess_to_Manipulation (Sound sound, PointProcess point) { |
| 143 |
try { |
| 144 |
autoManipulation me = Manipulation_create (sound -> xmin, sound -> xmax); |
| 145 |
my sound = Sound_convertToMono (sound); |
| 146 |
Vector_subtractMean (my sound.get()); |
| 147 |
my pulses = Data_copy (point); |
| 148 |
my pitch = PointProcess_to_PitchTier (point, MAX_T); |
| 149 |
return me; |
| 150 |
} catch (MelderError) { |
| 151 |
Melder_throw (sound, U" & ", point, U": not converted to Manipulation."); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void Manipulation_playPart (Manipulation me, double tmin, double tmax, int method) { |
| 156 |
try { |
| 157 |
if (method == Manipulation_OVERLAPADD) { |
| 158 |
if (! my sound) |
| 159 |
Melder_throw (U"Cannot synthesize overlap-add without a sound."); |
| 160 |
autoSound part = Data_copy (my sound.get()); |
| 161 |
integer imin = Sampled_xToLowIndex (part.get(), tmin), imax = Sampled_xToHighIndex (part.get(), tmax); |
| 162 |
VEC amp = part -> z.row (1); |
| 163 |
for (integer i = 1; i <= imin; i ++) |
| 164 |
amp [i] = 0.0; |
| 165 |
for (integer i = imax; i <= part -> nx; i ++) |
| 166 |
amp [i] = 0.0; |
| 167 |
autoSound saved = my sound.move(); |
| 168 |
my sound = part.move(); |
| 169 |
try { |
| 170 |
autoSound played = Manipulation_to_Sound (me, Manipulation_OVERLAPADD); |
| 171 |
my sound = saved.move(); |
| 172 |
amp = played -> z.row (1); |
| 173 |
for (imin = 1; imin <= played -> nx; imin ++) |
| 174 |
if (amp [imin] != 0.0) |
| 175 |
break; |
| 176 |
for (imax = played -> nx; imax >= 1; imax --) |
| 177 |
if (amp [imax] != 0.0) |
| 178 |
break; |
| 179 |
Sound_playPart (played.get(), played -> x1 + (imin - 1.5) * played -> dx, played -> x1 + (imax - 0.5) * played -> dx, nullptr, nullptr); |
| 180 |
} catch (MelderError) { |
| 181 |
my sound = saved.move(); |
| 182 |
throw; |
| 183 |
} |
| 184 |
} else { |
| 185 |
autoSound sound = Manipulation_to_Sound (me, method); |
| 186 |
Sound_playPart (sound.get(), tmin, tmax, nullptr, nullptr); |
| 187 |
} |
| 188 |
} catch (MelderError) { |
| 189 |
Melder_throw (me, U": not played."); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void Manipulation_play (Manipulation me, int method) { |
| 194 |
try { |
| 195 |
autoSound sound = Manipulation_to_Sound (me, method); |
| 196 |
Sound_play (sound.get(), nullptr, nullptr); |
| 197 |
} catch (MelderError) { |
| 198 |
Melder_throw (me, U": not played."); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
static integer PointProcess_getFirstVoicedPoint (PointProcess me, double maxT) { |
| 203 |
for (integer i = 1; i < my nt; i ++) |
| 204 |
if (my t [i + 1] - my t [i] <= maxT) |
| 205 |
return i; |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
static void copyRise (Sound me, double tmin, double tmax, Sound thee, double tmaxTarget) { |
| 210 |
integer imin = Sampled_xToHighIndex (me, tmin); |
| 211 |
if (imin < 1) |
| 212 |
imin = 1; |
| 213 |
integer imax = Sampled_xToHighIndex (me, tmax) - 1; // not xToLowIndex: ensure separation of subsequent calls |
| 214 |
if (imax > my nx) |
| 215 |
imax = my nx; |
| 216 |
if (imax < imin) |
| 217 |
return; |
| 218 |
integer imaxTarget = Sampled_xToHighIndex (thee, tmaxTarget) - 1; |
| 219 |
integer distance = imaxTarget - imax; |
| 220 |
double dphase = NUMpi / (imax - imin + 1); |
| 221 |
for (integer i = imin; i <= imax; i ++) { |
| 222 |
integer iTarget = i + distance; |
| 223 |
if (iTarget >= 1 && iTarget <= thy nx) |
| 224 |
thy z [1] [iTarget] += my z [1] [i] * 0.5 * (1.0 - cos (dphase * (i - imin + 0.5))); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static void copyFall (Sound me, double tmin, double tmax, Sound thee, double tminTarget) { |
| 229 |
integer imin = Sampled_xToHighIndex (me, tmin); |
| 230 |
if (imin < 1) |
| 231 |
imin = 1; |
| 232 |
integer imax = Sampled_xToHighIndex (me, tmax) - 1; // not xToLowIndex: ensure separation of subsequent calls |
| 233 |
if (imax > my nx) |
| 234 |
imax = my nx; |
| 235 |
if (imax < imin) |
| 236 |
return; |
| 237 |
integer iminTarget = Sampled_xToHighIndex (thee, tminTarget); |
| 238 |
integer distance = iminTarget - imin; |
| 239 |
double dphase = NUMpi / (imax - imin + 1); |
| 240 |
for (integer i = imin; i <= imax; i ++) { |
| 241 |
integer iTarget = i + distance; |
| 242 |
if (iTarget >= 1 && iTarget <= thy nx) |
| 243 |
thy z [1] [iTarget] += my z [1] [i] * 0.5 * (1.0 + cos (dphase * (i - imin + 0.5))); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
static void copyBell (Sound me, double tmid, double leftWidth, double rightWidth, Sound thee, double tmidTarget) { |
| 248 |
copyRise (me, tmid - leftWidth, tmid, thee, tmidTarget); |
| 249 |
copyFall (me, tmid, tmid + rightWidth, thee, tmidTarget); |
| 250 |
} |
| 251 |
|
| 252 |
static void copyBell2 (Sound me, PointProcess source, integer isource, double leftWidth, double rightWidth, |
| 253 |
Sound thee, double tmidTarget, double maxT) |
| 254 |
{ |
| 255 |
/* |
| 256 |
Replace 'leftWidth' and 'rightWidth' by the lengths of the intervals in the source (instead of target), |
| 257 |
if these are shorter. |
| 258 |
*/ |
| 259 |
const double tmid = source -> t [isource]; |
| 260 |
if (isource > 1 && tmid - source -> t [isource - 1] <= maxT) { |
| 261 |
const double sourceLeftWidth = tmid - source -> t [isource - 1]; |
| 262 |
if (sourceLeftWidth < leftWidth) |
| 263 |
leftWidth = sourceLeftWidth; |
| 264 |
} |
| 265 |
if (isource < source -> nt && source -> t [isource + 1] - tmid <= maxT) { |
| 266 |
const double sourceRightWidth = source -> t [isource + 1] - tmid; |
| 267 |
if (sourceRightWidth < rightWidth) |
| 268 |
rightWidth = sourceRightWidth; |
| 269 |
} |
| 270 |
copyBell (me, tmid, leftWidth, rightWidth, thee, tmidTarget); |
| 271 |
} |
| 272 |
|
| 273 |
static void copyFlat (Sound me, double tmin, double tmax, Sound thee, double tminTarget) { |
| 274 |
integer imin = Sampled_xToHighIndex (me, tmin); |
| 275 |
if (imin < 1) imin = 1; |
| 276 |
integer imax = Sampled_xToHighIndex (me, tmax) - 1; // not xToLowIndex: ensure separation of subsequent calls |
| 277 |
if (imax > my nx) imax = my nx; |
| 278 |
if (imax < imin) return; |
| 279 |
integer iminTarget = Sampled_xToHighIndex (thee, tminTarget); |
| 280 |
if (iminTarget < 1) iminTarget = 1; |
| 281 |
trace (tmin, U" ", tmax, U" ", tminTarget, U" ", imin, U" ", imax, U" ", iminTarget); |
| 282 |
const integer imaxTarget = iminTarget + (imax - imin); |
| 283 |
Melder_assert (imaxTarget <= thy nx); |
| 284 |
thy z.row (1).part (iminTarget, imaxTarget) <<= my z.row (1).part (imin, imax); |
| 285 |
} |
| 286 |
|
| 287 |
autoSound Sound_Point_Point_to_Sound (Sound me, PointProcess source, PointProcess target, double maxT) { |
| 288 |
try { |
| 289 |
autoSound thee = Sound_create (1, my xmin, my xmax, my nx, my dx, my x1); |
| 290 |
if (source -> nt < 2 || target -> nt < 2) { // almost completely voiceless? |
| 291 |
thy z.all() <<= my z.all(); |
| 292 |
return thee; |
| 293 |
} |
| 294 |
for (integer i = 1; i <= target -> nt; i ++) { |
| 295 |
double tmid = target -> t [i]; |
| 296 |
double tleft = i > 1 ? target -> t [i - 1] : my xmin; |
| 297 |
double tright = i < target -> nt ? target -> t [i + 1] : my xmax; |
| 298 |
double leftWidth = tmid - tleft, rightWidth = tright - tmid; |
| 299 |
int leftVoiced = i > 1 && leftWidth <= maxT; |
| 300 |
int rightVoiced = i < target -> nt && rightWidth <= maxT; |
| 301 |
integer isource = PointProcess_getNearestIndex (source, tmid); |
| 302 |
if (! leftVoiced) leftWidth = rightWidth; // symmetric bell |
| 303 |
if (! rightVoiced) rightWidth = leftWidth; // symmetric bell |
| 304 |
if (leftVoiced || rightVoiced) { |
| 305 |
copyBell2 (me, source, isource, leftWidth, rightWidth, thee.get(), tmid, maxT); |
| 306 |
if (! leftVoiced) { |
| 307 |
double startOfFlat = ( i == 1 ? tleft : (tleft + tmid) / 2.0 ); |
| 308 |
double endOfFlat = tmid - leftWidth; |
| 309 |
copyFlat (me, startOfFlat, endOfFlat, thee.get(), startOfFlat); |
| 310 |
copyFall (me, endOfFlat, tmid, thee.get(), endOfFlat); |
| 311 |
} else if (! rightVoiced) { |
| 312 |
double startOfFlat = tmid + rightWidth; |
| 313 |
double endOfFlat = ( i == target -> nt ? tright : (tmid + tright) / 2.0 ); |
| 314 |
copyRise (me, tmid, startOfFlat, thee.get(), startOfFlat); |
| 315 |
copyFlat (me, startOfFlat, endOfFlat, thee.get(), startOfFlat); |
| 316 |
} |
| 317 |
} else { |
| 318 |
double startOfFlat = ( i == 1 ? tleft : (tleft + tmid) / 2.0 ); |
| 319 |
double endOfFlat = ( i == target -> nt ? tright : (tmid + tright) / 2.0 ); |
| 320 |
copyFlat (me, startOfFlat, endOfFlat, thee.get(), startOfFlat); |
| 321 |
} |
| 322 |
} |
| 323 |
return thee; |
| 324 |
} catch (MelderError) { |
| 325 |
Melder_throw (me, U": not manipulated."); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
autoSound Sound_Point_Pitch_Duration_to_Sound (Sound me, PointProcess pulses, |
| 330 |
PitchTier pitch, DurationTier duration, double maxT) |
| 331 |
{ |
| 332 |
try { |
| 333 |
integer ipointleft, ipointright; |
| 334 |
double deltat = 0.0, handledTime = my xmin; |
| 335 |
double startOfSourceNoise, endOfSourceNoise, startOfTargetNoise, endOfTargetNoise; |
| 336 |
double durationOfSourceNoise, durationOfTargetNoise; |
| 337 |
double startOfSourceVoice, endOfSourceVoice, startOfTargetVoice, endOfTargetVoice; |
| 338 |
double durationOfSourceVoice, durationOfTargetVoice; |
| 339 |
double startingPeriod, finishingPeriod, ttarget, voicelessPeriod; |
| 340 |
if (duration -> points.size == 0) |
| 341 |
Melder_throw (U"No duration points."); |
| 342 |
|
| 343 |
/* |
| 344 |
* Create a Sound long enough to hold the longest possible duration-manipulated sound. |
| 345 |
*/ |
| 346 |
autoSound thee = Sound_create (1, my xmin, my xmin + 3 * (my xmax - my xmin), 3 * my nx, my dx, my x1); |
| 347 |
|
| 348 |
/* |
| 349 |
* Below, I'll abbreviate the voiced interval as "voice" and the voiceless interval as "noise". |
| 350 |
*/ |
| 351 |
if (pitch && pitch -> points.size) for (ipointleft = 1; ipointleft <= pulses -> nt; ipointleft = ipointright + 1) { |
| 352 |
/* |
| 353 |
* Find the beginning of the voice. |
| 354 |
*/ |
| 355 |
startOfSourceVoice = pulses -> t [ipointleft]; // the first pulse of the voice |
| 356 |
startingPeriod = 1.0 / RealTier_getValueAtTime (pitch, startOfSourceVoice); |
| 357 |
startOfSourceVoice -= 0.5 * startingPeriod; // the first pulse is in the middle of a period |
| 358 |
|
| 359 |
/* |
| 360 |
* Measure one noise. |
| 361 |
*/ |
| 362 |
startOfSourceNoise = handledTime; |
| 363 |
endOfSourceNoise = startOfSourceVoice; |
| 364 |
durationOfSourceNoise = endOfSourceNoise - startOfSourceNoise; |
| 365 |
startOfTargetNoise = startOfSourceNoise + deltat; |
| 366 |
endOfTargetNoise = startOfTargetNoise + RealTier_getArea (duration, startOfSourceNoise, endOfSourceNoise); |
| 367 |
durationOfTargetNoise = endOfTargetNoise - startOfTargetNoise; |
| 368 |
|
| 369 |
/* |
| 370 |
* Copy the noise. |
| 371 |
*/ |
| 372 |
voicelessPeriod = NUMrandomUniform (0.008, 0.012); |
| 373 |
ttarget = startOfTargetNoise + 0.5 * voicelessPeriod; |
| 374 |
while (ttarget < endOfTargetNoise) { |
| 375 |
double tleft = startOfSourceNoise, tright = endOfSourceNoise; |
| 376 |
for (int i = 1; i <= 15; i ++) { |
| 377 |
const double tsourcemid = 0.5 * (tleft + tright); |
| 378 |
const double ttargetmid = startOfTargetNoise + RealTier_getArea (duration, startOfSourceNoise, tsourcemid); |
| 379 |
if (ttargetmid < ttarget) |
| 380 |
tleft = tsourcemid; |
| 381 |
else |
| 382 |
tright = tsourcemid; |
| 383 |
} |
| 384 |
const double tsource = 0.5 * (tleft + tright); |
| 385 |
copyBell (me, tsource, voicelessPeriod, voicelessPeriod, thee.get(), ttarget); |
| 386 |
voicelessPeriod = NUMrandomUniform (0.008, 0.012); |
| 387 |
ttarget += voicelessPeriod; |
| 388 |
} |
| 389 |
deltat += durationOfTargetNoise - durationOfSourceNoise; |
| 390 |
|
| 391 |
/* |
| 392 |
* Find the end of the voice. |
| 393 |
*/ |
| 394 |
for (ipointright = ipointleft + 1; ipointright <= pulses -> nt; ipointright ++) |
| 395 |
if (pulses -> t [ipointright] - pulses -> t [ipointright - 1] > maxT) |
| 396 |
break; |
| 397 |
ipointright --; |
| 398 |
endOfSourceVoice = pulses -> t [ipointright]; // the last pulse of the voice |
| 399 |
const double finishingPitch = RealTier_getValueAtTime (pitch, endOfSourceVoice); |
| 400 |
if (finishingPitch == 0.0) { |
| 401 |
for (integer ipoint = 1; ipoint <= pitch -> points.size; ipoint ++) |
| 402 |
Melder_casual (U"Pitch point ", ipoint, U" is ", pitch -> points.at [ipoint], U" Hz"); |
| 403 |
Melder_casual (U"ipointleft = ", ipointleft); |
| 404 |
Melder_throw (U"Unexpected zero pitch value."); |
| 405 |
} |
| 406 |
finishingPeriod = 1.0 / finishingPitch; |
| 407 |
endOfSourceVoice += 0.5 * finishingPeriod; // the last pulse is in the middle of a period |
| 408 |
/* |
| 409 |
* Measure one voice. |
| 410 |
*/ |
| 411 |
durationOfSourceVoice = endOfSourceVoice - startOfSourceVoice; |
| 412 |
|
| 413 |
/* |
| 414 |
* This will be copied to an interval with a different location and duration. |
| 415 |
*/ |
| 416 |
startOfTargetVoice = startOfSourceVoice + deltat; |
| 417 |
endOfTargetVoice = startOfTargetVoice + |
| 418 |
RealTier_getArea (duration, startOfSourceVoice, endOfSourceVoice); |
| 419 |
durationOfTargetVoice = endOfTargetVoice - startOfTargetVoice; |
| 420 |
|
| 421 |
/* |
| 422 |
* Copy the voiced part. |
| 423 |
*/ |
| 424 |
ttarget = startOfTargetVoice + 0.5 * startingPeriod; |
| 425 |
while (ttarget < endOfTargetVoice) { |
| 426 |
double tleft = startOfSourceVoice, tright = endOfSourceVoice; |
| 427 |
for (int i = 1; i <= 15; i ++) { |
| 428 |
const double tsourcemid = 0.5 * (tleft + tright); |
| 429 |
const double ttargetmid = startOfTargetVoice + |
| 430 |
RealTier_getArea (duration, startOfSourceVoice, tsourcemid); |
| 431 |
if (ttargetmid < ttarget) |
| 432 |
tleft = tsourcemid; |
| 433 |
else |
| 434 |
tright = tsourcemid; |
| 435 |
} |
| 436 |
const double tsource = 0.5 * (tleft + tright); |
| 437 |
const double period = 1.0 / RealTier_getValueAtTime (pitch, tsource); |
| 438 |
const integer isourcepulse = PointProcess_getNearestIndex (pulses, tsource); |
| 439 |
copyBell2 (me, pulses, isourcepulse, period, period, thee.get(), ttarget, maxT); |
| 440 |
ttarget += period; |
| 441 |
} |
| 442 |
deltat += durationOfTargetVoice - durationOfSourceVoice; |
| 443 |
handledTime = endOfSourceVoice; |
| 444 |
} |
| 445 |
|
| 446 |
/* |
| 447 |
* Copy the remaining unvoiced part, if we are at the end. |
| 448 |
*/ |
| 449 |
startOfSourceNoise = handledTime; |
| 450 |
endOfSourceNoise = my xmax; |
| 451 |
durationOfSourceNoise = endOfSourceNoise - startOfSourceNoise; |
| 452 |
startOfTargetNoise = startOfSourceNoise + deltat; |
| 453 |
endOfTargetNoise = startOfTargetNoise + RealTier_getArea (duration, startOfSourceNoise, endOfSourceNoise); |
| 454 |
durationOfTargetNoise = endOfTargetNoise - startOfTargetNoise; |
| 455 |
voicelessPeriod = NUMrandomUniform (0.008, 0.012); |
| 456 |
ttarget = startOfTargetNoise + 0.5 * voicelessPeriod; |
| 457 |
while (ttarget < endOfTargetNoise) { |
| 458 |
double tleft = startOfSourceNoise, tright = endOfSourceNoise; |
| 459 |
for (int i = 1; i <= 15; i ++) { |
| 460 |
const double tsourcemid = 0.5 * (tleft + tright); |
| 461 |
const double ttargetmid = startOfTargetNoise + |
| 462 |
RealTier_getArea (duration, startOfSourceNoise, tsourcemid); |
| 463 |
if (ttargetmid < ttarget) |
| 464 |
tleft = tsourcemid; |
| 465 |
else |
| 466 |
tright = tsourcemid; |
| 467 |
} |
| 468 |
const double tsource = 0.5 * (tleft + tright); |
| 469 |
copyBell (me, tsource, voicelessPeriod, voicelessPeriod, thee.get(), ttarget); |
| 470 |
voicelessPeriod = NUMrandomUniform (0.008, 0.012); |
| 471 |
ttarget += voicelessPeriod; |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* Find the number of trailing zeroes and hack the sound's time domain. |
| 476 |
*/ |
| 477 |
thy xmax = thy xmin + RealTier_getArea (duration, my xmin, my xmax); |
| 478 |
if (fabs (thy xmax - my xmax) < 1e-12) // common situation |
| 479 |
thy xmax = my xmax; |
| 480 |
thy nx = Sampled_xToLowIndex (thee.get(), thy xmax); |
| 481 |
if (thy nx > 3 * my nx) |
| 482 |
thy nx = 3 * my nx; |
| 483 |
thy z.ncol = thy nx; // maintain invariant |
| 484 |
|
| 485 |
return thee; |
| 486 |
} catch (MelderError) { |
| 487 |
Melder_throw (me, U": not manipulated."); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
static autoSound synthesize_overlapAdd_nodur (Manipulation me) { |
| 492 |
try { |
| 493 |
if (! my sound) Melder_throw (U"Missing original sound."); |
| 494 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 495 |
if (! my pitch) Melder_throw (U"Missing pitch manipulation."); |
| 496 |
autoPointProcess targetPulses = PitchTier_Point_to_PointProcess (my pitch.get(), my pulses.get(), MAX_T); |
| 497 |
return Sound_Point_Point_to_Sound (my sound.get(), my pulses.get(), targetPulses.get(), MAX_T); |
| 498 |
} catch (MelderError) { |
| 499 |
Melder_throw (me, U": overlap-add synthesis (without duration) not performed."); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
static autoSound synthesize_overlapAdd (Manipulation me) { |
| 504 |
if (! my duration || my duration -> points.size == 0) return synthesize_overlapAdd_nodur (me); |
| 505 |
try { |
| 506 |
if (! my sound) Melder_throw (U"Missing original sound."); |
| 507 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 508 |
if (! my pitch) Melder_throw (U"Missing pitch manipulation."); |
| 509 |
return Sound_Point_Pitch_Duration_to_Sound (my sound.get(), my pulses.get(), my pitch.get(), my duration.get(), MAX_T); |
| 510 |
} catch (MelderError) { |
| 511 |
Melder_throw (me, U": overlap-add synthesis not performed."); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
static autoSound synthesize_pulses (Manipulation me) { |
| 516 |
try { |
| 517 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 518 |
return PointProcess_to_Sound_pulseTrain (my pulses.get(), 44100.0, 0.7, 0.05, 30); |
| 519 |
} catch (MelderError) { |
| 520 |
Melder_throw (me, U": pulses synthesis not performed."); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
static autoSound synthesize_pulses_hum (Manipulation me) { |
| 525 |
try { |
| 526 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 527 |
return PointProcess_to_Sound_hum (my pulses.get()); |
| 528 |
} catch (MelderError) { |
| 529 |
Melder_throw (me, U": pulses hum synthesis not performed."); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
static autoSound synthesize_pitch (Manipulation me) { |
| 534 |
try { |
| 535 |
if (! my pitch) Melder_throw (U"Missing pitch tier."); |
| 536 |
autoPointProcess pulses = PitchTier_to_PointProcess (my pitch.get()); |
| 537 |
return PointProcess_to_Sound_pulseTrain (pulses.get(), 44100.0, 0.7, 0.05, 30); |
| 538 |
} catch (MelderError) { |
| 539 |
Melder_throw (me, U": pitch manipulation not synthesized."); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
static autoSound synthesize_pitch_hum (Manipulation me) { |
| 544 |
try { |
| 545 |
if (! my pitch) Melder_throw (U"Missing pitch tier."); |
| 546 |
autoPointProcess pulses = PitchTier_to_PointProcess (my pitch.get()); |
| 547 |
return PointProcess_to_Sound_hum (pulses.get()); |
| 548 |
} catch (MelderError) { |
| 549 |
Melder_throw (me, U": pitch hum manipulation not synthesized."); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
static autoSound synthesize_pulses_pitch (Manipulation me) { |
| 554 |
try { |
| 555 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 556 |
if (! my pitch) Melder_throw (U"Missing pitch tier."); |
| 557 |
autoPointProcess pulses = PitchTier_Point_to_PointProcess (my pitch.get(), my pulses.get(), MAX_T); |
| 558 |
return PointProcess_to_Sound_pulseTrain (pulses.get(), 44100.0, 0.7, 0.05, 30); |
| 559 |
} catch (MelderError) { |
| 560 |
Melder_throw (me, U": pitch pulses manipulation not synthesized."); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
static autoSound synthesize_pulses_pitch_hum (Manipulation me) { |
| 565 |
try { |
| 566 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 567 |
if (! my pitch) Melder_throw (U"Missing pitch tier."); |
| 568 |
autoPointProcess pulses = PitchTier_Point_to_PointProcess (my pitch.get(), my pulses.get(), MAX_T); |
| 569 |
return PointProcess_to_Sound_hum (pulses.get()); |
| 570 |
} catch (MelderError) { |
| 571 |
Melder_throw (me, U": pitch pulses hum manipulation not synthesized."); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
/* |
| 576 |
void Sound_Formant_Intensity_filter (Sound me, FormantTier formantTier, IntensityTier intensity) { |
| 577 |
Sound_FormantTier_filter_inplace (me, formantTier); |
| 578 |
if (intensity) |
| 579 |
Sound_IntensityTier_multiply_inplace (me, intensity); |
| 580 |
VECdeemphasize_f_inplace (my z.row (1), my dx, 50.0); |
| 581 |
Vector_scale (me, 0.99); |
| 582 |
} |
| 583 |
|
| 584 |
static Sound synthesize_pulses_formant (Manipulation me, int useIntensity) { |
| 585 |
try { |
| 586 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 587 |
if (! my formant) Melder_throw (U"Missing formant information."); |
| 588 |
autoSound thee = PointProcess_to_Sound (my pulses, 44100, 0.7, 0.05, 30); |
| 589 |
Sound_Formant_Intensity_filter (get(), my formant, useIntensity ? my intensity : nullptr); |
| 590 |
return thee; |
| 591 |
} catch (MelderError) { |
| 592 |
Melder_throw (me, U": formant and pulses manipulation not synthesized."); |
| 593 |
} |
| 594 |
} |
| 595 |
*/ |
| 596 |
|
| 597 |
static void Sound_PointProcess_fillVoiceless (Sound me, PointProcess pulses) { |
| 598 |
integer ipointleft, ipointright; |
| 599 |
double beginVoiceless = my xmin, endVoiceless; |
| 600 |
for (ipointleft = 1; ipointleft <= pulses -> nt; ipointleft = ipointright + 1) { |
| 601 |
integer i1, i2, i; |
| 602 |
endVoiceless = pulses -> t [ipointleft] - 0.005; |
| 603 |
i1 = Sampled_xToHighIndex (me, beginVoiceless); |
| 604 |
if (i1 < 1) i1 = 1; if (i1 > my nx) i1 = my nx; |
| 605 |
i2 = Sampled_xToLowIndex (me, endVoiceless); |
| 606 |
if (i2 < 1) i2 = 1; if (i2 > my nx) i2 = my nx; |
| 607 |
if (i2 - i1 > 10) for (i = i1; i <= i2; i ++) |
| 608 |
my z [1] [i] = NUMrandomGauss (0.0, 0.3); |
| 609 |
for (ipointright = ipointleft + 1; ipointright <= pulses -> nt; ipointright ++) |
| 610 |
if (pulses -> t [ipointright] - pulses -> t [ipointright - 1] > MAX_T) |
| 611 |
break; |
| 612 |
ipointright --; |
| 613 |
beginVoiceless = pulses -> t [ipointright] + 0.005; |
| 614 |
} |
| 615 |
endVoiceless = my xmax; |
| 616 |
{ |
| 617 |
integer i1, i2, i; |
| 618 |
i1 = Sampled_xToHighIndex (me, beginVoiceless); |
| 619 |
if (i1 < 1) i1 = 1; if (i1 > my nx) i1 = my nx; |
| 620 |
i2 = Sampled_xToLowIndex (me, endVoiceless); |
| 621 |
if (i2 < 1) i2 = 1; if (i2 > my nx) i2 = my nx; |
| 622 |
if (i2 - i1 > 10) for (i = i1; i <= i2; i ++) |
| 623 |
my z [1] [i] = NUMrandomGauss (0.0, 0.3); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
static autoSound synthesize_pulses_lpc (Manipulation me) { |
| 628 |
try { |
| 629 |
if (! my lpc) { |
| 630 |
if (! my sound) Melder_throw (U"Missing original sound."); |
| 631 |
autoSound sound10k = Sound_resample (my sound.get(), 10000.0, 50); |
| 632 |
my lpc = Sound_to_LPC_burg (sound10k.get(), 20, 0.025, 0.01, 50.0); |
| 633 |
} |
| 634 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 635 |
autoSound train = PointProcess_to_Sound_pulseTrain (my pulses.get(), 1.0 / my lpc -> samplingPeriod, 0.7, 0.05, 30); |
| 636 |
train -> dx = my lpc -> samplingPeriod; // to be exact |
| 637 |
Sound_PointProcess_fillVoiceless (train.get(), my pulses.get()); |
| 638 |
autoSound result = LPC_Sound_filter (my lpc.get(), train.get(), true); |
| 639 |
VECdeemphasize_f_inplace (result -> z.row (1), result -> dx, 50.0); |
| 640 |
Vector_scale (result.get(), 0.99); |
| 641 |
return result; |
| 642 |
} catch (MelderError) { |
| 643 |
Melder_throw (me, U": LPC synthesis not performed."); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
static autoSound synthesize_pitch_lpc (Manipulation me) { |
| 648 |
try { |
| 649 |
if (! my lpc) { |
| 650 |
if (! my sound) Melder_throw (U"Missing original sound."); |
| 651 |
autoSound sound10k = Sound_resample (my sound.get(), 10000.0, 50); |
| 652 |
my lpc = Sound_to_LPC_burg (sound10k.get(), 20, 0.025, 0.01, 50.0); |
| 653 |
} |
| 654 |
if (! my pitch) Melder_throw (U"Missing pitch manipulation."); |
| 655 |
if (! my pulses) Melder_throw (U"Missing pulses analysis."); |
| 656 |
autoPointProcess pulses = PitchTier_Point_to_PointProcess (my pitch.get(), my pulses.get(), MAX_T); |
| 657 |
autoSound train = PointProcess_to_Sound_pulseTrain (pulses.get(), 1 / my lpc -> samplingPeriod, 0.7, 0.05, 30); |
| 658 |
train -> dx = my lpc -> samplingPeriod; // to be exact |
| 659 |
Sound_PointProcess_fillVoiceless (train.get(), my pulses.get()); |
| 660 |
autoSound result = LPC_Sound_filter (my lpc.get(), train.get(), true); |
| 661 |
VECdeemphasize_f_inplace (result -> z.row (1), result -> dx, 50.0); |
| 662 |
Vector_scale (result.get(), 0.99); |
| 663 |
return result; |
| 664 |
} catch (MelderError) { |
| 665 |
Melder_throw (me, U": pitch LPC synthesis not performed."); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
autoSound Manipulation_to_Sound (Manipulation me, int method) { |
| 670 |
switch (method) { |
| 671 |
case Manipulation_OVERLAPADD: return synthesize_overlapAdd (me); |
| 672 |
case Manipulation_PULSES: return synthesize_pulses (me); |
| 673 |
case Manipulation_PULSES_HUM: return synthesize_pulses_hum (me); |
| 674 |
case Manipulation_PITCH: return synthesize_pitch (me); |
| 675 |
case Manipulation_PITCH_HUM: return synthesize_pitch_hum (me); |
| 676 |
case Manipulation_PULSES_PITCH: return synthesize_pulses_pitch (me); |
| 677 |
case Manipulation_PULSES_PITCH_HUM: return synthesize_pulses_pitch_hum (me); |
| 678 |
case Manipulation_OVERLAPADD_NODUR: return synthesize_overlapAdd_nodur (me); |
| 679 |
case Manipulation_PULSES_FORMANT: return autoSound(); |
| 680 |
case Manipulation_PULSES_FORMANT_INTENSITY: return autoSound(); |
| 681 |
case Manipulation_PULSES_LPC: return synthesize_pulses_lpc (me); |
| 682 |
case Manipulation_PULSES_LPC_INTENSITY: return autoSound(); |
| 683 |
case Manipulation_PITCH_LPC: return synthesize_pitch_lpc (me); |
| 684 |
case Manipulation_PITCH_LPC_INTENSITY: return autoSound(); |
| 685 |
default: return synthesize_overlapAdd (me); |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
autoManipulation Manipulation_AnyTier_to_Manipulation (Manipulation me, AnyTier tier) { |
| 690 |
try { |
| 691 |
if (! my pitch) Melder_throw (U"Missing pitch manipulation."); |
| 692 |
autoManipulation result = Data_copy (me); |
| 693 |
result -> pitch = PitchTier_AnyTier_to_PitchTier (my pitch.get(), tier); |
| 694 |
return result; |
| 695 |
} catch (MelderError) { |
| 696 |
Melder_throw (me, U": not converted."); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
void Manipulation_writeToTextFileWithoutSound (Manipulation me, MelderFile file) { |
| 701 |
autoSound saved = my sound.move(); |
| 702 |
try { |
| 703 |
Data_writeToTextFile (me, file); |
| 704 |
my sound = saved.move(); |
| 705 |
} catch (MelderError) { |
| 706 |
my sound = saved.move(); |
| 707 |
Melder_throw (me, U": not saved to text file."); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
void Manipulation_writeToBinaryFileWithoutSound (Manipulation me, MelderFile file) { |
| 712 |
autoSound saved = my sound.move(); |
| 713 |
try { |
| 714 |
Data_writeToBinaryFile (me, file); |
| 715 |
my sound = saved.move(); |
| 716 |
} catch (MelderError) { |
| 717 |
my sound = saved.move(); |
| 718 |
Melder_throw (me, U": not saved to binary file."); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
/* End of file Manipulation.cpp */ |