Sound_extensions.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  1. /* Sound_extensions.cpp
  2. *
  3. * Copyright (C) 1993-2018 David Weenink, 2017 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. 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. djmw 20001109 Sound_scale->Vector_scale
  20. djmw 20020516 GPL header
  21. djmw 20020523 Removed Sound_read/writeWAVAudioFile
  22. djmw 20030926 Sound_changeGender
  23. djmw 20040405 Renamed: Sound_overrideSamplingFrequency
  24. djmw 20041124 Changed call to Sound_to_Spectrum & Spectrum_to_Sound.
  25. djmw 20050620 Changed Pitch_HERTZ to Pitch_UNIT_HERTZ
  26. djmw 20050628 New and extended Sound_createShepardToneComplex that corrects incorrect amplitudes of tones in complex.
  27. (amplitudes were on linear instead of log scale)
  28. djmw 20060921 Added Sound_to_IntervalTier_detectSilence
  29. djmw 20061010 Removed crashing bug in Sound_to_IntervalTier_detectSilence.
  30. djmw 20061201 Interface change: removed minimumPitch parameter from Sound_Pitch_changeGender.
  31. djmw 20061214 Sound_Pitch_changeSpeaker removed warning.
  32. djmw 20070103 Sound interface changes
  33. djmw 20070129 Warning added in changeGender_old.
  34. djmw 20071022 Possible (bug?) correction in Sound_createShepardToneComplex
  35. djmw 20071030 Sound_preEmphasis: no pre-emphasis above the Nyquist frequency.
  36. djmw 20071202 Melder_warning<n>
  37. djmw 20080122 float -> double
  38. djmw 20080320 +Sound_fade.
  39. djmw 20080530 +Sound_localAverage
  40. pb 20090926 Correction in Sound_Pitch_changeGender_old
  41. djmw 20091023 Added Sound_drawIntervals
  42. djmw 20091028 Sound_drawIntervals -> Sound_drawParts + Graphics_function
  43. djmw 20091126 Sound_drawParts -> Sound_drawWheres
  44. djmw 20091211 Sound_fade: removed erroneous warning
  45. djmw 20100318 Cross-correlation, convolution and autocorrelation
  46. djmw 20100325 -Cross-correlation, convolution and autocorrelation
  47. djmw 20111227 Sound_trimSilencesAtStartAndEnd and Sound_getStartAndEndTimesOfSounding
  48. djmw 20120616 Change 0.8 to 1.25 in Sound_Point_Pitch_Duration_to_Sound
  49. */
  50. #include <ctype.h>
  51. #include "Formula.h"
  52. #include "Intensity_extensions.h"
  53. #include "Sound_extensions.h"
  54. #include "Sound_and_Spectrum.h"
  55. #include "Spectrum_extensions.h"
  56. #include "Sound_to_Intensity.h"
  57. #include "Sound_to_Pitch.h"
  58. #include "Vector.h"
  59. #include "Pitch_extensions.h"
  60. #include "Pitch_to_PitchTier.h"
  61. #include "Pitch_to_PointProcess.h"
  62. #include "Polygon_extensions.h"
  63. #include "TextGrid_extensions.h"
  64. #include "DurationTier.h"
  65. #include "Ltas.h"
  66. #include "Manipulation.h"
  67. #include "NUMcomplex.h"
  68. #define MAX_T 0.02000000001 /* Maximum interval between two voice pulses (otherwise voiceless). */
  69. static void PitchTier_modifyExcursionRange (PitchTier me, double tmin, double tmax, double multiplier, double fref_Hz) {
  70. if (fref_Hz <= 0) {
  71. return;
  72. }
  73. double fref_st = 12.0 * log (fref_Hz / 100.0) / NUMln2;
  74. for (integer i = 1; i <= my points.size; i ++) {
  75. RealPoint point = my points.at [i];
  76. double f = point -> value;
  77. if (point -> number < tmin || point -> number > tmax) {
  78. continue;
  79. }
  80. if (f > 0) {
  81. double f_st = fref_st + 12.0 * log2 (f / fref_Hz) * multiplier;
  82. point -> value = 100.0 * exp (f_st * (NUMln2 / 12.0));
  83. }
  84. }
  85. }
  86. static void Pitch_scaleDuration (Pitch me, double multiplier) {
  87. if (multiplier != 1) {
  88. // keep xmin at the same value
  89. my dx *= multiplier;
  90. my x1 = my xmin + (my x1 - my xmin) * multiplier;
  91. my xmax = my xmin + (my xmax - my xmin) * multiplier;
  92. }
  93. }
  94. static void Pitch_scalePitch (Pitch me, double multiplier) {
  95. for (integer i = 1; i <= my nx; i ++) {
  96. double f = my frame [i].candidate [1].frequency;
  97. f *= multiplier;
  98. if (f < my ceiling) {
  99. my frame [i].candidate [1].frequency = f;
  100. }
  101. }
  102. }
  103. static void i1write (Sound me, FILE *f, integer *nClip) {
  104. double *s = my z [1], min = -128, max = 127;
  105. *nClip = 0;
  106. for (integer i = 1; i <= my nx; i ++) {
  107. double sample = round (s [i] * 128);
  108. if (sample > max) {
  109. sample = max;
  110. (*nClip) ++;
  111. } else if (sample < min) {
  112. sample = min;
  113. (*nClip) ++;
  114. }
  115. binputi8 ((int) sample, f);
  116. }
  117. }
  118. static void i1read (Sound me, FILE *f) {
  119. double *s = my z [1];
  120. for (integer i = 1; i <= my nx; i ++) {
  121. s [i] = bingeti8 (f) / 128.0;
  122. }
  123. }
  124. static void u1write (Sound me, FILE *f, integer *nClip) {
  125. double *s = my z [1], min = 0, max = 255;
  126. *nClip = 0;
  127. for (integer i = 1; i <= my nx; i ++) {
  128. double sample = round ( (s [i] + 1) * 255 / 2);
  129. if (sample > max) {
  130. sample = max;
  131. (*nClip) ++;
  132. } else if (sample < min) {
  133. sample = min;
  134. (*nClip) ++;
  135. }
  136. binputu8 ((unsigned int) sample, f);
  137. }
  138. }
  139. static void u1read (Sound me, FILE *f) {
  140. double *s = my z [1];
  141. for (integer i = 1; i <= my nx; i ++) {
  142. s [i] = bingetu8 (f) / 128.0 - 1.0;
  143. }
  144. }
  145. static void i2write (Sound me, FILE *f, bool littleEndian, integer *nClip) {
  146. double *s = my z [1], min = -32768, max = 32767;
  147. void (*put) (int16, FILE *) = littleEndian ? binputi16LE : binputi16;
  148. *nClip = 0;
  149. for (integer i = 1; i <= my nx; i ++) {
  150. double sample = round (s [i] * 32768);
  151. if (sample > max) {
  152. sample = max;
  153. (*nClip) ++;
  154. } else if (sample < min) {
  155. sample = min;
  156. (*nClip) ++;
  157. }
  158. put ((int16) sample, f);
  159. }
  160. }
  161. static void i2read (Sound me, FILE *f, bool littleEndian) {
  162. double *s = my z [1];
  163. int16 (*get) (FILE *) = littleEndian ? bingeti16LE : bingeti16;
  164. for (integer i = 1; i <= my nx; i ++) {
  165. s [i] = get (f) / 32768.;
  166. }
  167. }
  168. static void u2write (Sound me, FILE *f, bool littleEndian, integer *nClip) {
  169. double *s = my z [1], min = 0, max = 65535;
  170. void (*put) (uint16, FILE *) = littleEndian ? binputu16LE : binputu16;
  171. *nClip = 0;
  172. for (integer i = 1; i <= my nx; i ++) {
  173. double sample = round ( (s [i] + 1) * 65535 / 2);
  174. if (sample > max) {
  175. sample = max;
  176. (*nClip) ++;
  177. } else if (sample < min) {
  178. sample = min;
  179. (*nClip) ++;
  180. }
  181. put ((uint16) sample, f);
  182. }
  183. }
  184. static void u2read (Sound me, FILE *f, bool littleEndian) {
  185. double *s = my z [1];
  186. uint16 (*get) (FILE *) = littleEndian ? bingetu16LE : bingetu16;
  187. for (integer i = 1; i <= my nx; i ++) {
  188. s [i] = get (f) / 32768.0 - 1.0;
  189. }
  190. }
  191. static void i4write (Sound me, FILE *f, bool littleEndian, integer *nClip) {
  192. double *s = my z [1]; double min = -2147483648.0, max = 2147483647.0;
  193. void (*put) (int32, FILE *) = littleEndian ? binputi32LE : binputi32;
  194. *nClip = 0;
  195. for (integer i = 1; i <= my nx; i ++) {
  196. double sample = round (s [i] * 2147483648.0);
  197. if (sample > max) {
  198. sample = max;
  199. (*nClip) ++;
  200. } else if (sample < min) {
  201. sample = min;
  202. (*nClip) ++;
  203. }
  204. put ((int32) sample, f);
  205. }
  206. }
  207. static void i4read (Sound me, FILE *f, bool littleEndian) {
  208. double *s = my z [1];
  209. int32 (*get) (FILE *) = littleEndian ? bingeti32LE : bingeti32;
  210. for (integer i = 1; i <= my nx; i ++) {
  211. s [i] = get (f) / 2147483648.0;
  212. }
  213. }
  214. static void u4write (Sound me, FILE *f, bool littleEndian, integer *nClip) {
  215. double *s = my z [1]; double min = 0.0, max = 4294967295.0;
  216. void (*put) (uint32, FILE *) = littleEndian ? binputu32LE : binputu32;
  217. *nClip = 0;
  218. for (integer i = 1; i <= my nx; i ++) {
  219. double sample = Melder_round_tieUp (s [i] * 4294967295.0);
  220. if (sample > max) {
  221. sample = max;
  222. (*nClip) ++;
  223. } else if (sample < min) {
  224. sample = min;
  225. (*nClip) ++;
  226. }
  227. put ((uint32) sample, f);
  228. }
  229. }
  230. static void u4read (Sound me, FILE *f, bool littleEndian) {
  231. double *s = my z [1];
  232. int32 (*get) (FILE *) = littleEndian ? bingeti32LE : bingeti32;
  233. for (integer i = 1; i <= my nx; i ++) {
  234. s [i] = get (f) / 2147483648.0 - 1.0;
  235. }
  236. }
  237. static void r4write (Sound me, FILE *f) {
  238. double *s = my z [1];
  239. for (integer i = 1; i <= my nx; i ++) {
  240. binputr32 (s [i], f);
  241. }
  242. }
  243. static void r4read (Sound me, FILE *f) {
  244. double *s = my z [1];
  245. for (integer i = 1; i <= my nx; i ++) {
  246. s [i] = bingetr32 (f);
  247. }
  248. }
  249. /* Old TIMIT sound-file format */
  250. autoSound Sound_readFromCmuAudioFile (MelderFile file) {
  251. try {
  252. bool littleEndian = true;
  253. autofile f = Melder_fopen (file, "rb");
  254. Melder_require (bingeti16LE (f) == 6, U"Incorrect header size.");
  255. bingeti16LE (f);
  256. short nChannels = bingeti16LE (f);
  257. Melder_require (nChannels == 1, U"Incorrect number of channels.");
  258. Melder_require (bingeti16LE (f) > 0, U"Incorrect sampling frequency.");
  259. integer nSamples = bingeti32LE (f);
  260. Melder_require (nSamples > 0, U"Incorrect number of samples.");
  261. autoSound me = Sound_createSimple (1, nSamples / 16000.0, 16000);
  262. i2read (me.get(), f, littleEndian);
  263. f.close (file);
  264. return me;
  265. } catch (MelderError) {
  266. Melder_throw (U"Sound not read from CMU audio file ", MelderFile_messageName (file), U".");
  267. }
  268. }
  269. autoSound Sound_readFromRawFile (MelderFile file, const char *format, int nBitsCoding, bool littleEndian, bool unSigned, integer skipNBytes, double samplingFrequency) {
  270. try {
  271. autofile f = Melder_fopen (file, "rb");
  272. if (! format) {
  273. format = "integer";
  274. }
  275. if (nBitsCoding <= 0) {
  276. nBitsCoding = 16;
  277. }
  278. integer nBytesPerSample = (nBitsCoding + 7) / 8;
  279. if (strequ (format, "float")) {
  280. nBytesPerSample = 4;
  281. }
  282. Melder_require (! (nBytesPerSample == 3), U"Number of bytes per sample should be 1, 2 or 4.");
  283. if (skipNBytes <= 0) {
  284. skipNBytes = 0;
  285. }
  286. integer nSamples = (MelderFile_length (file) - skipNBytes) / nBytesPerSample;
  287. Melder_require (nSamples > 0, U"No samples left to read.");
  288. autoSound me = Sound_createSimple (1, nSamples / samplingFrequency, samplingFrequency);
  289. fseek (f, skipNBytes, SEEK_SET);
  290. if (nBytesPerSample == 1 && unSigned) {
  291. u1read (me.get(), f);
  292. } else if (nBytesPerSample == 1 && ! unSigned) {
  293. i1read (me.get(), f);
  294. } else if (nBytesPerSample == 2 && unSigned) {
  295. u2read (me.get(), f, littleEndian);
  296. } else if (nBytesPerSample == 2 && ! unSigned) {
  297. i2read (me.get(), f, littleEndian);
  298. } else if (nBytesPerSample == 4 && unSigned) {
  299. u4read (me.get(), f, littleEndian);
  300. } else if (nBytesPerSample == 4 && ! unSigned) {
  301. i4read (me.get(), f, littleEndian);
  302. } else if (nBytesPerSample == 4 && strequ (format, "float")) {
  303. r4read (me.get(), f);
  304. }
  305. f.close (file);
  306. return me;
  307. } catch (MelderError) {
  308. Melder_throw (U"Sound not read from raw audio file ", MelderFile_messageName (file), U".");
  309. }
  310. }
  311. void Sound_writeToRawFile (Sound me, MelderFile file, const char *format, bool littleEndian, int nBitsCoding, bool unSigned) {
  312. try {
  313. integer nClip = 0;
  314. autofile f = Melder_fopen (file, "wb");
  315. if (! format) {
  316. format = "integer";
  317. }
  318. if (nBitsCoding <= 0) {
  319. nBitsCoding = 16;
  320. }
  321. integer nBytesPerSample = (nBitsCoding + 7) / 8;
  322. if (strequ (format, "float")) {
  323. nBytesPerSample = 4;
  324. }
  325. Melder_require (! (nBytesPerSample == 3), U"number of bytes per sample should be 1, 2 or 4.");
  326. if (nBytesPerSample == 1 && unSigned) {
  327. u1write (me, f, & nClip);
  328. } else if (nBytesPerSample == 1 && ! unSigned) {
  329. i1write (me, f, & nClip);
  330. } else if (nBytesPerSample == 2 && unSigned) {
  331. u2write (me, f, littleEndian, & nClip);
  332. } else if (nBytesPerSample == 2 && ! unSigned) {
  333. i2write (me, f, littleEndian, & nClip);
  334. } else if (nBytesPerSample == 4 && unSigned) {
  335. u4write (me, f, littleEndian, & nClip);
  336. } else if (nBytesPerSample == 4 && ! unSigned) {
  337. i4write (me, f, littleEndian, & nClip);
  338. } else if (nBytesPerSample == 4 && strequ (format, "float")) {
  339. r4write (me, f);
  340. }
  341. if (nClip > 0) {
  342. Melder_warning (nClip, U" from ", my nx, U" samples have been clipped.\nAdvice: you could scale the amplitudes or save as a binary file.");
  343. }
  344. Melder_require (feof ((FILE *) f) == 0 && ferror ((FILE *) f) == 0, U"Sound_writeToRawFile: not completed");
  345. f.close (file);
  346. } catch (MelderError) {
  347. Melder_throw (me, U": saving as raw file not performed.");
  348. }
  349. }
  350. struct dialogic_adpcm {
  351. char code;
  352. short last, index;
  353. short step_size [49];
  354. short adjust [8];
  355. };
  356. static void dialogic_adpcm_init (struct dialogic_adpcm *adpcm) {
  357. short step_size [49] = {
  358. 16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
  359. 41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
  360. 107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
  361. 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
  362. 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
  363. };
  364. short adjust [8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
  365. adpcm -> last = 0;
  366. adpcm -> index = 0;
  367. for (integer i = 0; i < 49; i ++) {
  368. adpcm -> step_size [i] = step_size [i];
  369. }
  370. for (integer i = 0; i < 8; i ++) {
  371. adpcm -> adjust [i] = adjust [i];
  372. }
  373. }
  374. /*
  375. The code is adapted from:
  376. Bob Edgar (), "PC Telephony - The complete guide to designing,
  377. building and programming systems using Dialogic and Related
  378. Hardware", 272-276.
  379. */
  380. static float dialogic_adpcm_decode (struct dialogic_adpcm *adpcm) {
  381. float scale = 32767.0 / 32768.0 / 2048.0;
  382. // nibble = B3 B2 B1 B0 (4 lower bits)
  383. // d(n) = ss(n)*B2 + ss(n)/2 *B1 + ss(n)/4*B0 + ss(n)/8
  384. short ss = adpcm -> step_size [adpcm -> index];
  385. short e = ss / 8;
  386. if (adpcm -> code & 0x01) {
  387. e += ss / 4;
  388. }
  389. if (adpcm -> code & 0x02) {
  390. e += ss / 2;
  391. }
  392. if (adpcm -> code & 0x04) {
  393. e += ss;
  394. }
  395. // If B3==1 then d(n) = -d(n);
  396. short diff = (adpcm -> code & 0x08) ? -e : e;
  397. // x(n) = x(n-1)+d(n)
  398. short s = adpcm -> last + diff;
  399. if (s > 2048) {
  400. s = 2048;
  401. }
  402. if (s < -2048) {
  403. s = -2048;
  404. }
  405. adpcm -> last = s;
  406. // ss(n+1) = ss(n) * 1.1*M(L(n)) via lookup table
  407. adpcm -> index += adpcm -> adjust [adpcm -> code & 0x07];
  408. if (adpcm -> index < 0) {
  409. adpcm -> index = 0;
  410. }
  411. if (adpcm -> index > 48) {
  412. adpcm -> index = 48;
  413. }
  414. return scale * s;
  415. }
  416. autoSound Sound_readFromDialogicADPCMFile (MelderFile file, double sampleRate) {
  417. try {
  418. autofile f = Melder_fopen (file, "rb");
  419. integer filelength = MelderFile_length (file);
  420. Melder_require (filelength > 0, U"File should not be empty.");
  421. // Two samples in each byte
  422. integer numberOfSamples = 2 * filelength;
  423. autoSound me = Sound_createSimple (1, numberOfSamples / sampleRate, sampleRate);
  424. // Read all bytes and decode
  425. struct dialogic_adpcm adpcm;
  426. dialogic_adpcm_init (& adpcm);
  427. integer n = 1;
  428. for (integer i = 1; i <= filelength; i ++) {
  429. unsigned char sc;
  430. integer nread = fread (& sc, 1, 1, f);
  431. Melder_require (nread == 1, U"Error: trying to read byte number ", i, U".");
  432. adpcm.code = (char) ((sc >> 4) & 0x0f);
  433. my z [1] [n ++] = dialogic_adpcm_decode (& adpcm);
  434. adpcm.code = (char) (sc & 0x0f);
  435. my z [1] [n ++] = dialogic_adpcm_decode (& adpcm);
  436. }
  437. f.close (file);
  438. return me;
  439. } catch (MelderError) {
  440. Melder_throw (U"Sound not read from Dialogic ADPCM file ", MelderFile_messageName (file), U".");
  441. }
  442. }
  443. void Sound_preEmphasis (Sound me, double preEmphasisFrequency) {
  444. if (preEmphasisFrequency >= 0.5 / my dx)
  445. return; // above Nyquist?
  446. double preEmphasis = exp (- 2.0 * NUMpi * preEmphasisFrequency * my dx);
  447. for (integer channel = 1; channel <= my ny; channel ++) {
  448. double *s = my z [channel];
  449. for (integer i = my nx; i >= 2; i --)
  450. s [i] -= preEmphasis * s [i - 1];
  451. }
  452. }
  453. void Sound_deEmphasis (Sound me, double deEmphasisFrequency) {
  454. double deEmphasis = exp (- 2.0 * NUMpi * deEmphasisFrequency * my dx);
  455. for (integer channel = 1; channel <= my ny; channel ++) {
  456. double *s = my z [channel];
  457. for (integer i = 2; i <= my nx; i ++) {
  458. s [i] += deEmphasis * s [i - 1];
  459. }
  460. }
  461. }
  462. autoSound Sound_createGaussian (double windowDuration, double samplingFrequency) {
  463. try {
  464. autoSound me = Sound_createSimple (1, windowDuration, samplingFrequency);
  465. double *s = my z [1];
  466. double imid = 0.5 * (my nx + 1), edge = exp (-12.0);
  467. for (integer i = 1; i <= my nx; i ++) {
  468. s [i] = (exp (-48.0 * (i - imid) * (i - imid) / (my nx + 1) / (my nx + 1)) - edge) / (1 - edge);
  469. }
  470. return me;
  471. } catch (MelderError) {
  472. Melder_throw (U"Sound not created from Gaussian function.");
  473. }
  474. }
  475. autoSound Sound_createHamming (double windowDuration, double samplingFrequency) {
  476. try {
  477. autoSound me = Sound_createSimple (1, windowDuration, samplingFrequency);
  478. double *s = my z [1];
  479. double p = 2.0 * NUMpi / (my nx - 1);
  480. for (integer i = 1; i <= my nx; i ++) {
  481. s [i] = 0.54 - 0.46 * cos ((i - 1) * p);
  482. }
  483. return me;
  484. } catch (MelderError) {
  485. Melder_throw (U"Sound not created from Hamming function.");
  486. };
  487. }
  488. static autoSound Sound_create2 (double minimumTime, double maximumTime, double samplingFrequency) {
  489. return Sound_create (1, minimumTime, maximumTime, Melder_iround ( (maximumTime - minimumTime) * samplingFrequency),
  490. 1.0 / samplingFrequency, minimumTime + 0.5 / samplingFrequency);
  491. }
  492. /*
  493. Trig functions whose arguments form a linear sequence x = x1 + n.dx,
  494. for n=0,1,2,... are efficiently calculated by the following recurrence:
  495. cos(a+dx) = cos(a) - (alpha . cos(a) + beta . sin(a))
  496. sin(a+dx) = sin(a) - (alpha . sin(a) - beta . sin(a))
  497. where alpha and beta are precomputed coefficients
  498. alpha = 2 sin^2(dx/2) and beta = sin(dx)
  499. In this way aplha and beta do not lose significance if the increment
  500. dx is small.
  501. */
  502. static autoSound Sound_createToneComplex (double minimumTime, double maximumTime, double samplingFrequency, double firstFrequency, integer numberOfComponents, double frequencyDistance, integer mistunedComponent, double mistuningFraction, bool scaleAmplitudes) {
  503. try {
  504. autoSound me = Sound_create2 (minimumTime, maximumTime, samplingFrequency);
  505. for (integer j = 1; j <= numberOfComponents; j ++) {
  506. double fraction = j == mistunedComponent ? mistuningFraction : 0;
  507. double w = 2 * NUMpi * (firstFrequency + (j - 1 + fraction) * frequencyDistance);
  508. double delta = w * my dx;
  509. double alpha = 2 * sin (delta / 2) * sin (delta / 2);
  510. double beta = sin (delta);
  511. double sint = sin (w * my x1);
  512. double cost = cos (w * my x1);
  513. my z [1] [1] += sint;
  514. for (integer i = 2; i <= my nx; i ++) {
  515. double costd = cost - (alpha * cost + beta * sint);
  516. double sintd = sint - (alpha * sint - beta * cost);
  517. my z [1] [i] += sintd;
  518. cost = costd; sint = sintd;
  519. }
  520. }
  521. if (scaleAmplitudes) {
  522. Vector_scale (me.get(), 0.99996948);
  523. }
  524. return me;
  525. } catch (MelderError) {
  526. Melder_throw (U"Sound not created from tone complex.");
  527. }
  528. }
  529. autoSound Sound_createSimpleToneComplex (double minimumTime, double maximumTime, double samplingFrequency, double firstFrequency, integer numberOfComponents, double frequencyDistance, bool scaleAmplitudes) {
  530. if (firstFrequency + (numberOfComponents - 1) * frequencyDistance > samplingFrequency / 2) {
  531. Melder_warning (U"Sound_createSimpleToneComplex: frequency of (some) components too high.");
  532. numberOfComponents = Melder_ifloor (1.0 + (0.5 * samplingFrequency - firstFrequency) / frequencyDistance);
  533. }
  534. return Sound_createToneComplex (minimumTime, maximumTime, samplingFrequency,
  535. firstFrequency, numberOfComponents, frequencyDistance, 0, 0, scaleAmplitudes);
  536. }
  537. autoSound Sound_createMistunedHarmonicComplex (double minimumTime, double maximumTime, double samplingFrequency, double firstFrequency, integer numberOfComponents, integer mistunedComponent, double mistuningFraction, bool scaleAmplitudes) {
  538. if (firstFrequency + (numberOfComponents - 1) * firstFrequency > samplingFrequency / 2) {
  539. Melder_warning (U"Sound_createMistunedHarmonicComplex: frequency of (some) components too high.");
  540. numberOfComponents = Melder_ifloor (1.0 + (0.5 * samplingFrequency - firstFrequency) / firstFrequency);
  541. }
  542. if (mistunedComponent > numberOfComponents) {
  543. Melder_warning (U"Sound_createMistunedHarmonicComplex: mistuned component too high.");
  544. }
  545. return Sound_createToneComplex (minimumTime, maximumTime, samplingFrequency, firstFrequency, numberOfComponents, firstFrequency, mistunedComponent, mistuningFraction, scaleAmplitudes);
  546. }
  547. /*
  548. The gammachirp is a "chirp tone" with a gamma-function envelope:
  549. f(t) = t^(n-1) exp (-2 pi b t) cos (2 pi f0 t + c ln (t) + p0)
  550. = t^(n-1) exp (-2 pi b t) cos (phi(t))
  551. Instantaneous frequency f is defined as f = d phi(t) / dt / (2 pi)
  552. and so: f = f0 + c /(2 pi t)
  553. Irino: bandwidth = (frequency * (6.23e-6 * frequency + 93.39e-3) + 28.52)
  554. */
  555. autoSound Sound_createGammaTone (double minimumTime, double maximumTime, double samplingFrequency, double gamma, double frequency, double bandwidth, double initialPhase, double addition, bool scaleAmplitudes) {
  556. try {
  557. autoSound me = Sound_create2 (minimumTime, maximumTime, samplingFrequency);
  558. for (integer i = 1; i <= my nx; i ++) {
  559. double t = (i - 0.5) * my dx;
  560. double f = frequency + addition / (NUM2pi * t);
  561. if (f > 0 && f < samplingFrequency / 2) {
  562. my z [1] [i] = pow (t, gamma - 1.0) * exp (- NUM2pi * bandwidth * t) *
  563. cos (NUM2pi * frequency * t + addition * log (t) + initialPhase);
  564. }
  565. }
  566. if (scaleAmplitudes) {
  567. Vector_scale (me.get(), 0.99996948);
  568. }
  569. return me;
  570. } catch (MelderError) {
  571. Melder_throw (U"Sound not created from gammatone function.");
  572. }
  573. }
  574. #if 0
  575. // This routine is unstable for small values of f and large b. Better to use cross-correlation of gammatone with sound.
  576. static void NUMgammatoneFilter4 (double *x, double *y, integer n, double centre_frequency, double bandwidth, double samplingFrequency) {
  577. double a [5], b [9], dt = 1.0 / samplingFrequency, wt = NUMpi * centre_frequency * dt;
  578. double bt = 2 * NUMpi * bandwidth * dt, dt2 = dt * dt, dt4 = dt2 * dt2;
  579. Melder_assert (n > 0 && centre_frequency > 0 && bandwidth >= 0 && samplingFrequency > 0);
  580. /*
  581. The filter function is:
  582. H(z) = sum (i=0..4, a [i] z^-i) / sum (j=0..4, b [j] z^-j)
  583. Coefficients a & b according to:
  584. Slaney (1993), An efficient implementation of the Patterson-Holdsworth
  585. auditory filterbank, Apple Computer Technical Report 35, 41 pages.
  586. For the a's we have left out an overal scale factor of dt^4.
  587. This makes a [0] = 1.
  588. */
  589. a [0] = dt4;
  590. a [1] = -4 * dt4 * cos (2 * wt) * exp (- bt);
  591. a [2] = 6 * dt4 * cos (4 * wt) * exp (-2 * bt);
  592. a [3] = -4 * dt4 * cos (6 * wt) * exp (-3 * bt);
  593. a [4] = dt4 * cos (8 * wt) * exp (-4 * bt);
  594. b [0] = 1;
  595. b [1] = -8 * cos (2 * wt) * exp (- bt);
  596. b [2] = (16 + 12 * cos (4 * wt)) * exp (-2 * bt);
  597. b [3] = (-48 * cos (2 * wt) - 8 * cos (6 * wt)) * exp (-3 * bt);
  598. b [4] = (36 + 32 * cos (4 * wt) + 2 * cos (8 * wt)) * exp (-4 * bt);
  599. b [5] = (-48 * cos (2 * wt) - 8 * cos (6 * wt)) * exp (-5 * bt);
  600. b [6] = (16 + 12 * cos (4 * wt)) * exp (-6 * bt);
  601. b [7] = -8 * cos (2 * wt) * exp (-7 * bt);
  602. b [8] = exp (-8 * bt);
  603. // Calculate gain (= Abs (H(z); f=fc) and scale a [0-4] with it.
  604. double zr = cos (2 * wt), zi = -sin (2 * wt);
  605. double dr = a [4], di = 0, tr, ti, nr, ni;
  606. for (integer j = 1; j <= 4; j ++) {
  607. tr = a [4 - j] + zr * dr - zi * di;
  608. ti = zi * dr + zr * di;
  609. dr = tr; di = ti;
  610. }
  611. dr = b [8];
  612. di = 0;
  613. for (integer j = 1; j <= 8; j ++) {
  614. nr = b [8 - j] + zr * dr - zi * di;
  615. ni = zi * dr + zr * di;
  616. dr = nr; di = ni;
  617. }
  618. double n2 = nr * nr + ni * ni;
  619. double gr = tr * nr + ti * ni;
  620. double gi = ti * nr - tr * ni;
  621. double gain = sqrt (gr * gr + gi * gi) / n2;
  622. for (integer j = 0; j <= 4; j ++) {
  623. a [j] /= gain;
  624. }
  625. if (Melder_debug == -1) {
  626. Melder_casual (
  627. U" --gammatonefilter4 --\nF = ", centre_frequency,
  628. U", B = ", bandwidth,
  629. U", T = ", dt,
  630. U"\nGain = ", gain
  631. );
  632. for (integer i = 0; i <= 4; i ++) {
  633. Melder_casual (U"a [", i, U"] = ", a [i]);
  634. }
  635. for (integer i = 0; i <= 8; i ++) {
  636. Melder_casual (U"b [", i, U"] = ", b [i]);
  637. }
  638. }
  639. /* Perform the filtering. For the first 8 samples we must do some extra work.
  640. y [1] = a [0] * x [1];
  641. if (n > 1) {
  642. y [2] = a [0] * x [2];
  643. y [2] += a [1] * x [1] - b [1] * y [1];
  644. }
  645. if (n > 2) {
  646. y [2] = a [0] * x [2];
  647. y [2] += a [2] * x [i - 2] - b [2] * y [i - 2];
  648. }
  649. */
  650. integer n8 = n < 8 ? n : 8;
  651. for (integer i = 1; i <= n8; i ++) {
  652. y [i] = a [0] * x [i];
  653. if (i > 1) {
  654. y [i] += a [1] * x [i - 1] - b [1] * y [i - 1];
  655. } else {
  656. continue;
  657. }
  658. if (i > 2) {
  659. y [i] += a [2] * x [i - 2] - b [2] * y [i - 2];
  660. } else {
  661. continue;
  662. }
  663. if (i > 3) {
  664. y [i] += a [3] * x [i - 3] - b [3] * y [i - 3];
  665. } else {
  666. continue;
  667. }
  668. if (i > 4) {
  669. y [i] += a [4] * x [i - 4] - b [4] * y [i - 4];
  670. } else {
  671. continue;
  672. }
  673. if (i > 5) {
  674. y [i] -= b [5] * y [i - 5];
  675. } else {
  676. continue;
  677. }
  678. if (i > 6) {
  679. y [i] -= b [6] * y [i - 6];
  680. } else {
  681. continue;
  682. }
  683. if (i > 7) {
  684. y [i] -= b [7] * y [i - 7];
  685. }
  686. }
  687. for (integer i = n8 + 1; i <= n; i ++) {
  688. // y [i] = a [0] * x [i];
  689. // y [i] += a [1] * x [i-1] + a [2] * x [i-2] + a [3] * x [i-3] + a [4] * x [i-4];
  690. // y [i] -= b [1] * y [i-1] + b [2] * y [i-2] + b [3] * y [i-3] + b [4] * y [i-4];
  691. // y [i] -= b [5] * y [i-5] + b [6] * y [i-6] + b [7] * y [i-7] + b [8] * y [i-8];
  692. y [i] = a [0] * x [i] + a [1] * x [i - 1] + a [2] * x [i - 2] + a [3] * x [i - 3] + a [4] * x [i - 4]
  693. - b [1] * y [i - 1] - b [2] * y [i - 2] - b [3] * y [i - 3] - b [4] * y [i - 4]
  694. - b [5] * y [i - 5] - b [6] * y [i - 6] - b [7] * y [i - 7] - b [8] * y [i - 8];
  695. }
  696. }
  697. autoSound Sound_filterByGammaToneFilter4 (Sound me, double centre_frequency, double bandwidth) {
  698. try {
  699. Meldr_require (centre_frequency > 0, U"Centre frequency should be positive.");
  700. Melder_require (bandwidth > 0, U"Bandwidth should be positive.");
  701. autoSound thee = Sound_create (my ny, my xmin, my xmax, my nx, my dx, my x1);
  702. autoNUMvector<double> y (1, my nx);
  703. autoNUMvector<double> x (1, my nx);
  704. double fs = 1 / my dx;
  705. for (integer channel = 1; channel <= my ny; channel ++) {
  706. for (integer i = 1; i <= my nx; i ++) {
  707. x [i] = my z [channel] [i];
  708. }
  709. NUMgammatoneFilter4 (x.peek(), y.peek(), my nx, centre_frequency, bandwidth, fs);
  710. for (integer i = 1; i <= my nx; i ++) {
  711. thy z [channel] [i] = y [i];
  712. }
  713. }
  714. return thee;
  715. } catch (MelderError) {
  716. Melder_throw (U"Sound not filtered by gammatone filter4.");
  717. }
  718. }
  719. #endif
  720. autoSound Sound_filterByGammaToneFilter4 (Sound me, double centre_frequency, double bandwidth) {
  721. return Sound_filterByGammaToneFilter (me, centre_frequency, bandwidth, 4.0, 0.0);
  722. }
  723. autoSound Sound_filterByGammaToneFilter (Sound me, double centre_frequency, double bandwidth, double gamma, double initialPhase) {
  724. try {
  725. autoSound gammaTone = Sound_createGammaTone (my xmin, my xmax, 1.0 / my dx, gamma, centre_frequency, bandwidth, initialPhase, 0.0, 0);
  726. // kSounds_convolve_scaling_INTEGRAL, SUM, NORMALIZE, PEAK_099
  727. autoSound thee = Sounds_convolve (me, gammaTone.get(), kSounds_convolve_scaling::INTEGRAL, kSounds_convolve_signalOutsideTimeDomain::ZERO);
  728. double response_re, response_im;
  729. dcomplex r = gammaToneFilterResponseAtCentreFrequency (centre_frequency, bandwidth, gamma, initialPhase, my xmax - my xmin);
  730. double scale = 1.0 / sqrt (r.re * r.re + r.im * r.im);
  731. for (integer i = 1; i <= thy nx; i ++) {
  732. thy z [1] [i] *= scale;
  733. }
  734. return thee;
  735. } catch (MelderError) {
  736. Melder_throw (U"Sound not filtered by gammatone filter4.");
  737. }
  738. }
  739. /*
  740. Sound Sound_createShepardTone (double minimumTime, double maximumTime, double samplingFrequency,
  741. double baseFrequency, double frequencyShiftFraction, double maximumFrequency, double amplitudeRange)
  742. {
  743. Sound me; integer i, j, nComponents = 1 + log2 (maximumFrequency / 2 / baseFrequency);
  744. double lmin = pow (10, - amplitudeRange / 10);
  745. double twoPi = 2.0 * NUMpi, f = baseFrequency * (1 + frequencyShiftFraction);
  746. if (nComponents < 2) Melder_warning (U"Sound_createShepardTone: only 1 component.");
  747. Melder_casual (U"Sound_createShepardTone: ", nComponents, U" components.");
  748. if (! (me = Sound_create2 (minimumTime, maximumTime, samplingFrequency))) return nullptr;
  749. for (j=1; j <= nComponents; j ++)
  750. {
  751. double fj = f * pow (2, j-1), wj = twoPi * fj;
  752. double amplitude = lmin + (1 - lmin) *
  753. (1 - cos (twoPi * log (fj + 1) / log (maximumFrequency + 1))) / 2;
  754. for (i=1; i <= my nx; i ++)
  755. {
  756. my z [1] [i] += amplitude * sin (wj * (i - 0.5) * my dx);
  757. }
  758. }
  759. Vector_scale (me, 0.99996948);
  760. return me;
  761. }
  762. */
  763. autoSound Sound_createShepardToneComplex (double minimumTime, double maximumTime, double samplingFrequency, double lowestFrequency, integer numberOfComponents, double frequencyChange_st, double amplitudeRange, double octaveShiftFraction) {
  764. try {
  765. double highestFrequency = lowestFrequency * pow (2, numberOfComponents);
  766. double lmax_db = 0, lmin_db = lmax_db - fabs (amplitudeRange);
  767. Melder_require (highestFrequency <= samplingFrequency / 2.0,U"The highest frequency you want to generate is "
  768. U"above the Nyquist frequency. Choose a larger value for \"Sampling frequency\", or lower values for "
  769. U"\"Number of components\" or \"Lowest frequency\".");
  770. Melder_require (octaveShiftFraction >= 0.0 && octaveShiftFraction < 1.0, U"Octave offset fraction should be greater or equal zero and smaller than one.");
  771. double octaveTime, sweeptime;
  772. if (frequencyChange_st != 0.0) {
  773. octaveTime = 12.0 / fabs (frequencyChange_st);
  774. sweeptime = numberOfComponents * octaveTime;
  775. } else {
  776. octaveTime = sweeptime = 1e308;
  777. }
  778. autoSound me = Sound_create2 (minimumTime, maximumTime, samplingFrequency);
  779. double a = frequencyChange_st / 12.0;
  780. for (integer i = 1; i <= numberOfComponents; i ++) {
  781. double tswitch;
  782. double freqi = lowestFrequency * pow (2.0, i - 1 + octaveShiftFraction);
  783. double b1, b2;
  784. double phase1 = 0, phasejm1 = 0;
  785. /*
  786. The frequency is f(t) = lowestFrequency * 2^tone(t)
  787. The tone is parametrized with a straight line: tone(t) = a * t + b
  788. where a = frequencyChange_st / 12 and b depends on the component
  789. If frequencyChange_st >=0
  790. The tone rises until highest frequency at t=tswich, then falls to lowest and starts rising again.
  791. The slope is always the same. The offsets are b1 and b2 respectively.
  792. We count octaveShiftFraction as distance from tone base
  793. else if frequencyChange_st < 0
  794. The tone falls until the lowest frequency at t=tswich, then jumps to highest and starts falling again
  795. All tones start one octave higher as in rising case.
  796. We also count octaveShiftFraction down from this tone base.
  797. else
  798. No changes in frequency of the components.
  799. endif
  800. */
  801. if (frequencyChange_st >= 0) {
  802. b1 = i - 1 + octaveShiftFraction;
  803. b2 = 0.0;
  804. tswitch = (numberOfComponents - b1) * octaveTime;
  805. } else {
  806. freqi *= 2;
  807. b1 = i - octaveShiftFraction;
  808. b2 = numberOfComponents;
  809. tswitch = b1 * octaveTime;
  810. }
  811. for (integer j = 1; j <= my nx; j ++) {
  812. double t = Sampled_indexToX (me.get(), j);
  813. double tmod = fmod (t, sweeptime);
  814. double tone = tmod <= tswitch ? b1 + a * tmod : b2 + a * (tmod - tswitch);
  815. double f = lowestFrequency * pow (2, tone);
  816. /* double theta = 2 * NUMpi * log2 (f / lowestFrequency) / numberOfComponents; */
  817. double theta = 2 * NUMpi * tone / numberOfComponents;
  818. double level = pow (10, (lmin_db + (lmax_db - lmin_db) * (1 - cos (theta)) / 2) / 20);
  819. double phasej = phasejm1 + 2 * NUMpi * f * my dx; /* Integrate 2*pi*f(t) */
  820. if (j == 1) {
  821. phase1 = phasej; // phase1 = j == 1 ? phasej : phase1;
  822. }
  823. my z [1] [j] += level * sin (phasej - phase1); // si
  824. phasejm1 = phasej;
  825. }
  826. }
  827. Vector_scale (me.get(), 0.99996948);
  828. return me;
  829. } catch (MelderError) {
  830. Melder_throw (U"Sound not created from Shepard tone complex.");
  831. }
  832. }
  833. /* can be implemented more efficiently with sin recurrence? */
  834. /* amplitude(f) = min + (1-min)*(1-cos(2*pi*(ln(f/f1) / ln(fn/f1)))/2 */
  835. autoSound Sound_createShepardTone (double minimumTime, double maximumTime, double samplingFrequency, double lowestFrequency, integer numberOfComponents, double frequencyChange_st, double amplitudeRange) {
  836. try {
  837. double scale = pow (2.0, numberOfComponents);
  838. double maximumFrequency = lowestFrequency * scale;
  839. double lmin = pow (10.0, - amplitudeRange / 10.0), twoPi = 2.0 * NUMpi;
  840. double ln2t0 = log (2.0) * frequencyChange_st / 12.0;
  841. double lnf1 = log (lowestFrequency + 1.0);
  842. double amplarg = twoPi / log ((maximumFrequency + 1.0) / (lowestFrequency + 1.0));
  843. Melder_require (lowestFrequency <= 0.5 * samplingFrequency, U"Sound_createShepardTone: lowest frequency too high.");
  844. Melder_require (maximumFrequency <= 0.5 * samplingFrequency, U"Sound_createShepardTone: frequency of highest component too high.");
  845. autoSound me = Sound_create2 (minimumTime, maximumTime, samplingFrequency);
  846. for (integer i = 1; i <= my nx; i ++) {
  847. double argt, t = (i - 0.5) * my dx, ft = lowestFrequency;
  848. if (frequencyChange_st != 0.0) {
  849. double expt = exp (ln2t0 * t);
  850. argt = twoPi * lowestFrequency * (expt - 1.0) / ln2t0;
  851. ft *= expt;
  852. } else {
  853. argt = twoPi * ft * t;
  854. }
  855. for (integer j = 1; j <= numberOfComponents; j ++) {
  856. while (ft >= maximumFrequency) {
  857. ft /= scale;
  858. argt /= scale;
  859. }
  860. //amplitude = lmin + (1 - lmin) * (1 - cos (twoPi * log (ft + 1) / log (maximumFrequency + 1))) / 2;
  861. double amplitude = lmin + (1 - lmin) * (1 - cos (amplarg * (log (ft + 1) - lnf1))) / 2.0;
  862. my z [1] [i] += amplitude * sin (argt);
  863. ft *= 2.0; argt *= 2.0;
  864. }
  865. }
  866. Vector_scale (me.get(), 0.99996948);
  867. return me;
  868. } catch (MelderError) {
  869. Melder_throw (U"Sound not created from Shepard tone.");
  870. }
  871. }
  872. autoSound Sound_createPattersonWightmanTone (double minimumTime, double maximumTime, double samplingFrequency, double baseFrequency, double frequencyShiftRatio, integer numberOfComponents) {
  873. try {
  874. Melder_require ((numberOfComponents - 1 + frequencyShiftRatio) * baseFrequency <= samplingFrequency / 2.0,
  875. U"Frequency of one or more components too large.");
  876. autoSound me = Sound_create2 (minimumTime, maximumTime, samplingFrequency);
  877. double w0 = NUM2pi * baseFrequency;
  878. for (integer i = 1; i <= my nx; i ++) {
  879. double a = 0, t = (i - 0.5) * my dx;
  880. for (integer j = 1; j <= numberOfComponents; j ++) {
  881. a += sin ( (j + frequencyShiftRatio) * w0 * t);
  882. }
  883. my z [1] [i] = a;
  884. }
  885. Vector_scale (me.get(), 0.99996948);
  886. return me;
  887. } catch (MelderError) {
  888. Melder_throw (U"Sound not created from Patterson Wightman tone.");
  889. }
  890. }
  891. autoSound Sound_createPlompTone (double minimumTime, double maximumTime, double samplingFrequency, double baseFrequency, double frequencyFraction, integer m) {
  892. try {
  893. Melder_require (12.0 * (1.0 + frequencyFraction) * baseFrequency <= samplingFrequency / 2.0,
  894. U"Sound_createPlompTone: frequency of one or more components too large.");
  895. double w1 = NUM2pi * (1.0 - frequencyFraction) * baseFrequency;
  896. double w2 = NUM2pi * (1.0 + frequencyFraction) * baseFrequency;
  897. autoSound me = Sound_create2 (minimumTime, maximumTime, samplingFrequency);
  898. for (integer i = 1; i <= my nx; i ++) {
  899. double a = 0, t = (i - 0.5) * my dx;
  900. for (integer j = 1; j <= m; j ++) {
  901. a += sin (j * w1 * t);
  902. }
  903. for (integer j = m + 1; j <= 12; j ++) {
  904. a += sin (j * w2 * t);
  905. }
  906. my z [1] [i] = a;
  907. }
  908. Vector_scale (me.get(), 0.99996948);
  909. return me;
  910. } catch (MelderError) {
  911. Melder_throw (U"Sound not created from Plomp tone.");
  912. }
  913. }
  914. void Sounds_multiply (Sound me, Sound thee) {
  915. integer n = my nx < thy nx ? my nx : thy nx;
  916. double *s1 = my z [1], *s2 = thy z [1];
  917. for (integer i = 1; i <= n; i ++) {
  918. s1 [i] *= s2 [i];
  919. }
  920. }
  921. double Sound_power (Sound me) {
  922. double e = 0.0, *s = my z [1];
  923. for (integer i = 1; i <= my nx; i ++) {
  924. e += s [i] * s [i];
  925. }
  926. return sqrt (e) * my dx / (my xmax - my xmin);
  927. }
  928. double Sound_correlateParts (Sound me, double tx, double ty, double duration) {
  929. if (ty < tx) {
  930. double t = tx;
  931. tx = ty;
  932. ty = t;
  933. }
  934. integer nbx = Sampled_xToNearestIndex (me, tx);
  935. integer nby = Sampled_xToNearestIndex (me, ty);
  936. integer ney = Sampled_xToNearestIndex (me, ty + duration);
  937. integer increment = 0, decrement = 0;
  938. if (nbx < 1) {
  939. increment = 1 - nbx;
  940. }
  941. if (ney > my nx) {
  942. decrement = ney - my nx;
  943. }
  944. integer ns = Melder_ifloor (duration / my dx) - increment - decrement;
  945. if (ns < 1) {
  946. return 0.0;
  947. }
  948. double *x = & my z [1] [nbx + increment - 1];
  949. double *y = & my z [1] [nby + increment - 1];
  950. double xm = 0.0, ym = 0.0, sxx = 0.0, syy = 0.0, sxy = 0.0;
  951. for (integer i = 1; i <= ns; i ++) {
  952. xm += x [i];
  953. ym += y [i];
  954. }
  955. xm /= ns;
  956. ym /= ns;
  957. for (integer i = 1; i <= ns; i ++) {
  958. double xt = x [i] - xm, yt = y [i] - ym;
  959. sxx += xt * xt;
  960. syy += yt * yt;
  961. sxy += xt * yt;
  962. }
  963. double denum = sxx * syy;
  964. double rxy = denum > 0.0 ? sxy / sqrt (denum) : 0.0;
  965. return rxy;
  966. }
  967. void Sound_localMean (Sound me, double fromTime, double toTime, double *p_mean) {
  968. integer n1 = Sampled_xToNearestIndex (me, fromTime);
  969. integer n2 = Sampled_xToNearestIndex (me, toTime);
  970. double mean = 0.0;
  971. if (fromTime <= toTime) {
  972. if (n1 < 1) {
  973. n1 = 1;
  974. }
  975. if (n2 > my nx) {
  976. n2 = my nx;
  977. }
  978. Melder_assert (n1 <= n2);
  979. mean = NUMmean ({& my z [1] [n1], n2 - n1 + 1});
  980. }
  981. if (p_mean) {
  982. *p_mean = mean;
  983. }
  984. }
  985. void Sound_localPeak (Sound me, double fromTime, double toTime, double ref, double *p_peak) {
  986. integer n1 = Sampled_xToNearestIndex (me, fromTime);
  987. integer n2 = Sampled_xToNearestIndex (me, toTime);
  988. double *s = my z [1], peak = -1e308;
  989. if (fromTime <= toTime) {
  990. if (n1 < 1) {
  991. n1 = 1;
  992. }
  993. if (n2 > my nx) {
  994. n2 = my nx;
  995. }
  996. for (integer i = n1; i <= n2; i ++) {
  997. double ds = fabs (s [i] - ref);
  998. if (ds > peak) {
  999. peak = ds;
  1000. }
  1001. }
  1002. }
  1003. if (p_peak) {
  1004. *p_peak = peak;
  1005. }
  1006. }
  1007. void Sound_into_Sound (Sound me, Sound to, double startTime) {
  1008. integer index = Sampled_xToNearestIndex (me, startTime);
  1009. for (integer i = 1; i <= to -> nx; i ++) {
  1010. integer j = index - 1 + i;
  1011. to -> z [1] [i] = j < 1 || j > my nx ? 0 : my z [1] [j];
  1012. }
  1013. }
  1014. /*
  1015. IntervalTier Sound_PointProcess_to_IntervalTier (Sound me, PointProcess thee, double window);
  1016. IntervalTier Sound_PointProcess_to_IntervalTier (Sound me, PointProcess thee, double window)
  1017. {
  1018. double window2 = window / 2;
  1019. double t1 = thy t [1] - window2;
  1020. if (t1 < my xmin) t1 = my xmin;
  1021. double t2 = t1 + window2;
  1022. if (t2 > my xmax) t2 = my xmax;
  1023. autoIntervalTier him = IntervalTier_create (my xmin, my xmax);
  1024. autoTextInterval interval = TextInterval_create (t1, t2, "yes");
  1025. his intervals -> addItem_move (interval.move());
  1026. for (integer i = 2; i <= thy nt; i ++)
  1027. {
  1028. double t = thy t [i];
  1029. if (t <= t2)
  1030. {
  1031. integer index = his points.size;
  1032. RealPoint point = his points->at [index];
  1033. t2 = t + window2;
  1034. if (t2 > my xmax) t2 = my xmax;
  1035. point -> value = t2;
  1036. }
  1037. else
  1038. {
  1039. t2 = t + window2;
  1040. if (t2 > my xmax) t2 = my xmax;
  1041. RealTier_addPoint (him, t, t2);
  1042. }
  1043. }
  1044. return him;
  1045. }
  1046. */
  1047. /*
  1048. First approximation on the basis of differences in the sampled signal.
  1049. The underlying analog signal still could have jumps undetected by this algorithm.
  1050. We could get a better approximation by first upsampling the signal.
  1051. */
  1052. autoPointProcess Sound_to_PointProcess_getJumps (Sound me, double minimumJump, double dt) {
  1053. try {
  1054. autoPointProcess thee = PointProcess_create (my xmin, my xmax, 10);
  1055. integer i = 1, dtn = Melder_ifloor (dt / my dx);
  1056. if (dtn < 1) {
  1057. dtn = 1;
  1058. }
  1059. double *s = my z [1];
  1060. while (i < my nx) {
  1061. integer j = i + 1, step = 1;
  1062. while (j <= i + dtn && j <= my nx) {
  1063. if (fabs (s [i] - s [j]) > minimumJump) {
  1064. double t = Sampled_indexToX (me, i);
  1065. PointProcess_addPoint (thee.get(), t);
  1066. step = j - i + 1; break;
  1067. }
  1068. j ++;
  1069. }
  1070. i += step;
  1071. }
  1072. return thee;
  1073. } catch (MelderError) {
  1074. Melder_throw (me, U": no PointProcess created.");
  1075. }
  1076. }
  1077. /* Internal pitch representation in semitones */
  1078. autoSound Sound_Pitch_changeSpeaker (Sound me, Pitch him, double formantMultiplier, double pitchMultiplier, double pitchRangeMultiplier, double durationMultiplier) {
  1079. try {
  1080. double samplingFrequency_old = 1.0 / my dx;
  1081. Melder_require (my xmin == his xmin && my xmax == his xmax, U"The Pitch and the Sound object should have the same domain.");
  1082. autoSound sound = Data_copy (me);
  1083. Vector_subtractMean (sound.get());
  1084. if (formantMultiplier != 1.0) {
  1085. // Shift all frequencies (inclusive pitch!) */
  1086. Sound_overrideSamplingFrequency (sound.get(), samplingFrequency_old * formantMultiplier);
  1087. }
  1088. autoPitch pitch = Data_copy (him);
  1089. Pitch_scaleDuration (pitch.get(), 1.0 / formantMultiplier); //
  1090. Pitch_scalePitch (pitch.get(), formantMultiplier);
  1091. autoPointProcess pulses = Sound_Pitch_to_PointProcess_cc (sound.get(), pitch.get());
  1092. autoPitchTier pitchTier = Pitch_to_PitchTier (pitch.get());
  1093. double median = Pitch_getQuantile (pitch.get(), 0.0, 0.0, 0.5, kPitch_unit::HERTZ);
  1094. if (isdefined (median) && median != 0.0) {
  1095. /* Incorporate pitch shift from overriding the sampling frequency */
  1096. PitchTier_multiplyFrequencies (pitchTier.get(), sound -> xmin, sound -> xmax, pitchMultiplier / formantMultiplier);
  1097. PitchTier_modifyExcursionRange (pitchTier.get(), sound -> xmin, sound -> xmax, pitchRangeMultiplier, median);
  1098. } else if (pitchMultiplier != 1) {
  1099. Melder_warning (U"Pitch has not been changed because the sound was entirely voiceless.");
  1100. }
  1101. autoDurationTier duration = DurationTier_create (my xmin, my xmax);
  1102. RealTier_addPoint (duration.get(), (my xmin + my xmax) / 2, formantMultiplier * durationMultiplier);
  1103. autoSound thee = Sound_Point_Pitch_Duration_to_Sound (sound.get(), pulses.get(), pitchTier.get(), duration.get(), MAX_T);
  1104. // Resample to the original sampling frequency
  1105. if (formantMultiplier != 1.0) {
  1106. thee = Sound_resample (thee.get(), samplingFrequency_old, 10);
  1107. }
  1108. return thee;
  1109. } catch (MelderError) {
  1110. Melder_throw (U"Sound not created from Pitch & Sound.");
  1111. }
  1112. }
  1113. autoSound Sound_changeSpeaker (Sound me, double pitchMin, double pitchMax, double formantMultiplier, double pitchMultiplier, double pitchRangeMultiplier, double durationMultiplier) { // > 0
  1114. try {
  1115. autoPitch pitch = Sound_to_Pitch (me, 0.8 / pitchMin, pitchMin, pitchMax);
  1116. autoSound thee = Sound_Pitch_changeSpeaker (me, pitch.get(), formantMultiplier, pitchMultiplier, pitchRangeMultiplier, durationMultiplier);
  1117. return thee;
  1118. } catch (MelderError) {
  1119. Melder_throw (me, U": speaker not changed.");
  1120. }
  1121. }
  1122. autoTextGrid Sound_to_TextGrid_detectSilences (Sound me, double minPitch, double timeStep,
  1123. double silenceThreshold, double minSilenceDuration, double minSoundingDuration,
  1124. conststring32 silentLabel, conststring32 soundingLabel) {
  1125. try {
  1126. bool subtractMeanPressure = true;
  1127. autoSound filtered = Sound_filter_passHannBand (me, 80.0, 8000.0, 80.0);
  1128. autoIntensity thee = Sound_to_Intensity (filtered.get(), minPitch, timeStep, subtractMeanPressure);
  1129. autoTextGrid him = Intensity_to_TextGrid_detectSilences (thee.get(), silenceThreshold, minSilenceDuration, minSoundingDuration, silentLabel, soundingLabel);
  1130. return him;
  1131. } catch (MelderError) {
  1132. Melder_throw (me, U": no TextGrid with silences created.");
  1133. }
  1134. }
  1135. void Sound_getStartAndEndTimesOfSounding (Sound me, double minPitch, double timeStep, double silenceThreshold, double minSilenceDuration, double minSoundingDuration, double *t1, double *t2) {
  1136. try {
  1137. conststring32 silentLabel = U"-", soundingLabel = U"+";
  1138. autoTextGrid dbs = Sound_to_TextGrid_detectSilences (me, minPitch, timeStep, silenceThreshold, minSilenceDuration, minSoundingDuration, silentLabel, soundingLabel);
  1139. IntervalTier tier = (IntervalTier) dbs -> tiers->at [1];
  1140. Melder_assert (tier -> intervals.size > 0);
  1141. TextInterval interval = tier -> intervals.at [1];
  1142. if (t1) {
  1143. *t1 = my xmin;
  1144. if (Melder_equ (interval -> text.get(), silentLabel)) {
  1145. *t1 = interval -> xmax;
  1146. }
  1147. }
  1148. if (t2) {
  1149. *t2 = my xmax;
  1150. interval = tier -> intervals.at [tier -> intervals.size];
  1151. if (Melder_equ (interval -> text.get(), silentLabel)) {
  1152. *t2 = interval -> xmin;
  1153. }
  1154. }
  1155. } catch (MelderError) {
  1156. Melder_throw (U"Sounding times not found.");
  1157. }
  1158. }
  1159. autoSound Sound_IntervalTier_cutPartsMatchingLabel (Sound me, IntervalTier thee, conststring32 match) {
  1160. try {
  1161. // count samples of the trimmed sound
  1162. integer ixmin, ixmax, numberOfSamples = 0, previous_ixmax = 0;
  1163. double xmin = my xmin; // start time of output sound is start time of input sound
  1164. for (integer iint = 1; iint <= thy intervals.size; iint ++) {
  1165. TextInterval interval = thy intervals.at [iint];
  1166. if (! Melder_equ (interval -> text.get(), match)) {
  1167. numberOfSamples += Sampled_getWindowSamples (me, interval -> xmin, interval -> xmax, & ixmin, & ixmax);
  1168. // if two contiguous intervals have to be copied then the last sample of previous interval
  1169. // and first sample of current interval might sometimes be equal
  1170. if (ixmin == previous_ixmax) {
  1171. -- numberOfSamples;
  1172. }
  1173. previous_ixmax = ixmax;
  1174. } else { // matches label
  1175. if (iint == 1) { // Start time of output sound is end time of first interval
  1176. xmin = interval -> xmax;
  1177. }
  1178. }
  1179. }
  1180. // Now copy the parts. The output sound starts at xmin
  1181. autoSound him = Sound_create (my ny, xmin, xmin + numberOfSamples * my dx, numberOfSamples, my dx, xmin + 0.5 * my dx);
  1182. numberOfSamples = 0;
  1183. previous_ixmax = 0;
  1184. for (integer iint = 1; iint <= thy intervals.size; iint ++) {
  1185. TextInterval interval = thy intervals.at [iint];
  1186. if (! Melder_equ (interval -> text.get(), match)) {
  1187. Sampled_getWindowSamples (me, interval -> xmin, interval -> xmax, & ixmin, & ixmax);
  1188. if (ixmin == previous_ixmax) {
  1189. ixmin ++;
  1190. }
  1191. integer ipos = 0; // to prevent compiler warning -Wmaybe-uninitialized
  1192. previous_ixmax = ixmax;
  1193. for (integer ichan = 1; ichan <= my ny; ichan ++) {
  1194. ipos = numberOfSamples + 1;
  1195. for (integer i = ixmin; i <= ixmax; i ++, ipos ++) {
  1196. his z [ichan] [ipos] = my z [ichan] [i];
  1197. }
  1198. }
  1199. numberOfSamples = -- ipos;
  1200. }
  1201. }
  1202. Melder_assert (numberOfSamples == his nx);
  1203. return him;
  1204. } catch (MelderError) {
  1205. Melder_throw (me, U": intervals not trimmed.");
  1206. }
  1207. }
  1208. autoSound Sound_trimSilences (Sound me, double trimDuration, bool onlyAtStartAndEnd, double minPitch, double timeStep, double silenceThreshold, double minSilenceDuration, double minSoundingDuration, autoTextGrid *p_tg, conststring32 trimLabel) {
  1209. try {
  1210. Melder_require (my ny == 1, U"The sound should be a mono sound.");
  1211. conststring32 silentLabel = U"silent", soundingLabel = U"sounding";
  1212. conststring32 copyLabel = U"";
  1213. autoTextGrid tg = Sound_to_TextGrid_detectSilences (me, minPitch, timeStep, silenceThreshold, minSilenceDuration, minSoundingDuration, silentLabel, soundingLabel);
  1214. autoIntervalTier itg = Data_copy ((IntervalTier) tg -> tiers->at [1]);
  1215. IntervalTier tier = (IntervalTier) tg -> tiers->at [1];
  1216. for (integer iint = 1; iint <= tier -> intervals.size; iint ++) {
  1217. TextInterval ti = tier -> intervals.at [iint];
  1218. TextInterval ati = itg -> intervals.at [iint];
  1219. double duration = ti -> xmax - ti -> xmin;
  1220. if (duration > trimDuration && Melder_equ (ti -> text.get(), silentLabel)) { // silent
  1221. conststring32 label = trimLabel;
  1222. if (iint == 1) { // first is special
  1223. double trim_t = ti -> xmax - trimDuration;
  1224. IntervalTier_moveBoundary (itg.get(), iint, false, trim_t);
  1225. } else if (iint == tier -> intervals.size) { // last is special
  1226. double trim_t = ti -> xmin + trimDuration;
  1227. IntervalTier_moveBoundary (itg.get(), iint, true, trim_t);
  1228. } else {
  1229. if (onlyAtStartAndEnd) {
  1230. label = ati -> text.get();
  1231. } else {
  1232. double trim_t = ti -> xmin + 0.5 * trimDuration;
  1233. IntervalTier_moveBoundary (itg.get(), iint, true, trim_t);
  1234. trim_t = ti -> xmax - 0.5 * trimDuration;
  1235. IntervalTier_moveBoundary (itg.get(), iint, false, trim_t);
  1236. }
  1237. }
  1238. TextInterval_setText (ati, label);
  1239. } else { // sounding
  1240. TextInterval_setText (ati, copyLabel);
  1241. }
  1242. }
  1243. autoSound thee = Sound_IntervalTier_cutPartsMatchingLabel (me, itg.get(), trimLabel);
  1244. if (p_tg) {
  1245. TextGrid_addTier_copy (tg.get(), itg.get());
  1246. *p_tg = tg.move();
  1247. }
  1248. return thee;
  1249. } catch (MelderError) {
  1250. Melder_throw (me, U": silences not trimmed.");
  1251. }
  1252. }
  1253. autoSound Sound_trimSilencesAtStartAndEnd (Sound me, double trimDuration, double minPitch, double timeStep,
  1254. double silenceThreshold, double minSilenceDuration, double minSoundingDuration, double *startTimeOfSounding, double *endTimeOfSounding)
  1255. {
  1256. try {
  1257. autoTextGrid tg;
  1258. autoSound thee = Sound_trimSilences (me, trimDuration, true, minPitch, timeStep, silenceThreshold,
  1259. minSilenceDuration, minSoundingDuration, & tg, U"trimmed");
  1260. IntervalTier trim = (IntervalTier) tg -> tiers->at [2];
  1261. TextInterval ti1 = trim -> intervals.at [1];
  1262. if (startTimeOfSounding) {
  1263. *startTimeOfSounding = my xmin;
  1264. if (Melder_equ (ti1 -> text.get(), U"trimmed")) *startTimeOfSounding = ti1 -> xmax;
  1265. }
  1266. TextInterval ti2 = trim -> intervals.at [trim -> intervals.size];
  1267. if (endTimeOfSounding) {
  1268. *endTimeOfSounding = my xmax;
  1269. if (Melder_equ (ti2 -> text.get(), U"trimmed")) *endTimeOfSounding = ti2 -> xmin;
  1270. }
  1271. return thee;
  1272. } catch (MelderError) {
  1273. Melder_throw (me, U": silences not trimmed.");
  1274. }
  1275. }
  1276. /* Compatibility with old Sound(&pitch)_changeGender ***********************************/
  1277. static void PitchTier_modifyRange_old (PitchTier me, double tmin, double tmax, double factor, double fmid) {
  1278. for (integer i = 1; i <= my points.size; i ++) {
  1279. RealPoint point = my points.at [i];
  1280. double f = point -> value;
  1281. if (point -> number < tmin || point -> number > tmax) {
  1282. continue;
  1283. }
  1284. f = fmid + (f - fmid) * factor;
  1285. point -> value = f < 0.0 ? 0.0 : f;
  1286. }
  1287. }
  1288. static autoPitch Pitch_scaleTime_old (Pitch me, double scaleFactor) {
  1289. try {
  1290. double dx = my dx, x1 = my x1, xmax = my xmax;
  1291. if (scaleFactor != 1.0) {
  1292. dx = my dx * scaleFactor;
  1293. x1 = my xmin + 0.5 * dx;
  1294. xmax = my xmin + my nx * dx;
  1295. }
  1296. autoPitch thee = Pitch_create (my xmin, xmax, my nx, dx, x1, my ceiling, 2);
  1297. for (integer i = 1; i <= my nx; i ++) {
  1298. double f = my frame [i].candidate [1].frequency;
  1299. thy frame [i].candidate [1].strength = my frame [i].candidate [1].strength;
  1300. f /= scaleFactor;
  1301. if (f < my ceiling) {
  1302. thy frame [i]. candidate [1]. frequency = f;
  1303. }
  1304. }
  1305. return thee;
  1306. } catch (MelderError) {
  1307. Melder_throw (U"Pitch not scaled.");
  1308. }
  1309. }
  1310. autoSound Sound_Pitch_changeGender_old (Sound me, Pitch him, double formantRatio, double new_pitch, double pitchRangeFactor, double durationFactor) {
  1311. try {
  1312. double samplingFrequency_old = 1 / my dx;
  1313. Melder_require (my ny == 1, U"Change Gender works only on mono sounds.");
  1314. Melder_require (my xmin == his xmin && my xmax == his xmax, U"The Pitch and the Sound object should have the same domain.");
  1315. Melder_require (new_pitch >= 0, U"The new pitch median should not be negative.");
  1316. autoSound sound = Data_copy (me);
  1317. Vector_subtractMean (sound.get());
  1318. if (formantRatio != 1.0) {
  1319. // Shift all frequencies (inclusive pitch!)
  1320. Sound_overrideSamplingFrequency (sound.get(), samplingFrequency_old * formantRatio);
  1321. }
  1322. autoPitch pitch = Pitch_scaleTime_old (him, 1 / formantRatio);
  1323. autoPointProcess pulses = Sound_Pitch_to_PointProcess_cc (sound.get(), pitch.get());
  1324. autoPitchTier pitchTier = Pitch_to_PitchTier (pitch.get());
  1325. double median = Pitch_getQuantile (pitch.get(), 0, 0, 0.5, kPitch_unit::HERTZ);
  1326. if (isdefined (median) && median != 0.0) {
  1327. // Incorporate pitch shift from overriding the sampling frequency
  1328. if (new_pitch == 0.0) {
  1329. new_pitch = median / formantRatio;
  1330. }
  1331. double factor = new_pitch / median;
  1332. PitchTier_multiplyFrequencies (pitchTier.get(), sound -> xmin, sound -> xmax, factor);
  1333. PitchTier_modifyRange_old (pitchTier.get(), sound -> xmin, sound -> xmax, pitchRangeFactor, new_pitch);
  1334. } else {
  1335. Melder_warning (U"There were no voiced segments found.");
  1336. }
  1337. autoDurationTier duration = DurationTier_create (my xmin, my xmax);
  1338. RealTier_addPoint (duration.get(), (my xmin + my xmax) / 2, formantRatio * durationFactor);
  1339. autoSound thee = Sound_Point_Pitch_Duration_to_Sound (sound.get(), pulses.get(), pitchTier.get(),
  1340. duration.get(), 1.25 / Pitch_getMinimum (pitch.get(), 0.0, 0.0, kPitch_unit::HERTZ, false));
  1341. // Resample to the original sampling frequency
  1342. if (formantRatio != 1.0) {
  1343. thee = Sound_resample (thee.get(), samplingFrequency_old, 10);
  1344. }
  1345. return thee;
  1346. } catch (MelderError) {
  1347. Melder_throw (U"Sound not created from Pitch & Sound.");
  1348. }
  1349. }
  1350. autoSound Sound_changeGender_old (Sound me, double fmin, double fmax, double formantRatio, double new_pitch, double pitchRangeFactor, double durationFactor) {
  1351. try {
  1352. autoPitch pitch = Sound_to_Pitch (me, 0.8 / fmin, fmin, fmax);
  1353. autoSound thee = Sound_Pitch_changeGender_old (me, pitch.get(), formantRatio, new_pitch, pitchRangeFactor, durationFactor);
  1354. return thee;
  1355. } catch (MelderError) {
  1356. Melder_throw (U"Sound not created for gender change.");
  1357. }
  1358. }
  1359. /* End of compatibility with Sound_changeGender and Sound_Pitch_changeGender ***********************************/
  1360. /* Draw a sound vertically, from bottom to top */
  1361. void Sound_draw_btlr (Sound me, Graphics g, double tmin, double tmax, double amin, double amax, int direction, bool garnish) {
  1362. double xmin, xmax, ymin, ymax;
  1363. if (tmin == tmax) {
  1364. tmin = my xmin; tmax = my xmax;
  1365. }
  1366. integer itmin, itmax;
  1367. Matrix_getWindowSamplesX (me, tmin, tmax, &itmin, &itmax);
  1368. if (amin == amax) {
  1369. Matrix_getWindowExtrema (me, itmin, itmax, 1, my ny, &amin, &amax);
  1370. if (amin == amax) {
  1371. amin -= 1.0; amax += 1.0;
  1372. }
  1373. }
  1374. /* In bottom-to-top-drawing the maximum amplitude is on the left, minimum on the right */
  1375. if (direction == FROM_BOTTOM_TO_TOP) {
  1376. xmin = amax; xmax = amin; ymin = tmin; ymax = tmax;
  1377. } else if (direction == FROM_TOP_TO_BOTTOM) {
  1378. xmin = amin; xmax = amax; ymin = tmax; ymax = tmin;
  1379. } else if (direction == FROM_RIGHT_TO_LEFT) {
  1380. xmin = tmax; xmax = tmin; ymin = amin; ymax = amax;
  1381. } else { //if (direction == FROM_LEFT_TO_RIGHT)
  1382. xmin = tmin; xmax = tmax; ymin = amin; ymax = amax;
  1383. }
  1384. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  1385. double a1 = my z [1] [itmin];
  1386. double t1 = Sampled_indexToX (me, itmin);
  1387. for (integer it = itmin + 1; it <= itmax; it ++) {
  1388. double t2 = Sampled_indexToX (me, it);
  1389. double a2 = my z [1] [it];
  1390. if (direction == FROM_BOTTOM_TO_TOP || direction == FROM_TOP_TO_BOTTOM) {
  1391. Graphics_line (g, a1, t1, a2, t2);
  1392. } else {
  1393. Graphics_line (g, t1, a1, t2, a2);
  1394. }
  1395. a1 = a2; t1 = t2;
  1396. }
  1397. if (garnish) {
  1398. if (direction == FROM_BOTTOM_TO_TOP) {
  1399. if (amin * amax < 0.0) {
  1400. Graphics_markBottom (g, 0.0, false, true, true, nullptr);
  1401. }
  1402. } else if (direction == FROM_TOP_TO_BOTTOM) {
  1403. if (amin * amax < 0.0) {
  1404. Graphics_markTop (g, 0.0, false, true, true, nullptr);
  1405. }
  1406. } else if (direction == FROM_RIGHT_TO_LEFT) {
  1407. if (amin * amax < 0.0) {
  1408. Graphics_markRight (g, 0.0, false, true, true, nullptr);
  1409. }
  1410. } else { //if (direction == FROM_LEFT_TO_RIGHT)
  1411. if (amin * amax < 0.0) {
  1412. Graphics_markLeft (g, 0.0, false, true, true, nullptr);
  1413. }
  1414. }
  1415. Graphics_rectangle (g, xmin, xmax, ymin, ymax);
  1416. }
  1417. }
  1418. void Sound_fade (Sound me, int channel, double t, double fadeTime, int inout, bool fadeGlobal) {
  1419. integer numberOfSamples = Melder_ifloor (fabs (fadeTime) / my dx);
  1420. double t1 = t, t2 = t1 + fadeTime;
  1421. conststring32 fade_inout = inout > 0 ? U"out" : U"in";
  1422. Melder_require (channel >= 0 && channel <= my ny, U"Invalid channel number: ", channel, U".");
  1423. if (t > my xmax) {
  1424. t = my xmax;
  1425. if (inout <= 0) { // fade in
  1426. Melder_warning (U"The start time of the fade-in is after the end time of the sound. The fade-in will not happen.");
  1427. return;
  1428. }
  1429. } else if (t < my xmin) {
  1430. t = my xmin;
  1431. if (inout > 0) { // fade out
  1432. Melder_warning (U"The start time of the fade-out is before the start time of the sound. The fade-out will not happen.");
  1433. return;
  1434. }
  1435. }
  1436. if (fadeTime < 0.0) {
  1437. t1 = t + fadeTime;
  1438. t2 = t;
  1439. } else if (fadeTime > 0.0) {
  1440. t1 = t;
  1441. t2 = t + fadeTime;
  1442. } else {
  1443. Melder_warning (U"You have given a \"Fade time\" of zero seconds. The fade-", fade_inout, U" will not happen.");
  1444. return;
  1445. }
  1446. integer i0 = 0, iystart, iyend;
  1447. if (channel == 0) { // all
  1448. iystart = 1; iyend = my ny;
  1449. } else {
  1450. iystart = iyend = channel;
  1451. }
  1452. integer istart = Sampled_xToNearestIndex (me, t1);
  1453. if (istart < 1) {
  1454. istart = 1;
  1455. }
  1456. if (istart >= my nx) {
  1457. Melder_warning (U"The part to fade ", fade_inout, U" lies after the end time of the sound. The fade-", fade_inout, U" will not happen.");
  1458. return;
  1459. }
  1460. integer iend = Sampled_xToNearestIndex (me, t2);
  1461. if (iend <= 1) {
  1462. Melder_warning (U"The part to fade ", fade_inout, U" lies before the start time of the sound. Fade-", fade_inout, U" will be incomplete.");
  1463. return;
  1464. }
  1465. if (iend > my nx) {
  1466. iend = my nx;
  1467. }
  1468. if (iend - istart + 1 >= numberOfSamples) {
  1469. numberOfSamples = iend - istart + 1;
  1470. } else {
  1471. // If the start of the fade is before xmin, arrange starting phase.
  1472. // The end of the fade after xmax presents no problems (i0 = 0).
  1473. if (fadeTime < 0) {
  1474. i0 = numberOfSamples - (iend - istart + 1);
  1475. }
  1476. Melder_warning (U"The fade time is larger than the part of the sound to fade ", fade_inout, U". Fade-", fade_inout, U" will be incomplete.");
  1477. }
  1478. for (integer ichannel = iystart; ichannel <= iyend; ichannel ++) {
  1479. for (integer i = istart; i <= iend; i ++) {
  1480. double cosp = cos (NUMpi * (i0 + i - istart) / (numberOfSamples - 1));
  1481. if (inout <= 0) {
  1482. cosp = -cosp; // fade-in
  1483. }
  1484. my z [ichannel] [i] *= 0.5 * (1.0 + cosp);
  1485. }
  1486. if (fadeGlobal) {
  1487. if (inout <= 0) {
  1488. for (integer i = 1; i < istart; i ++) {
  1489. my z [ichannel] [i] = 0.0;
  1490. }
  1491. } else {
  1492. for (integer i = iend; i < my nx; i ++) {
  1493. my z [ichannel] [i] = 0.0;
  1494. }
  1495. }
  1496. }
  1497. }
  1498. }
  1499. /* 1; rect 2:hamming 3: bartlet 4: welch 5: hanning 6:gaussian */
  1500. autoSound Sound_createFromWindowFunction (double windowDuration, double samplingFrequency, int windowType) {
  1501. try {
  1502. autoSound me = Sound_createSimple (1, windowDuration, samplingFrequency);
  1503. for (integer i = 1; i <= my nx; i ++) {
  1504. double phase = (my x1 + (i - 1) * my dx) / windowDuration;
  1505. double value = 1.0;
  1506. switch (windowType) {
  1507. case 1:
  1508. value = 1.0;
  1509. break;
  1510. case 2: /* Hamming */
  1511. value = 0.54 - 0.46 * cos (2.0 * NUMpi * phase);
  1512. break;
  1513. case 3: /* Bartlett */
  1514. value = 1.0 - fabs ( (2.0 * phase - 1.0));
  1515. break;
  1516. case 4: /* Welch */
  1517. value = 1.0 - (2.0 * phase - 1.0) * (2.0 * phase - 1.0);
  1518. break;
  1519. case 5: /* Hanning */
  1520. value = 0.5 * (1.0 - cos (2.0 * NUMpi * phase));
  1521. break;
  1522. case 6: { /* Gaussian */
  1523. double edge = exp (-12.0);
  1524. phase -= 0.5; /* -0.5 .. +0.5 */
  1525. value = (exp (-48.0 * phase * phase) - edge) / (1.0 - edge);
  1526. break;
  1527. }
  1528. break;
  1529. }
  1530. my z [1] [i] = value;
  1531. }
  1532. return me;
  1533. } catch (MelderError) {
  1534. Melder_throw (U"Sound not created from window function.");
  1535. }
  1536. }
  1537. /* y [n] = sum(i=-n, i=n, x [n+mi])/(2*n+1) */
  1538. autoSound Sound_localAverage (Sound me, double averagingInterval, int windowType) {
  1539. try {
  1540. double windowDuration = windowType == 6 ? 2 * averagingInterval : averagingInterval;
  1541. autoSound thee = Data_copy (me);
  1542. autoSound window = Sound_createFromWindowFunction (windowDuration, 1 / my dx, windowType);
  1543. integer nswindow2 = window -> nx / 2;
  1544. integer nswindow2p = (window -> nx - 1) / 2; // nx is odd: one sample less in the forward direction
  1545. if (nswindow2 < 1) {
  1546. return thee;
  1547. }
  1548. double *w = window -> z [1];
  1549. for (integer k = 1; k <= thy ny; k ++) {
  1550. for (integer i = 1; i <= my nx; i ++) {
  1551. double sum = 0, wsum = 0;
  1552. integer m = (nswindow2 + 1 - i + 1) < 1 ? 1 : (nswindow2 + 1 - i + 1);
  1553. integer jfrom = (i - nswindow2) < 1 ? 1 : (i - nswindow2);
  1554. integer jto = (i + nswindow2p) > my nx ? my nx : (i + nswindow2p);
  1555. for (integer j = jfrom; j <= jto; j ++, m ++) {
  1556. sum += my z [k] [j] * w [m];
  1557. wsum += w [m];
  1558. }
  1559. thy z [k] [i] = sum / wsum;
  1560. }
  1561. }
  1562. return thee;
  1563. } catch (MelderError) {
  1564. Melder_throw (me, U": no Sound (local average) created.");
  1565. }
  1566. }
  1567. static void _Sound_garnish (Sound me, Graphics g, double tmin, double tmax, double minimum, double maximum) {
  1568. Graphics_drawInnerBox (g);
  1569. Graphics_textBottom (g, true, U"Time (s)");
  1570. Graphics_marksBottom (g, 2, true, true, false);
  1571. Graphics_setWindow (g, tmin, tmax, minimum - (my ny - 1) * (maximum - minimum), maximum);
  1572. Graphics_markLeft (g, minimum, true, true, false, nullptr);
  1573. Graphics_markLeft (g, maximum, true, true, false, nullptr);
  1574. if (minimum != 0.0 && maximum != 0.0 && (minimum > 0.0) != (maximum > 0.0)) {
  1575. Graphics_markLeft (g, 0.0, true, true, true, nullptr);
  1576. }
  1577. if (my ny == 2) {
  1578. Graphics_setWindow (g, tmin, tmax, minimum, maximum + (my ny - 1) * (maximum - minimum));
  1579. Graphics_markRight (g, minimum, true, true, false, nullptr);
  1580. Graphics_markRight (g, maximum, true, true, false, nullptr);
  1581. if (minimum != 0.0 && maximum != 0.0 && (minimum > 0.0) != (maximum > 0.0)) {
  1582. Graphics_markRight (g, 0.0, true, true, true, nullptr);
  1583. }
  1584. }
  1585. }
  1586. static void _Sound_getWindowExtrema (Sound me, double *tmin, double *tmax, double *minimum, double *maximum, integer *ixmin, integer *ixmax) {
  1587. if (*tmin == *tmax) {
  1588. *tmin = my xmin;
  1589. *tmax = my xmax;
  1590. }
  1591. Matrix_getWindowSamplesX (me, *tmin, *tmax, ixmin, ixmax);
  1592. if (*minimum == *maximum) {
  1593. Matrix_getWindowExtrema (me, *ixmin, *ixmax, 1, my ny, minimum, maximum);
  1594. if (*minimum == *maximum) {
  1595. *minimum -= 1.0;
  1596. *maximum += 1.0;
  1597. }
  1598. }
  1599. }
  1600. /*
  1601. Given sample numbers ileft and ileft+1, where the formula evaluates to the booleans left and right, respectively.
  1602. We want to find the x value in this interval where the formula switches from true to false.
  1603. The x-value of the best point is approximated by a number of bisections.
  1604. It is essential that the intermediate interpolated y-values are always between the values at samples ileft and ileft+1.
  1605. We cannot use a sinc-interpolation because at strong amplitude changes high-frequency oscillations may occur.
  1606. */
  1607. static void Sound_findIntermediatePoint_bs (Sound me, integer ichannel, integer ileft, bool left, bool right,
  1608. conststring32 formula, Interpreter interpreter, integer numberOfBisections, double *x, double *y)
  1609. {
  1610. Melder_require (left != right, U"Invalid situation.");
  1611. if (left) {
  1612. *x = Matrix_columnToX (me, ileft);
  1613. *y = my z [ichannel] [ileft];
  1614. } else {
  1615. *x = Matrix_columnToX (me, ileft + 1);
  1616. *y = my z [ichannel] [ileft + 1];
  1617. }
  1618. if (numberOfBisections < 1)
  1619. return;
  1620. /*
  1621. For the bisection we create a Sound with only 3 samples in it which is sufficient for doing linear interpolation.
  1622. The domain needs to be the same as the sound otherwise the formula might give wrong answers because
  1623. it might contains references to other matrix objects!
  1624. We also need all the channels because the formula might involve relations between the
  1625. sample values in these channels too!
  1626. */
  1627. double xleft = Matrix_columnToX (me, ileft);
  1628. autoSound thee = Sound_create (my ny, my xmin, my xmax, 3, 0.5 * my dx, xleft);
  1629. for (integer channel = 1; channel <= my ny; channel ++) {
  1630. thy z [channel] [1] = my z [channel] [ileft];
  1631. thy z [channel] [3] = my z [channel] [ileft + 1];
  1632. }
  1633. integer istep = 1;
  1634. double xright = xleft + my dx, xmid; // !!
  1635. do {
  1636. xmid = 0.5 * (xleft + xright); // the bisection
  1637. for (integer channel = 1; channel <= my ny; channel ++)
  1638. thy z [channel] [2] = Vector_getValueAtX (me, xmid, channel, Vector_VALUE_INTERPOLATION_LINEAR);
  1639. Formula_compile (interpreter, thee.get(), formula, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  1640. Formula_Result result;
  1641. Formula_run (ichannel, 2, & result);
  1642. bool mid = ( result. numericResult != 0.0 );
  1643. thy dx *= 0.5;
  1644. if (left == mid) {
  1645. xleft = xmid;
  1646. for (integer channel = 1; channel <= my ny; channel ++)
  1647. thy z [channel] [1] = thy z [channel] [2];
  1648. thy x1 = xleft;
  1649. } else {
  1650. xright = xmid;
  1651. for (integer channel = 1; channel <= my ny; channel ++)
  1652. thy z [channel] [3] = thy z [channel] [2];
  1653. }
  1654. istep ++;
  1655. } while (istep < numberOfBisections);
  1656. *x = xmid;
  1657. *y = thy z [ichannel] [2];
  1658. }
  1659. void Sound_drawWhere (Sound me, Graphics g, double tmin, double tmax, double minimum, double maximum,
  1660. bool garnish, conststring32 method, integer numberOfBisections, conststring32 formula, Interpreter interpreter) {
  1661. Formula_compile (interpreter, me, formula, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  1662. Formula_Result result;
  1663. integer ixmin, ixmax;
  1664. _Sound_getWindowExtrema (me, & tmin, & tmax, & minimum, & maximum, & ixmin, & ixmax);
  1665. // Set coordinates for drawing.
  1666. Graphics_setInner (g);
  1667. for (integer channel = 1; channel <= my ny; channel ++) {
  1668. Graphics_setWindow (g, tmin, tmax, minimum - (my ny - channel) * (maximum - minimum), maximum + (channel - 1) * (maximum - minimum));
  1669. if (str32str (method, U"bars") || str32str (method, U"Bars")) {
  1670. for (integer ix = ixmin; ix <= ixmax; ix ++) {
  1671. Formula_run (channel, ix, & result);
  1672. if (result. numericResult != 0.0) {
  1673. double x = Sampled_indexToX (me, ix);
  1674. double y = my z [channel] [ix];
  1675. double left = x - 0.5 * my dx, right = x + 0.5 * my dx;
  1676. if (y > maximum) {
  1677. y = maximum;
  1678. }
  1679. if (left < tmin) {
  1680. left = tmin;
  1681. }
  1682. if (right > tmax) {
  1683. right = tmax;
  1684. }
  1685. Graphics_line (g, left, y, right, y);
  1686. Graphics_line (g, left, y, left, minimum);
  1687. Graphics_line (g, right, y, right, minimum);
  1688. }
  1689. }
  1690. } else if (str32str (method, U"poles") || str32str (method, U"Poles")) {
  1691. for (integer ix = ixmin; ix <= ixmax; ix ++) {
  1692. Formula_run (channel, ix, & result);
  1693. if (result. numericResult != 0.0) {
  1694. double x = Sampled_indexToX (me, ix);
  1695. double y = my z [channel] [ix];
  1696. if (y > maximum) {
  1697. y = maximum;
  1698. }
  1699. if (y < minimum) {
  1700. y = minimum;
  1701. }
  1702. Graphics_line (g, x, 0, x, y);
  1703. }
  1704. }
  1705. } else if (str32str (method, U"speckles") || str32str (method, U"Speckles")) {
  1706. for (integer ix = ixmin; ix <= ixmax; ix ++) {
  1707. Formula_run (channel, ix, & result);
  1708. if (result. numericResult != 0.0) {
  1709. double x = Sampled_indexToX (me, ix);
  1710. Graphics_speckle (g, x, my z [channel] [ix]);
  1711. }
  1712. }
  1713. } else {
  1714. // The default: draw as a curve.
  1715. Formula_run (channel, 1, & result);
  1716. bool previous = (result. numericResult != 0.0); // numericResult == 0.0 means false!
  1717. integer istart = ixmin; // first sample of segment to be drawn
  1718. double xb, yb, xe, ye;
  1719. for (integer ix = ixmin + 1; ix <= ixmax; ix ++) {
  1720. Formula_run (channel, ix, & result);
  1721. bool current = (result. numericResult != 0.0); // numericResult == 0.0 means false!
  1722. if (previous && not current) {
  1723. /*
  1724. T to F change: we are leaving a segment to be drawn
  1725. 1. Draw the curve between the sample numbers from istart to ix-1 (previous).
  1726. 2. Find the (x,y) in the interval between sample numbers ix-1 and ix (current) where the change from
  1727. T to F occurs and draw the line between the previous point and (x,y).
  1728. 3. Compile the formula again because it has been changed during the interpolation
  1729. */
  1730. xb = Matrix_columnToX (me, ix - 1);
  1731. yb = my z [channel] [ix - 1];
  1732. if (ix - istart > 1) {
  1733. double x1 = Matrix_columnToX (me, istart);
  1734. Graphics_function (g, my z [channel], istart, ix - 1, x1, xb);
  1735. }
  1736. Sound_findIntermediatePoint_bs (me, channel, ix - 1, previous, current, formula, interpreter, numberOfBisections, & xe, & ye);
  1737. Graphics_line (g, xb, yb, xe, ye);
  1738. Formula_compile (interpreter, me, formula, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  1739. } else if (not previous && current ) {
  1740. /*
  1741. F to T change: we are entering a segment to be drawn.
  1742. 1. Find the (x,y) where the F changes to T and then draw the line from that (x,y) to the current point.
  1743. 2. Compile the formula again
  1744. */
  1745. istart = ix;
  1746. Sound_findIntermediatePoint_bs (me, channel, ix - 1, previous, current, formula, interpreter, numberOfBisections, & xb, & yb);
  1747. xe = Sampled_indexToX (me, ix);
  1748. ye = my z [channel] [ix];
  1749. Graphics_line (g, xb, yb, xe, ye);
  1750. Formula_compile (interpreter, me, formula, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  1751. }
  1752. previous = current;
  1753. }
  1754. if (previous && ixmax - istart > 0) {
  1755. xb = Matrix_columnToX (me, istart);
  1756. xe = Matrix_columnToX (me, ixmax);
  1757. Graphics_function (g, my z [channel], istart, ixmax, xb, xe);
  1758. }
  1759. }
  1760. }
  1761. Graphics_setWindow (g, tmin, tmax, minimum, maximum);
  1762. if (garnish && my ny == 2) {
  1763. Graphics_line (g, tmin, 0.5 * (minimum + maximum), tmax, 0.5 * (minimum + maximum));
  1764. }
  1765. Graphics_unsetInner (g);
  1766. if (garnish) {
  1767. _Sound_garnish (me, g, tmin, tmax, minimum, maximum);
  1768. }
  1769. }
  1770. void Sound_paintWhere (Sound me, Graphics g, Graphics_Colour colour, double tmin, double tmax,
  1771. double minimum, double maximum, double level, bool garnish,
  1772. integer numberOfBisections, conststring32 formula, Interpreter interpreter)
  1773. {
  1774. try {
  1775. Formula_compile (interpreter, me, formula, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  1776. Formula_Result result;
  1777. integer ixmin, ixmax;
  1778. _Sound_getWindowExtrema (me, & tmin, & tmax, & minimum, & maximum, & ixmin, & ixmax);
  1779. Graphics_setColour (g, colour);
  1780. Graphics_setInner (g);
  1781. for (integer channel = 1; channel <= my ny; channel ++) {
  1782. Graphics_setWindow (g, tmin, tmax, minimum - (my ny - channel) * (maximum - minimum), maximum + (channel - 1) * (maximum - minimum));
  1783. bool current, previous = true, fill = false; // fill only when leaving area
  1784. double tmini = tmin, tmaxi = tmax, xe, ye;
  1785. integer ix = ixmin;
  1786. do {
  1787. Formula_run (channel, ix, & result);
  1788. current = ( result. numericResult != 0.0 );
  1789. if (ix == ixmin) {
  1790. previous = current;
  1791. }
  1792. if (previous != current) {
  1793. Sound_findIntermediatePoint_bs (me, channel, ix - 1, previous, current, formula, interpreter, numberOfBisections, & xe, & ye);
  1794. if (current) { // entering painting area
  1795. tmini = xe;
  1796. } else { //leaving painting area
  1797. tmaxi = xe;
  1798. fill = true;
  1799. }
  1800. Formula_compile (interpreter, me, formula, kFormula_EXPRESSION_TYPE_NUMERIC, true);
  1801. }
  1802. if (ix == ixmax && current) {
  1803. tmaxi = tmax;
  1804. fill = true;
  1805. }
  1806. if (fill) {
  1807. autoPolygon him = Sound_to_Polygon (me, channel, tmini, tmaxi, minimum, maximum, level);
  1808. Graphics_fillArea (g, his numberOfPoints, &his x [1], &his y [1]);
  1809. fill = false;
  1810. }
  1811. previous = current;
  1812. } while ( ++ix <= ixmax);
  1813. }
  1814. Graphics_setWindow (g, tmin, tmax, minimum, maximum);
  1815. if (garnish && my ny == 2) {
  1816. Graphics_line (g, tmin, 0.5 * (minimum + maximum), tmax, 0.5 * (minimum + maximum));
  1817. }
  1818. Graphics_unsetInner (g);
  1819. if (garnish) {
  1820. _Sound_garnish (me, g, tmin, tmax, minimum, maximum);
  1821. }
  1822. } catch (MelderError) {
  1823. Melder_clearError ();
  1824. }
  1825. }
  1826. void Sounds_paintEnclosed (Sound me, Sound thee, Graphics g, Graphics_Colour colour, double tmin, double tmax, double minimum, double maximum, bool garnish) {
  1827. try {
  1828. integer ixmin, ixmax, numberOfChannels = my ny > thy ny ? my ny : thy ny;
  1829. double min1 = minimum, max1 = maximum, tmin1 = tmin, tmax1 = tmax;
  1830. double min2 = min1, max2 = max1, tmin2 = tmin1, tmax2 = tmax1;
  1831. double xmin = my xmin > thy xmin ? my xmin : thy xmin;
  1832. double xmax = my xmax < thy xmax ? my xmax : thy xmax;
  1833. if (xmax <= xmin) {
  1834. return;
  1835. }
  1836. if (tmin >= tmax) {
  1837. tmin = xmin;
  1838. tmax = xmax;
  1839. }
  1840. _Sound_getWindowExtrema (thee, & tmin1, & tmax1, & min1, & max1, & ixmin, & ixmax);
  1841. _Sound_getWindowExtrema (me, & tmin2, & tmax2, & min2, & max2, & ixmin, & ixmax);
  1842. minimum = min1 < min2 ? min1 : min2;
  1843. maximum = max1 > max2 ? max1 : max2;
  1844. Graphics_setColour (g, colour);
  1845. Graphics_setInner (g);
  1846. for (integer channel = 1; channel <= numberOfChannels; channel ++) {
  1847. autoPolygon him = Sounds_to_Polygon_enclosed (me, thee, channel, tmin, tmax, minimum, maximum);
  1848. Graphics_setWindow (g, tmin, tmax, minimum - (numberOfChannels - channel) * (maximum - minimum), maximum + (channel - 1) * (maximum - minimum));
  1849. Graphics_fillArea (g, his numberOfPoints, &his x [1], &his y [1]);
  1850. }
  1851. Graphics_setWindow (g, tmin, tmax, minimum, maximum);
  1852. if (garnish && (my ny == 2 || thy ny == 2)) {
  1853. Graphics_line (g, tmin, 0.5 * (minimum + maximum), tmax, 0.5 * (minimum + maximum));
  1854. }
  1855. Graphics_unsetInner (g);
  1856. if (garnish) {
  1857. _Sound_garnish (my ny == 2 ? me : thee, g, tmin, tmax, minimum, maximum);
  1858. }
  1859. } catch (MelderError) {
  1860. Melder_clearError ();
  1861. }
  1862. }
  1863. autoSound Sound_copyChannelRanges (Sound me, conststring32 ranges) {
  1864. try {
  1865. autoINTVEC channels = NUMstring_getElementsOfRanges (ranges, my ny, U"channel", true);
  1866. autoSound thee = Sound_create (channels.size, my xmin, my xmax, my nx, my dx, my x1);
  1867. for (integer i = 1; i <= channels.size; i ++) {
  1868. VECcopy_preallocated (thy z.row (i), my z.row (channels [i]));
  1869. }
  1870. return thee;
  1871. } catch (MelderError) {
  1872. Melder_throw (me, U": could not extract channels.");
  1873. }
  1874. }
  1875. /* After a script by Ton Wempe */
  1876. static autoSound Sound_removeNoiseBySpectralSubtraction_mono (Sound me, Sound noise, double windowLength) {
  1877. try {
  1878. Melder_require (my dx == noise -> dx, U"The sound and the noise should have the same sampling frequency.");
  1879. Melder_require (noise -> ny == 1 && noise -> ny == 1, U"The number of channels in the noise and the sound should equal 1.");
  1880. double samplingFrequency = 1.0 / my dx;
  1881. autoSound denoised = Sound_create (1, my xmin, my xmax, my nx, my dx, my x1);
  1882. autoSound analysisWindow = Sound_createSimple (1, windowLength, samplingFrequency);
  1883. integer windowSamples = analysisWindow -> nx;
  1884. autoSound noise_copy = Data_copy (noise);
  1885. Sound_multiplyByWindow (noise_copy.get(), kSound_windowShape::HANNING);
  1886. double bandwidth = samplingFrequency / windowSamples;
  1887. autoLtas noiseLtas = Sound_to_Ltas (noise_copy.get(), bandwidth);
  1888. autoNUMvector<double> noiseAmplitudes (1, noiseLtas -> nx);
  1889. for (integer i = 1; i <= noiseLtas -> nx; i ++) {
  1890. noiseAmplitudes [i] = pow (10.0, (noiseLtas -> z [1] [i] - 94) / 20);
  1891. }
  1892. autoMelderProgress progress (U"Remove noise");
  1893. integer stepSizeSamples = windowSamples / 4;
  1894. integer numberOfSteps = my nx / stepSizeSamples;
  1895. for (integer istep = 1; istep <= numberOfSteps; istep ++) {
  1896. integer istart = (istep - 1) * stepSizeSamples + 1;
  1897. if (istart >= my nx) {
  1898. break; // finished
  1899. }
  1900. integer nsamples = istart + windowSamples - 1 > my nx ? my nx - istart + 1 : windowSamples;
  1901. for (integer j = 1; j <= nsamples; j ++) {
  1902. analysisWindow -> z [1] [j] = my z [1] [istart - 1 + j];
  1903. }
  1904. for (integer j = nsamples + 1; j <= windowSamples; j ++) {
  1905. analysisWindow -> z [1] [j] = 0;
  1906. }
  1907. autoSpectrum analysisSpectrum = Sound_to_Spectrum (analysisWindow.get(), false);
  1908. // Suppress noise in the analysisSpectrum by subtracting the noise spectrum
  1909. double *x = analysisSpectrum -> z [1], *y = analysisSpectrum -> z [2];
  1910. for (integer i = 1; i <= analysisSpectrum -> nx; i ++) {
  1911. double amp = sqrt (x [i] * x [i] + y [i] * y [i]);
  1912. double factor = 1 - 1.5 * noiseAmplitudes [i] / amp;
  1913. factor = factor < 1e-6 ? 1e-6 : factor;
  1914. x [i] *= factor; y [i] *= factor;
  1915. }
  1916. autoSound suppressed = Spectrum_to_Sound (analysisSpectrum.get());
  1917. Sound_multiplyByWindow (suppressed.get(), kSound_windowShape::HANNING);
  1918. for (integer j = 1; j <= nsamples; j ++) {
  1919. denoised -> z [1] [istart - 1 + j] += 0.5 * suppressed -> z [1] [j]; // 0.5 because of 2-fold oversampling
  1920. }
  1921. if ((istep % 10) == 1) {
  1922. Melder_progress ( (double) istep / numberOfSteps, U"Remove noise: frame ", istep, U" out of ", numberOfSteps, U".");
  1923. }
  1924. }
  1925. return denoised;
  1926. } catch (MelderError) {
  1927. Melder_throw (me, U": noise not subtracted.");
  1928. }
  1929. }
  1930. static void Sound_findNoise (Sound me, double minimumNoiseDuration, double *noiseStart, double *noiseEnd) {
  1931. try {
  1932. *noiseStart = undefined;
  1933. *noiseEnd = undefined;
  1934. autoIntensity intensity = Sound_to_Intensity (me, 20.0, 0.005, true);
  1935. double tmin = Vector_getXOfMinimum (intensity.get(), intensity -> xmin, intensity -> xmax, 1) - minimumNoiseDuration / 2;
  1936. double tmax = tmin + minimumNoiseDuration;
  1937. if (tmin < my xmin) {
  1938. tmin = my xmin; tmax = tmin + minimumNoiseDuration;
  1939. }
  1940. if (tmax > my xmax) {
  1941. tmax = my xmax; tmin = tmax - minimumNoiseDuration;
  1942. }
  1943. Melder_require (tmin >= my xmin, U"Sound too short, or window length too long.");
  1944. *noiseStart = tmin;
  1945. *noiseEnd = tmax;
  1946. } catch (MelderError) {
  1947. Melder_throw (me, U": noise not found.");
  1948. }
  1949. }
  1950. autoSound Sound_removeNoise (Sound me, double noiseStart, double noiseEnd, double windowLength, double minBandFilterFrequency, double maxBandFilterFrequency, double smoothing, int method) {
  1951. try {
  1952. autoSound filtered = Sound_filter_passHannBand (me, minBandFilterFrequency, maxBandFilterFrequency, smoothing);
  1953. autoSound denoised = Sound_create (my ny, my xmin, my xmax, my nx, my dx, my x1);
  1954. bool findNoise = noiseEnd <= noiseStart;
  1955. double minimumNoiseDuration = 2 * windowLength;
  1956. for (integer ichannel = 1; ichannel <= my ny; ichannel ++) {
  1957. autoSound denoisedi, channeli = Sound_extractChannel (filtered.get(), ichannel);
  1958. if (findNoise) {
  1959. Sound_findNoise (channeli.get(), minimumNoiseDuration, & noiseStart, & noiseEnd);
  1960. }
  1961. autoSound noise = Sound_extractPart (channeli.get(), noiseStart, noiseEnd, kSound_windowShape::RECTANGULAR, 1.0, false);
  1962. if (method == 1) { // spectral subtraction
  1963. denoisedi = Sound_removeNoiseBySpectralSubtraction_mono (filtered.get(), noise.get(), windowLength);
  1964. }
  1965. NUMvector_copyElements<double> (denoisedi -> z [1], denoised -> z [ichannel], 1, my nx);
  1966. }
  1967. return denoised;
  1968. } catch (MelderError) {
  1969. Melder_throw (me, U": not denoised.");
  1970. }
  1971. }
  1972. void Sound_playAsFrequencyShifted (Sound me, double shiftBy, double newSamplingFrequency, integer precision) {
  1973. try {
  1974. autoSpectrum spectrum = Sound_to_Spectrum (me, true);
  1975. autoSpectrum shifted = Spectrum_shiftFrequencies (spectrum.get(), shiftBy, newSamplingFrequency / 2, precision);
  1976. autoSound thee = Spectrum_to_Sound (shifted.get());
  1977. Sound_playPart (thee.get(), my xmin, my xmax, nullptr, nullptr);
  1978. } catch (MelderError) {
  1979. Melder_throw (me, U" not played with frequencies shifted.");
  1980. }
  1981. }
  1982. /* End of file Sound_extensions.cpp */