| 1 | /* melder_audiofiles.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2010-2019,2021,2023-2026 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 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 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 | |
| 19 | #include "melder.h" |
| 20 | #define FLAC__NO_DLL |
| 21 | #include "../external/flac/flac_FLAC_metadata.h" |
| 22 | #include "../external/flac/flac_FLAC_stream_decoder.h" |
| 23 | #include "../external/flac/flac_FLAC_stream_encoder.h" |
| 24 | #include "../external/mp3/mp3.h" |
| 25 | |
| 26 | /***** WRITING *****/ |
| 27 | |
| 28 | #ifndef WAVE_FORMAT_PCM |
| 29 | #define WAVE_FORMAT_PCM 0x0001 |
| 30 | #endif |
| 31 | #define WAVE_FORMAT_IEEE_FLOAT 0x0003 |
| 32 | #define WAVE_FORMAT_ALAW 0x0006 |
| 33 | #define WAVE_FORMAT_MULAW 0x0007 |
| 34 | #define WAVE_FORMAT_DVI_ADPCM 0x0011 |
| 35 | #define WAVE_FORMAT_EXTENSIBLE 0xFFFE |
| 36 | |
| 37 | void MelderFile_writeAudioFileHeader (MelderFile file, int audioFileType, integer sampleRate, integer numberOfSamples, integer numberOfChannels, int numberOfBitsPerSamplePoint) { |
| 38 | try { |
| 39 | FILE *f = file -> filePointer; |
| 40 | if (! f) return; |
| 41 | const int numberOfBytesPerSamplePoint = (numberOfBitsPerSamplePoint + 7) / 8; |
| 42 | switch (audioFileType) { |
| 43 | case Melder_AIFF: { |
| 44 | try { |
| 45 | integer dataSize = numberOfSamples * numberOfBytesPerSamplePoint * numberOfChannels; |
| 46 | |
| 47 | /* Form Chunk: contains all other chunks. */ |
| 48 | if (fwrite ("FORM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FORM statement."); |
| 49 | binputi32 (4 + (8 + 4) + (8 + 18) + (8 + 8 + dataSize), f); // the size of the Form Chunk |
| 50 | if (fwrite ("AIFF", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the AIFF file type."); |
| 51 | |
| 52 | /* Format Version Chunk: 8 + 4 bytes. */ |
| 53 | if (fwrite ("FVER", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FVER statement."); |
| 54 | binputu32 (4, f); // the size of what follows |
| 55 | binputu32 (0xA2805140, f); // time of version |
| 56 | |
| 57 | /* Common Chunk: 8 + 18 bytes. */ |
| 58 | if (fwrite ("COMM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the COMM statement."); |
| 59 | binputi32 (18, f); // the size of what follows |
| 60 | binputi16 (numberOfChannels, f); |
| 61 | binputi32 (numberOfSamples, f); |
| 62 | binputi16 (numberOfBitsPerSamplePoint, f); |
| 63 | binputr80 (sampleRate, f); |
| 64 | |
| 65 | /* Sound Data Chunk: 8 + 8 bytes + samples. */ |
| 66 | if (fwrite ("SSND", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the SSND statement."); |
| 67 | binputi32 (8 + dataSize, f); // the size of what follows |
| 68 | binputi32 (0, f); // offset |
| 69 | binputi32 (0, f); // block size |
| 70 | } catch (MelderError) { |
| 71 | Melder_throw (U"AIFF header not written."); |
| 72 | } |
| 73 | } break; |
| 74 | case Melder_AIFC: { |
| 75 | try { |
| 76 | integer dataSize = numberOfSamples * numberOfBytesPerSamplePoint * numberOfChannels; |
| 77 | |
| 78 | /* Form Chunk: contains all other chunks. */ |
| 79 | if (fwrite ("FORM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FORM statement."); |
| 80 | binputi32 (4 + (8 + 4) + (8 + 24) + (8 + 8 + dataSize), f); // the size of the Form Chunk |
| 81 | if (fwrite ("AIFC", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the AIFC file type."); |
| 82 | |
| 83 | /* Format Version Chunk: 8 + 4 bytes. */ |
| 84 | if (fwrite ("FVER", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FVER statement."); |
| 85 | binputu32 (4, f); // the size of what follows |
| 86 | binputu32 (0xA2805140, f); // time of version |
| 87 | |
| 88 | /* Common Chunk: 8 + 24 bytes. */ |
| 89 | if (fwrite ("COMM", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the COMM statement."); |
| 90 | binputi32 (24, f); // the size of what follows |
| 91 | binputi16 (numberOfChannels, f); |
| 92 | binputi32 (numberOfSamples, f); |
| 93 | binputi16 (numberOfBitsPerSamplePoint, f); |
| 94 | binputr80 (sampleRate, f); |
| 95 | if (fwrite ("NONE", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the compression type."); |
| 96 | binputi16 (0, f); // name of compression |
| 97 | |
| 98 | /* Sound Data Chunk: 8 + 8 bytes + samples. */ |
| 99 | if (fwrite ("SSND", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the SSND statement."); |
| 100 | binputi32 (8 + dataSize, f); // the size of what follows |
| 101 | binputi32 (0, f); // offset |
| 102 | binputi32 (0, f); // block size |
| 103 | } catch (MelderError) { |
| 104 | Melder_throw (U"AIFC header not written."); |
| 105 | } |
| 106 | } break; |
| 107 | case Melder_WAV: { |
| 108 | try { |
| 109 | bool needExtensibleFormat = |
| 110 | numberOfBitsPerSamplePoint > 16 || |
| 111 | numberOfChannels > 2 || |
| 112 | numberOfBitsPerSamplePoint != numberOfBytesPerSamplePoint * 8; |
| 113 | const int formatSize = needExtensibleFormat ? 40 : 16; |
| 114 | double dataSize_f = (double) numberOfSamples * (double) numberOfBytesPerSamplePoint * (double) numberOfChannels; |
| 115 | if (dataSize_f > INT54_MAX) |
| 116 | Melder_throw (U"Cannot save data over the 9-petabyte limit."); |
| 117 | int64 dataSize = (int64) dataSize_f; |
| 118 | |
| 119 | /* RIFF Chunk: contains all other chunks. */ |
| 120 | if (fwrite ("RIFF", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the RIFF statement."); |
| 121 | int64 sizeOfRiffChunk_i64 = 4 + (12 + formatSize) + (4 + dataSize); |
| 122 | if (sizeOfRiffChunk_i64 > UINT32_MAX) |
| 123 | Melder_throw (U"Cannot save a WAV file with more than ", UINT32_MAX, U" bytes."); |
| 124 | uint32 sizeOfRiffChunk_u32 = (uint32) sizeOfRiffChunk_i64; |
| 125 | binputu32LE (sizeOfRiffChunk_u32, f); |
| 126 | if (fwrite ("WAVE", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the WAV file type."); |
| 127 | |
| 128 | /* Format Chunk: if 16-bits audio, then 8 + 16 bytes; else 8 + 40 bytes. */ |
| 129 | if (fwrite ("fmt ", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the FMT statement."); |
| 130 | binputi32LE (formatSize, f); |
| 131 | binputi16LE (needExtensibleFormat ? WAVE_FORMAT_EXTENSIBLE : WAVE_FORMAT_PCM, f); |
| 132 | binputi16LE (numberOfChannels, f); |
| 133 | binputi32LE (sampleRate, f); // number of samples per second |
| 134 | binputi32LE (sampleRate * numberOfBytesPerSamplePoint * numberOfChannels, f); // average number of bytes per second |
| 135 | binputi16LE (numberOfBytesPerSamplePoint * numberOfChannels, f); // block alignment |
| 136 | binputi16LE (numberOfBytesPerSamplePoint * 8, f); // padded bits per sample |
| 137 | if (needExtensibleFormat) { |
| 138 | binputi16LE (22, f); // extensionSize |
| 139 | binputi16LE (numberOfBitsPerSamplePoint, f); // valid bits per sample |
| 140 | binputi32LE (0, f); // speaker position mask |
| 141 | binputi16LE (WAVE_FORMAT_PCM, f); |
| 142 | if (fwrite ("\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71", |
| 143 | 1, 14, f) != 14) Melder_throw (U"Error in file while trying to write the subformat."); |
| 144 | } |
| 145 | |
| 146 | /* Data Chunk: 8 bytes + samples. */ |
| 147 | if (fwrite ("data", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the DATA statement."); |
| 148 | binputi32LE (dataSize, f); |
| 149 | } catch (MelderError) { |
| 150 | Melder_throw (U"WAV header not written."); |
| 151 | } |
| 152 | } break; |
| 153 | case Melder_NEXT_SUN: { |
| 154 | try { |
| 155 | if (fwrite (".snd", 1, 4, f) != 4) Melder_throw (U"Error in file while trying to write the .snd file type tag."); |
| 156 | binputi32 (32, f); // length of header |
| 157 | binputi32 (numberOfSamples * 2 * numberOfChannels, f); // length of data |
| 158 | binputi32 (3, f); // 16-bits linear, not mu-law or A-law |
| 159 | binputi32 (sampleRate, f); |
| 160 | binputi32 (numberOfChannels, f); |
| 161 | binputi32 (0, f); |
| 162 | binputi32 (0, f); |
| 163 | } catch (MelderError) { |
| 164 | Melder_throw (U"NeXT/Sun header not written."); |
| 165 | } |
| 166 | } break; |
| 167 | case Melder_NIST: { |
| 168 | try { |
| 169 | char header [1024]; |
| 170 | memset (header, 0, 1024); |
| 171 | snprintf (header,1024, "NIST_1A\n 1024\n" |
| 172 | "channel_count -i %s\n" |
| 173 | "sample_count -i %s\n" |
| 174 | "sample_n_bytes -i %s\n" |
| 175 | "sample_byte_format -s2 01\n" /* 01=LE 10=BE */ |
| 176 | "sample_coding -s3 pcm\n" |
| 177 | "sample_rate -i %s\n" |
| 178 | "sample_min -i -32768\n" |
| 179 | "sample_max -i 32767\n" |
| 180 | "end_head\n", |
| 181 | Melder8_integer (numberOfChannels), Melder8_integer (numberOfSamples), |
| 182 | Melder8_integer (numberOfBytesPerSamplePoint), Melder8_integer (sampleRate)); |
| 183 | if (fwrite (header, 1, 1024, f) != 1024) Melder_throw (U"Error in file while trying to write the NIST header."); |
| 184 | } catch (MelderError) { |
| 185 | Melder_throw (U"NIST header not written."); |
| 186 | } |
| 187 | } break; |
| 188 | case Melder_FLAC: { |
| 189 | try { |
| 190 | if (numberOfChannels > (int) FLAC__MAX_CHANNELS) |
| 191 | Melder_throw (U"FLAC files cannot have more than 8 channels."); |
| 192 | FLAC__StreamEncoder *encoder = FLAC__stream_encoder_new (); |
| 193 | if (! encoder) |
| 194 | Melder_throw (U"Error creating FLAC stream encoder."); |
| 195 | FLAC__stream_encoder_set_bits_per_sample (encoder, numberOfBitsPerSamplePoint); |
| 196 | FLAC__stream_encoder_set_channels (encoder, numberOfChannels); |
| 197 | FLAC__stream_encoder_set_sample_rate (encoder, sampleRate); |
| 198 | FLAC__stream_encoder_set_total_samples_estimate (encoder, numberOfSamples); |
| 199 | if (FLAC__stream_encoder_init_FILE (encoder, file -> filePointer, nullptr, nullptr) != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { |
| 200 | FLAC__stream_encoder_delete (encoder); |
| 201 | Melder_throw (U"Error creating FLAC stream encoder"); |
| 202 | } |
| 203 | file -> flacEncoder = encoder; // only after we know it's correct (for MelderFile_close) |
| 204 | file -> outputEncoding = kMelder_textOutputEncoding_FLAC; // only after we know it's correct (for MelderFile_close) |
| 205 | } catch (MelderError) { |
| 206 | Melder_throw (U"FLAC header not written."); |
| 207 | } |
| 208 | } break; |
| 209 | default: Melder_throw (U"Unknown audio file type ", audioFileType, U"."); |
| 210 | } |
| 211 | } catch (MelderError) { |
| 212 | Melder_throw (U"16-bit audio file header not written."); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void MelderFile_writeAudioFileTrailer (MelderFile file, int audioFileType, integer sampleRate, integer numberOfSamples, integer numberOfChannels, int numberOfBitsPerSamplePoint) { |
| 217 | (void) sampleRate; |
| 218 | bool shouldPadTheDataToAnEvenNumberOfBytes = audioFileType == Melder_WAV; |
| 219 | bool numberOfSamplesIsOdd = (numberOfSamples & 1) != 0; |
| 220 | bool numberOfChannelsIsOdd = (numberOfChannels & 1) != 0; |
| 221 | int numberOfBytesPerSamplePoint = (numberOfBitsPerSamplePoint + 7) / 8; |
| 222 | bool numberOfBytesPerSamplePointIsOdd = (numberOfBytesPerSamplePoint & 1) != 0; |
| 223 | bool needToPadOneByte = shouldPadTheDataToAnEvenNumberOfBytes && numberOfSamplesIsOdd && numberOfChannelsIsOdd && numberOfBytesPerSamplePointIsOdd; |
| 224 | if (needToPadOneByte && file -> filePointer) |
| 225 | binputi8 (0, file -> filePointer); |
| 226 | } |
| 227 | |
| 228 | static const conststring32 audioFileTypeString [] = { U"none", U"AIFF", U"AIFC", U"WAV", U"NeXT/Sun", U"NIST", U"FLAC", U"MP3" }; |
| 229 | conststring32 Melder_audioFileTypeString (int audioFileType) { return audioFileType > Melder_NUMBER_OF_AUDIO_FILE_TYPES ? U"unknown" : audioFileTypeString [audioFileType]; } |
| 230 | static const conststring32 macAudioFileType [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES] |
| 231 | = { U"", U"AIFF", U"AIFC", U"WAVE", U"ULAW", U"NIST", U"FLAC", U"MP3" }; |
| 232 | static const conststring32 winAudioFileExtension [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES] |
| 233 | = { U"", U".aiff", U".aifc", U".wav", U".au", U".nist", U".flac", U".mp3" }; |
| 234 | static int defaultAudioFileEncoding16 [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES] |
| 235 | = { 0, Melder_LINEAR_16_BIG_ENDIAN, Melder_LINEAR_16_BIG_ENDIAN, Melder_LINEAR_16_LITTLE_ENDIAN, |
| 236 | Melder_LINEAR_16_BIG_ENDIAN, Melder_LINEAR_16_LITTLE_ENDIAN, |
| 237 | Melder_FLAC_COMPRESSION_16, Melder_MPEG_COMPRESSION_16 }; |
| 238 | static int defaultAudioFileEncoding24 [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES] |
| 239 | = { 0, Melder_LINEAR_24_BIG_ENDIAN, Melder_LINEAR_24_BIG_ENDIAN, Melder_LINEAR_24_LITTLE_ENDIAN, |
| 240 | Melder_LINEAR_24_BIG_ENDIAN, Melder_LINEAR_24_LITTLE_ENDIAN, |
| 241 | Melder_FLAC_COMPRESSION_24, Melder_MPEG_COMPRESSION_24 }; |
| 242 | static int defaultAudioFileEncoding32 [1+Melder_NUMBER_OF_AUDIO_FILE_TYPES] |
| 243 | = { 0, Melder_LINEAR_32_BIG_ENDIAN, Melder_LINEAR_32_BIG_ENDIAN, Melder_LINEAR_32_LITTLE_ENDIAN, |
| 244 | Melder_LINEAR_32_BIG_ENDIAN, Melder_LINEAR_32_LITTLE_ENDIAN, |
| 245 | Melder_FLAC_COMPRESSION_32, Melder_MPEG_COMPRESSION_32 }; |
| 246 | int Melder_defaultAudioFileEncoding (int audioFileType, int numberOfBitsPerSamplePoint) { |
| 247 | return |
| 248 | numberOfBitsPerSamplePoint == 16 ? defaultAudioFileEncoding16 [audioFileType] : |
| 249 | numberOfBitsPerSamplePoint == 24 ? defaultAudioFileEncoding24 [audioFileType] : |
| 250 | defaultAudioFileEncoding32 [audioFileType]; |
| 251 | } |
| 252 | |
| 253 | void MelderFile_writeAudioFile (MelderFile file, int audioFileType, const short *buffer, integer sampleRate, integer numberOfSamples, integer numberOfChannels, int numberOfBitsPerSamplePoint) { |
| 254 | try { |
| 255 | autoMelderFile mfile = MelderFile_create (file); |
| 256 | MelderFile_writeAudioFileHeader (file, audioFileType, sampleRate, numberOfSamples, numberOfChannels, numberOfBitsPerSamplePoint); |
| 257 | MelderFile_writeShortToAudio (file, numberOfChannels, defaultAudioFileEncoding16 [audioFileType], buffer, numberOfSamples); |
| 258 | MelderFile_writeAudioFileTrailer (file, audioFileType, sampleRate, numberOfSamples, numberOfChannels, numberOfBitsPerSamplePoint); |
| 259 | mfile.close (); |
| 260 | } catch (MelderError) { |
| 261 | Melder_throw (U"16-bit audio file not written."); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /***** READING *****/ |
| 266 | |
| 267 | int Melder_bytesPerSamplePoint (int encoding) { |
| 268 | return |
| 269 | encoding == Melder_LINEAR_16_BIG_ENDIAN || encoding == Melder_LINEAR_16_LITTLE_ENDIAN ? 2 : |
| 270 | encoding == Melder_LINEAR_24_BIG_ENDIAN || encoding == Melder_LINEAR_24_LITTLE_ENDIAN ? 3 : |
| 271 | encoding == Melder_LINEAR_32_BIG_ENDIAN || encoding == Melder_LINEAR_32_LITTLE_ENDIAN || |
| 272 | encoding == Melder_IEEE_FLOAT_32_BIG_ENDIAN || encoding == Melder_IEEE_FLOAT_32_LITTLE_ENDIAN ? 4 : |
| 273 | encoding == Melder_IEEE_FLOAT_64_BIG_ENDIAN || encoding == Melder_IEEE_FLOAT_64_LITTLE_ENDIAN ? 8 : |
| 274 | 1; |
| 275 | } |
| 276 | |
| 277 | static int16 ulaw2linear [] = { |
| 278 | -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, |
| 279 | -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, |
| 280 | -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412, |
| 281 | -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, |
| 282 | -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, |
| 283 | -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, |
| 284 | -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, |
| 285 | -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, |
| 286 | -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, |
| 287 | -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, |
| 288 | -876, -844, -812, -780, -748, -716, -684, -652, |
| 289 | -620, -588, -556, -524, -492, -460, -428, -396, |
| 290 | -372, -356, -340, -324, -308, -292, -276, -260, |
| 291 | -244, -228, -212, -196, -180, -164, -148, -132, |
| 292 | -120, -112, -104, -96, -88, -80, -72, -64, |
| 293 | -56, -48, -40, -32, -24, -16, -8, 0, |
| 294 | 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, |
| 295 | 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, |
| 296 | 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, |
| 297 | 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, |
| 298 | 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, |
| 299 | 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, |
| 300 | 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, |
| 301 | 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, |
| 302 | 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, |
| 303 | 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, |
| 304 | 876, 844, 812, 780, 748, 716, 684, 652, |
| 305 | 620, 588, 556, 524, 492, 460, 428, 396, |
| 306 | 372, 356, 340, 324, 308, 292, 276, 260, |
| 307 | 244, 228, 212, 196, 180, 164, 148, 132, |
| 308 | 120, 112, 104, 96, 88, 80, 72, 64, |
| 309 | 56, 48, 40, 32, 24, 16, 8, 0 |
| 310 | }; |
| 311 | |
| 312 | static int16 alaw2linear [] = { |
| 313 | -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, |
| 314 | -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, |
| 315 | -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, |
| 316 | -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, |
| 317 | -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944, |
| 318 | -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, |
| 319 | -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472, |
| 320 | -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, |
| 321 | -344, -328, -376, -360, -280, -264, -312, -296, |
| 322 | -472, -456, -504, -488, -408, -392, -440, -424, |
| 323 | -88, -72, -120, -104, -24, -8, -56, -40, |
| 324 | -216, -200, -248, -232, -152, -136, -184, -168, |
| 325 | -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184, |
| 326 | -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, |
| 327 | -688, -656, -752, -720, -560, -528, -624, -592, |
| 328 | -944, -912, -1008, -976, -816, -784, -880, -848, |
| 329 | 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736, |
| 330 | 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, |
| 331 | 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368, |
| 332 | 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, |
| 333 | 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944, |
| 334 | 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, |
| 335 | 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472, |
| 336 | 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, |
| 337 | 344, 328, 376, 360, 280, 264, 312, 296, |
| 338 | 472, 456, 504, 488, 408, 392, 440, 424, |
| 339 | 88, 72, 120, 104, 24, 8, 56, 40, |
| 340 | 216, 200, 248, 232, 152, 136, 184, 168, |
| 341 | 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184, |
| 342 | 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, |
| 343 | 688, 656, 752, 720, 560, 528, 624, 592, |
| 344 | 944, 912, 1008, 976, 816, 784, 880, 848 |
| 345 | }; |
| 346 | |
| 347 | /* |
| 348 | The Shorten encoding employs Alaw, but because they use prediction, |
| 349 | they have to use *sorted* Alaw, so we provide the table here. |
| 350 | |
| 351 | The table was created from the previous table as follows: |
| 352 | 1. Copy all lines between the braces to a file `alawValues.txt`. |
| 353 | 2. Remove all commas (with Replace in BBEdit). |
| 354 | 3. Run the following script: |
| 355 | { |
| 356 | matrix = Read Matrix from raw text file: "alawValues.txt" |
| 357 | matrix## = Get all values |
| 358 | vector# = sort#: combine# (matrix##) |
| 359 | writeInfo: "" |
| 360 | for i to 256 |
| 361 | if i mod 8 = 1 |
| 362 | appendInfo: newline$, " " |
| 363 | else |
| 364 | appendInfo: " " |
| 365 | endif |
| 366 | value$ = string$ (vector# [i]) |
| 367 | spaces$ = left$ (" ", 6 - length (value$)) |
| 368 | appendInfo: spaces$, value$, "," |
| 369 | endfor |
| 370 | } |
| 371 | */ |
| 372 | static int16 sortedAlawValues [] = { |
| 373 | -32256, -31232, -30208, -29184, -28160, -27136, -26112, -25088, |
| 374 | -24064, -23040, -22016, -20992, -19968, -18944, -17920, -16896, |
| 375 | -16128, -15616, -15104, -14592, -14080, -13568, -13056, -12544, |
| 376 | -12032, -11520, -11008, -10496, -9984, -9472, -8960, -8448, |
| 377 | -8064, -7808, -7552, -7296, -7040, -6784, -6528, -6272, |
| 378 | -6016, -5760, -5504, -5248, -4992, -4736, -4480, -4224, |
| 379 | -4032, -3904, -3776, -3648, -3520, -3392, -3264, -3136, |
| 380 | -3008, -2880, -2752, -2624, -2496, -2368, -2240, -2112, |
| 381 | -2016, -1952, -1888, -1824, -1760, -1696, -1632, -1568, |
| 382 | -1504, -1440, -1376, -1312, -1248, -1184, -1120, -1056, |
| 383 | -1008, -976, -944, -912, -880, -848, -816, -784, |
| 384 | -752, -720, -688, -656, -624, -592, -560, -528, |
| 385 | -504, -488, -472, -456, -440, -424, -408, -392, |
| 386 | -376, -360, -344, -328, -312, -296, -280, -264, |
| 387 | -248, -232, -216, -200, -184, -168, -152, -136, |
| 388 | -120, -104, -88, -72, -56, -40, -24, -8, |
| 389 | 8, 24, 40, 56, 72, 88, 104, 120, |
| 390 | 136, 152, 168, 184, 200, 216, 232, 248, |
| 391 | 264, 280, 296, 312, 328, 344, 360, 376, |
| 392 | 392, 408, 424, 440, 456, 472, 488, 504, |
| 393 | 528, 560, 592, 624, 656, 688, 720, 752, |
| 394 | 784, 816, 848, 880, 912, 944, 976, 1008, |
| 395 | 1056, 1120, 1184, 1248, 1312, 1376, 1440, 1504, |
| 396 | 1568, 1632, 1696, 1760, 1824, 1888, 1952, 2016, |
| 397 | 2112, 2240, 2368, 2496, 2624, 2752, 2880, 3008, |
| 398 | 3136, 3264, 3392, 3520, 3648, 3776, 3904, 4032, |
| 399 | 4224, 4480, 4736, 4992, 5248, 5504, 5760, 6016, |
| 400 | 6272, 6528, 6784, 7040, 7296, 7552, 7808, 8064, |
| 401 | 8448, 8960, 9472, 9984, 10496, 11008, 11520, 12032, |
| 402 | 12544, 13056, 13568, 14080, 14592, 15104, 15616, 16128, |
| 403 | 16896, 17920, 18944, 19968, 20992, 22016, 23040, 24064, |
| 404 | 25088, 26112, 27136, 28160, 29184, 30208, 31232, 32256 |
| 405 | }; |
| 406 | |
| 407 | static void Melder_checkAiffFile (FILE *f, integer *numberOfChannels, int *encoding, |
| 408 | double *sampleRate, integer *startOfData, integer *numberOfSamples) |
| 409 | { |
| 410 | /* |
| 411 | Read header of AIFF(-C) file: 12 bytes. |
| 412 | */ |
| 413 | char data [8]; |
| 414 | Melder_require (fread (data, 1, 4, f) == 4, |
| 415 | U"File too small: no FORM statement."); |
| 416 | Melder_require (strnequ (data, "FORM", 4), |
| 417 | U"Not an AIFF or AIFC file (FORM statement expected)."); |
| 418 | Melder_require (fread (data, 1, 4, f) == 4, |
| 419 | U"File too small: no size of FORM chunk."); |
| 420 | Melder_require (fread (data, 1, 4, f) == 4, |
| 421 | U"File too small: no file type info (expected AIFF or AIFC)."); |
| 422 | Melder_require (strnequ (data, "AIFF", 4) || strnequ (data, "AIFC", 4), |
| 423 | U"Not an AIFF or AIFC file (wrong file type info)."); |
| 424 | const bool isAifc = strnequ (data, "AIFC", 4); |
| 425 | |
| 426 | /* Search for Common Chunk and Data Chunk. */ |
| 427 | |
| 428 | char chunkID [4]; |
| 429 | bool commonChunkPresent = false, dataChunkPresent = false; |
| 430 | int numberOfBitsPerSamplePoint; |
| 431 | while (fread (chunkID, 1, 4, f) == 4) { |
| 432 | integer chunkSize = bingeti32 (f); |
| 433 | if (chunkSize & 1) |
| 434 | ++ chunkSize; // round up to nearest even number |
| 435 | /* IN SOUND FILES PRODUCED BY THE SGI'S soundeditor PROGRAM, */ |
| 436 | /* THE COMMON CHUNK HAS A chunkSize OF 18 INSTEAD OF 38, */ |
| 437 | /* ALTHOUGH THE COMMON CHUNK CONTAINS */ |
| 438 | /* THE 20-BYTE SEQUENCE "\016not compressed\0". */ |
| 439 | /* START FIX OF FOREIGN BUG */ |
| 440 | if(strnequ(chunkID,"NONE",4)&& |
| 441 | (chunkSize==(14<<24)+('n'<<16)+('o'<<8)+'t'||chunkSize==('t'<<24)+('o'<<16)+('n'<<8)+14)) |
| 442 | {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;} |
| 443 | /* FINISH FIX OF FOREIGN BUG */ |
| 444 | if (strnequ (chunkID, "COMM", 4)) { |
| 445 | /* |
| 446 | Found a Common Chunk. |
| 447 | */ |
| 448 | commonChunkPresent = true; |
| 449 | *numberOfChannels = bingeti16 (f); |
| 450 | Melder_require (*numberOfChannels > 0, |
| 451 | U"Too few sound channels (", *numberOfChannels, U")."); |
| 452 | *numberOfSamples = bingeti32 (f); |
| 453 | Melder_require (*numberOfSamples > 0, |
| 454 | U"Too few samples ", *numberOfSamples, U")."); |
| 455 | numberOfBitsPerSamplePoint = bingeti16 (f); |
| 456 | Melder_require (numberOfBitsPerSamplePoint <= 32, |
| 457 | U"Too many bits per sample (", numberOfBitsPerSamplePoint, U"; the maximum is 32)."); |
| 458 | *encoding = |
| 459 | numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_BIG_ENDIAN : |
| 460 | numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_BIG_ENDIAN : |
| 461 | numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_BIG_ENDIAN : |
| 462 | Melder_LINEAR_8_SIGNED; |
| 463 | *sampleRate = bingetr80 (f); |
| 464 | Melder_require (*sampleRate > 0.0, |
| 465 | U"Wrong sampling frequency (", *sampleRate, U" Hz)."); |
| 466 | if (isAifc) { |
| 467 | /* |
| 468 | Read compression data; should be "NONE" or "sowt". |
| 469 | */ |
| 470 | Melder_require (fread (data, 1, 4, f) == 4, |
| 471 | U"File too small: no compression info."); |
| 472 | if (! strnequ (data, "NONE", 4) && ! strnequ (data, "sowt", 4)) { |
| 473 | data [4] = '\0'; |
| 474 | Melder_throw (U"Cannot read compressed AIFC files (compression type ", Melder_peek8to32_u (data), U")."); |
| 475 | } |
| 476 | if (strnequ (data, "sowt", 4)) |
| 477 | *encoding = |
| 478 | numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_LITTLE_ENDIAN : |
| 479 | numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_LITTLE_ENDIAN : |
| 480 | numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_LITTLE_ENDIAN : |
| 481 | Melder_LINEAR_8_SIGNED; |
| 482 | /* |
| 483 | Read rest of compression info. |
| 484 | */ |
| 485 | for (integer i = 23; i <= chunkSize; i ++) |
| 486 | Melder_require (fread (data, 1, 1, f) == 1, |
| 487 | U"File too small: expected chunk of ", chunkSize, U" bytes, but found ", i + 22, U"."); |
| 488 | } |
| 489 | } else if (strnequ (chunkID, "SSND", 4)) { |
| 490 | /* |
| 491 | Found a Data Chunk. |
| 492 | */ |
| 493 | dataChunkPresent = true; |
| 494 | *startOfData = ftell (f) + 8; // ignore "offset" (4 bytes) and "blocksize" (4 bytes) |
| 495 | if (commonChunkPresent) |
| 496 | break; // optimization: do not read whole data chunk if we have already read the common chunk |
| 497 | } else // ignore Version Chunk and unknown chunks |
| 498 | for (integer i = 1; i <= chunkSize; i ++) |
| 499 | Melder_require (fread (data, 1, 1, f) == 1, |
| 500 | U"File too small: expected ", chunkSize, U" bytes, but found ", i, U"."); |
| 501 | } |
| 502 | |
| 503 | Melder_require (commonChunkPresent, |
| 504 | U"Found no Common Chunk."); |
| 505 | Melder_require (dataChunkPresent, |
| 506 | U"Found no Data Chunk."); |
| 507 | } |
| 508 | |
| 509 | static void Melder_checkWavFile (FILE *f, integer *numberOfChannels, int *encoding, |
| 510 | double *sampleRate, integer *startOfData, integer *numberOfSamples) |
| 511 | { |
| 512 | char data [14], chunkID [4]; |
| 513 | bool formatChunkPresent = false, dataChunkPresent = false; |
| 514 | int numberOfBitsPerSamplePoint = -1; |
| 515 | uint32 dataChunkSize = 0xffffffff; |
| 516 | |
| 517 | Melder_require (fread (data, 1, 4, f) == 4, |
| 518 | U"File too small: no RIFF statement."); |
| 519 | Melder_require (strnequ (data, "RIFF", 4), |
| 520 | U"Not a WAV file (RIFF statement expected)."); |
| 521 | Melder_require (fread (data, 1, 4, f) == 4, |
| 522 | U"File too small: no size of RIFF chunk."); |
| 523 | Melder_require (fread (data, 1, 4, f) == 4, |
| 524 | U"File too small: no file type info (expected WAVE statement)."); |
| 525 | Melder_require (strnequ (data, "WAVE", 4) || strnequ (data, "CDDA", 4), |
| 526 | U"Not a WAVE or CD audio file (wrong file type info)."); |
| 527 | |
| 528 | /* |
| 529 | Search for Format Chunk and Data Chunk. |
| 530 | */ |
| 531 | while (fread (chunkID, 1, 4, f) == 4) { |
| 532 | uint32 chunkSize = bingetu32LE (f); |
| 533 | if (Melder_debug == 23) { |
| 534 | Melder_warning (chunkID [0], U" ", chunkID [1], U" ", |
| 535 | chunkID [2], U" ", chunkID [3], U" ", chunkSize); |
| 536 | } |
| 537 | if (strnequ (chunkID, "fmt ", 4)) { |
| 538 | /* |
| 539 | Found a Format Chunk. |
| 540 | */ |
| 541 | uint16 winEncoding = bingetu16LE (f); |
| 542 | formatChunkPresent = true; |
| 543 | *numberOfChannels = bingeti16LE (f); |
| 544 | Melder_require (*numberOfChannels >= 1, |
| 545 | U"Too few sound channels (", *numberOfChannels, U")."); |
| 546 | *sampleRate = (double) bingeti32LE (f); |
| 547 | Melder_require (*sampleRate > 0.0, |
| 548 | U"Wrong sampling frequency (", *sampleRate, U" Hz)."); |
| 549 | (void) bingeti32LE (f); // avgBytesPerSec |
| 550 | (void) bingeti16LE (f); // blockAlign |
| 551 | numberOfBitsPerSamplePoint = bingeti16LE (f); |
| 552 | if (numberOfBitsPerSamplePoint == 0) |
| 553 | numberOfBitsPerSamplePoint = 16; // the default |
| 554 | Melder_require (numberOfBitsPerSamplePoint >= 4, |
| 555 | U"Too few bits per sample (", numberOfBitsPerSamplePoint, U"; the minimum is 4)."); |
| 556 | Melder_require (numberOfBitsPerSamplePoint <= 64, |
| 557 | U"Too many bits per sample (", numberOfBitsPerSamplePoint, U"; the maximum is 64)."); |
| 558 | switch (winEncoding) { |
| 559 | case WAVE_FORMAT_PCM: |
| 560 | *encoding = |
| 561 | numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_LITTLE_ENDIAN : |
| 562 | numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_LITTLE_ENDIAN : |
| 563 | numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_LITTLE_ENDIAN : |
| 564 | Melder_LINEAR_8_UNSIGNED; |
| 565 | break; |
| 566 | case WAVE_FORMAT_IEEE_FLOAT: |
| 567 | *encoding = |
| 568 | numberOfBitsPerSamplePoint == 64 ? Melder_IEEE_FLOAT_64_LITTLE_ENDIAN : |
| 569 | Melder_IEEE_FLOAT_32_LITTLE_ENDIAN; |
| 570 | break; |
| 571 | case WAVE_FORMAT_ALAW: |
| 572 | *encoding = Melder_ALAW; |
| 573 | break; |
| 574 | case WAVE_FORMAT_MULAW: |
| 575 | *encoding = Melder_MULAW; |
| 576 | break; |
| 577 | case WAVE_FORMAT_DVI_ADPCM: |
| 578 | Melder_throw (U"Cannot read lossy compressed audio files (this one is DVI ADPCM).\n" |
| 579 | U"Please use uncompressed audio files. If you must open this file,\n" |
| 580 | U"please use an audio converter program to convert it first to normal (PCM) WAV format\n" |
| 581 | U"(Praat may have difficulty analysing the poor recording, though)."); |
| 582 | case WAVE_FORMAT_EXTENSIBLE: { |
| 583 | Melder_require (chunkSize >= 40, |
| 584 | U"Not enough format data in extensible WAV format"); |
| 585 | (void) bingeti16LE (f); // extensionSize |
| 586 | (void) bingeti16LE (f); // validBitsPerSample |
| 587 | (void) bingeti32LE (f); // channelMask |
| 588 | uint16 winEncoding2 = bingetu16LE (f); // override |
| 589 | switch (winEncoding2) { |
| 590 | case WAVE_FORMAT_PCM: |
| 591 | *encoding = |
| 592 | numberOfBitsPerSamplePoint > 24 ? Melder_LINEAR_32_LITTLE_ENDIAN : |
| 593 | numberOfBitsPerSamplePoint > 16 ? Melder_LINEAR_24_LITTLE_ENDIAN : |
| 594 | numberOfBitsPerSamplePoint > 8 ? Melder_LINEAR_16_LITTLE_ENDIAN : |
| 595 | Melder_LINEAR_8_UNSIGNED; |
| 596 | break; |
| 597 | case WAVE_FORMAT_IEEE_FLOAT: |
| 598 | *encoding = |
| 599 | numberOfBitsPerSamplePoint == 64 ? Melder_IEEE_FLOAT_64_LITTLE_ENDIAN : |
| 600 | Melder_IEEE_FLOAT_32_LITTLE_ENDIAN; |
| 601 | break; |
| 602 | case WAVE_FORMAT_ALAW: |
| 603 | *encoding = Melder_ALAW; |
| 604 | break; |
| 605 | case WAVE_FORMAT_MULAW: |
| 606 | *encoding = Melder_MULAW; |
| 607 | break; |
| 608 | case WAVE_FORMAT_DVI_ADPCM: |
| 609 | Melder_throw (U"Cannot read lossy compressed audio files (this one is DVI ADPCM).\n" |
| 610 | U"Please use uncompressed audio files. If you must open this file,\n" |
| 611 | U"please use an audio converter program to convert it first to normal (PCM) WAV format\n" |
| 612 | U"(Praat may have difficulty analysing the poor recording, though)."); |
| 613 | default: |
| 614 | Melder_throw (U"Unsupported Windows audio encoding ", winEncoding2, U"."); |
| 615 | } |
| 616 | Melder_require (fread (data, 1, 14, f) == 14, |
| 617 | U"File too small: no SubFormat data."); |
| 618 | continue; // next chunk |
| 619 | } |
| 620 | default: |
| 621 | Melder_throw (U"Unsupported Windows audio encoding ", winEncoding, U"."); |
| 622 | } |
| 623 | if (chunkSize & 1) |
| 624 | chunkSize += 1; |
| 625 | for (integer i = 17; i <= chunkSize; i ++) |
| 626 | Melder_require (fread (data, 1, 1, f) == 1, |
| 627 | U"File too small: expected ", chunkSize, U" bytes in fmt chunk, but found ", i, U"."); |
| 628 | } else if (strnequ (chunkID, "data", 4)) { |
| 629 | /* |
| 630 | Found a Data Chunk. |
| 631 | */ |
| 632 | dataChunkPresent = true; |
| 633 | dataChunkSize = chunkSize; |
| 634 | *startOfData = ftell (f); |
| 635 | if (chunkSize > UINT32_MAX - 100) { // incorrect data chunk (sometimes -1 or -44); assume that the data run till the end of the file |
| 636 | fseeko (f, 0LL, SEEK_END); |
| 637 | off_t endOfData = ftello (f); |
| 638 | dataChunkSize = chunkSize = endOfData - *startOfData; |
| 639 | fseeko (f, *startOfData, SEEK_SET); |
| 640 | } |
| 641 | /* |
| 642 | ppgb 20191211: |
| 643 | The following check had to be moved here (from six lines up), |
| 644 | because the original chunkSize could have been 0xffffffff, |
| 645 | leading to it becoming 0 here and skipping the above block, |
| 646 | thus violating the assert on dataChunkSize below. |
| 647 | */ |
| 648 | if (chunkSize & 1) |
| 649 | chunkSize += 1; |
| 650 | if (Melder_debug == 23) { |
| 651 | for (integer i = 1; i <= chunkSize; i ++) |
| 652 | Melder_require (fread (data, 1, 1, f) == 1, |
| 653 | U"File too small: expected ", chunkSize, U" bytes of data, but found ", i, U"."); |
| 654 | } else { |
| 655 | if (formatChunkPresent) |
| 656 | break; // OPTIMIZE: do not read the whole data chunk if we have already read the format chunk |
| 657 | } |
| 658 | } else { // ignore other chunks |
| 659 | if (chunkSize & 1) |
| 660 | chunkSize += 1; |
| 661 | for (integer i = 1; i <= chunkSize; i ++) |
| 662 | Melder_require (fread (data, 1, 1, f) == 1, |
| 663 | U"File too small: expected ", chunkSize, U" bytes, but found ", i, U"."); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | Melder_require (formatChunkPresent, |
| 668 | U"Found no Format Chunk."); |
| 669 | Melder_require (dataChunkPresent, |
| 670 | U"Found no Data Chunk."); |
| 671 | Melder_assert (numberOfBitsPerSamplePoint != -1); |
| 672 | Melder_assert (dataChunkSize != 0xffffffff); |
| 673 | *numberOfSamples = dataChunkSize / *numberOfChannels / ((numberOfBitsPerSamplePoint + 7) / 8); |
| 674 | } |
| 675 | |
| 676 | static void Melder_checkNextSunFile (FILE *f, integer *numberOfChannels, int *encoding, |
| 677 | double *sampleRate, integer *startOfData, integer *numberOfSamples) |
| 678 | { |
| 679 | char tag [4]; |
| 680 | fread (tag, 1, 4, f); |
| 681 | if (strncmp (tag, ".snd", 4)) Melder_throw (U"Not a Sun audio file."); |
| 682 | *startOfData = bingeti32 (f); |
| 683 | if (*startOfData < 24 || *startOfData > 320) |
| 684 | Melder_throw (U"Cannot read header of audio file. Length ", *startOfData, U"."); |
| 685 | integer dataSize = bingeti32 (f); |
| 686 | if (dataSize <= 0) { |
| 687 | /* |
| 688 | * Incorrect information. Get it from file length. |
| 689 | */ |
| 690 | integer save = ftell (f); |
| 691 | fseek (f, 0, SEEK_END); |
| 692 | dataSize = ftell (f) - *startOfData; |
| 693 | fseek (f, save, SEEK_SET); |
| 694 | } |
| 695 | integer sunEncoding = bingeti32 (f); |
| 696 | switch (sunEncoding) { |
| 697 | case 1: *encoding = Melder_MULAW; break; |
| 698 | case 2: *encoding = Melder_LINEAR_8_SIGNED; break; |
| 699 | case 3: *encoding = Melder_LINEAR_16_BIG_ENDIAN; break; |
| 700 | case 27: *encoding = Melder_ALAW; break; |
| 701 | default: Melder_throw (U"Cannot translate audio file encoding ", sunEncoding, U"."); |
| 702 | } |
| 703 | *sampleRate = bingeti32 (f); |
| 704 | if (*sampleRate <= 0) Melder_throw (U"Impossible sampling frequency ", *sampleRate, U" Hz."); |
| 705 | *numberOfChannels = bingeti32 (f); |
| 706 | if (*numberOfChannels < 1) |
| 707 | Melder_throw (U"Wrong number of channels in audio file (", *numberOfChannels, U")."); |
| 708 | *numberOfSamples = dataSize / Melder_bytesPerSamplePoint (*encoding) / *numberOfChannels; |
| 709 | integer skip = *startOfData - 24; |
| 710 | while (skip -- > 0) (void) fgetc (f); |
| 711 | } |
| 712 | |
| 713 | static int nistGetValue (const char *header, const char *object, double *rval, char *sval) { |
| 714 | char obj [30], type [10]; |
| 715 | const char *match = strstr (header, object); |
| 716 | if (! match) |
| 717 | return 0; |
| 718 | if (sscanf (match, "%29s%9s%99s", obj, type, sval) != 3) |
| 719 | return 0; |
| 720 | if (strequ (type, "-i") || strequ (type, "-r")) |
| 721 | *rval = atof (sval); |
| 722 | else if (strncmp (type, "-s", 2)) |
| 723 | return 0; |
| 724 | return 1; |
| 725 | } |
| 726 | static void Melder_checkNistFile (FILE *f, integer *numberOfChannels, int *encoding, |
| 727 | double *sampleRate, integer *startOfData, integer *numberOfSamples) |
| 728 | { |
| 729 | char header [2048+1], sval [100]; |
| 730 | double rval = 0.0; |
| 731 | int numberOfBytesPerSamplePoint; |
| 732 | if (fread (header, 1, 1024, f) != 1024) |
| 733 | Melder_throw (U"Cannot read NISTheader."); |
| 734 | if (! strnequ (header, "NIST_1A", 7)) |
| 735 | Melder_throw (U"Not a NIST sound file."); |
| 736 | if (strnequ (header + 7, "\n 1024", 8)) { |
| 737 | header [1024] = '\0'; |
| 738 | } else if (strnequ (header + 7, "\n 2048", 8)) { |
| 739 | if (fread (header + 1024, 1, 1024, f) != 1024) |
| 740 | Melder_throw (U"Cannot read longer NISTheader."); |
| 741 | header [2048] = '\0'; |
| 742 | } else |
| 743 | Melder_throw (U"Incomplete or too long NISTheader."); |
| 744 | *startOfData = atol (header + 9); |
| 745 | if (! nistGetValue (header, "sample_count", & rval, sval) || rval < 1.0) |
| 746 | Melder_throw (U"Incorrect number of samples in NIST file."); |
| 747 | *numberOfSamples = Melder_iround (rval); |
| 748 | if (! nistGetValue (header, "sample_n_bytes", & rval, sval) || rval < 1.0 || rval > 2.0) |
| 749 | Melder_throw (U"Incorrect number of bytes per sample (should be 1 or 2)."); |
| 750 | numberOfBytesPerSamplePoint = Melder_iround (rval); |
| 751 | if (! nistGetValue (header, "channel_count", & rval, sval) || rval < 1.0) |
| 752 | Melder_throw (U"Incorrect number of channels."); |
| 753 | *numberOfChannels = Melder_iround (rval); |
| 754 | if (! nistGetValue (header, "sample_rate", sampleRate, sval) || *sampleRate < 1) |
| 755 | Melder_throw (U"Incorrect sampling frequency ", *sampleRate, U" Hz."); |
| 756 | *encoding = Melder_LINEAR_16_BIG_ENDIAN; |
| 757 | if (nistGetValue (header, "sample_byte_format", & rval, sval) && strequ (sval, "01")) |
| 758 | *encoding = Melder_LINEAR_16_LITTLE_ENDIAN; |
| 759 | if (numberOfBytesPerSamplePoint == 1) |
| 760 | *encoding = Melder_LINEAR_8_SIGNED; |
| 761 | if (nistGetValue (header, "sample_coding", & rval, sval)) { |
| 762 | if (strnequ (sval, "ulaw", 4)) |
| 763 | *encoding = Melder_MULAW; |
| 764 | else if (strstr (sval, "embedded-shorten-v")) |
| 765 | if (nistGetValue (header, "database_id", & rval, sval) && strequ (sval, "POLYPHONE-NL")) |
| 766 | *encoding = Melder_POLYPHONE; |
| 767 | else |
| 768 | *encoding = Melder_SHORTEN; |
| 769 | else if (strnequ (sval, "alaw", 4)) // must be after previous, because some files have "alaw,embedded..." |
| 770 | *encoding = Melder_ALAW; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | static void Melder_checkFlacFile (MelderFile file, integer *numberOfChannels_out, int *encoding_out, |
| 775 | double *sampleRate_out, integer *startOfData_out, integer *numberOfSamples_out) |
| 776 | { |
| 777 | const conststring8 fileName_utf8 = MelderFile_peekPath8 (file); |
| 778 | FLAC__StreamMetadata metadata; |
| 779 | if (! FLAC__metadata_get_streaminfo (fileName_utf8, & metadata)) // Unicode-savvy (test/fon/soundFiles.praat 2024-08-11) |
| 780 | Melder_throw (U"Invalid FLAC file"); |
| 781 | FLAC__StreamMetadata_StreamInfo *info = & metadata. data. stream_info; |
| 782 | |
| 783 | /* |
| 784 | Get and check number of channels. |
| 785 | */ |
| 786 | const uint32 numberOfChannels_uint32 = info -> channels; // no conversion |
| 787 | if (sizeof (integer) == 4) |
| 788 | Melder_require (numberOfChannels_uint32 <= (uint32) INT32_MAX, |
| 789 | U"FLAC file has too many channels (", Melder_bigInteger (numberOfChannels_uint32), U") for 32-bit Praat."); |
| 790 | *numberOfChannels_out = (integer) numberOfChannels_uint32; // guarded conversion down (on 32-bit) |
| 791 | |
| 792 | *encoding_out = Melder_FLAC_COMPRESSION_16; |
| 793 | |
| 794 | /* |
| 795 | Get and check sampling frequency. |
| 796 | */ |
| 797 | const uint32 samplingFrequency_uint32 = info -> sample_rate; // no conversion |
| 798 | Melder_require (samplingFrequency_uint32 > 0, |
| 799 | U"FLAC file cannot have a sampling frequency of 0 Hz."); |
| 800 | *sampleRate_out = (double) samplingFrequency_uint32; // safe conversion from uint32 to 54-bit-mantissa float |
| 801 | |
| 802 | *startOfData_out = 0; // meaningless: libFLAC does the I/O |
| 803 | |
| 804 | /* |
| 805 | Get and check number of samples. |
| 806 | */ |
| 807 | const uint64 numberOfSamples_uint64 = info -> total_samples; // no conversion |
| 808 | Melder_require (numberOfSamples_uint64 <= (uint64) INT64_MAX, |
| 809 | U"FLAC file claims that it contains more than ", Melder_bigInteger (INT64_MAX), U" samples; probably a mistake."); |
| 810 | const int64 numberOfSamples_int64 = (int64) numberOfSamples_uint64; // guarded conversion down |
| 811 | if (sizeof (integer) == 4) |
| 812 | Melder_require (numberOfSamples_int64 <= (int64) INT32_MAX, |
| 813 | U"FLAC file too long for 32-bit Praat, namely ", Melder_bigInteger (numberOfSamples_int64), U" samples." |
| 814 | "You may want to try a 64-bit edition of Praat." |
| 815 | ); |
| 816 | *numberOfSamples_out = (integer) numberOfSamples_int64; // guarded conversion down |
| 817 | } |
| 818 | |
| 819 | static void Melder_checkMp3File (FILE *f, integer *numberOfChannels, int *encoding, |
| 820 | double *sampleRate, integer *startOfData, integer *numberOfSamples) |
| 821 | { |
| 822 | MP3_FILE mp3f = mp3f_new (); |
| 823 | mp3f_set_file (mp3f, f); |
| 824 | if (! mp3f_analyze (mp3f)) { |
| 825 | mp3f_delete (mp3f); |
| 826 | Melder_throw (U"Cannot analyze MP3 file"); |
| 827 | } |
| 828 | *encoding = Melder_MPEG_COMPRESSION_16; |
| 829 | *numberOfChannels = mp3f_channels (mp3f); |
| 830 | *sampleRate = mp3f_frequency (mp3f); |
| 831 | *numberOfSamples = mp3f_samples (mp3f); |
| 832 | if ((MP3F_OFFSET)*numberOfSamples != mp3f_samples (mp3f)) // BUG: loses bits above INT32_MAX |
| 833 | Melder_throw (U"MP3 file too long."); |
| 834 | *startOfData = 0; // meaningless |
| 835 | mp3f_delete (mp3f); |
| 836 | } |
| 837 | |
| 838 | int MelderFile_checkSoundFile (MelderFile file, integer *numberOfChannels, int *encoding, |
| 839 | double *sampleRate, integer *startOfData, integer *numberOfSamples) |
| 840 | { |
| 841 | char data [16]; |
| 842 | FILE *f = file -> filePointer; |
| 843 | if (! f || fread (data, 1, 16, f) < 16) return 0; |
| 844 | rewind (f); |
| 845 | if (strnequ (data, "FORM", 4) && strnequ (data + 8, "AIFF", 4)) { |
| 846 | Melder_checkAiffFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples); |
| 847 | return Melder_AIFF; |
| 848 | } |
| 849 | if (strnequ (data, "FORM", 4) && strnequ (data + 8, "AIFC", 4)) { |
| 850 | Melder_checkAiffFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples); |
| 851 | return Melder_AIFC; |
| 852 | } |
| 853 | if (strnequ (data, "RIFF", 4) && (strnequ (data + 8, "WAVE", 4) || strnequ (data + 8, "CDDA", 4))) { |
| 854 | Melder_checkWavFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples); |
| 855 | return Melder_WAV; |
| 856 | } |
| 857 | if (strnequ (data, ".snd", 4)) { |
| 858 | Melder_checkNextSunFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples); |
| 859 | return Melder_NEXT_SUN; |
| 860 | } |
| 861 | if (strnequ (data, "NIST_1A", 7)) { |
| 862 | Melder_checkNistFile (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples); |
| 863 | return Melder_NIST; |
| 864 | } |
| 865 | if (strnequ (data, "fLaC", 4)) { |
| 866 | Melder_checkFlacFile (file, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples); |
| 867 | return Melder_FLAC; |
| 868 | } |
| 869 | if (mp3_recognize (16, data)) { |
| 870 | Melder_checkMp3File (f, numberOfChannels, encoding, sampleRate, startOfData, numberOfSamples); |
| 871 | return Melder_MP3; |
| 872 | } |
| 873 | return 0; // not a recognized sound file |
| 874 | } |
| 875 | |
| 876 | /* libFLAC works through callbacks, so we need a context struct. */ |
| 877 | |
| 878 | typedef struct { |
| 879 | FILE *file; |
| 880 | integer numberOfChannels; |
| 881 | integer numberOfSamples; |
| 882 | double *channels [FLAC__MAX_CHANNELS]; |
| 883 | } MelderDecodeFlacContext; |
| 884 | |
| 885 | /* The same goes for MP3 */ |
| 886 | |
| 887 | typedef struct { |
| 888 | integer numberOfChannels; |
| 889 | integer numberOfSamples; |
| 890 | double *channels [2]; |
| 891 | } MelderDecodeMp3Context; |
| 892 | |
| 893 | static FLAC__StreamDecoderReadStatus Melder_DecodeFlac_read (const FLAC__StreamDecoder *decoder, |
| 894 | FLAC__byte buffer [], size_t *bytes, void *client_data) |
| 895 | { |
| 896 | MelderDecodeFlacContext *c = (MelderDecodeFlacContext *) client_data; |
| 897 | (void) decoder; |
| 898 | if (*bytes <= 0) |
| 899 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 900 | *bytes = fread (buffer, sizeof (FLAC__byte), *bytes, c -> file); |
| 901 | if (ferror (c -> file)) |
| 902 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 903 | if (*bytes == 0) |
| 904 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 905 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 906 | } |
| 907 | |
| 908 | static FLAC__StreamDecoderWriteStatus Melder_DecodeFlac_convert (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data) |
| 909 | { |
| 910 | MelderDecodeFlacContext *c = (MelderDecodeFlacContext *) client_data; |
| 911 | const FLAC__FrameHeader *header = & frame -> header; |
| 912 | integer count = header -> blocksize; |
| 913 | double multiplier; |
| 914 | |
| 915 | (void) decoder; |
| 916 | |
| 917 | switch (header -> bits_per_sample) { |
| 918 | case 8: multiplier = (1.0f / 128); break; |
| 919 | case 16: multiplier = (1.0f / 32768); break; |
| 920 | case 24: multiplier = (1.0f / 8388608); break; |
| 921 | case 32: multiplier = (1.0f / 32768 / 65536); break; |
| 922 | default: return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 923 | } |
| 924 | |
| 925 | for (integer i = 0; i < c -> numberOfChannels; ++ i) { |
| 926 | const FLAC__int32 *input = buffer [i]; |
| 927 | double *output = c -> channels [i]; |
| 928 | for (integer j = 0; j < count; ++ j) |
| 929 | output [j] = ((integer) input [j]) * multiplier; |
| 930 | c -> channels [i] += count; |
| 931 | } |
| 932 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 933 | } |
| 934 | |
| 935 | static void Melder_DecodeMp3_convert (const MP3F_SAMPLE *channels [MP3F_MAX_CHANNELS], integer count, void *context) { |
| 936 | MelderDecodeMp3Context *c = (MelderDecodeMp3Context *) context; |
| 937 | const MP3F_SAMPLE *input; |
| 938 | double *output; |
| 939 | for (integer i = 0; i < c -> numberOfChannels; ++ i) { |
| 940 | input = channels [i]; |
| 941 | output = c -> channels [i]; |
| 942 | for (integer j = 0; j < count; ++ j) |
| 943 | output [j] = mp3f_sample_to_float (input [j]); |
| 944 | c -> channels [i] += count; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | static void Melder_DecodeFlac_error (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) { |
| 949 | (void) decoder; |
| 950 | (void) client_data; |
| 951 | Melder_warning (U"FLAC decoder error: ", Melder_peek8to32_u (FLAC__StreamDecoderErrorStatusString [status])); |
| 952 | } |
| 953 | |
| 954 | static void Melder_readFlacFile (FILE *f, MAT buffer) { |
| 955 | int result = 0; |
| 956 | |
| 957 | MelderDecodeFlacContext c; |
| 958 | c.file = f; |
| 959 | c.numberOfChannels = buffer.nrow; |
| 960 | for (int ichan = 1; ichan <= buffer.nrow; ichan ++) |
| 961 | c.channels [ichan - 1] = & buffer [ichan] [1]; |
| 962 | c.numberOfSamples = buffer.ncol; |
| 963 | |
| 964 | FLAC__StreamDecoder *decoder = FLAC__stream_decoder_new (); |
| 965 | if (! decoder) { |
| 966 | Melder_appendError (U"Couldn’t create FLAC decoder."); |
| 967 | goto end; |
| 968 | } |
| 969 | if (FLAC__stream_decoder_init_stream (decoder, |
| 970 | Melder_DecodeFlac_read, |
| 971 | nullptr, nullptr, nullptr, nullptr, |
| 972 | Melder_DecodeFlac_convert, nullptr, |
| 973 | Melder_DecodeFlac_error, & c) != FLAC__STREAM_DECODER_INIT_STATUS_OK) |
| 974 | { |
| 975 | Melder_appendError (U"Couldn’t initialize FLAC decoder."); |
| 976 | goto end; |
| 977 | } |
| 978 | result = FLAC__stream_decoder_process_until_end_of_stream (decoder); |
| 979 | FLAC__stream_decoder_finish (decoder); |
| 980 | end: |
| 981 | if (decoder) |
| 982 | FLAC__stream_decoder_delete (decoder); |
| 983 | if (result == 0) |
| 984 | Melder_throw (U"Error decoding FLAC file."); |
| 985 | } |
| 986 | |
| 987 | static void Melder_readMp3File (FILE *f, MAT buffer) { |
| 988 | int result = 0; |
| 989 | MelderDecodeMp3Context c; |
| 990 | c.numberOfChannels = buffer.nrow; |
| 991 | for (int ichan = 1; ichan <= buffer.nrow; ichan ++) |
| 992 | c.channels [ichan - 1] = & buffer [ichan] [1]; |
| 993 | c.numberOfSamples = buffer.ncol; |
| 994 | MP3_FILE mp3f = mp3f_new (); |
| 995 | mp3f_set_file (mp3f, f); |
| 996 | mp3f_set_callback (mp3f, Melder_DecodeMp3_convert, & c); |
| 997 | result = mp3f_read (mp3f, buffer.ncol); |
| 998 | mp3f_delete (mp3f); |
| 999 | if (result == 0) |
| 1000 | Melder_throw (U"Error decoding MP3 file."); |
| 1001 | } |
| 1002 | |
| 1003 | static int bitsInReadBuffer = 0; |
| 1004 | static uint32 readOneBit (FILE *f) { |
| 1005 | static uint8 readBuffer; |
| 1006 | if (bitsInReadBuffer == 0) { |
| 1007 | int externalValue = fgetc (f); |
| 1008 | Melder_require (externalValue >= 0, |
| 1009 | U"Could not read a bit."); |
| 1010 | readBuffer = (uint8) externalValue; |
| 1011 | bitsInReadBuffer = 8; |
| 1012 | } |
| 1013 | return (readBuffer >> -- bitsInReadBuffer) & 1; |
| 1014 | } |
| 1015 | |
| 1016 | static uint32 bingetu32_shortened_direct (FILE *f, const uint32 mantissaLength) { |
| 1017 | //TRACE |
| 1018 | uint32 numberOfLeadingZeroes = 0; |
| 1019 | for (; readOneBit (f) == 0; numberOfLeadingZeroes ++) |
| 1020 | ; |
| 1021 | uint32 result = numberOfLeadingZeroes * (1u << mantissaLength); |
| 1022 | for (uint32 i = 1; i <= mantissaLength; i ++) |
| 1023 | if (readOneBit (f) != 0) |
| 1024 | result += 1u << (mantissaLength - i); |
| 1025 | trace (numberOfLeadingZeroes, U" ", mantissaLength); |
| 1026 | return result; |
| 1027 | } |
| 1028 | |
| 1029 | static uint32 bingetu32_shortened_indirect (FILE *stream) { |
| 1030 | const uint32 mantissaLength = bingetu32_shortened_direct (stream, 2); |
| 1031 | return bingetu32_shortened_direct (stream, mantissaLength); |
| 1032 | } |
| 1033 | |
| 1034 | static int32 bingeti32_shortened_direct (FILE *f, const uint32 mantissaLength) { |
| 1035 | uint32 unsignedVersion = bingetu32_shortened_direct (f, mantissaLength + 1); // a sign bit at the end |
| 1036 | uint32 signBit = unsignedVersion & 1u; |
| 1037 | uint32 absoluteValue = unsignedVersion >> 1; |
| 1038 | return signBit ? (int32) ~ absoluteValue: (int32) absoluteValue; // two's complement |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | The table `alawBitshiftTable` was created by the following script (last checked 2024-12-03): |
| 1043 | |
| 1044 | sortedAlawValues# = { |
| 1045 | ... -32256, -31232, -30208, -29184, -28160, -27136, -26112, -25088, |
| 1046 | ... -24064, -23040, -22016, -20992, -19968, -18944, -17920, -16896, |
| 1047 | ... -16128, -15616, -15104, -14592, -14080, -13568, -13056, -12544, |
| 1048 | ... -12032, -11520, -11008, -10496, -9984, -9472, -8960, -8448, |
| 1049 | ... -8064, -7808, -7552, -7296, -7040, -6784, -6528, -6272, |
| 1050 | ... -6016, -5760, -5504, -5248, -4992, -4736, -4480, -4224, |
| 1051 | ... -4032, -3904, -3776, -3648, -3520, -3392, -3264, -3136, |
| 1052 | ... -3008, -2880, -2752, -2624, -2496, -2368, -2240, -2112, |
| 1053 | ... -2016, -1952, -1888, -1824, -1760, -1696, -1632, -1568, |
| 1054 | ... -1504, -1440, -1376, -1312, -1248, -1184, -1120, -1056, |
| 1055 | ... -1008, -976, -944, -912, -880, -848, -816, -784, |
| 1056 | ... -752, -720, -688, -656, -624, -592, -560, -528, |
| 1057 | ... -504, -488, -472, -456, -440, -424, -408, -392, |
| 1058 | ... -376, -360, -344, -328, -312, -296, -280, -264, |
| 1059 | ... -248, -232, -216, -200, -184, -168, -152, -136, |
| 1060 | ... -120, -104, -88, -72, -56, -40, -24, -8, |
| 1061 | ... 8, 24, 40, 56, 72, 88, 104, 120, |
| 1062 | ... 136, 152, 168, 184, 200, 216, 232, 248, |
| 1063 | ... 264, 280, 296, 312, 328, 344, 360, 376, |
| 1064 | ... 392, 408, 424, 440, 456, 472, 488, 504, |
| 1065 | ... 528, 560, 592, 624, 656, 688, 720, 752, |
| 1066 | ... 784, 816, 848, 880, 912, 944, 976, 1008, |
| 1067 | ... 1056, 1120, 1184, 1248, 1312, 1376, 1440, 1504, |
| 1068 | ... 1568, 1632, 1696, 1760, 1824, 1888, 1952, 2016, |
| 1069 | ... 2112, 2240, 2368, 2496, 2624, 2752, 2880, 3008, |
| 1070 | ... 3136, 3264, 3392, 3520, 3648, 3776, 3904, 4032, |
| 1071 | ... 4224, 4480, 4736, 4992, 5248, 5504, 5760, 6016, |
| 1072 | ... 6272, 6528, 6784, 7040, 7296, 7552, 7808, 8064, |
| 1073 | ... 8448, 8960, 9472, 9984, 10496, 11008, 11520, 12032, |
| 1074 | ... 12544, 13056, 13568, 14080, 14592, 15104, 15616, 16128, |
| 1075 | ... 16896, 17920, 18944, 19968, 20992, 22016, 23040, 24064, |
| 1076 | ... 25088, 26112, 27136, 28160, 29184, 30208, 31232, 32256 |
| 1077 | ...} |
| 1078 | |
| 1079 | procedure convert: .bitshift, .value |
| 1080 | .value -= 2 ^ (.bitshift + 2) |
| 1081 | for .lookup to 256 |
| 1082 | if sortedAlawValues# [.lookup] >= .value |
| 1083 | goto .found |
| 1084 | endif |
| 1085 | endfor |
| 1086 | label .found |
| 1087 | .result = .lookup |
| 1088 | endproc |
| 1089 | procedure convertAndCheck: .bitshift, .value, .target |
| 1090 | @convert: .bitshift, .value |
| 1091 | if sortedAlawValues# [convert.result] <> .target |
| 1092 | appendInfoLine: sortedAlawValues# [convert.result], " ", .target |
| 1093 | endif |
| 1094 | endproc |
| 1095 | |
| 1096 | # |
| 1097 | # Tests of known conversions appearing in the Polyphone database. |
| 1098 | # |
| 1099 | @convertAndCheck: 2, 2*24, 40 |
| 1100 | @convertAndCheck: 2, 2*40, 72 |
| 1101 | @convertAndCheck: 5, 128, 8 |
| 1102 | @convertAndCheck: 5, 1408, 1312 |
| 1103 | @convertAndCheck: 5, 1152, 1056 |
| 1104 | @convertAndCheck: 5, -3456, -3520 |
| 1105 | @convertAndCheck: 5, -2431, -2496 |
| 1106 | @convertAndCheck: 5, -3968, -4032 |
| 1107 | @convertAndCheck: 5, -3712, -3776 |
| 1108 | @convertAndCheck: 7, 1536, 1056 |
| 1109 | @convertAndCheck: 7, -2560, -3008 |
| 1110 | @convertAndCheck: 7, -6656, -7040 |
| 1111 | @convertAndCheck: 7, -8704, -8960 |
| 1112 | @convertAndCheck: 7, -10752, -11008 |
| 1113 | @convertAndCheck: 7, -12800, -13056 |
| 1114 | @convertAndCheck: 7, -14848, -15104 |
| 1115 | @convertAndCheck: 8, -5120, -6016 |
| 1116 | @convertAndCheck: 8, -9216, -9984 |
| 1117 | @convertAndCheck: 9, -10240, -12032 |
| 1118 | @convertAndCheck: 10, -4096, -8064 |
| 1119 | @convertAndCheck: 11, -8192, -16128 |
| 1120 | @convertAndCheck: 12, -16384, -32256 |
| 1121 | @convertAndCheck: 12, 16384, 8 |
| 1122 | |
| 1123 | shifted# = zero# (256) |
| 1124 | for bitshift from 1 to 12 |
| 1125 | for i to 256 |
| 1126 | shifted# [i] = -1 |
| 1127 | linearValue = sortedAlawValues# [i] |
| 1128 | # |
| 1129 | # First approximation: bitshift the linear value. |
| 1130 | # |
| 1131 | shiftedLinearValue = linearValue * 2 ^ (bitshift - 1) |
| 1132 | # |
| 1133 | # We have to round to a linear value that corresponds to an actual A-law value. |
| 1134 | # The direction of rounding is down. We first go down by a full step. |
| 1135 | # |
| 1136 | shiftedLinearValue -= 2 ^ (bitshift + 2) |
| 1137 | # |
| 1138 | # We then go up until we pass the current value. |
| 1139 | # |
| 1140 | for lookup from 1 to 256 |
| 1141 | if shiftedLinearValue <= sortedAlawValues# [lookup] |
| 1142 | shifted# [i] = lookup - 1 |
| 1143 | if i > 1 and shifted# [i] = 0 |
| 1144 | shifted# [i - 1] = -1 ; accept only one zero in each table |
| 1145 | endif |
| 1146 | goto found |
| 1147 | endif |
| 1148 | endfor |
| 1149 | label found |
| 1150 | endfor |
| 1151 | appendInfoLine: "Bitshift ", bitshift, ":" |
| 1152 | @writeTable: shifted# |
| 1153 | endfor |
| 1154 | |
| 1155 | procedure writeTable: .table# |
| 1156 | maximum = max (.table#) |
| 1157 | minimum = min (.table#) |
| 1158 | maximum$ = string$ (maximum) |
| 1159 | minimum$ = string$ (minimum) |
| 1160 | maximumTextLength = max (length (maximum$), length (minimum$)) |
| 1161 | appendInfo: "{" |
| 1162 | for i to 256 |
| 1163 | if i mod 16 = 1 |
| 1164 | appendInfo: newline$, tab$ |
| 1165 | endif |
| 1166 | appendInfo: padLeft$ (string$ (.table# [i]), maximumTextLength + 1) |
| 1167 | if i <> 256 |
| 1168 | appendInfo: "," |
| 1169 | endif |
| 1170 | if i mod 4 = 0 and i mod 16 <> 0 |
| 1171 | appendInfo: " " |
| 1172 | endif |
| 1173 | if i mod 64 = 0 |
| 1174 | appendInfo: newline$ |
| 1175 | endif |
| 1176 | endfor |
| 1177 | appendInfoLine: "}" |
| 1178 | endproc |
| 1179 | */ |
| 1180 | static int alawBitshiftTable [12] [256] = { |
| 1181 | { // 1 |
| 1182 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1183 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 1184 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 1185 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 1186 | |
| 1187 | 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 1188 | 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, |
| 1189 | 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, |
| 1190 | 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, |
| 1191 | |
| 1192 | 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, |
| 1193 | 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, |
| 1194 | 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, |
| 1195 | 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 1196 | |
| 1197 | 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, |
| 1198 | 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, |
| 1199 | 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, |
| 1200 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 |
| 1201 | }, |
| 1202 | { // 2 |
| 1203 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1204 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1205 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 1206 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 1207 | |
| 1208 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 1209 | 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 1210 | 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, |
| 1211 | 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, |
| 1212 | |
| 1213 | 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, |
| 1214 | 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, |
| 1215 | 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 1216 | 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, |
| 1217 | |
| 1218 | 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, |
| 1219 | 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, |
| 1220 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, |
| 1221 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1222 | }, |
| 1223 | { // 3 |
| 1224 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1225 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1226 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1227 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 1228 | |
| 1229 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 1230 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 1231 | 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 1232 | 80, 82, 84, 86, 88, 90, 92, 94, 96, 100, 104, 108, 112, 116, 120, 124, |
| 1233 | |
| 1234 | 128, 132, 136, 140, 144, 148, 152, 156, 160, 162, 164, 166, 168, 170, 172, 174, |
| 1235 | 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 1236 | 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, |
| 1237 | 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, |
| 1238 | |
| 1239 | 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, |
| 1240 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, |
| 1241 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1242 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1243 | }, |
| 1244 | { // 4 |
| 1245 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1246 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1247 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1248 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1249 | |
| 1250 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 1251 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 1252 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 1253 | 64, 66, 68, 70, 72, 74, 76, 78, 80, 84, 88, 92, 96, 104, 112, 120, |
| 1254 | |
| 1255 | 128, 136, 144, 152, 160, 164, 168, 172, 176, 178, 180, 182, 184, 186, 188, 190, |
| 1256 | 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, |
| 1257 | 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, |
| 1258 | 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, |
| 1259 | |
| 1260 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, |
| 1261 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1262 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1263 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1264 | }, |
| 1265 | { // 5 |
| 1266 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1267 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1268 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1269 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1270 | |
| 1271 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1272 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 1273 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 1274 | 48, 50, 52, 54, 56, 58, 60, 62, 64, 68, 72, 76, 80, 88, 96, 112, |
| 1275 | |
| 1276 | 128, 144, 160, 168, 176, 180, 184, 188, 192, 194, 196, 198, 200, 202, 204, 206, |
| 1277 | 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, |
| 1278 | 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, |
| 1279 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, |
| 1280 | |
| 1281 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1282 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1283 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1284 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1285 | }, |
| 1286 | { // 6 |
| 1287 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1288 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1289 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1290 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1291 | |
| 1292 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1293 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1294 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 1295 | 32, 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64, 72, 80, 96, |
| 1296 | |
| 1297 | 128, 160, 176, 184, 192, 196, 200, 204, 208, 210, 212, 214, 216, 218, 220, 222, |
| 1298 | 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, |
| 1299 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, |
| 1300 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1301 | |
| 1302 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1303 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1304 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1305 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1306 | }, |
| 1307 | { // 7 |
| 1308 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1309 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1310 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1311 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1312 | |
| 1313 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1314 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1315 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1316 | 16, 18, 20, 22, 24, 26, 28, 30, 32, 36, 40, 44, 48, 56, 64, 80, |
| 1317 | |
| 1318 | 128, 176, 192, 200, 208, 212, 216, 220, 224, 226, 228, 230, 232, 234, 236, 238, |
| 1319 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, |
| 1320 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1321 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1322 | |
| 1323 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1324 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1325 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1326 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1327 | }, |
| 1328 | { // 8 |
| 1329 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1330 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1331 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1332 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1333 | |
| 1334 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1335 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1336 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1337 | 0, 2, 4, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 64, |
| 1338 | |
| 1339 | 128, 192, 208, 216, 224, 228, 232, 236, 240, 242, 244, 246, 248, 250, 252, 254, |
| 1340 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1341 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1342 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1343 | |
| 1344 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1345 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1346 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1347 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1348 | }, |
| 1349 | { // 9 |
| 1350 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1351 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1352 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1353 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1354 | |
| 1355 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1356 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1357 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1358 | -1, -1, -1, -1, -1, -1, -1, -1, 0, 4, 8, 12, 16, 24, 32, 48, |
| 1359 | |
| 1360 | 128, 208, 224, 232, 240, 244, 248, 252, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1361 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1362 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1363 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1364 | |
| 1365 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1366 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1367 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1368 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1369 | }, |
| 1370 | { // 10 |
| 1371 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1372 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1373 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1374 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1375 | |
| 1376 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1377 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1378 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1379 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 8, 16, 32, |
| 1380 | |
| 1381 | 128, 224, 240, 248, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1382 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1383 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1384 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1385 | |
| 1386 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1387 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1388 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1389 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1390 | }, |
| 1391 | { // 11 |
| 1392 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1393 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1394 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1395 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1396 | |
| 1397 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1398 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1399 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1400 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 16, |
| 1401 | |
| 1402 | 128, 240, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1403 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1404 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1405 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1406 | |
| 1407 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1408 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1409 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1410 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1411 | }, |
| 1412 | { // 12 |
| 1413 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1414 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1415 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1416 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1417 | |
| 1418 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1419 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1420 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1421 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, |
| 1422 | |
| 1423 | 128, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1424 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1425 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1426 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1427 | |
| 1428 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1429 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1430 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 1431 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 1432 | } |
| 1433 | }; |
| 1434 | |
| 1435 | static void Melder_readPolyphoneFile (MelderFile file, MAT buffer) { |
| 1436 | //TRACE |
| 1437 | FILE *f = file -> filePointer; |
| 1438 | Melder_require (buffer.nrow == 1, |
| 1439 | U"Can read Polyphone files only if they are mono. Write to [email protected] for more information."); |
| 1440 | trace (U"expecting ", buffer.ncol, U" samples"); |
| 1441 | const integer startOfData = ftell (f); |
| 1442 | fseek (f, 0, SEEK_END); |
| 1443 | const integer endOfData = ftell (f); |
| 1444 | const integer numberOfDataBytesInFile = endOfData - startOfData; |
| 1445 | trace (numberOfDataBytesInFile, U" data bytes in file"); |
| 1446 | fseek (f, startOfData, SEEK_SET); |
| 1447 | /* |
| 1448 | The format of Shorten files is described in: |
| 1449 | Tony Robinson (1994): "Shorten: sample lossless and near-lossless waveform compression", |
| 1450 | Technical report CUED/F-INFENG/TR.156, |
| 1451 | Cambridge University Engineering Department. |
| 1452 | |
| 1453 | The first four bytes are lower-case letters, namelu "ajkg"; |
| 1454 | "aj" seem to be Tony Robinson's initials. |
| 1455 | */ |
| 1456 | uint32 letter = bingetu8 (f); |
| 1457 | Melder_require (letter == 'a', |
| 1458 | U"First letter of magic word “ajkg” is character ", letter, U" instead of “a”."); |
| 1459 | letter = bingetu8 (f); |
| 1460 | Melder_require (letter == 'j', |
| 1461 | U"Second letter of magic word “ajkg” is character ", letter, U" instead of “j”."); |
| 1462 | letter = bingetu8 (f); |
| 1463 | Melder_require (letter == 'k', |
| 1464 | U"Third letter of magic word “ajkg” is character ", letter, U" instead of “k”."); |
| 1465 | letter = bingetu8 (f); |
| 1466 | Melder_require (letter == 'g', |
| 1467 | U"Fourth letter of magic word “ajkg” is character ", letter, U" instead of “g”."); |
| 1468 | const uint32 version = bingetu8 (f); |
| 1469 | Melder_require (version == 1, |
| 1470 | U"Can read only Shorten version 1, not ", version, U"."); |
| 1471 | integer numberOfDataBytesLeft = numberOfDataBytesInFile - 5; |
| 1472 | trace (numberOfDataBytesLeft, U" data bytes left in file"); |
| 1473 | /* |
| 1474 | The following six bytes are always 255 114 224 19 50 214. |
| 1475 | In binary: |
| 1476 | 255 = 0b11111111 |
| 1477 | 114 = 0b01110010 |
| 1478 | 224 = 0b11100000 |
| 1479 | 19 = 0b00010011 |
| 1480 | 50 = 0b00110010 |
| 1481 | 214 = 0b11010110 |
| 1482 | These contain several uint32 numbers in indirect shortened format. |
| 1483 | This indirected format first encodes the length of the mantissa |
| 1484 | of the uint32 number, in direct shortened format, with a mantissa of 2 bits. |
| 1485 | According to the Shorten paper, the number of fours is given by the number |
| 1486 | of leading zeroes, then there's a one to signal the end of the zeroes, |
| 1487 | and finally there are the two bits of the mantissa, as follows: |
| 1488 | 100 -> 0 |
| 1489 | 101 -> 1 |
| 1490 | 110 -> 2 |
| 1491 | 111 -> 3 |
| 1492 | 0100 -> 4 |
| 1493 | 0101 -> 5 |
| 1494 | 0110 -> 6 |
| 1495 | 0111 -> 7 |
| 1496 | 00100 -> 8 |
| 1497 | 00101 -> 9 |
| 1498 | 00110 -> 10 |
| 1499 | 00111 -> 11 |
| 1500 | 000100 -> 12 |
| 1501 | 000101 -> 13 |
| 1502 | 000110 -> 14 |
| 1503 | 000111 -> 15 |
| 1504 | 0000100 -> 16 |
| 1505 | 00000100 -> 20 |
| 1506 | 000000100 -> 24 |
| 1507 | 0000000100 -> 28 |
| 1508 | Now we have just only implemented the number of bits of the mantissa |
| 1509 | of the whole number. Let's walk through the fixed bits. |
| 1510 | |
| 1511 | The first mantissa length is encoded as 111 (the first 1+2 bits of 255 |
| 1512 | that follow the 0 zeroes), meaning 3. So the whole number is encoded |
| 1513 | with a mantissa length of 3. The table for that is: |
| 1514 | 1000 -> 0 |
| 1515 | 1001 -> 1 |
| 1516 | 1111 -> 7 |
| 1517 | 01000 -> 8 |
| 1518 | After the first 111 of 255 we find 0 zeroes, follow by 1+3 ones, |
| 1519 | which means 7. This whole number has been encoded as 111.1111. |
| 1520 | */ |
| 1521 | bitsInReadBuffer = 0; // flush |
| 1522 | const uint32 fileType = bingetu32_shortened_indirect (f); |
| 1523 | trace (U"File type: ", fileType); |
| 1524 | Melder_require (fileType == 7, |
| 1525 | U"Polyphone files should have file type 7, not ", fileType, U"."); |
| 1526 | /* |
| 1527 | The remainder of the bits is now found after the pipe: |
| 1528 | 111.1111|10111001011100000000100110011001011010110 |
| 1529 | 7 |
| 1530 | The next mantissa length code is 101, i.e. 1. So we need the following table: |
| 1531 | 10 -> 0 |
| 1532 | 11 -> 1 |
| 1533 | 010 -> 2 |
| 1534 | 011 -> 3 |
| 1535 | 0010 -> 4 |
| 1536 | 0011 -> 5 |
| 1537 | 00010 -> 6 |
| 1538 | What we find after 101 is 11, meaning 1. |
| 1539 | */ |
| 1540 | const uint32 numberOfChannels = bingetu32_shortened_indirect (f); |
| 1541 | trace (U"Number of channels: ", numberOfChannels); |
| 1542 | Melder_require (numberOfChannels == 1, |
| 1543 | U"Polyphone files should have 1 channel (mono), not ", numberOfChannels); |
| 1544 | /* |
| 1545 | The remainder of the bits is now: |
| 1546 | 111.1111|101.11|001011100000000100110011001011010110 |
| 1547 | 7 1 |
| 1548 | The next mantissa length code is 00101, i.e. 9. So we need the following table: |
| 1549 | 1000000000 -> 0 |
| 1550 | 1000000001 -> 1 |
| 1551 | 1100000000 -> 256 |
| 1552 | 1111111111 -> 511 |
| 1553 | What we find after 00101 is 1100000000, meaning 256. |
| 1554 | */ |
| 1555 | uint32 blockSize = bingetu32_shortened_indirect (f); // the standard block size, until it's changed |
| 1556 | trace (U"Block size: ", blockSize); |
| 1557 | Melder_require (blockSize == 256, |
| 1558 | U"Polyphone files should have a block size of 256, not ", blockSize, U"."); |
| 1559 | /* |
| 1560 | The remainder of the bits is now: |
| 1561 | 111.1111|101.11|00101.1100000000|100110011001011010110 |
| 1562 | 7 1 256 |
| 1563 | The next mantissa length code is 100, i.e. 0. So we need the following table: |
| 1564 | 1 -> 0 |
| 1565 | 01 -> 1 |
| 1566 | 001 -> 2 |
| 1567 | 0001 -> 3 |
| 1568 | What we find after 100 is 1, meaning 0. |
| 1569 | */ |
| 1570 | const uint32 numberOfLpcCoefficients = bingetu32_shortened_indirect (f); |
| 1571 | trace (U"Number of LPC coefficients: ", numberOfLpcCoefficients); |
| 1572 | Melder_require (numberOfLpcCoefficients == 0, |
| 1573 | U"Polyphone files should not have LPC encoding."); |
| 1574 | /* |
| 1575 | The remainder of the bits is now: |
| 1576 | 111.1111|101.11|00101.1100000000|100.1|10011001011010110 |
| 1577 | 7 1 256 0 |
| 1578 | The next mantissa length code is again 100, i.e. 0. |
| 1579 | What we find after 100 is 1, meaning 0 again. |
| 1580 | This is only one of two zeroes: |
| 1581 | */ |
| 1582 | const uint32 dummy1 = bingetu32_shortened_indirect (f); |
| 1583 | trace (U"dummy1: ", dummy1); |
| 1584 | Melder_require (dummy1 == 0, |
| 1585 | U"Polyphone files should have a “dummy1” of 0."); |
| 1586 | const uint32 dummy2 = bingetu32_shortened_indirect (f); |
| 1587 | trace (U"dummy2: ", dummy2); |
| 1588 | Melder_require (dummy2 == 0, |
| 1589 | U"Polyphone files should have a “dummy2” of 0."); |
| 1590 | /* |
| 1591 | The remainder of the bits is now: |
| 1592 | 111.1111|101.11|00101.1100000000|100.1|100.1|100.1|01101011011 |
| 1593 | 7 1 256 0 0 0 |
| 1594 | Note that we already appended the high two bits of the number, |
| 1595 | which is always over 192. |
| 1596 | The next mantissa length code is 0110, i.e. 6. |
| 1597 | What we find after 0110 is 1011011, meaning 27. |
| 1598 | So we have found: |
| 1599 | 111.1111|101.11|00101.1100000000|100.1|100.1|100.1|0110.1011011| |
| 1600 | 7 1 256 0 0 0 27 |
| 1601 | Great. We stuffed 7 numbers into 6 bytes plus 2 bits, |
| 1602 | instead of wasting 28 (or perhaps 14) bytes on it. |
| 1603 | |
| 1604 | But... what is this 27? |
| 1605 | We are supposed to see "commands", i.e. numbers between 0 and 8, |
| 1606 | encoded as direct shortened numbers with a length-2 mantissa. |
| 1607 | Thus, 0110 would actually represent the number 6, which means "bitshift", |
| 1608 | which should be followed by the bitshift itself, which is also |
| 1609 | mantissa-2-encoded, so the bitshift is 101, which means 1: |
| 1610 | 111.1111|101.11|00101.1100000000|100.1|100.1|100.1|0110|101|1011 |
| 1611 | 7 1 256 0 0 0 6 1 |
| 1612 | The next command then seems to be 101, which means 1 ("diff1"): |
| 1613 | 111.1111|101.11|00101.1100000000|100.1|100.1|100.1|0110|101|101|1 |
| 1614 | 7 1 256 0 0 0 6 1 1 |
| 1615 | */ |
| 1616 | autovector <int32> sampleBuffer = newvectorzero <int32> (blockSize + 3); |
| 1617 | constexpr uint32 COMMAND_DIFF0 = 0, COMMAND_DIFF1 = 1, COMMAND_DIFF2 = 2, COMMAND_DIFF3 = 3; |
| 1618 | constexpr uint32 COMMAND_QUIT = 4, COMMAND_BLOCKSIZE = 5, COMMAND_BITSHIFT = 6; |
| 1619 | integer numberOfSamplesRead = 0; |
| 1620 | uint32 bitshift = 0; |
| 1621 | for (;;) { |
| 1622 | uint32 command = bingetu32_shortened_direct (f, 2); |
| 1623 | trace (U"Command ", command); |
| 1624 | if (command == COMMAND_QUIT) |
| 1625 | break; |
| 1626 | if (command == COMMAND_BITSHIFT) { |
| 1627 | bitshift = bingetu32_shortened_direct (f, 2); |
| 1628 | trace (U"bitshift ", bitshift); |
| 1629 | Melder_require (bitshift <= 12, |
| 1630 | U"Can handle only a bitshift up to 12, not ", bitshift, U"."); |
| 1631 | } else if (command >= COMMAND_DIFF0 && command <= COMMAND_DIFF3) { |
| 1632 | uint32 mantissaLength = bingetu32_shortened_direct (f, 3); |
| 1633 | trace (U"diff", command, U" length ", mantissaLength); |
| 1634 | for (uint32 i = 4; i <= blockSize + 3; i ++) { |
| 1635 | const int32 storedValue = bingeti32_shortened_direct (f, mantissaLength); |
| 1636 | if (command == COMMAND_DIFF0) |
| 1637 | sampleBuffer [i] = storedValue; // Robinson (1994), eq. 3 |
| 1638 | else if (command == COMMAND_DIFF1) |
| 1639 | sampleBuffer [i] = storedValue |
| 1640 | + sampleBuffer [i - 1]; // Robinson (1994), eq. 4 |
| 1641 | else if (command == COMMAND_DIFF2) |
| 1642 | sampleBuffer [i] = storedValue |
| 1643 | + 2 * sampleBuffer [i - 1] - sampleBuffer [i - 2]; // Robinson (1994), eq. 5 |
| 1644 | else if (command == COMMAND_DIFF3) |
| 1645 | sampleBuffer [i] = storedValue |
| 1646 | + 3 * (sampleBuffer [i - 1] - sampleBuffer [i - 2]) + sampleBuffer [i - 3]; // Robinson (1994), eq. 6 |
| 1647 | trace (storedValue, U" ", sampleBuffer [i]); |
| 1648 | } |
| 1649 | for (uint32 i = 1; i <= 3; i ++) |
| 1650 | sampleBuffer [i] = sampleBuffer [blockSize + i]; // move them here, waiting for the next block |
| 1651 | Melder_require (bitshift > 0, |
| 1652 | U"Cannot work with a bitshift of 0."); |
| 1653 | Melder_require (bitshift <= 12, |
| 1654 | U"Can handle only a bitshift up to 12, not ", bitshift, U"."); |
| 1655 | for (uint32 i = 4; i <= blockSize + 3; i ++) { |
| 1656 | const int32 alawValue = sampleBuffer [i]; |
| 1657 | Melder_require (alawValue >= -128, |
| 1658 | U"A-law sample value is ", alawValue, U" but should be at least -128."); |
| 1659 | Melder_require (alawValue <= +127, |
| 1660 | U"A-law sample value is ", alawValue, U" but should be at most +127."); |
| 1661 | const integer placeInTable = alawBitshiftTable [bitshift - 1] [alawValue + 128]; |
| 1662 | Melder_require (placeInTable >= 0, // because minus 1 means undefined |
| 1663 | U"The A-law value ", alawValue, U" is too large for a bitshift of ", bitshift, U"."); |
| 1664 | const int32 linearValue = sortedAlawValues [placeInTable]; |
| 1665 | buffer [1] [++ numberOfSamplesRead] = linearValue / 32768.0; |
| 1666 | } |
| 1667 | } else if (command == COMMAND_BLOCKSIZE) { |
| 1668 | /* |
| 1669 | This might often occur before the last block, |
| 1670 | namely if the number of samples is not an integer multiple |
| 1671 | of the earlier block sizes (which are typically 256). |
| 1672 | */ |
| 1673 | const uint32 newBlockSize = bingetu32_shortened_indirect (f); |
| 1674 | Melder_require (newBlockSize <= blockSize, |
| 1675 | U"Cannot grow block size from ", blockSize, U" to ", newBlockSize, U"."); |
| 1676 | trace (newBlockSize); |
| 1677 | blockSize = newBlockSize; |
| 1678 | } else { |
| 1679 | Melder_throw (U"Unknown command ", command); |
| 1680 | } |
| 1681 | } |
| 1682 | Melder_require (numberOfSamplesRead == buffer.ncol, |
| 1683 | U"Expected ", buffer.ncol, U" samples but found ", numberOfSamplesRead, U"."); |
| 1684 | } |
| 1685 | |
| 1686 | static void warning_fileTooSmall (MelderFile file, integer numberOfChannels, const char32 *type) { |
| 1687 | Melder_warning (U"File ", MelderFile_messageName (file), U" too small (", |
| 1688 | numberOfChannels, U"-channel ", type, U").\nMissing samples were set to zero."); |
| 1689 | } |
| 1690 | |
| 1691 | void Melder_readAudioToFloat (MelderFile file, int encoding, MAT buffer) { |
| 1692 | FILE *f = file -> filePointer; // this function must have a MelderFile argument, because it can warn (which needs a filename) |
| 1693 | try { |
| 1694 | integer numberOfChannels = buffer.nrow; |
| 1695 | integer numberOfSamples = buffer.ncol; |
| 1696 | switch (encoding) { |
| 1697 | case Melder_LINEAR_8_SIGNED: { |
| 1698 | try { |
| 1699 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1700 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1701 | int8 value; |
| 1702 | if (fread (& value, 1, 1, f) < 1) throw MelderError (); |
| 1703 | buffer [ichan] [isamp] = value * (1.0 / 128); |
| 1704 | } |
| 1705 | } catch (MelderError) { |
| 1706 | Melder_clearError (); |
| 1707 | warning_fileTooSmall (file, numberOfChannels, U"8-bit signed"); |
| 1708 | } |
| 1709 | } break; |
| 1710 | case Melder_LINEAR_8_UNSIGNED: |
| 1711 | try { |
| 1712 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1713 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 1714 | buffer [ichan] [isamp] = bingetu8 (f) * (1.0 / 128) - 1.0; |
| 1715 | } catch (MelderError) { |
| 1716 | Melder_clearError (); |
| 1717 | warning_fileTooSmall (file, numberOfChannels, U"8-bit unsigned"); |
| 1718 | } |
| 1719 | break; |
| 1720 | case Melder_LINEAR_16_BIG_ENDIAN: |
| 1721 | case Melder_LINEAR_16_LITTLE_ENDIAN: |
| 1722 | case Melder_LINEAR_24_BIG_ENDIAN: |
| 1723 | case Melder_LINEAR_24_LITTLE_ENDIAN: |
| 1724 | case Melder_LINEAR_32_BIG_ENDIAN: |
| 1725 | case Melder_LINEAR_32_LITTLE_ENDIAN: |
| 1726 | { |
| 1727 | const int numberOfBytesPerSamplePerChannel = |
| 1728 | encoding == Melder_LINEAR_16_BIG_ENDIAN || encoding == Melder_LINEAR_16_LITTLE_ENDIAN ? 2 : |
| 1729 | encoding == Melder_LINEAR_24_BIG_ENDIAN || encoding == Melder_LINEAR_24_LITTLE_ENDIAN ? 3 : 4; |
| 1730 | const double numberOfBytes_f = (double) numberOfChannels * (double) numberOfSamples * (double) numberOfBytesPerSamplePerChannel; |
| 1731 | if (isinf (numberOfBytes_f) || numberOfBytes_f > (double) (1LL << 53)) { |
| 1732 | Melder_throw (U"Cannot read ", numberOfBytes_f, U" bytes, " |
| 1733 | U"because that crosses the 9-petabyte limit."); |
| 1734 | } |
| 1735 | if (numberOfBytes_f > (double) SIZE_MAX) { |
| 1736 | Melder_throw (U"Cannot read ", numberOfBytes_f, U" bytes. " |
| 1737 | U"Perhaps try a 64-bit edition of Praat?"); |
| 1738 | } |
| 1739 | Melder_assert (numberOfBytes_f >= 0.0); |
| 1740 | const size_t numberOfBytes = (size_t) numberOfBytes_f; // cast is safe because overflow and signedness have been checked |
| 1741 | /* mutable read */ uint8 * /* mutable cycle */ bytes = |
| 1742 | (uint8 *) & buffer [numberOfChannels] [numberOfSamples] + sizeof (double) - numberOfBytes; |
| 1743 | /* |
| 1744 | Read 16-bit data into the last quarter of the buffer, |
| 1745 | or 24-bit data into the last three-eighths of the buffer, |
| 1746 | or 32-bit data into the last half of the buffer. |
| 1747 | */ |
| 1748 | const size_t numberOfBytesRead = fread (bytes, 1, numberOfBytes, f); |
| 1749 | if (numberOfChannels == 1) { |
| 1750 | switch (encoding) { |
| 1751 | case Melder_LINEAR_16_BIG_ENDIAN: { |
| 1752 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) { |
| 1753 | const unsigned char byte1 = * bytes ++, byte2 = * bytes ++; |
| 1754 | const int value = (int) (int16) (((uint16) byte1 << 8) | (uint16) byte2); // extend sign |
| 1755 | buffer [1] [isamp] = value * (1.0 / 32768); |
| 1756 | } |
| 1757 | } break; |
| 1758 | case Melder_LINEAR_16_LITTLE_ENDIAN: { |
| 1759 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) { |
| 1760 | const unsigned char byte1 = * bytes ++, byte2 = * bytes ++; |
| 1761 | const int value = (int) (int16) (((uint16) byte2 << 8) | (uint16) byte1); // extend sign |
| 1762 | buffer [1] [isamp] = value * (1.0 / 32768); |
| 1763 | } |
| 1764 | } break; |
| 1765 | case Melder_LINEAR_24_BIG_ENDIAN: { |
| 1766 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) { |
| 1767 | const unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++; |
| 1768 | const int32 value = (int32) |
| 1769 | ((uint32) ((uint32) byte1 << 24) | |
| 1770 | (uint32) ((uint32) byte2 << 16) | |
| 1771 | ((uint32) byte3 << 8)); |
| 1772 | buffer [1] [isamp] = value * (1.0 / 32768 / 65536); |
| 1773 | } |
| 1774 | } break; |
| 1775 | case Melder_LINEAR_24_LITTLE_ENDIAN: { |
| 1776 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) { |
| 1777 | const unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++; |
| 1778 | const int32 value = (int32) |
| 1779 | ((uint32) ((uint32) byte3 << 24) | |
| 1780 | (uint32) ((uint32) byte2 << 16) | |
| 1781 | ((uint32) byte1 << 8)); |
| 1782 | buffer [1] [isamp] = value * (1.0 / 32768 / 65536); |
| 1783 | } |
| 1784 | } break; |
| 1785 | case Melder_LINEAR_32_BIG_ENDIAN: { |
| 1786 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) { |
| 1787 | const unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++; |
| 1788 | const int32 value = (int32) |
| 1789 | ((uint32) ((uint32) byte1 << 24) | |
| 1790 | (uint32) ((uint32) byte2 << 16) | |
| 1791 | (uint32) ((uint32) byte3 << 8) | |
| 1792 | (uint32) byte4); |
| 1793 | buffer [1] [isamp] = value * (1.0 / 32768 / 65536); |
| 1794 | } |
| 1795 | } break; |
| 1796 | case Melder_LINEAR_32_LITTLE_ENDIAN: { |
| 1797 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) { |
| 1798 | const unsigned char byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++; |
| 1799 | const int32 value = (int32) |
| 1800 | ((uint32) ((uint32) byte4 << 24) | |
| 1801 | (uint32) ((uint32) byte3 << 16) | |
| 1802 | (uint32) ((uint32) byte2 << 8) | |
| 1803 | (uint32) byte1); |
| 1804 | buffer [1] [isamp] = value * (1.0 / 32768 / 65536); |
| 1805 | } |
| 1806 | } break; |
| 1807 | } |
| 1808 | } else if (numberOfChannels <= (int) sizeof (double) / numberOfBytesPerSamplePerChannel) { |
| 1809 | switch (encoding) { |
| 1810 | case Melder_LINEAR_16_BIG_ENDIAN: { |
| 1811 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1812 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1813 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++; |
| 1814 | const int value = (int) (int16) (uint16) ((uint16) ((uint16) byte1 << 8) | (uint16) byte2); |
| 1815 | buffer [ichan] [isamp] = value * (1.0 / 32768); |
| 1816 | } |
| 1817 | } break; |
| 1818 | case Melder_LINEAR_16_LITTLE_ENDIAN: { |
| 1819 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1820 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1821 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++; |
| 1822 | const int value = (int) (int16) (uint16) ((uint16) ((uint16) byte2 << 8) | (uint16) byte1); |
| 1823 | buffer [ichan] [isamp] = value * (1.0 / 32768); |
| 1824 | } |
| 1825 | } break; |
| 1826 | case Melder_LINEAR_24_BIG_ENDIAN: { |
| 1827 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1828 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1829 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++; |
| 1830 | /* mutable sign */ uint32 unsignedValue = |
| 1831 | (uint32) ((uint32) byte1 << 16) | |
| 1832 | (uint32) ((uint32) byte2 << 8) | |
| 1833 | (uint32) byte3; |
| 1834 | if ((byte1 & 128) != 0) |
| 1835 | unsignedValue |= 0xFF000000; // extend sign |
| 1836 | buffer [ichan] [isamp] = (int32) unsignedValue * (1.0 / 8388608); |
| 1837 | } |
| 1838 | } break; |
| 1839 | case Melder_LINEAR_24_LITTLE_ENDIAN: { |
| 1840 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1841 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1842 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++; |
| 1843 | /* mutable sign */ uint32 unsignedValue = ((uint32) byte3 << 16) | ((uint32) byte2 << 8) | (uint32) byte1; |
| 1844 | if ((byte3 & 128) != 0) |
| 1845 | unsignedValue |= 0xFF000000; |
| 1846 | buffer [ichan] [isamp] = (int32) unsignedValue * (1.0 / 8388608); |
| 1847 | } |
| 1848 | } break; |
| 1849 | case Melder_LINEAR_32_BIG_ENDIAN: { |
| 1850 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1851 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1852 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++; |
| 1853 | const int32 value = (int32) |
| 1854 | ((uint32) ((uint32) byte1 << 24) | |
| 1855 | (uint32) ((uint32) byte2 << 16) | |
| 1856 | (uint32) ((uint32) byte3 << 8) | |
| 1857 | (uint32) byte4); |
| 1858 | buffer [ichan] [isamp] = value * (1.0 / 32768 / 65536); |
| 1859 | } |
| 1860 | } break; |
| 1861 | case Melder_LINEAR_32_LITTLE_ENDIAN: { |
| 1862 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1863 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1864 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++; |
| 1865 | const int32 value = (int32) |
| 1866 | ((uint32) ((uint32) byte4 << 24) | |
| 1867 | (uint32) ((uint32) byte3 << 16) | |
| 1868 | (uint32) ((uint32) byte2 << 8) | |
| 1869 | (uint32) byte1); |
| 1870 | buffer [ichan] [isamp] = value * (1.0 / 32768 / 65536); |
| 1871 | } |
| 1872 | } break; |
| 1873 | } |
| 1874 | } else { |
| 1875 | Melder_assert (sizeof (double) == 8); |
| 1876 | int32 *ints = (int32 *) & buffer [1] [1]; |
| 1877 | switch (encoding) { |
| 1878 | case Melder_LINEAR_16_BIG_ENDIAN: { |
| 1879 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1880 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1881 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++; |
| 1882 | const int32 value = (int32) |
| 1883 | ((uint32) ((uint32) byte1 << 24) | |
| 1884 | (uint32) ((uint32) byte2 << 16)); |
| 1885 | * ints ++ = value; |
| 1886 | * ints ++ = 0; // the marker |
| 1887 | } |
| 1888 | } break; |
| 1889 | case Melder_LINEAR_16_LITTLE_ENDIAN: { |
| 1890 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1891 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1892 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++; |
| 1893 | const int32 value = (int32) |
| 1894 | (((uint32) byte2 << 24) | |
| 1895 | ((uint32) byte1 << 16)); |
| 1896 | * ints ++ = value; |
| 1897 | * ints ++ = 0; // the marker |
| 1898 | } |
| 1899 | } break; |
| 1900 | case Melder_LINEAR_24_BIG_ENDIAN: { |
| 1901 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1902 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1903 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++; |
| 1904 | const int32 value = (int32) |
| 1905 | (((uint32) byte1 << 24) | |
| 1906 | ((uint32) byte2 << 16) | |
| 1907 | ((uint32) byte3 << 8)); |
| 1908 | * ints ++ = value; |
| 1909 | * ints ++ = 0; // the marker |
| 1910 | } |
| 1911 | } break; |
| 1912 | case Melder_LINEAR_24_LITTLE_ENDIAN: { |
| 1913 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1914 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1915 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++; |
| 1916 | const int32 value = (int32) |
| 1917 | (((uint32) byte3 << 24) | |
| 1918 | ((uint32) byte2 << 16) | |
| 1919 | ((uint32) byte1 << 8)); |
| 1920 | * ints ++ = value; |
| 1921 | * ints ++ = 0; // the marker |
| 1922 | } |
| 1923 | } break; |
| 1924 | case Melder_LINEAR_32_BIG_ENDIAN: { |
| 1925 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1926 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1927 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++; |
| 1928 | const int32 value = (int32) |
| 1929 | (((uint32) byte1 << 24) | |
| 1930 | ((uint32) byte2 << 16) | |
| 1931 | ((uint32) byte3 << 8) | |
| 1932 | (uint32) byte4); |
| 1933 | * ints ++ = value; |
| 1934 | * ints ++ = 0; // the marker |
| 1935 | } |
| 1936 | } break; |
| 1937 | case Melder_LINEAR_32_LITTLE_ENDIAN: { |
| 1938 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1939 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 1940 | const uint8 byte1 = * bytes ++, byte2 = * bytes ++, byte3 = * bytes ++, byte4 = * bytes ++; |
| 1941 | const int32 value = (int32) |
| 1942 | (((uint32) byte4 << 24) | |
| 1943 | ((uint32) byte3 << 16) | |
| 1944 | ((uint32) byte2 << 8) | |
| 1945 | (uint32) byte1); |
| 1946 | * ints ++ = value; |
| 1947 | * ints ++ = 0; // the marker |
| 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | double *doubles = & buffer [1] [1]; |
| 1952 | integer n = numberOfSamples * numberOfChannels; |
| 1953 | for (integer i = 0; i < n; i ++) { |
| 1954 | int32 *valuePosition = (int32 *) & doubles [i]; |
| 1955 | int32 *markerPosition = valuePosition + 1; |
| 1956 | if (! *markerPosition) { |
| 1957 | int32 value = *valuePosition; |
| 1958 | integer ichan = i / numberOfSamples, isamp = i % numberOfSamples; |
| 1959 | for (integer other = isamp * numberOfChannels + ichan; other != i; ) { |
| 1960 | int32 *otherValuePosition = (int32 *) & doubles [other]; |
| 1961 | *valuePosition = *otherValuePosition; |
| 1962 | *markerPosition = 1; |
| 1963 | valuePosition = otherValuePosition; |
| 1964 | markerPosition = valuePosition + 1; |
| 1965 | ichan = other / numberOfSamples, isamp = other % numberOfSamples; |
| 1966 | other = isamp * numberOfChannels + ichan; |
| 1967 | } |
| 1968 | *valuePosition = value; |
| 1969 | *markerPosition = 1; |
| 1970 | } |
| 1971 | } |
| 1972 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1973 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 1974 | buffer [ichan] [isamp] = * (int32 *) & buffer [ichan] [isamp] * (1.0 / 32768 / 65536); |
| 1975 | } |
| 1976 | if (numberOfBytesRead < numberOfBytes) |
| 1977 | Melder_warning (U"File too small (", numberOfChannels, U"-channel ", numberOfBytesPerSamplePerChannel * 8, U"-bit).\n" |
| 1978 | U"Missing samples were set to zero."); |
| 1979 | } break; |
| 1980 | case Melder_IEEE_FLOAT_32_BIG_ENDIAN: |
| 1981 | try { |
| 1982 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1983 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 1984 | buffer [ichan] [isamp] = bingetr32 (f); |
| 1985 | } catch (MelderError) { |
| 1986 | Melder_clearError (); |
| 1987 | warning_fileTooSmall (file, numberOfChannels, U"32-bit floating point"); |
| 1988 | } |
| 1989 | break; |
| 1990 | case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN: |
| 1991 | try { |
| 1992 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 1993 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 1994 | buffer [ichan] [isamp] = bingetr32LE (f); |
| 1995 | } catch (MelderError) { |
| 1996 | Melder_clearError (); |
| 1997 | warning_fileTooSmall (file, numberOfChannels, U"32-bit floating point"); |
| 1998 | } |
| 1999 | break; |
| 2000 | case Melder_IEEE_FLOAT_64_BIG_ENDIAN: |
| 2001 | try { |
| 2002 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2003 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 2004 | buffer [ichan] [isamp] = bingetr64 (f); |
| 2005 | } catch (MelderError) { |
| 2006 | Melder_clearError (); |
| 2007 | warning_fileTooSmall (file, numberOfChannels, U"64-bit floating point"); |
| 2008 | } |
| 2009 | break; |
| 2010 | case Melder_IEEE_FLOAT_64_LITTLE_ENDIAN: |
| 2011 | try { |
| 2012 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2013 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 2014 | buffer [ichan] [isamp] = bingetr64LE (f); |
| 2015 | } catch (MelderError) { |
| 2016 | Melder_clearError (); |
| 2017 | warning_fileTooSmall (file, numberOfChannels, U"64-bit floating point"); |
| 2018 | } |
| 2019 | break; |
| 2020 | case Melder_MULAW: |
| 2021 | try { |
| 2022 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2023 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 2024 | buffer [ichan] [isamp] = ulaw2linear [bingetu8 (f)] * (1.0 / 32768); |
| 2025 | } catch (MelderError) { |
| 2026 | Melder_clearError (); |
| 2027 | warning_fileTooSmall (file, numberOfChannels, U"8-bit μ-law"); |
| 2028 | } |
| 2029 | break; |
| 2030 | case Melder_ALAW: |
| 2031 | try { |
| 2032 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2033 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 2034 | buffer [ichan] [isamp] = alaw2linear [bingetu8 (f)] * (1.0 / 32768); |
| 2035 | } catch (MelderError) { |
| 2036 | Melder_clearError (); |
| 2037 | warning_fileTooSmall (file, numberOfChannels, U"8-bit A-law"); |
| 2038 | } |
| 2039 | break; |
| 2040 | case Melder_FLAC_COMPRESSION_16: |
| 2041 | case Melder_FLAC_COMPRESSION_24: |
| 2042 | case Melder_FLAC_COMPRESSION_32: |
| 2043 | Melder_readFlacFile (f, buffer); |
| 2044 | break; |
| 2045 | case Melder_MPEG_COMPRESSION_16: |
| 2046 | case Melder_MPEG_COMPRESSION_24: |
| 2047 | case Melder_MPEG_COMPRESSION_32: |
| 2048 | Melder_readMp3File (f, buffer); |
| 2049 | break; |
| 2050 | case Melder_POLYPHONE: |
| 2051 | Melder_readPolyphoneFile (file, buffer); |
| 2052 | break; |
| 2053 | default: |
| 2054 | Melder_throw (U"Unknown encoding ", encoding, U"."); |
| 2055 | } |
| 2056 | } catch (MelderError) { |
| 2057 | Melder_throw (U"Audio samples not read from file."); |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | void Melder_readAudioToShort (MelderFile file, integer numberOfChannels, int encoding, short *buffer, integer numberOfSamples) { |
| 2062 | FILE *f = file -> filePointer; // this function must have a MelderFile argument, because it can warn (which needs a filename) |
| 2063 | try { |
| 2064 | const integer n = numberOfSamples * numberOfChannels; |
| 2065 | static const unsigned short byteSwapTest = 3 * 256 + 1; |
| 2066 | switch (encoding) { |
| 2067 | case Melder_LINEAR_8_SIGNED: |
| 2068 | for (integer i = 0; i < n; i ++) { |
| 2069 | signed char value; |
| 2070 | if (! fread (& value, 1, 1, f)) |
| 2071 | Melder_throw (U"File too small (mono 8-bit)."); |
| 2072 | buffer [i] = value * 256; |
| 2073 | } |
| 2074 | break; |
| 2075 | case Melder_LINEAR_8_UNSIGNED: |
| 2076 | for (integer i = 0; i < n; i ++) |
| 2077 | buffer [i] = bingetu8 (f) * 256L - 32768; |
| 2078 | break; |
| 2079 | case Melder_LINEAR_16_BIG_ENDIAN: |
| 2080 | static_assert (sizeof (short) == 2); |
| 2081 | fread (buffer, sizeof (short), n, f); |
| 2082 | if (* (uint8 *) & byteSwapTest == 1) { |
| 2083 | for (integer i = 0; i < n; i ++) { |
| 2084 | const uint16 value = (uint16) buffer [i]; // cast sign |
| 2085 | buffer [i] = (int16) ((value >> 8) + (value << 8)); // truncate from int32 (left shifts are signed) to int16 |
| 2086 | } |
| 2087 | } |
| 2088 | break; |
| 2089 | case Melder_LINEAR_16_LITTLE_ENDIAN: |
| 2090 | fread (buffer, sizeof (short), n, f); |
| 2091 | if (* (unsigned char *) & byteSwapTest == 3) { |
| 2092 | for (integer i = 0; i < n; i ++) { |
| 2093 | const uint16 value = (uint16) buffer [i]; // cast sign |
| 2094 | buffer [i] = (int16) ((value >> 8) + (value << 8)); // truncate from int32 (left shifts are signed) to int16 |
| 2095 | } |
| 2096 | } |
| 2097 | break; |
| 2098 | case Melder_LINEAR_24_BIG_ENDIAN: |
| 2099 | for (integer i = 0; i < n; i ++) |
| 2100 | buffer [i] = bingeti24 (f) / 256; // BUG: truncation; not ideal |
| 2101 | break; |
| 2102 | case Melder_LINEAR_24_LITTLE_ENDIAN: |
| 2103 | for (integer i = 0; i < n; i ++) |
| 2104 | buffer [i] = bingeti24LE (f) / 256; // BUG: truncation; not ideal |
| 2105 | break; |
| 2106 | case Melder_LINEAR_32_BIG_ENDIAN: |
| 2107 | for (integer i = 0; i < n; i ++) |
| 2108 | buffer [i] = bingeti32 (f) / 65536; // BUG: truncation; not ideal |
| 2109 | break; |
| 2110 | case Melder_LINEAR_32_LITTLE_ENDIAN: |
| 2111 | for (integer i = 0; i < n; i ++) |
| 2112 | buffer [i] = bingeti32LE (f) / 65536; // BUG: truncation; not ideal |
| 2113 | break; |
| 2114 | case Melder_IEEE_FLOAT_32_BIG_ENDIAN: |
| 2115 | for (integer i = 0; i < n; i ++) |
| 2116 | buffer [i] = bingetr32 (f) * 32768; // BUG: truncation; not ideal |
| 2117 | break; |
| 2118 | case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN: |
| 2119 | for (integer i = 0; i < n; i ++) |
| 2120 | buffer [i] = bingetr32LE (f) * 32768; // BUG: truncation; not ideal |
| 2121 | break; |
| 2122 | case Melder_IEEE_FLOAT_64_BIG_ENDIAN: |
| 2123 | for (integer i = 0; i < n; i ++) |
| 2124 | buffer [i] = bingetr64 (f) * 32768; // BUG: truncation; not ideal |
| 2125 | break; |
| 2126 | case Melder_IEEE_FLOAT_64_LITTLE_ENDIAN: |
| 2127 | for (integer i = 0; i < n; i ++) |
| 2128 | buffer [i] = bingetr64LE (f) * 32768; // BUG: truncation; not ideal |
| 2129 | break; |
| 2130 | case Melder_MULAW: |
| 2131 | for (integer i = 0; i < n; i ++) |
| 2132 | buffer [i] = ulaw2linear [bingetu8 (f)]; |
| 2133 | break; |
| 2134 | case Melder_ALAW: |
| 2135 | for (integer i = 0; i < n; i ++) |
| 2136 | buffer [i] = alaw2linear [bingetu8 (f)]; |
| 2137 | break; |
| 2138 | default: |
| 2139 | Melder_throw (U"Unknown encoding ", encoding, U"."); |
| 2140 | } |
| 2141 | } catch (MelderError) { |
| 2142 | Melder_clearError (); |
| 2143 | Melder_warning (U"Audio file ", MelderFile_messageName (file), U" too short. Missing samples were set to zero."); |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | void MelderFile_writeShortToAudio (MelderFile file, integer numberOfChannels, int encoding, const short *buffer, integer numberOfSamples) { |
| 2148 | try { |
| 2149 | FILE *f = file -> filePointer; |
| 2150 | if (! f) |
| 2151 | Melder_throw (U"File ", MelderFile_messageName (file), U" is not open."); |
| 2152 | integer n = numberOfSamples * numberOfChannels, start = 0, step = 1; |
| 2153 | if (numberOfChannels < 0) { |
| 2154 | n = numberOfSamples * 2; // stereo |
| 2155 | step = 2; // only one channel will be written |
| 2156 | if (numberOfChannels == -2) |
| 2157 | start = 1; // right channel |
| 2158 | numberOfChannels = 1; |
| 2159 | } |
| 2160 | switch (encoding) { |
| 2161 | case Melder_LINEAR_8_SIGNED: |
| 2162 | for (integer i = start; i < n; i += step) |
| 2163 | binputi8 (buffer [i] >> 8, f); |
| 2164 | break; case Melder_LINEAR_8_UNSIGNED: |
| 2165 | for (integer i = start; i < n; i += step) |
| 2166 | binputu8 ((buffer [i] >> 8) + 128, f); |
| 2167 | break; case Melder_LINEAR_16_BIG_ENDIAN: |
| 2168 | for (integer i = start; i < n; i += step) |
| 2169 | binputi16 (buffer [i], f); |
| 2170 | break; case Melder_LINEAR_16_LITTLE_ENDIAN: |
| 2171 | for (integer i = start; i < n; i += step) |
| 2172 | binputi16LE (buffer [i], f); |
| 2173 | break; case Melder_LINEAR_24_BIG_ENDIAN: |
| 2174 | for (integer i = start; i < n; i += step) |
| 2175 | binputi24 (buffer [i] << 8, f); |
| 2176 | break; case Melder_LINEAR_24_LITTLE_ENDIAN: |
| 2177 | for (integer i = start; i < n; i += step) |
| 2178 | binputi24LE (buffer [i] << 8, f); |
| 2179 | break; case Melder_LINEAR_32_BIG_ENDIAN: |
| 2180 | for (integer i = start; i < n; i += step) |
| 2181 | binputi32 (buffer [i] << 16, f); |
| 2182 | break; case Melder_LINEAR_32_LITTLE_ENDIAN: |
| 2183 | for (integer i = start; i < n; i += step) |
| 2184 | binputi32LE (buffer [i] << 16, f); |
| 2185 | break; case Melder_IEEE_FLOAT_32_BIG_ENDIAN: |
| 2186 | for (integer i = start; i < n; i += step) |
| 2187 | binputr32 (buffer [i] / 32768.0, f); |
| 2188 | break; case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN: |
| 2189 | for (integer i = start; i < n; i += step) |
| 2190 | binputr32LE (buffer [i] / 32768.0, f); |
| 2191 | break; case Melder_IEEE_FLOAT_64_BIG_ENDIAN: |
| 2192 | for (integer i = start; i < n; i += step) |
| 2193 | binputr64 (buffer [i] / 32768.0, f); |
| 2194 | break; case Melder_IEEE_FLOAT_64_LITTLE_ENDIAN: |
| 2195 | for (integer i = start; i < n; i += step) |
| 2196 | binputr64LE (buffer [i] / 32768.0, f); |
| 2197 | break; |
| 2198 | case Melder_FLAC_COMPRESSION_16: |
| 2199 | case Melder_FLAC_COMPRESSION_24: |
| 2200 | case Melder_FLAC_COMPRESSION_32: |
| 2201 | if (! file -> flacEncoder) |
| 2202 | Melder_throw (U"FLAC encoder not initialized."); |
| 2203 | for (integer i = start; i < n; i += step * numberOfChannels) { |
| 2204 | FLAC__int32 samples [FLAC__MAX_CHANNELS]; |
| 2205 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 2206 | samples [ichan - 1] = buffer [i + ichan - 1]; |
| 2207 | if (! FLAC__stream_encoder_process_interleaved (file -> flacEncoder, samples, 1)) |
| 2208 | Melder_throw (U"Error encoding FLAC stream."); |
| 2209 | } |
| 2210 | break; case Melder_MULAW: case Melder_ALAW: default: |
| 2211 | Melder_throw (U"Unknown encoding ", encoding, U"."); |
| 2212 | } |
| 2213 | } catch (MelderError) { |
| 2214 | Melder_throw (U"Samples not written to audio file."); |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | void MelderFile_writeFloatToAudio (MelderFile file, constMATVU const& buffer, int encoding, bool warnIfClipped) { |
| 2219 | try { |
| 2220 | FILE *f = file -> filePointer; |
| 2221 | if (! f) |
| 2222 | Melder_throw (U"File ", MelderFile_messageName (file), U" is not open."); |
| 2223 | const integer numberOfSamples = buffer.ncol, numberOfChannels = buffer.nrow; |
| 2224 | integer nclipped = 0; |
| 2225 | switch (encoding) { |
| 2226 | case Melder_LINEAR_8_SIGNED: |
| 2227 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2228 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2229 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 128.0); |
| 2230 | if (value < -128.0) { value = -128.0; nclipped ++; } |
| 2231 | if (value > 127.0) { value = 127.0; nclipped ++; } |
| 2232 | binputi8 ((int) value, f); |
| 2233 | } |
| 2234 | break; |
| 2235 | case Melder_LINEAR_8_UNSIGNED: |
| 2236 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2237 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2238 | /* mutable clip */ double value = floor ((buffer [ichan] [isamp] + 1.0) * 128.0); |
| 2239 | if (value < 0.0) { value = 0.0; nclipped ++; } |
| 2240 | if (value > 255.0) { value = 255.0; nclipped ++; } |
| 2241 | binputu8 ((int) value, f); |
| 2242 | } |
| 2243 | break; |
| 2244 | case Melder_LINEAR_16_BIG_ENDIAN: |
| 2245 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2246 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2247 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 32768.0); |
| 2248 | if (value < -32768.0) { value = -32768.0; nclipped ++; } |
| 2249 | if (value > 32767.0) { value = 32767.0; nclipped ++; } |
| 2250 | binputi16 ((int16) value, f); |
| 2251 | } |
| 2252 | break; |
| 2253 | case Melder_LINEAR_16_LITTLE_ENDIAN: |
| 2254 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2255 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2256 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 32768.0); |
| 2257 | if (value < -32768.0) { value = -32768.0; nclipped ++; } |
| 2258 | if (value > 32767.0) { value = 32767.0; nclipped ++; } |
| 2259 | binputi16LE ((int16) value, f); |
| 2260 | } |
| 2261 | break; |
| 2262 | case Melder_LINEAR_24_BIG_ENDIAN: |
| 2263 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2264 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2265 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 8388608.0); |
| 2266 | if (value < -8388608.0) { value = -8388608.0; nclipped ++; } |
| 2267 | if (value > 8388607.0) { value = 8388607.0; nclipped ++; } |
| 2268 | binputi24 ((int32) value, f); |
| 2269 | } |
| 2270 | break; |
| 2271 | case Melder_LINEAR_24_LITTLE_ENDIAN: |
| 2272 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2273 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2274 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 8388608.0); |
| 2275 | if (value < -8388608.0) { value = -8388608.0; nclipped ++; } |
| 2276 | if (value > 8388607.0) { value = 8388607.0; nclipped ++; } |
| 2277 | binputi24LE ((int32) value, f); |
| 2278 | } |
| 2279 | break; |
| 2280 | case Melder_LINEAR_32_BIG_ENDIAN: |
| 2281 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2282 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2283 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 2147483648.0); |
| 2284 | if (value < -2147483648.0) { value = -2147483648.0; nclipped ++; } |
| 2285 | if (value > 2147483647.0) { value = 2147483647.0; nclipped ++; } |
| 2286 | binputi32 ((int32) value, f); // safe cast: rounding and range already handled |
| 2287 | } |
| 2288 | break; |
| 2289 | case Melder_LINEAR_32_LITTLE_ENDIAN: |
| 2290 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2291 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2292 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 2147483648.0); |
| 2293 | if (value < -2147483648.0) { value = -2147483648.0; nclipped ++; } |
| 2294 | if (value > 2147483647.0) { value = 2147483647.0; nclipped ++; } |
| 2295 | binputi32LE ((int32) value, f); |
| 2296 | } |
| 2297 | break; |
| 2298 | case Melder_IEEE_FLOAT_32_BIG_ENDIAN: |
| 2299 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2300 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2301 | const double value = buffer [ichan] [isamp]; |
| 2302 | binputr32 (value, f); |
| 2303 | } |
| 2304 | break; |
| 2305 | case Melder_IEEE_FLOAT_32_LITTLE_ENDIAN: |
| 2306 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2307 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2308 | const double value = buffer [ichan] [isamp]; |
| 2309 | binputr32LE (value, f); |
| 2310 | } |
| 2311 | break; |
| 2312 | case Melder_IEEE_FLOAT_64_BIG_ENDIAN: |
| 2313 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2314 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2315 | const double value = buffer [ichan] [isamp]; |
| 2316 | binputr64 (value, f); |
| 2317 | } |
| 2318 | break; |
| 2319 | case Melder_IEEE_FLOAT_64_LITTLE_ENDIAN: |
| 2320 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) |
| 2321 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2322 | const double value = buffer [ichan] [isamp]; |
| 2323 | binputr64LE (value, f); |
| 2324 | } |
| 2325 | break; |
| 2326 | case Melder_FLAC_COMPRESSION_16: |
| 2327 | case Melder_FLAC_COMPRESSION_24: |
| 2328 | case Melder_FLAC_COMPRESSION_32: |
| 2329 | if (! file -> flacEncoder) |
| 2330 | Melder_throw (U"FLAC encoder not initialized."); |
| 2331 | for (integer isamp = 1; isamp <= numberOfSamples; isamp ++) { |
| 2332 | FLAC__int32 samples [FLAC__MAX_CHANNELS]; |
| 2333 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 2334 | /* mutable clip */ double value = round (buffer [ichan] [isamp] * 32768.0); |
| 2335 | if (value < -32768.0) { value = -32768.0; nclipped ++; } |
| 2336 | if (value > 32767.0) { value = 32767.0; nclipped ++; } |
| 2337 | samples [ichan - 1] = (FLAC__int32) value; |
| 2338 | } |
| 2339 | if (! FLAC__stream_encoder_process_interleaved (file -> flacEncoder, samples, 1)) |
| 2340 | Melder_throw (U"Error encoding FLAC stream."); |
| 2341 | } |
| 2342 | break; |
| 2343 | case Melder_MULAW: |
| 2344 | case Melder_ALAW: |
| 2345 | default: |
| 2346 | Melder_throw (U"Unknown format."); |
| 2347 | } |
| 2348 | if (nclipped > 0 && warnIfClipped) |
| 2349 | Melder_warning (U"Writing samples to audio file ", MelderFile_messageName (file), U": ", |
| 2350 | nclipped, U" out of ", numberOfSamples, U" samples have been clipped.\n" |
| 2351 | U"Advice: you could scale the amplitudes or write to a binary file." |
| 2352 | ); |
| 2353 | } catch (MelderError) { |
| 2354 | Melder_throw (U"Samples not written to audio file."); |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | void Melder_audiofiles_init () { |
| 2359 | } |
| 2360 | |
| 2361 | /* End of file melder_audiofiles.cpp */ |