| 1 |
/* Sound_files.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2018,2020,2022-2024 Paul Boersma & David Weenink |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* pb 2002/07/16 GPL |
| 21 |
* pb 2003/09/12 MelderFile_checkSoundFile |
| 22 |
* pb 2004/10/17 test for NULL file pointer when closing in Sound_read(2)FromSoundFile |
| 23 |
* pb 2005/06/17 Mac headers |
| 24 |
* pb 2006/01/05 movies for Mac |
| 25 |
* pb 2006/05/29 QuickTime inclusion made optional |
| 26 |
* pb 2006/10/28 erased MacOS 9 stuff |
| 27 |
* pb 2006/12/30 stereo |
| 28 |
* pb 2006/01/01 more stereo |
| 29 |
* pb 2007/01/28 removed a warning |
| 30 |
* pb 2007/01/28 made readFromMovieFile compatible with stereo |
| 31 |
* pb 2007/03/17 resistant against conflicting declarations of Collection |
| 32 |
* pb 2007/05/08 removed warning about stereo sounds |
| 33 |
* pb 2007/10/05 removed FSSpec |
| 34 |
* pb 2007/10/05 made Sound_readFromMacSoundFile compatible with sampling frequencies between 32768 and 65535 Hz |
| 35 |
* pb 2008/01/19 double |
| 36 |
* pb 2009/09/21 made stereo movies readable |
| 37 |
* pb 2010/12/27 support for multiple channels (i.e. more than two) |
| 38 |
* pb 2011/06/07 C++ |
| 39 |
* pb 2012/04/29 removed Sound Manager stuff |
| 40 |
* pb 2012/04/29 removed QuickTime |
| 41 |
*/ |
| 42 |
|
| 43 |
/* |
| 44 |
static void Sound_ulawDecode (Sound me) { |
| 45 |
double mu = 100, lnu1 = log (1 + mu); |
| 46 |
for (integer i = 1; i <= my nx; i ++) { |
| 47 |
double zabs = (exp (fabs (my z [1] [i]) * lnu1) - 1.0) / mu; |
| 48 |
my z [1] [i] = my z [1] [i] < 0 ? -zabs : zabs; |
| 49 |
} |
| 50 |
} |
| 51 |
static void Sound_alawDecode (Sound me) { |
| 52 |
double a = 87.6, lna1 = 1.0 + log (a); |
| 53 |
for (integer i = 1; i <= my nx; i ++) { |
| 54 |
double zabs = fabs (my z [1] [i]); |
| 55 |
if (zabs <= 1.0 / lna1) { |
| 56 |
my z [1] [i] *= lna1 / a; |
| 57 |
} else { |
| 58 |
double t = exp (lna1 * zabs - 1.0) / a; |
| 59 |
my z [1] [i] = my z [1] [i] > 0 ? t : - t; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
*/ |
| 64 |
|
| 65 |
#include <time.h> |
| 66 |
#include "Sound.h" |
| 67 |
|
| 68 |
autoSound Sound_readFromSoundFile (MelderFile file) { |
| 69 |
try { |
| 70 |
autoMelderFile mfile = MelderFile_open (file); |
| 71 |
int encoding; |
| 72 |
double sampleRate; |
| 73 |
integer startOfData, numberOfSamples, numberOfChannels; |
| 74 |
int fileType = MelderFile_checkSoundFile (file, & numberOfChannels, & encoding, & sampleRate, & startOfData, & numberOfSamples); |
| 75 |
if (fileType == 0) |
| 76 |
Melder_throw (U"Not an audio file."); |
| 77 |
try { |
| 78 |
MelderFile_seek (file, startOfData, SEEK_SET); // start from beginning of Data Chunk |
| 79 |
} catch (MelderError) { |
| 80 |
Melder_throw (U"No data in audio file."); |
| 81 |
} |
| 82 |
if (numberOfSamples < 1) |
| 83 |
Melder_throw (U"Audio file contains 0 samples."); |
| 84 |
autoSound me = Sound_createSimple (numberOfChannels, numberOfSamples / sampleRate, sampleRate); |
| 85 |
Melder_assert (my z.ncol == numberOfSamples); |
| 86 |
if (encoding == Melder_SHORTEN) |
| 87 |
Melder_throw (U"Cannot unshorten. Write to [email protected] for more information."); |
| 88 |
Melder_readAudioToFloat (file, encoding, my z.get()); |
| 89 |
mfile.close (); |
| 90 |
return me; |
| 91 |
} catch (MelderError) { |
| 92 |
Melder_throw (U"Sound not read from sound file ", file, U"."); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
autoSound Sound_readFromSesamFile (MelderFile file) { |
| 97 |
try { |
| 98 |
autofile f = Melder_fopen (file, "rb"); |
| 99 |
int32 header [1 + 128]; |
| 100 |
for (integer i = 1; i <= 128; i ++) |
| 101 |
header [i] = bingeti32LE (f); |
| 102 |
/* |
| 103 |
* Try SESAM header. |
| 104 |
*/ |
| 105 |
double samplingFrequency = header [126]; // converting up (from 32 to 54 bits) |
| 106 |
int32 numberOfSamples = header [127]; |
| 107 |
if (samplingFrequency == 0.0 || numberOfSamples == 0) { |
| 108 |
/* |
| 109 |
Try LVS header. |
| 110 |
*/ |
| 111 |
samplingFrequency = header [62]; |
| 112 |
numberOfSamples = (header [6] << 8) - header [68]; |
| 113 |
} |
| 114 |
if (numberOfSamples < 1 || numberOfSamples > 1000000000 || samplingFrequency < 10.0 || samplingFrequency > 100000000.0) |
| 115 |
Melder_throw (U"Not a correct SESAM or LVS file."); |
| 116 |
autoSound me = Sound_createSimple (1, numberOfSamples / samplingFrequency, samplingFrequency); |
| 117 |
for (int32 i = 1; i <= numberOfSamples; i ++) { |
| 118 |
my z [1] [i] = (double) bingeti16LE (f) * (1.0 / 2048); // 12 bits |
| 119 |
} |
| 120 |
f.close (file); |
| 121 |
return me; |
| 122 |
} catch (MelderError) { |
| 123 |
Melder_throw (U"Sound not read from Sesam file ", file, U"."); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
autoSound Sound_readFromBellLabsFile (MelderFile file) { |
| 128 |
try { |
| 129 |
autofile f = Melder_fopen (file, "rb"); |
| 130 |
|
| 131 |
/* |
| 132 |
Check identity of file: first line is "SIG", second line contains a number. |
| 133 |
*/ |
| 134 |
char tag [200]; |
| 135 |
if (fread (tag, 1, 16, f) < 16 || ! strnequ (tag, "SIG\n", 4)) |
| 136 |
Melder_throw (U"Not a Bell-Labs sound file."); |
| 137 |
char *endOfTag = strchr (tag + 4, '\n'); |
| 138 |
if (! endOfTag) |
| 139 |
Melder_throw (U"Second line missing or too long."); |
| 140 |
integer tagLength = (endOfTag - tag) + 1; // probably 12 |
| 141 |
int64 headerLength = atol (tag + 4); |
| 142 |
if (headerLength <= 0) |
| 143 |
Melder_throw (U"Wrong header-length info."); |
| 144 |
|
| 145 |
/* |
| 146 |
Read data from header. |
| 147 |
Use defaults if necessary. |
| 148 |
*/ |
| 149 |
autostring8 lines (headerLength); |
| 150 |
if ((int64) fread (lines.get(), 1, (size_t) headerLength, f) < headerLength) |
| 151 |
Melder_throw (U"Header too short."); |
| 152 |
integer numberOfSamples = 0; |
| 153 |
char *psamples = & lines [-1]; |
| 154 |
while (!! (psamples = strstr (psamples + 1, "samples "))) // take last occurrence |
| 155 |
numberOfSamples = atol (psamples + 8); |
| 156 |
if (numberOfSamples < 1) { |
| 157 |
/* Use file length. */ |
| 158 |
fseek (f, 0, SEEK_END); /* Position file pointer at end of file. */ |
| 159 |
numberOfSamples = (ftell (f) - tagLength - headerLength) / 2; |
| 160 |
} |
| 161 |
if (numberOfSamples < 1) |
| 162 |
Melder_throw (U"No samples found."); |
| 163 |
double samplingFrequency = 0.0; |
| 164 |
char *pfrequency = & lines [-1]; |
| 165 |
while (!! (pfrequency = strstr (pfrequency + 1, "frequency "))) // take last occurrence |
| 166 |
samplingFrequency = atof (pfrequency + 10); |
| 167 |
if (samplingFrequency <= 0.0) |
| 168 |
samplingFrequency = 16000.0; |
| 169 |
|
| 170 |
/* |
| 171 |
Create sound. |
| 172 |
*/ |
| 173 |
autoSound me = Sound_createSimple (1, numberOfSamples / samplingFrequency, samplingFrequency); |
| 174 |
|
| 175 |
/* |
| 176 |
Read samples. |
| 177 |
*/ |
| 178 |
fseek (f, tagLength + headerLength, SEEK_SET); |
| 179 |
for (integer i = 1; i <= numberOfSamples; i ++) |
| 180 |
my z [1] [i] = (double) bingeti16 (f) * (1.0 / 32768); // 16-bits big-endian |
| 181 |
|
| 182 |
f.close (file); |
| 183 |
return me; |
| 184 |
} catch (MelderError) { |
| 185 |
Melder_throw (U"Sound not read from Bell Labs sound file ", file, U"."); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static void readError () { |
| 190 |
Melder_throw (U"Error reading bytes from file."); |
| 191 |
} |
| 192 |
|
| 193 |
static autoDaata Sound_readFromKayFile (MelderFile file) { |
| 194 |
FILE *f = file -> filePointer; |
| 195 |
uint32 chunkSize = bingetu32LE (f); |
| 196 |
if (chunkSize & 1) |
| 197 |
chunkSize += 1; // round up to even |
| 198 |
if (chunkSize != 32 && chunkSize != 44) |
| 199 |
Melder_throw (U"Unknown chunk size ", chunkSize, U". Please report to [email protected]."); |
| 200 |
char data [100]; |
| 201 |
if (fread (data, 1, 20, f) < 20) |
| 202 |
readError (); |
| 203 |
const double samplingFrequency = bingetu32LE (f); // converting up (from 32 to 53 bits) |
| 204 |
const integer numberOfSamples = bingetu32LE (f); |
| 205 |
if (samplingFrequency <= 0 || samplingFrequency > 1e7 || numberOfSamples >= 1000000000) |
| 206 |
Melder_throw (U"Not a correct Kay file."); |
| 207 |
const int16 tmp1 = bingeti16LE (f); |
| 208 |
const int16 tmp2 = bingeti16LE (f); |
| 209 |
integer numberOfChannels = ( tmp1 == -1 || tmp2 == -1 ? 1 : 2 ); |
| 210 |
if (chunkSize == 44) { |
| 211 |
const int16 tmp3 = bingeti16LE (f); |
| 212 |
if (tmp3 != -1) |
| 213 |
numberOfChannels ++; |
| 214 |
const int16 tmp4 = bingeti16LE (f); |
| 215 |
if (tmp4 != -1) |
| 216 |
numberOfChannels ++; |
| 217 |
const int16 tmp5 = bingeti16LE (f); |
| 218 |
if (tmp5 != -1) |
| 219 |
numberOfChannels ++; |
| 220 |
const int16 tmp6 = bingeti16LE (f); |
| 221 |
if (tmp6 != -1) |
| 222 |
numberOfChannels ++; |
| 223 |
const int16 tmp7 = bingeti16LE (f); |
| 224 |
if (tmp7 != -1) |
| 225 |
numberOfChannels ++; |
| 226 |
const int16 tmp8 = bingeti16LE (f); |
| 227 |
if (tmp8 != -1) |
| 228 |
numberOfChannels ++; |
| 229 |
} |
| 230 |
/* |
| 231 |
SD chunk. |
| 232 |
*/ |
| 233 |
autoSound me = Sound_createSimple (numberOfChannels, numberOfSamples / samplingFrequency, samplingFrequency); |
| 234 |
for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 235 |
if (fread (data, 1, 4, f) < 4) |
| 236 |
readError (); |
| 237 |
while (! strnequ (data, "SD", 2)) { |
| 238 |
if (feof ((FILE *) f)) |
| 239 |
Melder_throw (U"Missing or unreadable SD chunk. Please report to [email protected]."); |
| 240 |
chunkSize = bingetu32LE (f); |
| 241 |
if (chunkSize & 1) |
| 242 |
chunkSize += 1; // round up to even |
| 243 |
Melder_casual (U"Chunk ", |
| 244 |
data [0], U" ", data [1], U" ", data [2], U" ", data [3], U" ", chunkSize); |
| 245 |
fseek (f, chunkSize, SEEK_CUR); |
| 246 |
if (fread (data, 1, 4, f) < 4) |
| 247 |
readError (); |
| 248 |
} |
| 249 |
chunkSize = bingetu32LE (f); |
| 250 |
integer residual = chunkSize - numberOfSamples * 2; |
| 251 |
if (residual == -2 || residual == 2) { |
| 252 |
const integer localNumberOfSamples = numberOfSamples + residual / 2; |
| 253 |
//TRACE |
| 254 |
trace (U"Reading ", localNumberOfSamples, U" samples instead of the promised ", numberOfSamples, U"."); |
| 255 |
if (ichan == 1) { |
| 256 |
me = Sound_extractPart (me.get(), 0.0, localNumberOfSamples / samplingFrequency, kSound_windowShape::RECTANGULAR, 1.0, true); |
| 257 |
Melder_assert (my nx == localNumberOfSamples); |
| 258 |
} else { |
| 259 |
Melder_require (my nx == localNumberOfSamples, |
| 260 |
U"Not all channels have the same numbers of samples."); |
| 261 |
} |
| 262 |
for (integer i = 1; i <= localNumberOfSamples; i ++) |
| 263 |
my z [ichan] [i] = (double) bingeti16LE (f) / 32768.0; |
| 264 |
} else if (residual != 0) { |
| 265 |
Melder_throw (U"Incomplete SD chunk: attested size ", chunkSize, U" bytes," |
| 266 |
U" announced size ", numberOfSamples * 2, U" bytes. Please report to [email protected]."); |
| 267 |
} else { |
| 268 |
for (integer i = 1; i <= numberOfSamples; i ++) |
| 269 |
my z [ichan] [i] = (double) bingeti16LE (f) / 32768.0; |
| 270 |
} |
| 271 |
} |
| 272 |
//Melder_casual (ftell (f)); |
| 273 |
return me; |
| 274 |
} |
| 275 |
static autoDaata Sounds_readFromKayNasalityFile (MelderFile file) { |
| 276 |
FILE *f = file -> filePointer; |
| 277 |
uint32 chunkSize = bingetu32LE (f); |
| 278 |
if (chunkSize & 1) |
| 279 |
chunkSize += 1; // round up to even |
| 280 |
if (chunkSize != 100) |
| 281 |
Melder_throw (U"Unknown chunk size ", chunkSize, U". Please report to [email protected]."); |
| 282 |
char data [100]; |
| 283 |
if (fread (data, 1, 20, f) < 20) |
| 284 |
readError (); |
| 285 |
const int16 tmp1 = bingeti16LE (f); |
| 286 |
const int16 tmp2 = bingeti16LE (f); |
| 287 |
integer numberOfChannels = ( tmp1 == -1 || tmp2 == -1 ? 1 : 2 ); |
| 288 |
const int16 tmp3 = bingeti16LE (f); |
| 289 |
if (tmp3 != -1) |
| 290 |
numberOfChannels ++; |
| 291 |
const int16 tmp4 = bingeti16LE (f); |
| 292 |
if (tmp4 != -1) |
| 293 |
numberOfChannels ++; |
| 294 |
const int16 tmp5 = bingeti16LE (f); |
| 295 |
if (tmp5 != -1) |
| 296 |
numberOfChannels ++; |
| 297 |
const int16 tmp6 = bingeti16LE (f); |
| 298 |
if (tmp6 != -1) |
| 299 |
numberOfChannels ++; |
| 300 |
const int16 tmp7 = bingeti16LE (f); |
| 301 |
if (tmp7 != -1) |
| 302 |
numberOfChannels ++; |
| 303 |
const int16 tmp8 = bingeti16LE (f); |
| 304 |
if (tmp8 != -1) |
| 305 |
numberOfChannels ++; |
| 306 |
Melder_require (numberOfChannels >= 2, |
| 307 |
U"Expecting at least two channels (nasal and oral) instead of ", numberOfChannels, |
| 308 |
U". Please report to [email protected]." |
| 309 |
); |
| 310 |
Melder_require (numberOfChannels <= 3, |
| 311 |
U"Expecting at most three channels (nasal, oral and nasalance) instead of ", numberOfChannels, |
| 312 |
U". Please report to [email protected]." |
| 313 |
); |
| 314 |
double channelSamplingFrequencies [1+8] = { 0.0, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined }; |
| 315 |
for (int ichan = 1; ichan <= 8; ichan ++) |
| 316 |
channelSamplingFrequencies [ichan] = bingetu32LE (f); // converting up (from 32 to 53 bits) |
| 317 |
Melder_require (channelSamplingFrequencies [2] == channelSamplingFrequencies [1], |
| 318 |
U"Expecting equal sampling frequencies for channels 1 (nasal) and 2 (oral), " |
| 319 |
"but found differing sampling frequencies (", channelSamplingFrequencies [1], |
| 320 |
U" and ", channelSamplingFrequencies [2], U" Hz, respectively). Please report to [email protected]." |
| 321 |
); |
| 322 |
for (integer ichan = numberOfChannels + 1; ichan <= 8; ichan ++) |
| 323 |
Melder_require (channelSamplingFrequencies [ichan] == 0.0, |
| 324 |
U"Sampling frequency of non-existent channel ", ichan, |
| 325 |
U" should be 0 but is ", channelSamplingFrequencies [ichan], |
| 326 |
U" Hz. Please report to [email protected]." |
| 327 |
); |
| 328 |
integer channelNumbersOfSamples [1+8] = { 0, -1, -1, -1, -1, -1, -1, -1, -1 }; |
| 329 |
for (integer ichan = 1; ichan <= 8; ichan ++) |
| 330 |
channelNumbersOfSamples [ichan] = bingetu32LE (f); |
| 331 |
Melder_require (channelNumbersOfSamples [2] == channelNumbersOfSamples [1], |
| 332 |
U"Expecting equal numbers of samples in channels 1 (nasal) and 2 (oral), " |
| 333 |
"but found differing numbers of samples (", channelNumbersOfSamples [1], |
| 334 |
U" and ", channelNumbersOfSamples [2], U", respectively). Please report to [email protected]." |
| 335 |
); |
| 336 |
for (integer ichan = numberOfChannels + 1; ichan <= 8; ichan ++) |
| 337 |
Melder_require (channelNumbersOfSamples [ichan] == 0, |
| 338 |
U"Sample count of non-existent channel ", ichan, |
| 339 |
U" should be 0 but is ", channelNumbersOfSamples [ichan], |
| 340 |
U". Please report to [email protected]." |
| 341 |
); |
| 342 |
/* |
| 343 |
SD chunk. |
| 344 |
*/ |
| 345 |
autoCollection me = Collection_create (); |
| 346 |
for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) { |
| 347 |
if (fread (data, 1, 4, f) < 4) |
| 348 |
readError (); |
| 349 |
while (! strnequ (data, "SD", 2)) { |
| 350 |
if (feof ((FILE *) f)) |
| 351 |
Melder_throw (U"Missing or unreadable SD chunk. Please report to [email protected]."); |
| 352 |
chunkSize = bingetu32LE (f); |
| 353 |
if (chunkSize & 1) |
| 354 |
chunkSize += 1; // round up to even |
| 355 |
Melder_casual (U"Chunk ", |
| 356 |
data [0], U" ", data [1], U" ", data [2], U" ", data [3], U" ", chunkSize); |
| 357 |
fseek (f, chunkSize, SEEK_CUR); |
| 358 |
if (fread (data, 1, 4, f) < 4) |
| 359 |
readError (); |
| 360 |
} |
| 361 |
chunkSize = bingetu32LE (f); |
| 362 |
#if 0 |
| 363 |
integer residual = chunkSize - numberOfSamples * 2; |
| 364 |
if (residual < 0) |
| 365 |
Melder_throw (U"Incomplete SD chunk: attested size ", chunkSize, U" bytes," |
| 366 |
U" announced size ", numberOfSamples * 2, U" bytes. Please report to [email protected]."); |
| 367 |
#endif |
| 368 |
autoSound you = Sound_createSimple (1, |
| 369 |
channelNumbersOfSamples [ichan] / channelSamplingFrequencies [ichan], |
| 370 |
channelSamplingFrequencies [ichan] |
| 371 |
); |
| 372 |
if (channelNumbersOfSamples [ichan] == channelNumbersOfSamples [1]) |
| 373 |
for (integer i = 1; i <= channelNumbersOfSamples [ichan]; i ++) |
| 374 |
your z [1] [i] = (double) bingeti16LE (f) / 32768.0; |
| 375 |
else |
| 376 |
for (integer i = 1; i <= channelNumbersOfSamples [ichan]; i ++) |
| 377 |
your z [1] [i] = (double) bingeti16LE (f) / 100.0; |
| 378 |
//fseek (f, residual, SEEK_CUR); |
| 379 |
Thing_setName (you.get(), ichan == 1 ? U"_nasal" : ichan == 2 ? U"_oral" : U"_nasalance"); |
| 380 |
my addItem_move (you.move()); |
| 381 |
} |
| 382 |
//Melder_casual (ftell (f)); |
| 383 |
return me; |
| 384 |
} |
| 385 |
|
| 386 |
autoDaata Sound_readFromAnyKayFile (MelderFile file) { |
| 387 |
try { |
| 388 |
autofile f = Melder_fopen (file, "rb"); |
| 389 |
/* |
| 390 |
Read header of KAY file: 12 bytes. |
| 391 |
*/ |
| 392 |
char data [100]; |
| 393 |
if (fread (data, 1, 8, f) < 8) |
| 394 |
readError (); |
| 395 |
if (! strnequ (data, "FORMDS16", 8)) |
| 396 |
Melder_throw (U"Not a KAY DS-16 file."); |
| 397 |
if (fread (data, 1, 4, f) < 4) // this contains the size of the DS16 chunk: typically 12 bytes less than the size of the file |
| 398 |
readError (); |
| 399 |
/* |
| 400 |
Read header of HEDR or HDR8 or NHDR chunk. |
| 401 |
*/ |
| 402 |
if (fread (data, 1, 4, f) < 4) |
| 403 |
readError (); |
| 404 |
if (! strnequ (data, "HEDR", 4) && ! strnequ (data, "HDR8", 4) && ! strnequ (data, "NHDR", 4)) |
| 405 |
Melder_throw (U"Missing HEDR or HDR8 or NHDR chunk. Please report to [email protected]."); |
| 406 |
/* |
| 407 |
Read remainder of HEDR or HDR8 or NHDR chunk. |
| 408 |
*/ |
| 409 |
file -> filePointer = f; |
| 410 |
autoDaata result = ( strnequ (data, "NHDR", 4) ? Sounds_readFromKayNasalityFile (file) : Sound_readFromKayFile (file) ); |
| 411 |
f.close (file); |
| 412 |
return result; |
| 413 |
} catch (MelderError) { |
| 414 |
Melder_throw (U"Sound not read from Kay file ", file, U"."); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
autoSound Sound_readFromRawAlawFile (MelderFile file) { |
| 419 |
try { |
| 420 |
double sampleRate = 8000.0; |
| 421 |
autoMelderFile mfile = MelderFile_open (file); |
| 422 |
MelderFile_seek (file, 0, SEEK_END); |
| 423 |
integer numberOfSamples = MelderFile_tell (file); |
| 424 |
MelderFile_rewind (file); |
| 425 |
autoSound me = Sound_createSimple (1, numberOfSamples / sampleRate, sampleRate); |
| 426 |
Melder_assert (my z.ncol == numberOfSamples); |
| 427 |
Melder_readAudioToFloat (file, Melder_ALAW, my z.get()); |
| 428 |
mfile.close (); |
| 429 |
return me; |
| 430 |
} catch (MelderError) { |
| 431 |
Melder_throw (U"Sound not read from raw A-law file ", file, U"."); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
void Sound_saveAsAudioFile (constSound me, MelderFile file, int audioFileType, int numberOfBitsPerSamplePoint) { |
| 436 |
try { |
| 437 |
autoMelderFile mfile = MelderFile_create (file); |
| 438 |
MelderFile_writeAudioFileHeader (file, audioFileType, Melder_iround_tieDown (1.0 / my dx), my nx, my ny, numberOfBitsPerSamplePoint); |
| 439 |
MelderFile_writeFloatToAudio (file, my z.get(), Melder_defaultAudioFileEncoding (audioFileType, numberOfBitsPerSamplePoint), true); |
| 440 |
MelderFile_writeAudioFileTrailer (file, audioFileType, Melder_iround (1.0 / my dx), my nx, my ny, numberOfBitsPerSamplePoint); |
| 441 |
mfile.close (); |
| 442 |
} catch (MelderError) { |
| 443 |
Melder_throw (me, U": not written to 16-bit sound file ", file, U"."); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
void Sound_saveAsSesamFile (constSound me, MelderFile file) { |
| 448 |
try { |
| 449 |
autofile f = Melder_fopen (file, "wb"); |
| 450 |
integer header [1 + 128]; |
| 451 |
for (integer i = 1; i <= 128; i ++) |
| 452 |
header [i] = 0; |
| 453 |
/* |
| 454 |
ILS header. |
| 455 |
*/ |
| 456 |
header [6] = ((my nx - 1) >> 8) + 1; // number of disk blocks |
| 457 |
header [64] = 32149; // ILS magic |
| 458 |
/* |
| 459 |
LVS header. |
| 460 |
*/ |
| 461 |
header [62] = Melder_iround_tieDown (1.0 / my dx); // sampling frequency, rounded to n Hz |
| 462 |
header [63] = -32000; // magic: "sampled signal" |
| 463 |
header [66] = INT12_MAX; // maximum absolute value: 12 bits |
| 464 |
header [67] = 2047; // LVS magic |
| 465 |
header [68] = my nx % 256; // number of samples in last block |
| 466 |
header [69] = 1; // ? |
| 467 |
/* |
| 468 |
Sesam header. |
| 469 |
*/ |
| 470 |
header [126] = Melder_iround_tieDown (1.0 / my dx); // sampling frequency, rounded to n Hz |
| 471 |
header [127] = my nx; // number of samples |
| 472 |
for (integer i = 1; i <= 128; i ++) |
| 473 |
binputi32LE (header [i], f); |
| 474 |
for (integer i = 1; i <= my nx; i ++) |
| 475 |
binputi16LE (Melder_iround_tieDown (my z [1] [i] * 2048.0), f); |
| 476 |
integer tail = 256 - my nx % 256; |
| 477 |
if (tail == 256) |
| 478 |
tail = 0; |
| 479 |
for (integer i = 1; i <= tail; i ++) |
| 480 |
binputi16LE (0, f); // pad last block with zeroes |
| 481 |
f.close (file); |
| 482 |
} catch (MelderError) { |
| 483 |
Melder_throw (me, U": not written to Sesam file ", file, U"."); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
void Sound_saveAsKayFile (constSound me, MelderFile file) { |
| 488 |
try { |
| 489 |
Melder_require (my ny <= 8, |
| 490 |
U"Cannot write more than 8 channels into a Kay sound file."); |
| 491 |
constexpr int32 maximumNumberOfSamples32 = (INT32_MAX - 48) / 2; |
| 492 |
Melder_require (my nx <= maximumNumberOfSamples32, |
| 493 |
U"Cannot write more than ", maximumNumberOfSamples32, U" samples into a Kay sound file."); |
| 494 |
const int32 numberOfSamples32 = int (my nx); |
| 495 |
|
| 496 |
autoMelderFile mfile = MelderFile_create (file); |
| 497 |
|
| 498 |
/* |
| 499 |
Form Chunk: contains all other chunks. |
| 500 |
*/ |
| 501 |
fwrite ("FORMDS16", 1, 8, file -> filePointer); |
| 502 |
binputi32LE (48 + numberOfSamples32 * 2, file -> filePointer); // size of Form Chunk |
| 503 |
fwrite (my ny > 2 ? "HDR8" : "HEDR", 1, 4, file -> filePointer); |
| 504 |
binputi32LE (my ny > 2 ? 44 : 32, file -> filePointer); |
| 505 |
|
| 506 |
char date [100]; |
| 507 |
time_t today = time (nullptr); |
| 508 |
strcpy (date, ctime (& today)); |
| 509 |
fwrite (date+4, 1, 20, file -> filePointer); // skip weekday |
| 510 |
|
| 511 |
binputi32LE (Melder_iround_tieDown (1.0 / my dx), file -> filePointer); // sampling frequency |
| 512 |
binputi32LE (numberOfSamples32, file -> filePointer); // number of samples |
| 513 |
int maximumA = 0; |
| 514 |
for (integer i = 1; i <= my nx; i ++) { |
| 515 |
const integer value = Melder_iround_tieDown (my z [1] [i] * 32768.0); |
| 516 |
if (value < - maximumA) |
| 517 |
maximumA = - value; |
| 518 |
if (value > maximumA) |
| 519 |
maximumA = value; |
| 520 |
} |
| 521 |
binputi16LE (maximumA, file -> filePointer); // absolute maximum window A |
| 522 |
if (my ny == 1) { |
| 523 |
binputi16LE (-1, file -> filePointer); |
| 524 |
} else { |
| 525 |
for (integer ichannel = 2; ichannel <= my ny; ichannel ++) { |
| 526 |
int maximum = 0; |
| 527 |
for (integer i = 1; i <= my nx; i ++) { |
| 528 |
const integer value = Melder_iround_tieDown (my z [ichannel] [i] * 32768.0); |
| 529 |
if (value < - maximum) |
| 530 |
maximum = - value; |
| 531 |
if (value > maximum) |
| 532 |
maximum = value; |
| 533 |
} |
| 534 |
binputi16LE (maximum, file -> filePointer); // absolute maximum window B |
| 535 |
} |
| 536 |
if (my ny > 2) |
| 537 |
for (integer ichannel = my ny + 1; ichannel <= 8; ichannel ++) |
| 538 |
binputi16LE (-1, file -> filePointer); |
| 539 |
} |
| 540 |
fwrite ("SDA_", 1, 4, file -> filePointer); |
| 541 |
binputi32LE (numberOfSamples32 * 2, file -> filePointer); // chunk size |
| 542 |
MelderFile_writeFloatToAudio (file, |
| 543 |
my z.horizontalBand (1, 1), Melder_LINEAR_16_LITTLE_ENDIAN, true); |
| 544 |
if (my ny > 1) { |
| 545 |
fwrite ("SD_B", 1, 4, file -> filePointer); |
| 546 |
binputi32LE (numberOfSamples32 * 2, file -> filePointer); // chunk size |
| 547 |
MelderFile_writeFloatToAudio (file, |
| 548 |
my z.horizontalBand (2, 2), Melder_LINEAR_16_LITTLE_ENDIAN, true); |
| 549 |
} |
| 550 |
for (integer ichannel = 3; ichannel <= my ny; ichannel ++) { |
| 551 |
fwrite (Melder_peek32to8 (Melder_cat (U"SD_", ichannel)), 1, 4, file -> filePointer); |
| 552 |
binputi32LE (numberOfSamples32 * 2, file -> filePointer); // chunk size |
| 553 |
MelderFile_writeFloatToAudio (file, |
| 554 |
my z.horizontalBand (ichannel, ichannel), Melder_LINEAR_16_LITTLE_ENDIAN, true); |
| 555 |
} |
| 556 |
mfile.close (); |
| 557 |
} catch (MelderError) { |
| 558 |
Melder_throw (me, U": not written to Kay sound file ", file, U"."); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
void Sound_saveAsRawSoundFile (constSound me, MelderFile file, int encoding) { |
| 563 |
try { |
| 564 |
autoMelderFile mfile = MelderFile_create (file); |
| 565 |
MelderFile_writeFloatToAudio (file, my z.get(), encoding, true); |
| 566 |
mfile.close (); |
| 567 |
} catch (MelderError) { |
| 568 |
Melder_throw (me, U": not written to raw sound file ", file, U"."); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
/* End of file Sound_files.cpp */ |