| 1 |
/* TextGrid_and_PitchTier.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2017-2021,2026 David Weenink |
| 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 3 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. See the GNU |
| 13 |
* 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 "Interpreter.h" |
| 20 |
#include "NUM2.h" |
| 21 |
#include "RealTier.h" |
| 22 |
#include "Strings_extensions.h" |
| 23 |
#include "TextGrid_and_PitchTier.h" |
| 24 |
#include "Thing.h" |
| 25 |
|
| 26 |
|
| 27 |
#define TIME_OFFSET_AS_FRACTION_FROM_START 1 |
| 28 |
#define TIME_OFFSET_AS_PERCENTAGE_FROM_START 2 |
| 29 |
#define TIME_OFFSET_AS_SECONDS_FROM_START 3 |
| 30 |
|
| 31 |
#define PITCH_VALUE_AS_FREQUENCY 1 |
| 32 |
#define PITCH_VALUE_AS_FRACTION 2 |
| 33 |
#define PITCH_VALUE_AS_PERCENTAGE 3 |
| 34 |
#define PITCH_VALUE_AS_START_AND_SLOPES 4 |
| 35 |
#define PITCH_VALUE_AS_SLOPES_AND_END 5 |
| 36 |
#define PITCH_VALUE_AS_MUSIC_NOTE 6 |
| 37 |
#define PITCH_VALUE_AS_SEMITONES 7 |
| 38 |
|
| 39 |
#define PITCH_UNIT_HERTZ 1 |
| 40 |
|
| 41 |
#define PITCH_ANCHOR_IS_NOT_USED 1 |
| 42 |
#define PITCH_ANCHOR_IS_CURRENT 2 |
| 43 |
#define PITCH_ANCHOR_IS_START 3 |
| 44 |
#define PITCH_ANCHOR_IS_END 4 |
| 45 |
#define PITCH_ANCHOR_IS_MEAN_OF_CURVE 5 |
| 46 |
#define PITCH_ANCHOR_IS_MEAN_OF_POINTS 6 |
| 47 |
#define PITCH_ANCHOR_IS_MAXIMUM 7 |
| 48 |
#define PITCH_ANCHOR_IS_MINIMUM 8 |
| 49 |
|
| 50 |
static double RealTier_getMinimumValue_interval (RealTier me, double tmin, double tmax) { |
| 51 |
integer imin, imax; |
| 52 |
(void) AnyTier_getWindowPoints ((AnyTier) me, tmin, tmax, & imin, & imax); |
| 53 |
double result = undefined; |
| 54 |
for (integer i = imin; i <= imax; i ++) { |
| 55 |
const RealPoint point = my points.at [i]; |
| 56 |
if (isundef (result) || point -> value < result) |
| 57 |
result = point -> value; |
| 58 |
} |
| 59 |
return result; |
| 60 |
} |
| 61 |
|
| 62 |
static double RealTier_getMaximumValue_interval (RealTier me, double tmin, double tmax) { |
| 63 |
integer imin, imax; |
| 64 |
(void) AnyTier_getWindowPoints ((AnyTier) me, tmin, tmax, & imin, & imax); |
| 65 |
double result = undefined; |
| 66 |
for (integer i = imin; i <= imax; i ++) { |
| 67 |
const RealPoint point = my points.at [i]; |
| 68 |
if (isundef (result) || point -> value > result) |
| 69 |
result = point -> value; |
| 70 |
} |
| 71 |
return result; |
| 72 |
} |
| 73 |
|
| 74 |
static autoPitchTier PitchTier_createFromPoints (double xmin, double xmax, constVEC times, constVEC pitches) { |
| 75 |
try { |
| 76 |
Melder_assert (times.size == pitches.size); |
| 77 |
autoPitchTier me = PitchTier_create (xmin, xmax); |
| 78 |
for (integer i = 1; i <= times.size; i ++) |
| 79 |
RealTier_addPoint (me.get(), times [i], pitches [i]); |
| 80 |
return me; |
| 81 |
} catch (MelderError) { |
| 82 |
Melder_throw (U"No PitchTier created from points."); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static autoVEC getAbsoluteFromRelativeTimes (double tmin, double tmax, constVECVU const& relativeTimes, int time_offset) { |
| 87 |
autoVEC absoluteTimes = raw_VEC (relativeTimes.size); |
| 88 |
for (integer i = 1; i <= relativeTimes.size; i ++) { |
| 89 |
if (time_offset == TIME_OFFSET_AS_FRACTION_FROM_START) |
| 90 |
absoluteTimes [i] = tmin + relativeTimes [i] * (tmax - tmin); |
| 91 |
else if (time_offset == TIME_OFFSET_AS_PERCENTAGE_FROM_START) |
| 92 |
absoluteTimes [i] = tmin + relativeTimes [i] * (tmax - tmin) * 0.01; |
| 93 |
else if (time_offset == TIME_OFFSET_AS_SECONDS_FROM_START) |
| 94 |
absoluteTimes [i] = tmin + relativeTimes [i]; |
| 95 |
else { |
| 96 |
// we should not be here |
| 97 |
} |
| 98 |
} |
| 99 |
return absoluteTimes; |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
a1, a#1, b1, b#1, ... g#1, a2, a#2, b2, c2, ....; a a# b c c# d d# e f f# g g# |
| 104 |
*/ |
| 105 |
static double note_to_frequency (conststring32 token, double a4) { |
| 106 |
const double base = a4 / 8.0; |
| 107 |
integer octave, index; |
| 108 |
const char32 note = *token++, char2 = *token++; |
| 109 |
if (note == U'a' || note == U'A') |
| 110 |
index = 1; |
| 111 |
else if (note == U'b' || note == U'B') |
| 112 |
index = 3; |
| 113 |
else if (note == U'c' || note == U'C') |
| 114 |
index = 4; |
| 115 |
else if (note == U'd' || note == U'D') |
| 116 |
index = 6; |
| 117 |
else if (note == U'e' || note == U'E') |
| 118 |
index = 8; |
| 119 |
else if (note == U'f' || note == U'F') |
| 120 |
index = 9; |
| 121 |
else if (note == U'g' || note == U'G') |
| 122 |
index = 11; |
| 123 |
else |
| 124 |
return undefined; |
| 125 |
char32 char3; |
| 126 |
if (char2 == U'#') { |
| 127 |
index ++; |
| 128 |
char3 = *token++; |
| 129 |
} else { |
| 130 |
char3 = char2; |
| 131 |
} |
| 132 |
|
| 133 |
if (char3 >= U'0' && char3 <= U'9') |
| 134 |
octave = char3 - U'0'; |
| 135 |
else |
| 136 |
return undefined; |
| 137 |
|
| 138 |
const double frequency = base * pow (2.0, octave - 1.0 + (index - 1.0) / 12.0); |
| 139 |
return frequency; |
| 140 |
} |
| 141 |
|
| 142 |
static autoPitchTier PitchTier_createAsModifiedPart (PitchTier me, double tmin, double tmax, |
| 143 |
constVECVU const& relativeTimes, int time_offset, conststring32 pitches_string, int pitch_unit, int pitch_as, int pitchAnchor_status) |
| 144 |
{ |
| 145 |
(void) pitch_unit; |
| 146 |
try { |
| 147 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 148 |
|
| 149 |
if ((pitch_as == PITCH_VALUE_AS_FRACTION || pitch_as == PITCH_VALUE_AS_PERCENTAGE) && |
| 150 |
pitchAnchor_status == PITCH_ANCHOR_IS_NOT_USED) |
| 151 |
Melder_throw (U"You need to specify an anchor value to calculate ", (pitch_as == PITCH_VALUE_AS_FRACTION ? U"fractions" : U"percentages"), U"."); |
| 152 |
|
| 153 |
autoVEC times = getAbsoluteFromRelativeTimes (tmin, tmax, relativeTimes, time_offset); |
| 154 |
|
| 155 |
autoStrings items = Strings_createAsTokens (pitches_string, U" "); |
| 156 |
const integer numberOfPitches = items -> numberOfStrings; |
| 157 |
Melder_require (times.size == numberOfPitches, |
| 158 |
U"The number of items in the times and the pitches string have to be equal."); |
| 159 |
autoVEC pitchesraw = raw_VEC (numberOfPitches); |
| 160 |
for (integer i = 1; i <= numberOfPitches; i ++) { |
| 161 |
const conststring32 token = items -> strings [i].get(); |
| 162 |
if (pitch_as == PITCH_VALUE_AS_MUSIC_NOTE) |
| 163 |
pitchesraw [i] = note_to_frequency (token, 440.0); |
| 164 |
else |
| 165 |
Interpreter_numericExpression (0, token, & pitchesraw [i]); |
| 166 |
} |
| 167 |
/* |
| 168 |
Now we have the real times and we can sort them together with the pitches |
| 169 |
*/ |
| 170 |
autoVEC pitches = copy_VEC (pitchesraw.get()); |
| 171 |
NUMsortTogether (times.get(), pitches.get()); |
| 172 |
double pitchAnchor, pitch; |
| 173 |
for (integer i = 1; i <= times.size; i ++) { |
| 174 |
const integer index = pitch_as != PITCH_VALUE_AS_SLOPES_AND_END ? i : times.size - i + 1; |
| 175 |
const double time = times [index]; |
| 176 |
if (pitchAnchor_status == PITCH_ANCHOR_IS_NOT_USED) |
| 177 |
pitchAnchor = undefined; |
| 178 |
else if (pitchAnchor_status == PITCH_ANCHOR_IS_CURRENT) |
| 179 |
pitchAnchor = RealTier_getValueAtTime (me, time); |
| 180 |
else if (pitchAnchor_status == PITCH_ANCHOR_IS_START) |
| 181 |
pitchAnchor = i == 1 ? RealTier_getValueAtTime (me, tmin) : pitchAnchor; |
| 182 |
else if (pitchAnchor_status == PITCH_ANCHOR_IS_END) |
| 183 |
pitchAnchor = i == 1 ? RealTier_getValueAtTime (me, tmax) : pitchAnchor; |
| 184 |
else if (pitchAnchor_status == PITCH_ANCHOR_IS_MEAN_OF_CURVE) |
| 185 |
pitchAnchor = i == 1 ? RealTier_getMean_curve_u (me, tmin, tmax) : pitchAnchor; |
| 186 |
else if (pitchAnchor_status == PITCH_ANCHOR_IS_MEAN_OF_POINTS) |
| 187 |
pitchAnchor = i == 1 ? RealTier_getMean_points_u (me, tmin, tmax) : pitchAnchor; |
| 188 |
else if (pitchAnchor_status == PITCH_ANCHOR_IS_MAXIMUM) |
| 189 |
pitchAnchor = i == 1 ? RealTier_getMaximumValue_interval (me, tmin, tmax) : pitchAnchor; |
| 190 |
else if (pitchAnchor_status == PITCH_ANCHOR_IS_MINIMUM) |
| 191 |
pitchAnchor = i == 1 ? RealTier_getMinimumValue_interval (me, tmin, tmax) : pitchAnchor; |
| 192 |
else { |
| 193 |
// we should not be here |
| 194 |
} |
| 195 |
Melder_require (isdefined (pitchAnchor) || pitchAnchor_status == PITCH_ANCHOR_IS_NOT_USED, |
| 196 |
U"The pitch anchor value is undefined because the PitchTier is empty."); |
| 197 |
/* |
| 198 |
How to interpret the "pitch" value |
| 199 |
*/ |
| 200 |
if (pitch_as == PITCH_VALUE_AS_FREQUENCY) |
| 201 |
pitch = pitches [i]; |
| 202 |
else if (pitch_as == PITCH_VALUE_AS_FRACTION) |
| 203 |
pitch = pitchAnchor * (1.0 + pitches [i]); |
| 204 |
else if (pitch_as == PITCH_VALUE_AS_PERCENTAGE) |
| 205 |
pitch = pitchAnchor * (1.0 + pitches [i] * 0.01); |
| 206 |
else if (pitch_as == PITCH_VALUE_AS_START_AND_SLOPES) { |
| 207 |
if (i == 1) |
| 208 |
pitch = pitchAnchor; |
| 209 |
else |
| 210 |
pitch += (times [i] - times [i - 1]) * pitches [i]; |
| 211 |
} else if (pitch_as == PITCH_VALUE_AS_SLOPES_AND_END) { |
| 212 |
if (i == 1) |
| 213 |
pitch = pitchAnchor; |
| 214 |
else |
| 215 |
pitch -= (times [index + 1] - times [index]) * pitches [index]; |
| 216 |
} else if (pitch_as == PITCH_VALUE_AS_MUSIC_NOTE) |
| 217 |
pitch = pitches [i]; |
| 218 |
else if (pitch_as == PITCH_VALUE_AS_SEMITONES) |
| 219 |
pitch = NUMsemitonesToHertz (pitches [i]); |
| 220 |
else { |
| 221 |
// we should not be here |
| 222 |
} |
| 223 |
pitches [index] = pitch; |
| 224 |
} |
| 225 |
/* |
| 226 |
Remove old points |
| 227 |
*/ |
| 228 |
autoPitchTier thee = PitchTier_createFromPoints (times [1], times [times.size], times.get(), pitches.get()); |
| 229 |
|
| 230 |
return thee; |
| 231 |
} catch (MelderError) { |
| 232 |
Melder_throw (me, U": no modified PitchTier created."); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
static void PitchTiers_replacePoints (PitchTier me, PitchTier thee) { |
| 237 |
AnyTier_removePointsBetween ((AnyTier) me, thy xmin, thy xmax); |
| 238 |
for (integer i = 1; i <= thy points.size; i ++) { |
| 239 |
const RealPoint pp = thy points.at [i]; |
| 240 |
RealTier_addPoint (me, pp -> number, pp -> value); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
void PitchTier_modifyInterval (PitchTier me, double tmin, double tmax, |
| 245 |
constVECVU const& relativeTimes, int time_offset, conststring32 pitches_string, |
| 246 |
int pitch_unit, int pitch_as, int pitchAnchor_status) |
| 247 |
{ |
| 248 |
try { |
| 249 |
autoPitchTier thee = PitchTier_createAsModifiedPart (me, tmin, tmax, relativeTimes, time_offset, pitches_string, pitch_unit, pitch_as, pitchAnchor_status); |
| 250 |
PitchTiers_replacePoints (me, thee.get()); |
| 251 |
} catch (MelderError) { |
| 252 |
Melder_throw (me, U": interval modification not completed."); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/* |
| 257 |
We specify pitches as tone levels (1 - numberOfToneLevels). These levels are relative to the pitch range of a speaker. |
| 258 |
(normally in Mandarin Chinese they count 5 levels). |
| 259 |
*/ |
| 260 |
static autoPitchTier PitchTier_createAsModifiedPart_toneLevels (PitchTier me, double tmin, double tmax, double fmin, double fmax, |
| 261 |
integer numberOfToneLevels, constVECVU const& relativeTimes, int time_offset, constVECVU const& pitchesAsToneLevels) |
| 262 |
{ |
| 263 |
try { |
| 264 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 265 |
Melder_require (fmin < fmax, |
| 266 |
U"The lowest frequency should be lower than the highest frequency."); |
| 267 |
|
| 268 |
autoVEC times = getAbsoluteFromRelativeTimes (tmin, tmax, relativeTimes, time_offset); |
| 269 |
Melder_require (times.size == pitchesAsToneLevels.size, |
| 270 |
U"The number of times and the number of pitches have to be equal."); |
| 271 |
|
| 272 |
autoVEC pitches_hertz = raw_VEC (pitchesAsToneLevels.size); |
| 273 |
const double scale = log10 (fmax / fmin) / numberOfToneLevels; |
| 274 |
for (integer i = 1; i <= pitchesAsToneLevels.size; i ++) |
| 275 |
pitches_hertz [i] = fmin * pow (10.0, scale * pitchesAsToneLevels [i]); |
| 276 |
NUMsortTogether (times.get(), pitches_hertz.get()); |
| 277 |
autoPitchTier thee = PitchTier_createFromPoints (times [1], times [times.size], times.get(), pitches_hertz.get()); |
| 278 |
return thee; |
| 279 |
} catch (MelderError) { |
| 280 |
Melder_throw (me, U": interval modification not succeeded."); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void PitchTier_modifyInterval_toneLevels (PitchTier me, double tmin, double tmax, double fmin, double fmax, |
| 285 |
integer numberOfToneLevels, constVECVU const& relativeTimes, int time_offset, constVECVU const& pitchesAsToneLevels) |
| 286 |
{ |
| 287 |
try { |
| 288 |
autoPitchTier thee = PitchTier_createAsModifiedPart_toneLevels (me, tmin, tmax, fmin, fmax, numberOfToneLevels, relativeTimes, time_offset, pitchesAsToneLevels); |
| 289 |
PitchTiers_replacePoints (me, thee.get()); |
| 290 |
} catch (MelderError) { |
| 291 |
Melder_throw (me, U": interval modification as tone levels not succeeded."); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/* End of file TextGrid_and_PitchTier.cpp */ |