FormantGrid.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* FormantGrid.cpp
  2. *
  3. * Copyright (C) 2008-2011,2014,2015,2016,2017 Paul Boersma & 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.
  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. #include "FormantGrid.h"
  19. #include "PitchTier_to_Sound.h"
  20. #include "Formula.h"
  21. #include "oo_DESTROY.h"
  22. #include "FormantGrid_def.h"
  23. #include "oo_COPY.h"
  24. #include "FormantGrid_def.h"
  25. #include "oo_EQUAL.h"
  26. #include "FormantGrid_def.h"
  27. #include "oo_CAN_WRITE_AS_ENCODING.h"
  28. #include "FormantGrid_def.h"
  29. #include "oo_WRITE_TEXT.h"
  30. #include "FormantGrid_def.h"
  31. #include "oo_READ_TEXT.h"
  32. #include "FormantGrid_def.h"
  33. #include "oo_WRITE_BINARY.h"
  34. #include "FormantGrid_def.h"
  35. #include "oo_READ_BINARY.h"
  36. #include "FormantGrid_def.h"
  37. #include "oo_DESCRIPTION.h"
  38. #include "FormantGrid_def.h"
  39. Thing_implement (FormantGrid, Function, 0);
  40. double structFormantGrid :: v_getVector (integer irow, integer icol) {
  41. RealTier tier = our formants.at [irow];
  42. return RealTier_getValueAtIndex (tier, icol);
  43. }
  44. double structFormantGrid :: v_getFunction1 (integer irow, double x) {
  45. RealTier tier = our formants.at [irow];
  46. return RealTier_getValueAtTime (tier, x);
  47. }
  48. void structFormantGrid :: v_shiftX (double xfrom, double xto) {
  49. FormantGrid_Parent :: v_shiftX (xfrom, xto);
  50. for (integer i = 1; i <= our formants.size; i ++) {
  51. RealTier tier = our formants.at [i];
  52. tier -> v_shiftX (xfrom, xto);
  53. }
  54. for (integer i = 1; i <= our bandwidths.size; i ++) {
  55. RealTier tier = our bandwidths.at [i];
  56. tier -> v_shiftX (xfrom, xto);
  57. }
  58. }
  59. void structFormantGrid :: v_scaleX (double xminfrom, double xmaxfrom, double xminto, double xmaxto) {
  60. FormantGrid_Parent :: v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto);
  61. for (integer i = 1; i <= our formants.size; i ++) {
  62. RealTier tier = our formants.at [i];
  63. tier -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto);
  64. }
  65. for (integer i = 1; i <= our bandwidths.size; i ++) {
  66. RealTier tier = our bandwidths.at [i];
  67. tier -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto);
  68. }
  69. }
  70. void FormantGrid_init (FormantGrid me, double tmin, double tmax, integer numberOfFormants) {
  71. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  72. autoRealTier formant = RealTier_create (tmin, tmax);
  73. my formants. addItem_move (formant.move());
  74. autoRealTier bandwidth = RealTier_create (tmin, tmax);
  75. my bandwidths. addItem_move (bandwidth.move());
  76. }
  77. my xmin = tmin;
  78. my xmax = tmax;
  79. }
  80. autoFormantGrid FormantGrid_createEmpty (double tmin, double tmax, integer numberOfFormants) {
  81. try {
  82. autoFormantGrid me = Thing_new (FormantGrid);
  83. FormantGrid_init (me.get(), tmin, tmax, numberOfFormants);
  84. return me;
  85. } catch (MelderError) {
  86. Melder_throw (U"Empty FormantGrid not created.");
  87. }
  88. }
  89. autoFormantGrid FormantGrid_create (double tmin, double tmax, integer numberOfFormants,
  90. double initialFirstFormant, double initialFormantSpacing,
  91. double initialFirstBandwidth, double initialBandwidthSpacing)
  92. {
  93. try {
  94. autoFormantGrid me = FormantGrid_createEmpty (tmin, tmax, numberOfFormants);
  95. for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
  96. FormantGrid_addFormantPoint (me.get(), iformant, 0.5 * (tmin + tmax),
  97. initialFirstFormant + (iformant - 1) * initialFormantSpacing);
  98. FormantGrid_addBandwidthPoint (me.get(), iformant, 0.5 * (tmin + tmax),
  99. initialFirstBandwidth + (iformant - 1) * initialBandwidthSpacing);
  100. }
  101. return me;
  102. } catch (MelderError) {
  103. Melder_throw (U"FormantGrid not created.");
  104. }
  105. }
  106. void FormantGrid_addFormantPoint (FormantGrid me, integer iformant, double t, double value) {
  107. try {
  108. if (iformant < 1 || iformant > my formants.size)
  109. Melder_throw (U"No such formant number.");
  110. RealTier formantTier = my formants.at [iformant];
  111. RealTier_addPoint (formantTier, t, value);
  112. } catch (MelderError) {
  113. Melder_throw (me, U": formant point not added.");
  114. }
  115. }
  116. void FormantGrid_addBandwidthPoint (FormantGrid me, integer iformant, double t, double value) {
  117. try {
  118. if (iformant < 1 || iformant > my formants.size)
  119. Melder_throw (U"No such formant number.");
  120. RealTier bandwidthTier = my bandwidths.at [iformant];
  121. RealTier_addPoint (bandwidthTier, t, value);
  122. } catch (MelderError) {
  123. Melder_throw (me, U": bandwidth point not added.");
  124. }
  125. }
  126. double FormantGrid_getFormantAtTime (FormantGrid me, integer iformant, double t) {
  127. if (iformant < 1 || iformant > my formants.size) return undefined;
  128. return RealTier_getValueAtTime (my formants.at [iformant], t);
  129. }
  130. double FormantGrid_getBandwidthAtTime (FormantGrid me, integer iformant, double t) {
  131. if (iformant < 1 || iformant > my bandwidths.size) return undefined;
  132. return RealTier_getValueAtTime (my bandwidths.at [iformant], t);
  133. }
  134. void FormantGrid_removeFormantPointsBetween (FormantGrid me, integer iformant, double tmin, double tmax) {
  135. if (iformant < 1 || iformant > my formants.size) return;
  136. AnyTier_removePointsBetween (my formants.at [iformant]->asAnyTier(), tmin, tmax);
  137. }
  138. void FormantGrid_removeBandwidthPointsBetween (FormantGrid me, integer iformant, double tmin, double tmax) {
  139. if (iformant < 1 || iformant > my bandwidths.size) return;
  140. AnyTier_removePointsBetween (my bandwidths.at [iformant]->asAnyTier(), tmin, tmax);
  141. }
  142. void Sound_FormantGrid_filter_inplace (Sound me, FormantGrid formantGrid) {
  143. double dt = my dx;
  144. if (formantGrid -> formants.size > 0 && formantGrid -> bandwidths.size > 0) {
  145. for (integer iformant = 1; iformant <= formantGrid -> formants.size; iformant ++) {
  146. RealTier formantTier = formantGrid -> formants.at [iformant];
  147. RealTier bandwidthTier = formantGrid -> bandwidths.at [iformant];
  148. for (integer isamp = 1; isamp <= my nx; isamp ++) {
  149. double t = my x1 + (isamp - 1) * my dx;
  150. /*
  151. * Compute LP coefficients.
  152. */
  153. double formant, bandwidth;
  154. formant = RealTier_getValueAtTime (formantTier, t);
  155. bandwidth = RealTier_getValueAtTime (bandwidthTier, t);
  156. if (isdefined (formant) && isdefined (bandwidth)) {
  157. double cosomdt = cos (2 * NUMpi * formant * dt);
  158. double r = exp (- NUMpi * bandwidth * dt);
  159. /* Formants at 0 Hz or the Nyquist are single poles, others are double poles. */
  160. if (fabs (cosomdt) > 0.999999) { /* Allow for round-off errors. */
  161. /* single pole: D(z) = 1 - r z^-1 */
  162. for (integer channel = 1; channel <= my ny; channel ++) {
  163. if (isamp > 1) my z [channel] [isamp] += r * my z [channel] [isamp - 1];
  164. }
  165. } else {
  166. /* double pole: D(z) = 1 + p z^-1 + q z^-2 */
  167. double p = - 2 * r * cosomdt;
  168. double q = r * r;
  169. for (integer channel = 1; channel <= my ny; channel ++) {
  170. if (isamp > 1) my z [channel] [isamp] -= p * my z [channel] [isamp - 1];
  171. if (isamp > 2) my z [channel] [isamp] -= q * my z [channel] [isamp - 2];
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. autoSound Sound_FormantGrid_filter (Sound me, FormantGrid formantGrid) {
  180. try {
  181. autoSound thee = Data_copy (me);
  182. Sound_FormantGrid_filter_inplace (thee.get(), formantGrid);
  183. Vector_scale (thee.get(), 0.99);
  184. return thee;
  185. } catch (MelderError) {
  186. Melder_throw (me, U": not filtered with ", formantGrid, U".");
  187. }
  188. }
  189. autoSound Sound_FormantGrid_filter_noscale (Sound me, FormantGrid formantGrid) {
  190. try {
  191. autoSound thee = Data_copy (me);
  192. Sound_FormantGrid_filter_inplace (thee.get(), formantGrid);
  193. return thee;
  194. } catch (MelderError) {
  195. Melder_throw (me, U": not filtered with ", formantGrid, U".");
  196. }
  197. }
  198. autoSound FormantGrid_to_Sound (FormantGrid me, double samplingFrequency,
  199. double tStart, double f0Start, double tMid, double f0Mid, double tEnd, double f0End,
  200. double adaptFactor, double maximumPeriod, double openPhase, double collisionPhase, double power1, double power2)
  201. {
  202. try {
  203. autoPitchTier pitch = PitchTier_create (my xmin, my xmax);
  204. RealTier_addPoint (pitch.get(), my xmin + tStart * (my xmax - my xmin), f0Start);
  205. RealTier_addPoint (pitch.get(), my xmin + tMid * (my xmax - my xmin), f0Mid);
  206. RealTier_addPoint (pitch.get(), my xmax - (1.0 - tEnd) * (my xmax - my xmin), f0End);
  207. autoSound thee = PitchTier_to_Sound_phonation (pitch.get(), samplingFrequency,
  208. adaptFactor, maximumPeriod, openPhase, collisionPhase, power1, power2, false);
  209. Sound_FormantGrid_filter_inplace (thee.get(), me);
  210. return thee;
  211. } catch (MelderError) {
  212. Melder_throw (me, U": not converted to Sound.");
  213. }
  214. }
  215. void FormantGrid_playPart (FormantGrid me, double tmin, double tmax, double samplingFrequency,
  216. double tStart, double f0Start, double tMid, double f0Mid, double tEnd, double f0End,
  217. double adaptFactor, double maximumPeriod, double openPhase, double collisionPhase, double power1, double power2,
  218. Sound_PlayCallback playCallback, Thing playBoss)
  219. {
  220. try {
  221. autoSound sound = FormantGrid_to_Sound (me, samplingFrequency,
  222. tStart, f0Start, tMid, f0Mid, tEnd, f0End,
  223. adaptFactor, maximumPeriod, openPhase, collisionPhase, power1, power2);
  224. Vector_scale (sound.get(), 0.99);
  225. Sound_playPart (sound.get(), tmin, tmax, playCallback, playBoss);
  226. } catch (MelderError) {
  227. Melder_throw (me, U": not played.");
  228. }
  229. }
  230. void FormantGrid_formula_bandwidths (FormantGrid me, conststring32 expression, Interpreter interpreter, FormantGrid thee) {
  231. try {
  232. Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  233. Formula_Result result;
  234. if (! thee)
  235. thee = me;
  236. for (integer irow = 1; irow <= my formants.size; irow ++) {
  237. RealTier bandwidth = thy bandwidths.at [irow];
  238. for (integer icol = 1; icol <= bandwidth -> points.size; icol ++) {
  239. Formula_run (irow, icol, & result);
  240. if (isundef (result. numericResult))
  241. Melder_throw (U"Cannot put an undefined value into the tier.\nFormula not finished.");
  242. bandwidth -> points.at [icol] -> value = result. numericResult;
  243. }
  244. }
  245. } catch (MelderError) {
  246. Melder_throw (me, U": bandwidth formula not completed.");
  247. }
  248. }
  249. void FormantGrid_formula_frequencies (FormantGrid me, conststring32 expression, Interpreter interpreter, FormantGrid thee) {
  250. try {
  251. Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  252. Formula_Result result;
  253. if (! thee)
  254. thee = me;
  255. for (integer irow = 1; irow <= my formants.size; irow ++) {
  256. RealTier formant = thy formants.at [irow];
  257. for (integer icol = 1; icol <= formant -> points.size; icol ++) {
  258. Formula_run (irow, icol, & result);
  259. if (isundef (result. numericResult))
  260. Melder_throw (U"Cannot put an undefined value into the tier.\nFormula not finished.");
  261. formant -> points.at [icol] -> value = result. numericResult;
  262. }
  263. }
  264. } catch (MelderError) {
  265. Melder_throw (me, U": frequency formula not completed.");
  266. }
  267. }
  268. autoFormantGrid Formant_downto_FormantGrid (Formant me) {
  269. try {
  270. autoFormantGrid thee = FormantGrid_createEmpty (my xmin, my xmax, my maxnFormants);
  271. for (integer iframe = 1; iframe <= my nx; iframe ++) {
  272. Formant_Frame frame = & my d_frames [iframe];
  273. double t = Sampled_indexToX (me, iframe);
  274. for (integer iformant = 1; iformant <= frame -> nFormants; iformant ++) {
  275. Formant_Formant pair = & frame -> formant [iformant];
  276. FormantGrid_addFormantPoint (thee.get(), iformant, t, pair -> frequency);
  277. FormantGrid_addBandwidthPoint (thee.get(), iformant, t, pair -> bandwidth);
  278. }
  279. }
  280. return thee;
  281. } catch (MelderError) {
  282. Melder_throw (me, U": not converted to FormantGrid.");
  283. }
  284. }
  285. autoFormant FormantGrid_to_Formant (FormantGrid me, double dt, double intensity) {
  286. try {
  287. Melder_assert (dt > 0.0);
  288. Melder_assert (intensity >= 0.0);
  289. integer nt = Melder_ifloor ((my xmax - my xmin) / dt) + 1;
  290. double t1 = 0.5 * (my xmin + my xmax - (nt - 1) * dt);
  291. autoFormant thee = Formant_create (my xmin, my xmax, nt, dt, t1, my formants.size);
  292. for (integer iframe = 1; iframe <= nt; iframe ++) {
  293. Formant_Frame frame = & thy d_frames [iframe];
  294. frame -> intensity = intensity;
  295. frame -> nFormants = my formants.size;
  296. frame -> formant = NUMvector <structFormant_Formant> (1, my formants.size);
  297. double t = t1 + (iframe - 1) * dt;
  298. for (integer iformant = 1; iformant <= my formants.size; iformant ++) {
  299. Formant_Formant formant = & frame -> formant [iformant];
  300. formant -> frequency = RealTier_getValueAtTime (my formants.at [iformant], t);
  301. formant -> bandwidth = RealTier_getValueAtTime (my bandwidths.at [iformant], t);
  302. }
  303. }
  304. return thee;
  305. } catch (MelderError) {
  306. Melder_throw (me, U": not converted to Formant.");
  307. }
  308. }
  309. autoSound Sound_Formant_filter (Sound me, Formant formant) {
  310. try {
  311. autoFormantGrid grid = Formant_downto_FormantGrid (formant);
  312. autoSound thee = Sound_FormantGrid_filter (me, grid.get());
  313. return thee;
  314. } catch (MelderError) {
  315. Melder_throw (me, U": not filtered with ", formant, U".");
  316. }
  317. }
  318. autoSound Sound_Formant_filter_noscale (Sound me, Formant formant) {
  319. try {
  320. autoFormantGrid grid = Formant_downto_FormantGrid (formant);
  321. autoSound thee = Sound_FormantGrid_filter_noscale (me, grid.get());
  322. return thee;
  323. } catch (MelderError) {
  324. Melder_throw (me, U": not filtered with ", formant, U".");
  325. }
  326. }
  327. /* End of file FormantGrid.cpp */