melder_audiofiles.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. /* melder_audiofiles.cpp
  2. *
  3. * Copyright (C) 1992-2008,2010-2018 Paul Boersma & David Weenink, 2007 Erez Volk (for FLAC)
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "melder.h"
  19. #include "../external/flac/flac_FLAC_metadata.h"
  20. #include "../external/flac/flac_FLAC_stream_decoder.h"
  21. #include "../external/flac/flac_FLAC_stream_encoder.h"
  22. #include "../external/mp3/mp3.h"
  23. /***** WRITING *****/
  24. #ifndef WAVE_FORMAT_PCM
  25. #define WAVE_FORMAT_PCM 0x0001
  26. #endif
  27. #define WAVE_FORMAT_IEEE_FLOAT 0x0003
  28. #define WAVE_FORMAT_ALAW 0x0006
  29. #define WAVE_FORMAT_MULAW 0x0007
  30. #define WAVE_FORMAT_DVI_ADPCM 0x0011
  31. #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
  32. void MelderFile_writeAudioFileHeader (MelderFile file, int audioFileType, integer sampleRate, integer numberOfSamples, int numberOfChannels, int numberOfBitsPerSamplePoint) {
  33. try {
  34. FILE *f = file -> filePointer;
  35. if (! f) return;
  36. const int numberOfBytesPerSamplePoint = (numberOfBitsPerSamplePoint + 7) / 8;
  37. switch (audioFileType) {
  38. case Melder_AIFF: {
  39. try {
  40. integer dataSize = numberOfSamples * numberOfBytesPerSamplePoint * numberOfChannels;
  41. /* Form Chunk: contains all other chunks. */
  42. if (fwrite ("FORM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FORM statement.");
  43. binputi32 (4 + (8 + 4) + (8 + 18) + (8 + 8 + dataSize), f); // the size of the Form Chunk
  44. if (fwrite ("AIFF", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the AIFF file type.");
  45. /* Format Version Chunk: 8 + 4 bytes. */
  46. if (fwrite ("FVER", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FVER statement.");
  47. binputu32 (4, f); // the size of what follows
  48. binputu32 (0xA2805140, f); // time of version
  49. /* Common Chunk: 8 + 18 bytes. */
  50. if (fwrite ("COMM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the COMM statement.");
  51. binputi32 (18, f); // the size of what follows
  52. binputi16 (numberOfChannels, f);
  53. binputi32 (numberOfSamples, f);
  54. binputi16 (numberOfBitsPerSamplePoint, f);
  55. binputr80 (sampleRate, f);
  56. /* Sound Data Chunk: 8 + 8 bytes + samples. */
  57. if (fwrite ("SSND", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the SSND statement.");
  58. binputi32 (8 + dataSize, f); // the size of what follows
  59. binputi32 (0, f); // offset
  60. binputi32 (0, f); // block size
  61. } catch (MelderError) {
  62. Melder_throw (U"AIFF header not written.");
  63. }
  64. } break;
  65. case Melder_AIFC: {
  66. try {
  67. integer dataSize = numberOfSamples * numberOfBytesPerSamplePoint * numberOfChannels;
  68. /* Form Chunk: contains all other chunks. */
  69. if (fwrite ("FORM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FORM statement.");
  70. binputi32 (4 + (8 + 4) + (8 + 24) + (8 + 8 + dataSize), f); // the size of the Form Chunk
  71. if (fwrite ("AIFC", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the AIFC file type.");
  72. /* Format Version Chunk: 8 + 4 bytes. */
  73. if (fwrite ("FVER", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FVER statement.");
  74. binputu32 (4, f); // the size of what follows
  75. binputu32 (0xA2805140, f); // time of version
  76. /* Common Chunk: 8 + 24 bytes. */
  77. if (fwrite ("COMM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the COMM statement.");
  78. binputi32 (24, f); // the size of what follows
  79. binputi16 (numberOfChannels, f);
  80. binputi32 (numberOfSamples, f);
  81. binputi16 (numberOfBitsPerSamplePoint, f);
  82. binputr80 (sampleRate, f);
  83. if (fwrite ("NONE", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the compression type.");
  84. binputi16 (0, f); // name of compression
  85. /* Sound Data Chunk: 8 + 8 bytes + samples. */
  86. if (fwrite ("SSND", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the SSND statement.");
  87. binputi32 (8 + dataSize, f); // the size of what follows
  88. binputi32 (0, f); // offset
  89. binputi32 (0, f); // block size
  90. } catch (MelderError) {
  91. Melder_throw (U"AIFC header not written.");
  92. }
  93. } break;
  94. case Melder_WAV: {
  95. try {
  96. bool needExtensibleFormat =
  97. numberOfBitsPerSamplePoint > 16 ||
  98. numberOfChannels > 2 ||
  99. numberOfBitsPerSamplePoint != numberOfBytesPerSamplePoint * 8;
  100. const int formatSize = needExtensibleFormat ? 40 : 16;
  101. double dataSize_f = (double) numberOfSamples * (double) numberOfBytesPerSamplePoint * (double) numberOfChannels;
  102. if (dataSize_f > INT54_MAX)
  103. Melder_throw (U"Cannot save data over the 9-petabyte limit.");
  104. int64 dataSize = (int64) dataSize_f;
  105. /* RIFF Chunk: contains all other chunks. */
  106. if (fwrite ("RIFF", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the RIFF statement.");
  107. int64 sizeOfRiffChunk_i64 = 4 + (12 + formatSize) + (4 + dataSize);
  108. if (sizeOfRiffChunk_i64 > UINT32_MAX)
  109. Melder_throw (U"Cannot save a WAV file with more than ", UINT32_MAX, U" bytes.");
  110. uint32 sizeOfRiffChunk_u32 = (uint32) sizeOfRiffChunk_i64;
  111. binputu32LE (sizeOfRiffChunk_u32, f);
  112. if (fwrite ("WAVE", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the WAV file type.");
  113. /* Format Chunk: if 16-bits audio, then 8 + 16 bytes; else 8 + 40 bytes. */
  114. if (fwrite ("fmt ", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FMT statement.");
  115. binputi32LE (formatSize, f);
  116. binputi16LE (needExtensibleFormat ? WAVE_FORMAT_EXTENSIBLE : WAVE_FORMAT_PCM, f);
  117. binputi16LE (numberOfChannels, f);
  118. binputi32LE (sampleRate, f); // number of samples per second
  119. binputi32LE (sampleRate * numberOfBytesPerSamplePoint * numberOfChannels, f); // average number of bytes per second
  120. binputi16LE (numberOfBytesPerSamplePoint * numberOfChannels, f); // block alignment
  121. binputi16LE (numberOfBytesPerSamplePoint * 8, f); // padded bits per sample
  122. if (needExtensibleFormat) {
  123. binputi16LE (22, f); // extensionSize
  124. binputi16LE (numberOfBitsPerSamplePoint, f); // valid bits per sample
  125. binputi32LE (0, f); // speaker position mask
  126. binputi16LE (WAVE_FORMAT_PCM, f);
  127. if (fwrite ("\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71",
  128. 1, 14, f) != 14) Melder_throw (U"Error in file while trying to write the subformat.");
  129. }
  130. /* Data Chunk: 8 bytes + samples. */
  131. if (fwrite ("data", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the DATA statement.");
  132. binputi32LE (dataSize, f);
  133. } catch (MelderError) {
  134. Melder_throw (U"WAV header not written.");
  135. }
  136. } break;
  137. case Melder_NEXT_SUN: {
  138. try {
  139. if (fwrite (".snd", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the .snd file type tag.");
  140. binputi32 (32, f); // length of header
  141. binputi32 (numberOfSamples * 2 * numberOfChannels, f); // length of data
  142. binputi32 (3, f); // 16-bits linear, not mu-law or A-law
  143. binputi32 (sampleRate, f);
  144. binputi32 (numberOfChannels, f);
  145. binputi32 (0, f);
  146. binputi32 (0, f);
  147. } catch (MelderError) {
  148. Melder_throw (U"NeXT/Sun header not written.");
  149. }
  150. } break;
  151. case Melder_NIST: {
  152. try {
  153. char header [1024];
  154. memset (header, 0, 1024);
  155. sprintf (header, "NIST_1A\n 1024\n"
  156. "channel_count -i %s\n"
  157. "sample_count -i %s\n"
  158. "sample_n_bytes -i %s\n"
  159. "sample_byte_format -s2 01\n" /* 01=LE 10=BE */
  160. "sample_coding -s3 pcm\n"
  161. "sample_rate -i %s\n"
  162. "sample_min -i -32768\n"
  163. "sample_max -i 32767\n"
  164. "end_head\n",
  165. Melder8_integer (numberOfChannels), Melder8_integer (numberOfSamples),
  166. Melder8_integer (numberOfBytesPerSamplePoint), Melder8_integer (sampleRate));
  167. if (fwrite (header, 1, 1024, f) != 1024) Melder_throw (U"Error in file while trying to write the NIST header.");
  168. } catch (MelderError) {
  169. Melder_throw (U"NIST header not written.");
  170. }
  171. } break;
  172. case Melder_FLAC: {
  173. try {
  174. if (numberOfChannels > (int) FLAC__MAX_CHANNELS)
  175. Melder_throw (U"FLAC files cannot have more than 8 channels.");
  176. FLAC__StreamEncoder *encoder = FLAC__stream_encoder_new ();
  177. if (! encoder)
  178. Melder_throw (U"Error creating FLAC stream encoder.");
  179. FLAC__stream_encoder_set_bits_per_sample (encoder, numberOfBitsPerSamplePoint);
  180. FLAC__stream_encoder_set_channels (encoder, numberOfChannels);
  181. FLAC__stream_encoder_set_sample_rate (encoder, sampleRate);
  182. FLAC__stream_encoder_set_total_samples_estimate (encoder, numberOfSamples);
  183. if (FLAC__stream_encoder_init_FILE (encoder, file -> filePointer, nullptr, nullptr) != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
  184. FLAC__stream_encoder_delete (encoder);
  185. Melder_throw (U"Error creating FLAC stream encoder");
  186. }
  187. file -> flacEncoder = encoder; // only after we know it's correct (for MelderFile_close)
  188. file -> outputEncoding = kMelder_textOutputEncoding_FLAC; // only after we know it's correct (for MelderFile_close)
  189. } catch (MelderError) {
  190. Melder_throw (U"FLAC header not written.");
  191. }
  192. } break;
  193. default: Melder_throw (U"Unknown audio file type ", audioFileType, U".");
  194. }
  195. } catch (MelderError) {
  196. Melder_throw (U"16-bit audio file header not written.");
  197. }
  198. }
  199. void MelderFile_writeAudioFileTrailer (MelderFile file, int audioFileType, integer sampleRate, integer numberOfSamples, int numberOfChannels, int numberOfBitsPerSamplePoint) {
  200. (void) sampleRate;
  201. bool shouldPadTheDataToAnEvenNumberOfBytes = audioFileType == Melder_WAV;
  202. bool numberOfSamplesIsOdd = (numberOfSamples & 1) != 0;
  203. bool numberOfChannelsIsOdd = (numberOfChannels & 1) != 0;
  204. int numberOfBytesPerSamplePoint = (numberOfBitsPerSamplePoint + 7) / 8;
  205. bool numberOfBytesPerSamplePointIsOdd = (numberOfBytesPerSamplePoint & 1) != 0;
  206. bool needToPadOneByte = shouldPadTheDataToAnEvenNumberOfBytes && numberOfSamplesIsOdd && numberOfChannelsIsOdd && numberOfBytesPerSamplePointIsOdd;
  207. if (needToPadOneByte && file -> filePointer)
  208. binputi8 (0, file -> filePointer);
  209. }
  210. static const conststring32 audioFileTypeString [] = { U"none", U"AIFF", U"AIFC", U"WAV", U"NeXT/Sun", U"NIST", U"FLAC", U"MP3" };
  211. conststring32 Melder_audioFileTypeString (int audioFileType) { return audioFileType > Melder_NUMBER_OF_AUDIO_FILE_TYPES ? U"unknown" : audioFileTypeString [audioFileType]; }
  212. static const conststring32 macAudioFileType [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES]
  213. = { U"", U"AIFF", U"AIFC", U"WAVE", U"ULAW", U"NIST", U"FLAC", U"MP3" };
  214. static const conststring32 winAudioFileExtension [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES]
  215. = { U"", U".aiff", U".aifc", U".wav", U".au", U".nist", U".flac", U".mp3" };
  216. static int defaultAudioFileEncoding16 [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES]
  217. = { 0, Melder_LINEAR_16_BIG_ENDIAN, Melder_LINEAR_16_BIG_ENDIAN, Melder_LINEAR_16_LITTLE_ENDIAN,
  218. Melder_LINEAR_16_BIG_ENDIAN, Melder_LINEAR_16_LITTLE_ENDIAN,
  219. Melder_FLAC_COMPRESSION_16, Melder_MPEG_COMPRESSION_16 };
  220. static int defaultAudioFileEncoding24 [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES]
  221. = { 0, Melder_LINEAR_24_BIG_ENDIAN, Melder_LINEAR_24_BIG_ENDIAN, Melder_LINEAR_24_LITTLE_ENDIAN,
  222. Melder_LINEAR_24_BIG_ENDIAN, Melder_LINEAR_24_LITTLE_ENDIAN,
  223. Melder_FLAC_COMPRESSION_24, Melder_MPEG_COMPRESSION_24 };
  224. static int defaultAudioFileEncoding32 [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES]
  225. = { 0, Melder_LINEAR_32_BIG_ENDIAN, Melder_LINEAR_32_BIG_ENDIAN, Melder_LINEAR_32_LITTLE_ENDIAN,
  226. Melder_LINEAR_32_BIG_ENDIAN, Melder_LINEAR_32_LITTLE_ENDIAN,
  227. Melder_FLAC_COMPRESSION_32, Melder_MPEG_COMPRESSION_32 };
  228. int Melder_defaultAudioFileEncoding (int audioFileType, int numberOfBitsPerSamplePoint) {
  229. return
  230. numberOfBitsPerSamplePoint == 16 ? defaultAudioFileEncoding16 [audioFileType] :
  231. numberOfBitsPerSamplePoint == 24 ? defaultAudioFileEncoding24 [audioFileType] :
  232. defaultAudioFileEncoding32 [audioFileType];
  233. }
  234. void MelderFile_writeAudioFile (MelderFile file, int audioFileType, const short *buffer, integer sampleRate, integer numberOfSamples, int numberOfChannels, int numberOfBitsPerSamplePoint) {
  235. try {
  236. autoMelderFile mfile = MelderFile_create (file);
  237. MelderFile_writeAudioFileHeader (file, audioFileType, sampleRate, numberOfSamples, numberOfChannels, numberOfBitsPerSamplePoint);
  238. MelderFile_writeShortToAudio (file, numberOfChannels, defaultAudioFileEncoding16 [audioFileType], buffer, numberOfSamples);
  239. MelderFile_writeAudioFileTrailer (file, audioFileType, sampleRate, numberOfSamples, numberOfChannels, numberOfBitsPerSamplePoint);
  240. mfile.close ();
  241. } catch (MelderError) {
  242. Melder_throw (U"16-bit audio file not written.");
  243. }
  244. }
  245. /***** READING *****/
  246. int Melder_bytesPerSamplePoint (int encoding) {
  247. return
  248. encoding == Melder_LINEAR_16_BIG_ENDIAN || encoding == Melder_LINEAR_16_LITTLE_ENDIAN ? 2 :
  249. encoding == Melder_LINEAR_24_BIG_ENDIAN || encoding == Melder_LINEAR_24_LITTLE_ENDIAN ? 3 :
  250. encoding == Melder_LINEAR_32_BIG_ENDIAN || encoding == Melder_LINEAR_32_LITTLE_ENDIAN ||
  251. encoding == Melder_IEEE_FLOAT_32_BIG_ENDIAN || encoding == Melder_IEEE_FLOAT_32_LITTLE_ENDIAN ? 4 :
  252. 1;
  253. }
  254. static int ulaw2linear [] =
  255. { -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
  256. -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
  257. -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
  258. -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
  259. -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
  260. -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
  261. -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
  262. -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
  263. -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
  264. -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
  265. -876, -844, -812, -780, -748, -716, -684, -652,
  266. -620, -588, -556, -524, -492, -460, -428, -396,
  267. -372, -356, -340, -324, -308, -292, -276, -260,
  268. -244, -228, -212, -196, -180, -164, -148, -132,
  269. -120, -112, -104, -96, -88, -80, -72, -64,
  270. -56, -48, -40, -32, -24, -16, -8, 0,
  271. 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
  272. 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
  273. 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
  274. 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
  275. 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
  276. 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
  277. 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
  278. 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
  279. 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
  280. 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
  281. 876, 844, 812, 780, 748, 716, 684, 652,
  282. 620, 588, 556, 524, 492, 460, 428, 396,
  283. 372, 356, 340, 324, 308, 292, 276, 260,
  284. 244, 228, 212, 196, 180, 164, 148, 132,
  285. 120, 112, 104, 96, 88, 80, 72, 64,
  286. 56, 48, 40, 32, 24, 16, 8, 0
  287. };
  288. static short alaw2linear[] =
  289. {
  290. -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
  291. -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
  292. -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
  293. -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
  294. -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
  295. -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
  296. -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
  297. -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
  298. -344, -328, -376, -360, -280, -264, -312, -296,
  299. -472, -456, -504, -488, -408, -392, -440, -424,
  300. -88, -72, -120, -104, -24, -8, -56, -40,
  301. -216, -200, -248, -232, -152, -136, -184, -168,
  302. -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
  303. -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
  304. -688, -656, -752, -720, -560, -528, -624, -592,
  305. -944, -912, -1008, -976, -816, -784, -880, -848,
  306. 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
  307. 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
  308. 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
  309. 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
  310. 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
  311. 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
  312. 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
  313. 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
  314. 344, 328, 376, 360, 280, 264, 312, 296,
  315. 472, 456, 504, 488, 408, 392, 440, 424,
  316. 88, 72, 120, 104, 24, 8, 56, 40,
  317. 216, 200, 248, 232, 152, 136, 184, 168,
  318. 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
  319. 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
  320. 688, 656, 752, 720, 560, 528, 624, 592,
  321. 944, 912, 1008, 976, 816, 784, 880, 848
  322. };
  323. static void Melder_checkAiffFile (FILE *f, integer *numberOfChannels, int *encoding,
  324. double *sampleRate, integer *startOfData, integer *numberOfSamples)
  325. {
  326. char data [8], chunkID [4];
  327. bool commonChunkPresent = false, dataChunkPresent = false, isAifc = true;
  328. int numberOfBitsPerSamplePoint;
  329. /* Read header of AIFF(-C) file: 12 bytes. */
  330. if (fread (data, 1, 4, f) < 4) Melder_throw (U"File too small: no FORM statement.");
  331. if (! strnequ (data, "FORM", 4)) Melder_throw (U"Not an AIFF or AIFC file (FORM statement expected).");
  332. if (fread (data, 1, 4, f) < 4) Melder_throw (U"File too small: no size of FORM chunk.");
  333. if (fread (data, 1, 4, f) < 4) Melder_throw (U"File too small: no file type info (expected AIFF or AIFC).");
  334. if (! strnequ (data, "AIFF", 4) && ! strnequ (data, "AIFC", 4)) Melder_throw (U"Not an AIFF or AIFC file (wrong file type info).");
  335. if (strnequ (data, "AIFF", 4)) isAifc = false;
  336. /* Search for Common Chunk and Data Chunk. */
  337. while (fread (chunkID, 1, 4, f) == 4) {
  338. integer chunkSize = bingeti32 (f);
  339. if (chunkSize & 1) ++ chunkSize; // round up to nearest even number
  340. /* IN SOUND FILES PRODUCED BY THE SGI'S soundeditor PROGRAM, */
  341. /* THE COMMON CHUNK HAS A chunkSize OF 18 INSTEAD OF 38, */
  342. /* ALTHOUGH THE COMMON CHUNK CONTAINS */
  343. /* THE 20-BYTE SEQUENCE "\016not compressed\0". */
  344. /* START FIX OF FOREIGN BUG */
  345. if(strnequ(chunkID,"NONE",4)&&
  346. (chunkSize==(14<<24)+('n'<<16)+('o'<<8)+'t'||chunkSize==('t'<<24)+('o'<<16)+('n'<<8)+14))
  347. {Melder_casual(U"Ha! a buggy SGI \"soundeditor\" file...");for(integer i=1;i<=20/*diff*/-8/*header*/;i++)fread(data,1,1,f);continue;}
  348. /* FINISH FIX OF FOREIGN BUG */
  349. if (strnequ (chunkID, "COMM", 4)) {
  350. /*
  351. * Found a Common Chunk.
  352. */
  353. commonChunkPresent = true;
  354. *numberOfChannels = bingeti16 (f);
  355. if (*numberOfChannels < 1) Melder_throw (U"Too few sound channels (", *numberOfChannels, U").");
  356. *numberOfSamples = bingeti32 (f);
  357. if (*numberOfSamples <= 0) Melder_throw (U"Too few samples ", *numberOfSamples, U").");
  358. numberOfBitsPerSamplePoint = bingeti16 (f);
  359. if (numberOfBitsPerSamplePoint > 32) Melder_throw (U"Too many bits per sample (", numberOfBitsPerSamplePoint, U"; the maximum is 32).");
  360. *encoding =
  361. numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_BIG_ENDIAN :
  362. numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_BIG_ENDIAN :
  363. numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_BIG_ENDIAN :
  364. Melder_LINEAR_8_SIGNED;
  365. *sampleRate = bingetr80 (f);
  366. if (*sampleRate <= 0.0) Melder_throw (U"Wrong sampling frequency (", *sampleRate, U" Hz).");
  367. if (isAifc) {
  368. /*
  369. * Read compression data; should be "NONE" or "sowt".
  370. */
  371. if (fread (data, 1, 4, f) < 4) Melder_throw (U"File too small: no compression info.");
  372. if (! strnequ (data, "NONE", 4) && ! strnequ (data, "sowt", 4)) {
  373. data [4] = '\0';
  374. Melder_throw (U"Cannot read compressed AIFC files (compression type ", Melder_peek8to32 (data), U").");
  375. }
  376. if (strnequ (data, "sowt", 4))
  377. *encoding =
  378. numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_LITTLE_ENDIAN :
  379. numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_LITTLE_ENDIAN :
  380. numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_LITTLE_ENDIAN :
  381. Melder_LINEAR_8_SIGNED;
  382. /*
  383. * Read rest of compression info.
  384. */
  385. for (integer i = 23; i <= chunkSize; i ++)
  386. if (fread (data, 1, 1, f) < 1)
  387. Melder_throw (U"File too small: expected chunk of ", chunkSize, U" bytes, but found ", i + 22, U".");
  388. }
  389. } else if (strnequ (chunkID, "SSND", 4)) {
  390. /*
  391. * Found a Data Chunk.
  392. */
  393. dataChunkPresent = true;
  394. *startOfData = ftell (f) + 8; // ignore "offset" (4 bytes) and "blocksize" (4 bytes)
  395. if (commonChunkPresent) break; // optimization: do not read whole data chunk if we have already read the common chunk
  396. } else // ignore Version Chunk and unknown chunks
  397. for (integer i = 1; i <= chunkSize; i ++)
  398. if (fread (data, 1, 1, f) < 1)
  399. Melder_throw (U"File too small: expected ", chunkSize, U" bytes, but found ", i, U".");
  400. }
  401. if (! commonChunkPresent) Melder_throw (U"Found no Common Chunk.");
  402. if (! dataChunkPresent) Melder_throw (U"Found no Data Chunk.");
  403. }
  404. static void Melder_checkWavFile (FILE *f, integer *numberOfChannels, int *encoding,
  405. double *sampleRate, integer *startOfData, integer *numberOfSamples)
  406. {
  407. char data [14], chunkID [4];
  408. bool formatChunkPresent = false, dataChunkPresent = false;
  409. int numberOfBitsPerSamplePoint = -1;
  410. uint32 dataChunkSize = 0xffffffff;
  411. if (fread (data, 1, 4, f) < 4) Melder_throw (U"File too small: no RIFF statement.");
  412. if (! strnequ (data, "RIFF", 4)) Melder_throw (U"Not a WAV file (RIFF statement expected).");
  413. if (fread (data, 1, 4, f) < 4) Melder_throw (U"File too small: no size of RIFF chunk.");
  414. if (fread (data, 1, 4, f) < 4) Melder_throw (U"File too small: no file type info (expected WAVE statement).");
  415. if (! strnequ (data, "WAVE", 4) && ! strnequ (data, "CDDA", 4))
  416. Melder_throw (U"Not a WAVE or CD audio file (wrong file type info).");
  417. /* Search for Format Chunk and Data Chunk. */
  418. while (fread (chunkID, 1, 4, f) == 4) {
  419. uint32 chunkSize = bingetu32LE (f);
  420. if (Melder_debug == 23) {
  421. Melder_warning (chunkID [0], U" ", chunkID [1], U" ",
  422. chunkID [2], U" ", chunkID [3], U" ", chunkSize);
  423. }
  424. if (strnequ (chunkID, "fmt ", 4)) {
  425. /*
  426. * Found a Format Chunk.
  427. */
  428. uint16 winEncoding = bingetu16LE (f);
  429. formatChunkPresent = true;
  430. *numberOfChannels = bingeti16LE (f);
  431. if (*numberOfChannels < 1) Melder_throw (U"Too few sound channels (", *numberOfChannels, U").");
  432. *sampleRate = (double) bingeti32LE (f);
  433. if (*sampleRate <= 0.0) Melder_throw (U"Wrong sampling frequency (", *sampleRate, U" Hz).");
  434. (void) bingeti32LE (f); // avgBytesPerSec
  435. (void) bingeti16LE (f); // blockAlign
  436. numberOfBitsPerSamplePoint = bingeti16LE (f);
  437. if (numberOfBitsPerSamplePoint == 0)
  438. numberOfBitsPerSamplePoint = 16; // the default
  439. else if (numberOfBitsPerSamplePoint < 4)
  440. Melder_throw (U"Too few bits per sample (", numberOfBitsPerSamplePoint, U"; the minimum is 4).");
  441. else if (numberOfBitsPerSamplePoint > 32)
  442. Melder_throw (U"Too few bits per sample (", numberOfBitsPerSamplePoint, U"; the maximum is 32).");
  443. switch (winEncoding) {
  444. case WAVE_FORMAT_PCM:
  445. *encoding =
  446. numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_LITTLE_ENDIAN :
  447. numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_LITTLE_ENDIAN :
  448. numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_LITTLE_ENDIAN :
  449. Melder_LINEAR_8_UNSIGNED;
  450. break;
  451. case WAVE_FORMAT_IEEE_FLOAT:
  452. *encoding = Melder_IEEE_FLOAT_32_LITTLE_ENDIAN;
  453. break;
  454. case WAVE_FORMAT_ALAW:
  455. *encoding = Melder_ALAW;
  456. break;
  457. case WAVE_FORMAT_MULAW:
  458. *encoding = Melder_MULAW;
  459. break;
  460. case WAVE_FORMAT_DVI_ADPCM:
  461. Melder_throw (U"Cannot read lossy compressed audio files (this one is DVI ADPCM).\n"
  462. U"Please use uncompressed audio files. If you must open this file,\n"
  463. U"please use an audio converter program to convert it first to normal (PCM) WAV format\n"
  464. U"(Praat may have difficulty analysing the poor recording, though).");
  465. case WAVE_FORMAT_EXTENSIBLE: {
  466. if (chunkSize < 40)
  467. Melder_throw (U"Not enough format data in extensible WAV format");
  468. (void) bingeti16LE (f); // extensionSize
  469. (void) bingeti16LE (f); // validBitsPerSample
  470. (void) bingeti32LE (f); // channelMask
  471. uint16 winEncoding2 = bingetu16LE (f); // override
  472. switch (winEncoding2) {
  473. case WAVE_FORMAT_PCM:
  474. *encoding =
  475. numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_LITTLE_ENDIAN :
  476. numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_LITTLE_ENDIAN :
  477. numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_LITTLE_ENDIAN :
  478. Melder_LINEAR_8_UNSIGNED;
  479. break;
  480. case WAVE_FORMAT_IEEE_FLOAT:
  481. *encoding = Melder_IEEE_FLOAT_32_LITTLE_ENDIAN;
  482. break;
  483. case WAVE_FORMAT_ALAW:
  484. *encoding = Melder_ALAW;
  485. break;
  486. case WAVE_FORMAT_MULAW:
  487. *encoding = Melder_MULAW;
  488. break;
  489. case WAVE_FORMAT_DVI_ADPCM:
  490. Melder_throw (U"Cannot read lossy compressed audio files (this one is DVI ADPCM).\n"
  491. U"Please use uncompressed audio files. If you must open this file,\n"
  492. U"please use an audio converter program to convert it first to normal (PCM) WAV format\n"
  493. U"(Praat may have difficulty analysing the poor recording, though).");
  494. default:
  495. Melder_throw (U"Unsupported Windows audio encoding ", winEncoding2, U".");
  496. }
  497. if (fread (data, 1, 14, f) < 14) Melder_throw (U"File too small: no SubFormat data.");
  498. continue; // next chunk
  499. }
  500. default:
  501. Melder_throw (U"Unsupported Windows audio encoding ", winEncoding, U".");
  502. }
  503. if (chunkSize & 1) chunkSize ++;
  504. for (integer i = 17; i <= chunkSize; i ++)
  505. if (fread (data, 1, 1, f) < 1) Melder_throw (U"File too small: expected ", chunkSize, U" bytes in fmt chunk, but found ", i, U".");
  506. } else if (strnequ (chunkID, "data", 4)) {
  507. /*
  508. * Found a Data Chunk.
  509. */
  510. dataChunkPresent = true;
  511. dataChunkSize = chunkSize;
  512. *startOfData = ftell (f);
  513. if (chunkSize & 1) chunkSize ++;
  514. if (chunkSize > UINT32_MAX - 100) { // incorrect data chunk (sometimes -44); assume that the data run till the end of the file
  515. fseeko (f, 0LL, SEEK_END);
  516. off_t endOfData = ftello (f);
  517. dataChunkSize = chunkSize = endOfData - *startOfData;
  518. fseeko (f, *startOfData, SEEK_SET);
  519. }
  520. if (Melder_debug == 23) {
  521. for (integer i = 1; i <= chunkSize; i ++)
  522. if (fread (data, 1, 1, f) < 1) Melder_throw (U"File too small: expected ", chunkSize, U" bytes of data, but found ", i, U".");
  523. } else {
  524. if (formatChunkPresent) break; // OPTIMIZATION: do not read the whole data chunk if we have already read the format chunk
  525. }
  526. } else { // ignore other chunks
  527. if (chunkSize & 1) chunkSize ++;
  528. for (integer i = 1; i <= chunkSize; i ++)
  529. if (fread (data, 1, 1, f) < 1)
  530. Melder_throw (U"File too small: expected ", chunkSize, U" bytes, but found ", i, U".");
  531. }
  532. }
  533. if (! formatChunkPresent) Melder_throw (U"Found no Format Chunk.");
  534. if (! dataChunkPresent) Melder_throw (U"Found no Data Chunk.");
  535. Melder_assert (numberOfBitsPerSamplePoint != -1 && dataChunkSize != 0xffffffff);
  536. *numberOfSamples = dataChunkSize / *numberOfChannels / ((numberOfBitsPerSamplePoint + 7) / 8);
  537. }
  538. static void Melder_checkNextSunFile (FILE *f, integer *numberOfChannels, int *encoding,
  539. double *sampleRate, integer *startOfData, integer *numberOfSamples)
  540. {
  541. char tag [4];
  542. fread (tag, 1, 4, f);
  543. if (strncmp (tag, ".snd", 4)) Melder_throw (U"Not a Sun audio file.");
  544. *startOfData = bingeti32 (f);
  545. if (*startOfData < 24 || *startOfData > 320)
  546. Melder_throw (U"Cannot read header of audio file. Length ", *startOfData, U".");
  547. integer dataSize = bingeti32 (f);
  548. if (dataSize <= 0) {
  549. /*
  550. * Incorrect information. Get it from file length.
  551. */
  552. integer save = ftell (f);
  553. fseek (f, 0, SEEK_END);
  554. dataSize = ftell (f) - *startOfData;
  555. fseek (f, save, SEEK_SET);
  556. }
  557. integer sunEncoding = bingeti32 (f);
  558. switch (sunEncoding) {
  559. case 1: *encoding = Melder_MULAW; break;
  560. case 2: *encoding = Melder_LINEAR_8_SIGNED; break;
  561. case 3: *encoding = Melder_LINEAR_16_BIG_ENDIAN; break;
  562. case 27: *encoding = Melder_ALAW; break;
  563. default: Melder_throw (U"Cannot translate audio file encoding ", sunEncoding, U".");
  564. }
  565. *sampleRate = bingeti32 (f);
  566. if (*sampleRate <= 0) Melder_throw (U"Impossible sampling frequency ", *sampleRate, U" Hz.");
  567. *numberOfChannels = bingeti32 (f);
  568. if (*numberOfChannels < 1)
  569. Melder_throw (U"Wrong number of channels in audio file (", *numberOfChannels, U").");
  570. *numberOfSamples = dataSize / Melder_bytesPerSamplePoint (*encoding) / *numberOfChannels;
  571. integer skip = *startOfData - 24;
  572. while (skip -- > 0) (void) fgetc (f);
  573. }
  574. static int nistGetValue (const char *header, const char *object, double *rval, char *sval) {
  575. char obj [30], type [10];
  576. const char *match = strstr (header, object);
  577. if (! match) return 0;
  578. if (sscanf (match, "%29s%9s%99s", obj, type, sval) != 3) return 0;
  579. if (strequ (type, "-i") || strequ (type, "-r")) *rval = atof (sval);
  580. else if (strncmp (type, "-s", 2)) return 0;
  581. return 1;
  582. }
  583. static void Melder_checkNistFile (FILE *f, integer *numberOfChannels, int *encoding,
  584. double *sampleRate, integer *startOfData, integer *numberOfSamples)
  585. {
  586. char header [1024], sval [100];
  587. double rval = 0.0;
  588. int numberOfBytesPerSamplePoint;
  589. if (fread (header, 1, 1024, f) != 1024)
  590. Melder_throw (U"Cannot read NISTheader.");
  591. if (! strnequ (header, "NIST_1A", 7))
  592. Melder_throw (U"Not a NIST sound file.");
  593. *startOfData = atol (header + 9);
  594. if (! nistGetValue (header, "sample_count", & rval, sval) || rval < 1.0)
  595. Melder_throw (U"Incorrect number of samples in NIST file.");
  596. *numberOfSamples = Melder_iround (rval);
  597. if (! nistGetValue (header, "sample_n_bytes", & rval, sval) || rval < 1.0 || rval > 2.0)
  598. Melder_throw (U"Incorrect number of bytes per sample (should be 1 or 2).");
  599. numberOfBytesPerSamplePoint = Melder_iround (rval);
  600. if (! nistGetValue (header, "channel_count", & rval, sval) || rval < 1.0)
  601. Melder_throw (U"Incorrect number of channels.");
  602. *numberOfChannels = Melder_iround (rval);
  603. if (! nistGetValue (header, "sample_rate", sampleRate, sval) || *sampleRate < 1)
  604. Melder_throw (U"Incorrect sampling frequency ", *sampleRate, U" Hz.");
  605. *encoding = Melder_LINEAR_16_BIG_ENDIAN;
  606. if (nistGetValue (header, "sample_byte_format", & rval, sval) && strequ (sval, "01"))
  607. *encoding = Melder_LINEAR_16_LITTLE_ENDIAN;
  608. if (numberOfBytesPerSamplePoint == 1)
  609. *encoding = Melder_LINEAR_8_SIGNED;
  610. if (nistGetValue (header, "sample_coding", & rval, sval)) {
  611. if (strnequ (sval, "ulaw", 4))
  612. *encoding = Melder_MULAW;
  613. else if (strstr (sval, "embedded-shorten-v"))
  614. if (nistGetValue (header, "database_id", & rval, sval) && strequ (sval, "POLYPHONE-NL"))
  615. *encoding = Melder_POLYPHONE;
  616. else
  617. *encoding = Melder_SHORTEN;
  618. else if (strnequ (sval, "alaw", 4)) // must be after previous, because some files have "alaw,embedded..."
  619. *encoding = Melder_ALAW;
  620. }
  621. }
  622. static void Melder_checkFlacFile (MelderFile file, integer *numberOfChannels, int *encoding,
  623. double *sampleRate, integer *startOfData, integer *numberOfSamples)
  624. {
  625. FLAC__StreamMetadata metadata;
  626. FLAC__StreamMetadata_StreamInfo *info;
  627. if (! FLAC__metadata_get_streaminfo (Melder_peek32to8 (Melder_fileToPath (file)), & metadata)) // FIXME: not Unicode-compatible on Windows.
  628. Melder_throw (U"Invalid FLAC file");
  629. info = & metadata. data. stream_info;
  630. *numberOfChannels = info -> channels;
  631. *encoding = Melder_FLAC_COMPRESSION_16;
  632. *sampleRate = (double) info -> sample_rate;
  633. *startOfData = 0; // meaningless: libFLAC does the I/O
  634. *numberOfSamples = info -> total_samples; // BUG: loses bits above INT32_MAX
  635. if ((FLAC__uint64) *numberOfSamples != info -> total_samples)
  636. Melder_throw (U"FLAC file too long.");
  637. }
  638. static void Melder_checkMp3File (FILE *f, integer *numberOfChannels, int *encoding,
  639. double *sampleRate, integer *startOfData, integer *numberOfSamples)
  640. {
  641. MP3_FILE mp3f = mp3f_new ();
  642. mp3f_set_file (mp3f, f);
  643. if (! mp3f_analyze (mp3f)) {
  644. mp3f_delete (mp3f);
  645. Melder_throw (U"Cannot analyze MP3 file");
  646. }
  647. *encoding = Melder_MPEG_COMPRESSION_16;
  648. *numberOfChannels = mp3f_channels (mp3f);
  649. *sampleRate = mp3f_frequency (mp3f);
  650. *numberOfSamples = mp3f_samples (mp3f);
  651. if ((MP3F_OFFSET)*numberOfSamples != mp3f_samples (mp3f)) // BUG: loses bits above INT32_MAX
  652. Melder_throw (U"MP3 file too long.");
  653. *startOfData = 0; // meaningless
  654. mp3f_delete (mp3f);
  655. }
  656. int MelderFile_checkSoundFile (MelderFile file, integer *numberOfChannels, int *encoding,
  657. double *sampleRate, integer *startOfData, integer *numberOfSamples)
  658. {
  659. char data [16];
  660. FILE *f = file -> filePointer;
  661. if (! f || fread (data, 1, 16, f) < 16) return 0;
  662. rewind (f);
  663. if (strnequ (data, "FORM", 4) && strnequ (data + 8, "AIFF", 4)) {
  664. Melder_checkAiffFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples);
  665. return Melder_AIFF;
  666. }
  667. if (strnequ (data, "FORM", 4) && strnequ (data + 8, "AIFC", 4)) {
  668. Melder_checkAiffFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples);
  669. return Melder_AIFC;
  670. }
  671. if (strnequ (data, "RIFF", 4) && (strnequ (data + 8, "WAVE", 4) || strnequ (data + 8, "CDDA", 4))) {
  672. Melder_checkWavFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples);
  673. return Melder_WAV;
  674. }
  675. if (strnequ (data, ".snd", 4)) {
  676. Melder_checkNextSunFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples);
  677. return Melder_NEXT_SUN;
  678. }
  679. if (strnequ (data, "NIST_1A", 7)) {
  680. Melder_checkNistFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples);
  681. return Melder_NIST;
  682. }
  683. if (strnequ (data, "fLaC", 4)) {
  684. Melder_checkFlacFile (file, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples);
  685. return Melder_FLAC;
  686. }
  687. if (mp3_recognize (16, data)) {
  688. Melder_checkMp3File (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples);
  689. return Melder_MP3;
  690. }
  691. return 0; // not a recognized sound file
  692. }
  693. /* libFLAC works through callbacks, so we need a context struct. */
  694. typedef struct {
  695. FILE *file;
  696. integer numberOfChannels;
  697. integer numberOfSamples;
  698. double *channels [FLAC__MAX_CHANNELS];
  699. } MelderDecodeFlacContext;
  700. /* The same goes for MP3 */
  701. typedef struct {
  702. integer numberOfChannels;
  703. integer numberOfSamples;
  704. double *channels [2];
  705. } MelderDecodeMp3Context;
  706. static FLAC__StreamDecoderReadStatus Melder_DecodeFlac_read (const FLAC__StreamDecoder *decoder,
  707. FLAC__byte buffer [], size_t *bytes, void *client_data)
  708. {
  709. MelderDecodeFlacContext *c = (MelderDecodeFlacContext *) client_data;
  710. (void) decoder;
  711. if (*bytes <= 0)
  712. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  713. *bytes = fread (buffer, sizeof (FLAC__byte), *bytes, c -> file);
  714. if (ferror (c -> file))
  715. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  716. if (*bytes == 0)
  717. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  718. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  719. }
  720. static FLAC__StreamDecoderWriteStatus Melder_DecodeFlac_convert (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
  721. {
  722. MelderDecodeFlacContext *c = (MelderDecodeFlacContext *) client_data;
  723. const FLAC__FrameHeader *header = & frame -> header;
  724. integer count = header -> blocksize;
  725. double multiplier;
  726. (void) decoder;
  727. switch (header -> bits_per_sample) {
  728. case 8: multiplier = (1.0f / 128); break;
  729. case 16: multiplier = (1.0f / 32768); break;
  730. case 24: multiplier = (1.0f / 8388608); break;
  731. case 32: multiplier = (1.0f / 32768 / 65536); break;
  732. default: return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  733. }
  734. for (integer i = 0; i < c -> numberOfChannels; ++ i) {
  735. const FLAC__int32 *input = buffer [i];
  736. double *output = c -> channels [i];
  737. for (integer j = 0; j < count; ++ j)
  738. output [j] = ((integer) input [j]) * multiplier;
  739. c -> channels [i] += count;
  740. }
  741. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  742. }
  743. static void Melder_DecodeMp3_convert (const MP3F_SAMPLE *channels [MP3F_MAX_CHANNELS], integer count, void *context) {
  744. MelderDecodeMp3Context *c = (MelderDecodeMp3Context *) context;
  745. const MP3F_SAMPLE *input;
  746. double *output;
  747. for (integer i = 0; i < c -> numberOfChannels; ++ i) {
  748. input = channels [i];
  749. output = c -> channels [i];
  750. for (integer j = 0; j < count; ++ j)
  751. output [j] = mp3f_sample_to_float (input [j]);
  752. c -> channels [i] += count;
  753. }
  754. }
  755. static void Melder_DecodeFlac_error (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
  756. (void) decoder;
  757. (void) client_data;
  758. Melder_warning (U"FLAC decoder error: ", Melder_peek8to32 (FLAC__StreamDecoderErrorStatusString [status]));
  759. }
  760. static void Melder_readFlacFile (FILE *f, MAT buffer) {
  761. int result = 0;
  762. MelderDecodeFlacContext c;
  763. c.file = f;
  764. c.numberOfChannels = buffer.nrow;
  765. for (int ichan = 1; ichan <= buffer.nrow; ichan ++)
  766. c.channels [ichan - 1] = & buffer [ichan] [1];
  767. c.numberOfSamples = buffer.ncol;
  768. FLAC__StreamDecoder *decoder = FLAC__stream_decoder_new ();
  769. if (! decoder)
  770. goto end;
  771. if (FLAC__stream_decoder_init_stream (decoder,
  772. Melder_DecodeFlac_read,
  773. nullptr, nullptr, nullptr, nullptr,
  774. Melder_DecodeFlac_convert, nullptr,
  775. Melder_DecodeFlac_error, & c) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
  776. goto end;
  777. result = FLAC__stream_decoder_process_until_end_of_stream (decoder);
  778. FLAC__stream_decoder_finish (decoder);
  779. end:
  780. if (decoder)
  781. FLAC__stream_decoder_delete (decoder);
  782. if (result == 0)
  783. Melder_throw (U"Error decoding FLAC file.");
  784. }
  785. static void Melder_readMp3File (FILE *f, MAT buffer) {
  786. int result = 0;
  787. MelderDecodeMp3Context c;
  788. c.numberOfChannels = buffer.nrow;
  789. for (int ichan = 1; ichan <= buffer.nrow; ichan ++)
  790. c.channels [ichan - 1] = & buffer [ichan] [1];
  791. c.numberOfSamples = buffer.ncol;
  792. MP3_FILE mp3f = mp3f_new ();
  793. mp3f_set_file (mp3f, f);
  794. mp3f_set_callback (mp3f, Melder_DecodeMp3_convert, &c);
  795. result = mp3f_read (mp3f, buffer.ncol);
  796. mp3f_delete (mp3f);
  797. if (result == 0)
  798. Melder_throw (U"Error decoding MP3 file.");
  799. }
  800. void Melder_readAudioToFloat (FILE *f, int encoding, MAT buffer) {
  801. try {
  802. integer numberOfChannels = buffer.nrow;
  803. integer numberOfSamples = buffer.ncol;
  804. switch (encoding) {
  805. case Melder_LINEAR_8_SIGNED: {
  806. try {
  807. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  808. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  809. int8 value;
  810. if (fread (& value, 1, 1, f) < 1) throw MelderError ();
  811. buffer [ichan] [isamp] = value * (1.0 / 128);
  812. }
  813. }
  814. } catch (MelderError) {
  815. Melder_clearError ();
  816. Melder_warning (U"File too small (", numberOfChannels, U"-channel 8-bit).\nMissing samples set to zero.");
  817. }
  818. } break;
  819. case Melder_LINEAR_8_UNSIGNED:
  820. try {
  821. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  822. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++)
  823. buffer [ichan] [isamp] = bingetu8 (f) * (1.0 / 128) - 1.0;
  824. }
  825. } catch (MelderError) {
  826. Melder_clearError ();
  827. Melder_warning (U"File too small (", numberOfChannels, U"-channel 8-bit).\nMissing samples set to zero.");
  828. }
  829. break;
  830. case Melder_LINEAR_16_BIG_ENDIAN:
  831. case Melder_LINEAR_16_LITTLE_ENDIAN:
  832. case Melder_LINEAR_24_BIG_ENDIAN:
  833. case Melder_LINEAR_24_LITTLE_ENDIAN:
  834. case Melder_LINEAR_32_BIG_ENDIAN:
  835. case Melder_LINEAR_32_LITTLE_ENDIAN:
  836. {
  837. int numberOfBytesPerSamplePerChannel =
  838. encoding == Melder_LINEAR_16_BIG_ENDIAN || encoding == Melder_LINEAR_16_LITTLE_ENDIAN ? 2 :
  839. encoding == Melder_LINEAR_24_BIG_ENDIAN || encoding == Melder_LINEAR_24_LITTLE_ENDIAN ? 3 : 4;
  840. double numberOfBytes_f = (double) numberOfChannels * (double) numberOfSamples * (double) numberOfBytesPerSamplePerChannel;
  841. if (isinf (numberOfBytes_f) || numberOfBytes_f > (double) (1LL << 53)) {
  842. Melder_throw (U"Cannot read ", numberOfBytes_f, U" bytes, "
  843. U"because that crosses the 9-petabyte limit.");
  844. }
  845. if (numberOfBytes_f > (double) SIZE_MAX) {
  846. Melder_throw (U"Cannot read ", numberOfBytes_f, U" bytes. "
  847. U"Perhaps try a 64-bit edition of Praat?");
  848. }
  849. Melder_assert (numberOfBytes_f >= 0.0);
  850. size_t numberOfBytes = (size_t) numberOfBytes_f; // cast is safe because overflow and signedness have been checked
  851. uint8 *bytes = (uint8 *) & buffer [numberOfChannels] [numberOfSamples] + sizeof (double) - numberOfBytes;
  852. /*
  853. * Read 16-bit data into the last quarter of the buffer,
  854. * or 24-bit data into the last three-eighths of the buffer,
  855. * or 32-bit data into the last half of the buffer.
  856. */
  857. size_t numberOfBytesRead = fread (bytes, 1, numberOfBytes, f);
  858. if (numberOfChannels == 1) {
  859. switch (encoding) {
  860. case Melder_LINEAR_16_BIG_ENDIAN: {
  861. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  862. unsigned char byte1 = * bytes ++, byte2 = * bytes ++;
  863. int value = (int) (int16) (((uint16) byte1 << 8) | (uint16) byte2); // extend sign
  864. buffer [1] [isamp] = value * (1.0 / 32768);
  865. }
  866. } break;
  867. case Melder_LINEAR_16_LITTLE_ENDIAN: {
  868. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  869. unsigned char byte1 = * bytes ++, byte2 = * bytes ++;
  870. int value = (int) (int16) (((uint16) byte2 << 8) | (uint16) byte1); // extend sign
  871. buffer [1] [isamp] = value * (1.0 / 32768);
  872. }
  873. } break;
  874. case Melder_LINEAR_24_BIG_ENDIAN: {
  875. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  876. unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++;
  877. int32 value = (int32)
  878. ((uint32) ((uint32) byte1 << 24) |
  879. (uint32) ((uint32) byte2 << 16) |
  880. ((uint32) byte3 << 8));
  881. buffer [1] [isamp] = value * (1.0 / 32768 / 65536);
  882. }
  883. } break;
  884. case Melder_LINEAR_24_LITTLE_ENDIAN: {
  885. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  886. unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++;
  887. int32 value = (int32)
  888. ((uint32) ((uint32) byte3 << 24) |
  889. (uint32) ((uint32) byte2 << 16) |
  890. ((uint32) byte1 << 8));
  891. buffer [1] [isamp] = value * (1.0 / 32768 / 65536);
  892. }
  893. } break;
  894. case Melder_LINEAR_32_BIG_ENDIAN: {
  895. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  896. unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++;
  897. int32 value = (int32)
  898. ((uint32) ((uint32) byte1 << 24) |
  899. (uint32) ((uint32) byte2 << 16) |
  900. (uint32) ((uint32) byte3 << 8) |
  901. (uint32) byte4);
  902. buffer [1] [isamp] = value * (1.0 / 32768 / 65536);
  903. }
  904. } break;
  905. case Melder_LINEAR_32_LITTLE_ENDIAN: {
  906. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  907. unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++;
  908. int32 value = (int32)
  909. ((uint32) ((uint32) byte4 << 24) |
  910. (uint32) ((uint32) byte3 << 16) |
  911. (uint32) ((uint32) byte2 << 8) |
  912. (uint32) byte1);
  913. buffer [1] [isamp] = value * (1.0 / 32768 / 65536);
  914. }
  915. } break;
  916. }
  917. } else if (numberOfChannels <= (int) sizeof (double) / numberOfBytesPerSamplePerChannel) {
  918. switch (encoding) {
  919. case Melder_LINEAR_16_BIG_ENDIAN: {
  920. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  921. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  922. uint8 byte1 = * bytes ++, byte2 = * bytes ++;
  923. int value = (int) (int16) (uint16) ((uint16) ((uint16) byte1 << 8) | (uint16) byte2);
  924. buffer [ichan] [isamp] = value * (1.0 / 32768);
  925. }
  926. }
  927. } break;
  928. case Melder_LINEAR_16_LITTLE_ENDIAN: {
  929. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  930. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  931. uint8 byte1 = * bytes ++, byte2 = * bytes ++;
  932. int value = (int) (int16) (uint16) ((uint16) ((uint16) byte2 << 8) | (uint16) byte1);
  933. buffer [ichan] [isamp] = value * (1.0 / 32768);
  934. }
  935. }
  936. } break;
  937. case Melder_LINEAR_24_BIG_ENDIAN: {
  938. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  939. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  940. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++;
  941. uint32 unsignedValue =
  942. (uint32) ((uint32) byte1 << 16) |
  943. (uint32) ((uint32) byte2 << 8) |
  944. (uint32) byte3;
  945. if ((byte1 & 128) != 0) unsignedValue |= 0xFF000000; // extend sign
  946. buffer [ichan] [isamp] = (int32) unsignedValue * (1.0 / 8388608);
  947. }
  948. }
  949. } break;
  950. case Melder_LINEAR_24_LITTLE_ENDIAN: {
  951. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  952. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  953. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++;
  954. uint32 unsignedValue = ((uint32) byte3 << 16) | ((uint32) byte2 << 8) | (uint32) byte1;
  955. if ((byte3 & 128) != 0) unsignedValue |= 0xFF000000;
  956. buffer [ichan] [isamp] = (int32) unsignedValue * (1.0 / 8388608);
  957. }
  958. }
  959. } break;
  960. case Melder_LINEAR_32_BIG_ENDIAN: {
  961. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  962. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  963. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++;
  964. int32 value = (int32)
  965. ((uint32) ((uint32) byte1 << 24) |
  966. (uint32) ((uint32) byte2 << 16) |
  967. (uint32) ((uint32) byte3 << 8) |
  968. (uint32) byte4);
  969. buffer [ichan] [isamp] = value * (1.0 / 32768 / 65536);
  970. }
  971. }
  972. } break;
  973. case Melder_LINEAR_32_LITTLE_ENDIAN: {
  974. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  975. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  976. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++;
  977. int32 value = (int32)
  978. ((uint32) ((uint32) byte4 << 24) |
  979. (uint32) ((uint32) byte3 << 16) |
  980. (uint32) ((uint32) byte2 << 8) |
  981. (uint32) byte1);
  982. buffer [ichan] [isamp] = value * (1.0 / 32768 / 65536);
  983. }
  984. }
  985. } break;
  986. }
  987. } else {
  988. Melder_assert (sizeof (double) == 8);
  989. int32 *ints = (int32 *) & buffer [1] [1];
  990. switch (encoding) {
  991. case Melder_LINEAR_16_BIG_ENDIAN: {
  992. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  993. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  994. uint8 byte1 = * bytes ++, byte2 = * bytes ++;
  995. int32 value = (int32)
  996. ((uint32) ((uint32) byte1 << 24) |
  997. (uint32) ((uint32) byte2 << 16));
  998. * ints ++ = value;
  999. * ints ++ = 0; // the marker
  1000. }
  1001. }
  1002. } break;
  1003. case Melder_LINEAR_16_LITTLE_ENDIAN: {
  1004. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1005. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1006. uint8 byte1 = * bytes ++, byte2 = * bytes ++;
  1007. int32 value = (int32)
  1008. (((uint32) byte2 << 24) |
  1009. ((uint32) byte1 << 16));
  1010. * ints ++ = value;
  1011. * ints ++ = 0; // the marker
  1012. }
  1013. }
  1014. } break;
  1015. case Melder_LINEAR_24_BIG_ENDIAN: {
  1016. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1017. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1018. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++;
  1019. int32 value = (int32)
  1020. (((uint32) byte1 << 24) |
  1021. ((uint32) byte2 << 16) |
  1022. ((uint32) byte3 << 8));
  1023. * ints ++ = value;
  1024. * ints ++ = 0; // the marker
  1025. }
  1026. }
  1027. } break;
  1028. case Melder_LINEAR_24_LITTLE_ENDIAN: {
  1029. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1030. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1031. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++;
  1032. int32 value = (int32)
  1033. (((uint32) byte3 << 24) |
  1034. ((uint32) byte2 << 16) |
  1035. ((uint32) byte1 << 8));
  1036. * ints ++ = value;
  1037. * ints ++ = 0; // the marker
  1038. }
  1039. }
  1040. } break;
  1041. case Melder_LINEAR_32_BIG_ENDIAN: {
  1042. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1043. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1044. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++;
  1045. int32 value = (int32)
  1046. (((uint32) byte1 << 24) |
  1047. ((uint32) byte2 << 16) |
  1048. ((uint32) byte3 << 8) |
  1049. (uint32) byte4);
  1050. * ints ++ = value;
  1051. * ints ++ = 0; // the marker
  1052. }
  1053. }
  1054. } break;
  1055. case Melder_LINEAR_32_LITTLE_ENDIAN: {
  1056. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1057. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1058. uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++;
  1059. int32 value = (int32)
  1060. (((uint32) byte4 << 24) |
  1061. ((uint32) byte3 << 16) |
  1062. ((uint32) byte2 << 8) |
  1063. (uint32) byte1);
  1064. * ints ++ = value;
  1065. * ints ++ = 0; // the marker
  1066. }
  1067. }
  1068. }
  1069. }
  1070. double *doubles = & buffer [1] [1];
  1071. integer n = numberOfSamples * numberOfChannels;
  1072. for (integer i = 0; i < n; i ++) {
  1073. int32 *valuePosition = (int32 *) & doubles [i];
  1074. int32 *markerPosition = valuePosition + 1;
  1075. if (! *markerPosition) {
  1076. int32 value = *valuePosition;
  1077. integer ichan = i / numberOfSamples, isamp = i % numberOfSamples;
  1078. for (integer other = isamp * numberOfChannels + ichan; other != i; ) {
  1079. int32 *otherValuePosition = (int32 *) & doubles [other];
  1080. *valuePosition = *otherValuePosition;
  1081. *markerPosition = 1;
  1082. valuePosition = otherValuePosition;
  1083. markerPosition = valuePosition + 1;
  1084. ichan = other / numberOfSamples, isamp = other % numberOfSamples;
  1085. other = isamp * numberOfChannels + ichan;
  1086. }
  1087. *valuePosition = value;
  1088. *markerPosition = 1;
  1089. }
  1090. }
  1091. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1092. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1093. buffer [ichan] [isamp] = * (int32 *) & buffer [ichan] [isamp] * (1.0 / 32768 / 65536);
  1094. }
  1095. }
  1096. }
  1097. if (numberOfBytesRead < numberOfBytes)
  1098. Melder_warning (U"File too small (", numberOfChannels, U"-channel ", numberOfBytesPerSamplePerChannel * 8, U"-bit).\n"
  1099. U"Missing samples were set to zero.");
  1100. } break;
  1101. case Melder_IEEE_FLOAT_32_BIG_ENDIAN:
  1102. try {
  1103. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1104. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1105. buffer [ichan] [isamp] = bingetr32 (f);
  1106. }
  1107. }
  1108. } catch (MelderError) {
  1109. Melder_clearError ();
  1110. Melder_warning (U"File too small (", numberOfChannels, U"-channel 32-bit floating point).\nMissing samples set to zero.");
  1111. }
  1112. break;
  1113. case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN:
  1114. try {
  1115. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1116. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1117. buffer [ichan] [isamp] = bingetr32LE (f);
  1118. }
  1119. }
  1120. } catch (MelderError) {
  1121. Melder_clearError ();
  1122. Melder_warning (U"File too small (", numberOfChannels, U"-channel 32-bit floating point).\nMissing samples set to zero.");
  1123. }
  1124. break;
  1125. case Melder_MULAW:
  1126. try {
  1127. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1128. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1129. buffer [ichan] [isamp] = ulaw2linear [bingetu8 (f)] * (1.0 / 32768);
  1130. }
  1131. }
  1132. } catch (MelderError) {
  1133. Melder_clearError ();
  1134. Melder_warning (U"File too small (", numberOfChannels, U"-channel 8-bit " "-law).\nMissing samples set to zero.");
  1135. }
  1136. break;
  1137. case Melder_ALAW:
  1138. try {
  1139. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1140. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1141. buffer [ichan] [isamp] = alaw2linear [bingetu8 (f)] * (1.0 / 32768);
  1142. }
  1143. }
  1144. } catch (MelderError) {
  1145. Melder_clearError ();
  1146. Melder_warning (U"File too small (", numberOfChannels, U"-channel 8-bit A-law).\nMissing samples set to zero.");
  1147. }
  1148. break;
  1149. case Melder_FLAC_COMPRESSION_16:
  1150. case Melder_FLAC_COMPRESSION_24:
  1151. case Melder_FLAC_COMPRESSION_32:
  1152. Melder_readFlacFile (f, buffer);
  1153. break;
  1154. case Melder_MPEG_COMPRESSION_16:
  1155. case Melder_MPEG_COMPRESSION_24:
  1156. case Melder_MPEG_COMPRESSION_32:
  1157. Melder_readMp3File (f, buffer);
  1158. break;
  1159. default:
  1160. Melder_throw (U"Unknown encoding ", encoding, U".");
  1161. }
  1162. } catch (MelderError) {
  1163. Melder_throw (U"Audio samples not read from file.");
  1164. }
  1165. }
  1166. void Melder_readAudioToShort (FILE *f, integer numberOfChannels, int encoding, short *buffer, integer numberOfSamples) {
  1167. try {
  1168. integer n = numberOfSamples * numberOfChannels, i;
  1169. static const unsigned short byteSwapTest = 3 * 256 + 1;
  1170. switch (encoding) {
  1171. case Melder_LINEAR_8_SIGNED:
  1172. for (i = 0; i < n; i ++) {
  1173. signed char value;
  1174. if (! fread (& value, 1, 1, f)) Melder_throw (U"File too small (mono 8-bit).");
  1175. buffer [i] = value * 256;
  1176. }
  1177. break;
  1178. case Melder_LINEAR_8_UNSIGNED:
  1179. for (i = 0; i < n; i ++) {
  1180. buffer [i] = bingetu8 (f) * 256L - 32768;
  1181. }
  1182. break;
  1183. case Melder_LINEAR_16_BIG_ENDIAN:
  1184. fread (buffer, sizeof (short), n, f);
  1185. if (* (uint8 *) & byteSwapTest == 1) {
  1186. for (i = 0; i < n; i ++) {
  1187. unsigned short value = buffer [i];
  1188. buffer [i] = (value >> 8) + (value << 8);
  1189. }
  1190. }
  1191. break;
  1192. case Melder_LINEAR_16_LITTLE_ENDIAN:
  1193. fread (buffer, sizeof (short), n, f);
  1194. if (* (unsigned char *) & byteSwapTest == 3) {
  1195. for (i = 0; i < n; i ++) {
  1196. unsigned short value = buffer [i];
  1197. buffer [i] = (value >> 8) + (value << 8);
  1198. }
  1199. }
  1200. break;
  1201. case Melder_LINEAR_24_BIG_ENDIAN:
  1202. for (i = 0; i < n; i ++) {
  1203. buffer [i] = bingeti24 (f) / 256; // BUG: truncation; not ideal
  1204. }
  1205. break;
  1206. case Melder_LINEAR_24_LITTLE_ENDIAN:
  1207. for (i = 0; i < n; i ++) {
  1208. buffer [i] = bingeti24LE (f) / 256; // BUG: truncation; not ideal
  1209. }
  1210. break;
  1211. case Melder_LINEAR_32_BIG_ENDIAN:
  1212. for (i = 0; i < n; i ++) {
  1213. buffer [i] = bingeti32 (f) / 65536; // BUG: truncation; not ideal
  1214. }
  1215. break;
  1216. case Melder_LINEAR_32_LITTLE_ENDIAN:
  1217. for (i = 0; i < n; i ++) {
  1218. buffer [i] = bingeti32LE (f) / 65536; // BUG: truncation; not ideal
  1219. }
  1220. break;
  1221. case Melder_IEEE_FLOAT_32_BIG_ENDIAN:
  1222. for (i = 0; i < n; i ++) {
  1223. buffer [i] = bingetr32 (f) * 32768; // BUG: truncation; not ideal
  1224. }
  1225. break;
  1226. case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN:
  1227. for (i = 0; i < n; i ++) {
  1228. buffer [i] = bingetr32LE (f) * 32768; // BUG: truncation; not ideal
  1229. }
  1230. break;
  1231. case Melder_MULAW:
  1232. for (i = 0; i < n; i ++) {
  1233. buffer [i] = ulaw2linear [bingetu8 (f)];
  1234. }
  1235. break;
  1236. case Melder_ALAW:
  1237. for (i = 0; i < n; i ++) {
  1238. buffer [i] = alaw2linear [bingetu8 (f)];
  1239. }
  1240. break;
  1241. default:
  1242. Melder_throw (U"Unknown encoding ", encoding, U".");
  1243. }
  1244. } catch (MelderError) {
  1245. Melder_clearError ();
  1246. Melder_warning (U"Audio file too short. Missing samples were set to zero.");
  1247. }
  1248. }
  1249. void MelderFile_writeShortToAudio (MelderFile file, integer numberOfChannels, int encoding, const short *buffer, integer numberOfSamples) {
  1250. try {
  1251. FILE *f = file -> filePointer;
  1252. if (! f) Melder_throw (U"File not open.");
  1253. integer n = numberOfSamples * numberOfChannels, start = 0, step = 1, i;
  1254. if (numberOfChannels < 0) {
  1255. n = numberOfSamples * 2; // stereo
  1256. step = 2; // only one channel will be written
  1257. if (numberOfChannels == -2) {
  1258. start = 1; // right channel
  1259. }
  1260. numberOfChannels = 1;
  1261. }
  1262. switch (encoding) {
  1263. case Melder_LINEAR_8_SIGNED:
  1264. for (i = start; i < n; i += step)
  1265. binputi8 (buffer [i] >> 8, f);
  1266. break; case Melder_LINEAR_8_UNSIGNED:
  1267. for (i = start; i < n; i += step)
  1268. binputu8 ((buffer [i] >> 8) + 128, f);
  1269. break; case Melder_LINEAR_16_BIG_ENDIAN:
  1270. for (i = start; i < n; i += step)
  1271. binputi16 (buffer [i], f);
  1272. break; case Melder_LINEAR_16_LITTLE_ENDIAN:
  1273. for (i = start; i < n; i += step)
  1274. binputi16LE (buffer [i], f);
  1275. break; case Melder_LINEAR_24_BIG_ENDIAN:
  1276. for (i = start; i < n; i += step)
  1277. binputi24 (buffer [i] << 8, f);
  1278. break; case Melder_LINEAR_24_LITTLE_ENDIAN:
  1279. for (i = start; i < n; i += step)
  1280. binputi24LE (buffer [i] << 8, f);
  1281. break; case Melder_LINEAR_32_BIG_ENDIAN:
  1282. for (i = start; i < n; i += step)
  1283. binputi32 (buffer [i] << 16, f);
  1284. break; case Melder_LINEAR_32_LITTLE_ENDIAN:
  1285. for (i = start; i < n; i += step)
  1286. binputi32LE (buffer [i] << 16, f);
  1287. break; case Melder_IEEE_FLOAT_32_BIG_ENDIAN:
  1288. for (i = start; i < n; i += step)
  1289. binputr32 (buffer [i] / 32768.0, f);
  1290. break; case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN:
  1291. for (i = start; i < n; i += step)
  1292. binputr32LE (buffer [i] / 32768.0, f);
  1293. break;
  1294. case Melder_FLAC_COMPRESSION_16:
  1295. case Melder_FLAC_COMPRESSION_24:
  1296. case Melder_FLAC_COMPRESSION_32:
  1297. if (! file -> flacEncoder)
  1298. Melder_throw (U"FLAC encoder not initialized.");
  1299. for (i = start; i < n; i += step * numberOfChannels) {
  1300. FLAC__int32 samples [FLAC__MAX_CHANNELS];
  1301. for (int ichan = 1; ichan <= numberOfChannels; ichan ++)
  1302. samples [ichan - 1] = buffer [i + ichan - 1];
  1303. if (! FLAC__stream_encoder_process_interleaved (file -> flacEncoder, samples, 1))
  1304. Melder_throw (U"Error encoding FLAC stream.");
  1305. }
  1306. break; case Melder_MULAW: case Melder_ALAW: default:
  1307. Melder_throw (U"Unknown encoding ", encoding, U".");
  1308. }
  1309. } catch (MelderError) {
  1310. Melder_throw (U"Samples not written to audio file.");
  1311. }
  1312. }
  1313. void MelderFile_writeFloatToAudio (MelderFile file, constMAT buffer, int encoding, bool warnIfClipped) {
  1314. try {
  1315. FILE *f = file -> filePointer;
  1316. if (! f) Melder_throw (U"File not open.");
  1317. const integer numberOfSamples = buffer.ncol, numberOfChannels = buffer.nrow;
  1318. integer nclipped = 0;
  1319. switch (encoding) {
  1320. case Melder_LINEAR_8_SIGNED:
  1321. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1322. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1323. double value = round (buffer [ichan] [isamp] * 128.0);
  1324. if (value < -128.0) { value = -128.0; nclipped ++; }
  1325. if (value > 127.0) { value = 127.0; nclipped ++; }
  1326. binputi8 ((int) value, f);
  1327. }
  1328. }
  1329. break;
  1330. case Melder_LINEAR_8_UNSIGNED:
  1331. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1332. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1333. double value = floor ((buffer [ichan] [isamp] + 1.0) * 128.0);
  1334. if (value < 0.0) { value = 0.0; nclipped ++; }
  1335. if (value > 255.0) { value = 255.0; nclipped ++; }
  1336. binputu8 ((int) value, f);
  1337. }
  1338. }
  1339. break;
  1340. case Melder_LINEAR_16_BIG_ENDIAN:
  1341. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1342. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1343. double value = round (buffer [ichan] [isamp] * 32768.0);
  1344. if (value < -32768.0) { value = -32768.0; nclipped ++; }
  1345. if (value > 32767.0) { value = 32767.0; nclipped ++; }
  1346. binputi16 ((int16) value, f);
  1347. }
  1348. }
  1349. break;
  1350. case Melder_LINEAR_16_LITTLE_ENDIAN:
  1351. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1352. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1353. double value = round (buffer [ichan] [isamp] * 32768.0);
  1354. if (value < -32768.0) { value = -32768.0; nclipped ++; }
  1355. if (value > 32767.0) { value = 32767.0; nclipped ++; }
  1356. binputi16LE ((int16) value, f);
  1357. }
  1358. }
  1359. break;
  1360. case Melder_LINEAR_24_BIG_ENDIAN:
  1361. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1362. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1363. double value = round (buffer [ichan] [isamp] * 8388608.0);
  1364. if (value < -8388608.0) { value = -8388608.0; nclipped ++; }
  1365. if (value > 8388607.0) { value = 8388607.0; nclipped ++; }
  1366. binputi24 ((int32) value, f);
  1367. }
  1368. }
  1369. break;
  1370. case Melder_LINEAR_24_LITTLE_ENDIAN:
  1371. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1372. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1373. double value = round (buffer [ichan] [isamp] * 8388608.0);
  1374. if (value < -8388608.0) { value = -8388608.0; nclipped ++; }
  1375. if (value > 8388607.0) { value = 8388607.0; nclipped ++; }
  1376. binputi24LE ((int32) value, f);
  1377. }
  1378. }
  1379. break;
  1380. case Melder_LINEAR_32_BIG_ENDIAN:
  1381. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1382. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1383. double value = round (buffer [ichan] [isamp] * 2147483648.0);
  1384. if (value < -2147483648.0) { value = -2147483648.0; nclipped ++; }
  1385. if (value > 2147483647.0) { value = 2147483647.0; nclipped ++; }
  1386. binputi32 ((int32) value, f); // safe cast: rounding and range already handled
  1387. }
  1388. }
  1389. break;
  1390. case Melder_LINEAR_32_LITTLE_ENDIAN:
  1391. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1392. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1393. double value = round (buffer [ichan] [isamp] * 2147483648.0);
  1394. if (value < -2147483648.0) { value = -2147483648.0; nclipped ++; }
  1395. if (value > 2147483647.0) { value = 2147483647.0; nclipped ++; }
  1396. binputi32LE ((int32) value, f);
  1397. }
  1398. }
  1399. break;
  1400. case Melder_IEEE_FLOAT_32_BIG_ENDIAN:
  1401. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1402. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1403. double value = buffer [ichan] [isamp];
  1404. binputr32 (value, f);
  1405. }
  1406. }
  1407. break;
  1408. case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN:
  1409. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1410. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1411. double value = buffer [ichan] [isamp];
  1412. binputr32LE (value, f);
  1413. }
  1414. }
  1415. break;
  1416. case Melder_FLAC_COMPRESSION_16:
  1417. case Melder_FLAC_COMPRESSION_24:
  1418. case Melder_FLAC_COMPRESSION_32:
  1419. if (! file -> flacEncoder)
  1420. Melder_throw (U"FLAC encoder not initialized.");
  1421. for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) {
  1422. FLAC__int32 samples [FLAC__MAX_CHANNELS];
  1423. for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) {
  1424. double value = round (buffer [ichan] [isamp] * 32768.0);
  1425. if (value < -32768.0) { value = -32768.0; nclipped ++; }
  1426. if (value > 32767.0) { value = 32767.0; nclipped ++; }
  1427. samples [ichan - 1] = (FLAC__int32) value;
  1428. }
  1429. if (! FLAC__stream_encoder_process_interleaved (file -> flacEncoder, samples, 1))
  1430. Melder_throw (U"Error encoding FLAC stream.");
  1431. }
  1432. break;
  1433. case Melder_MULAW:
  1434. case Melder_ALAW:
  1435. default:
  1436. Melder_throw (U"Unknown format.");
  1437. }
  1438. if (nclipped > 0 && warnIfClipped)
  1439. Melder_warning (U"Writing samples to audio file: ", nclipped, U" out of ", numberOfSamples, U" samples have been clipped.\n"
  1440. U"Advice: you could scale the amplitudes or write to a binary file.");
  1441. } catch (MelderError) {
  1442. Melder_throw (U"Samples not written to audio file.");
  1443. }
  1444. }
  1445. /* End of file melder_audiofiles.cpp */