TextGrid_and_PitchTier.cpp 13 KB

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