| 1 |
#ifndef _Sound_h_ |
| 2 |
#define _Sound_h_ |
| 3 |
/* Sound.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2005,2006-2008,2010-2019,2021-2025 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* Sound inherits from Vector */ |
| 22 |
/* A Sound is a sampled signal, not quantized. */ |
| 23 |
#include "Vector.h" |
| 24 |
#include "Collection.h" |
| 25 |
|
| 26 |
#include "Sound_enums.h" |
| 27 |
|
| 28 |
Thing_define (Sound, Vector) { |
| 29 |
void v1_info () |
| 30 |
override; |
| 31 |
bool v_hasGetMatrix () const |
| 32 |
override { return true; } |
| 33 |
double v_getMatrix (integer irow, integer icol) const |
| 34 |
override; |
| 35 |
bool v_hasGetFunction2 () const |
| 36 |
override { return true; } |
| 37 |
double v_getFunction2 (double x, double y) const |
| 38 |
override; |
| 39 |
int v_domainQuantity () const |
| 40 |
override { return MelderQuantity_TIME_SECONDS; } |
| 41 |
}; |
| 42 |
|
| 43 |
/* Attributes: |
| 44 |
xmin // Start time (seconds). |
| 45 |
xmax // End time (seconds). |
| 46 |
nx // Number of samples. |
| 47 |
dx // Sampling period (seconds). |
| 48 |
x1 // Time of first sample (seconds). |
| 49 |
ymin == 1 // Left or only channel. |
| 50 |
ymax // Right or only channels. |
| 51 |
ny // Number of channels. |
| 52 |
dy == 1; y1 == 1 // y is channel number (1 = left or mono; 2 = right). |
| 53 |
z [i] [...] // Amplitude. |
| 54 |
z may be replaced (e.g., in pasting). |
| 55 |
*/ |
| 56 |
|
| 57 |
autoSound Sound_create (integer numberOfChannels, double xmin, double xmax, integer nx, double dx, double x1); |
| 58 |
/* |
| 59 |
Function: |
| 60 |
return a new silent Sound. |
| 61 |
Preconditions: |
| 62 |
xmax > xmin; |
| 63 |
nx >= 1; |
| 64 |
dx > 0.0; |
| 65 |
Postconditions: |
| 66 |
thy xmin == xmin; |
| 67 |
thy xmax == xmax; |
| 68 |
thy nx == nx; |
| 69 |
thy dx == dx; |
| 70 |
thy x1 == x1; |
| 71 |
thy ymin = 1.0; |
| 72 |
thy ymax = numberOfChannels; |
| 73 |
thy ny = numberOfChannels; |
| 74 |
thy dx = 1.0; |
| 75 |
thy x1 = 1.0; |
| 76 |
thy z [i] [1..nx] == 0.0; |
| 77 |
*/ |
| 78 |
|
| 79 |
autoSound Sound_createSimple (integer numberOfChannels, double duration, double samplingFrequency); |
| 80 |
/* |
| 81 |
Function: |
| 82 |
return a new silent Sound. |
| 83 |
Preconditions: |
| 84 |
duration > 0.0; |
| 85 |
samplingFrequency > 0.0; |
| 86 |
Postconditions: |
| 87 |
thy xmin == 0.0; |
| 88 |
thy xmax == duration; |
| 89 |
thy nx == Melder_iround (duration * samplingFrequency); |
| 90 |
thy dx == 1 / samplingFrequency; |
| 91 |
thy x1 == 0.5 * thy dx; // Centre of first sampling period. |
| 92 |
thy ymin = 1.0; |
| 93 |
thy ymax = numberOfChannels; |
| 94 |
thy ny = numberOfChannels; |
| 95 |
thy dx = 1.0; |
| 96 |
thy x1 = 1.0; |
| 97 |
thy z [i] [1..nx] == 0.0; |
| 98 |
*/ |
| 99 |
|
| 100 |
autoSound Sound_convertToMono (constSound me); |
| 101 |
autoSound Sound_convertToStereo (constSound me); |
| 102 |
autoSound Sound_extractChannel (constSound me, integer ichannel); |
| 103 |
autoSound Sound_extractChannels (constSound me, constINTVECVU const& channelNumbers); |
| 104 |
autoSound Sounds_combineToStereo (OrderedOf<structSound>* me); |
| 105 |
|
| 106 |
/* Levels for Sampled_getValueAtSample (me, index, level, unit) */ |
| 107 |
#define Sound_LEVEL_MONO 0 |
| 108 |
#define Sound_LEVEL_LEFT 1 |
| 109 |
#define Sound_LEVEL_RIGHT 2 |
| 110 |
|
| 111 |
autoSound Sound_upsample (constSound me); /* By a factor 2. */ |
| 112 |
|
| 113 |
autoSound Sound_resample (constSound me, double samplingFrequency, integer precision); |
| 114 |
/* |
| 115 |
Method: |
| 116 |
precision <= 1: linear interpolation. |
| 117 |
precision >= 2: sinx/x interpolation with maximum depth equal to 'precision'. |
| 118 |
*/ |
| 119 |
|
| 120 |
autoSound Sounds_append (constSound me, double silenceDuration, constSound thee); |
| 121 |
/* |
| 122 |
Function: |
| 123 |
append two Sounds. |
| 124 |
Failure: |
| 125 |
my dx != thy dx: "Sampling frequencies do not match." |
| 126 |
Postconditions: |
| 127 |
result -> xmin == 0; |
| 128 |
result -> xmax == result -> nx * my dx; |
| 129 |
result -> nx == my nx + thy nx + Melder_iround (silenceDuration / my dx); |
| 130 |
result -> dx == my dx; |
| 131 |
result -> x1 == 0.5 * my dx; |
| 132 |
for (i = 1..my nx) |
| 133 |
result -> z [1] [i] == my z [1] [i] |
| 134 |
for (i = 1..thy nx) |
| 135 |
result -> z [1] [i + my nx + Melder_iround (silenceDuration / my dx)] == thy z [1] [i] |
| 136 |
*/ |
| 137 |
|
| 138 |
autoSound Sounds_convolve (constSound me, constSound thee, kSounds_convolve_scaling scaling, kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain); |
| 139 |
/* |
| 140 |
Function: |
| 141 |
convolve two Sounds. |
| 142 |
Failure: |
| 143 |
my dx != thy dx: "Sampling frequencies do not match." |
| 144 |
Postconditions: |
| 145 |
result -> xmin == my xmin + thy xmin; |
| 146 |
result -> xmax == my xmax + thy xmax; |
| 147 |
result -> nx == my nx + thy nx - 1; |
| 148 |
result -> dx == my dx; |
| 149 |
result -> x1 == my x1 + thy x1; |
| 150 |
for (i = 1..result -> nx) |
| 151 |
result -> z [1] [i] == result -> dx * |
| 152 |
sum (j = 1..i, my z [1] [j] * thy z [1] [i - j + 1]) |
| 153 |
*/ |
| 154 |
autoSound Sounds_crossCorrelate (constSound me, constSound thee, kSounds_convolve_scaling scaling, kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain); |
| 155 |
autoSound Sounds_crossCorrelate_short (constSound me, constSound thee, double tmin, double tmax, bool normalize); |
| 156 |
autoSound Sound_autoCorrelate (constSound me, kSounds_convolve_scaling scaling, kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain); |
| 157 |
|
| 158 |
void Sound_shiftTimesToBetweenZeroAndPhysicalDuration (mutableSound me); |
| 159 |
|
| 160 |
double Sound_getRootMeanSquare (constSound me, double xmin, double xmax); |
| 161 |
double Sound_getEnergy (constSound me, double xmin, double xmax); |
| 162 |
double Sound_getPower (constSound me, double xmin, double xmax); |
| 163 |
double Sound_getEnergyInAir (constSound me); |
| 164 |
double Sound_getPowerInAir (constSound me); |
| 165 |
double Sound_getIntensity_dB (constSound me); |
| 166 |
|
| 167 |
double Sound_getNearestZeroCrossing (constSound me, double position, integer ichannel); |
| 168 |
void Sound_setZero (mutableSound me, double tmin, double tmax, bool roundTimesToNearestZeroCrossing); |
| 169 |
|
| 170 |
autoSound Sound_createAsPureTone (integer numberOfChannels, double startingTime, double endTime, |
| 171 |
double sampleRate, double frequency, double amplitude, double fadeInDuration, double fadeOutDuration); |
| 172 |
autoSound Sound_createAsToneComplex (double startingTime, double endTime, |
| 173 |
double sampleRate, int phase, double frequencyStep, |
| 174 |
double firstFrequency, double ceiling, integer numberOfComponents); |
| 175 |
/* Values for `phase' parameter: */ |
| 176 |
#define Sound_TONE_COMPLEX_SINE 0 |
| 177 |
#define Sound_TONE_COMPLEX_COSINE 1 |
| 178 |
|
| 179 |
void Sound_multiplyByWindow (mutableSound me, kSound_windowShape windowShape); |
| 180 |
void Sound_scaleIntensity (mutableSound me, double newAverageIntensity); |
| 181 |
void Sound_overrideSamplingFrequency (mutableSound me, double newSamplingFrequency); |
| 182 |
autoSound Sound_extractPart (constSound me, double t1, double t2, kSound_windowShape windowShape, double relativeWidth, bool preserveTimes); |
| 183 |
autoSound Sound_extractPartForOverlap (constSound me, double t1, double t2, double overlap); |
| 184 |
void Sound_filterWithFormants (mutableSound me, double tmin, double tmax, |
| 185 |
int numberOfFormants, double formant [], double bandwidth []); |
| 186 |
autoSound Sound_filter_oneFormant (constSound me, double frequency, double bandwidth); |
| 187 |
void Sound_filterWithOneFormantInplace (mutableSound me, double frequency, double bandwidth); |
| 188 |
|
| 189 |
double Sound_computeEmphasisFactor (constSound me, double cutoffFrequency); |
| 190 |
void Sound_preEmphasize_inplace (mutableSound me, double cutoffFrequency); |
| 191 |
void Sound_deEmphasize_inplace (Sound me, double cutoffFrequency); |
| 192 |
autoSound Sound_filter_preemphasis (constSound me, double cutoffFrequency); |
| 193 |
autoSound Sound_filter_deemphasis (constSound me, double cutoffFrequency); |
| 194 |
|
| 195 |
void Sound_reverse (mutableSound me, double tmin, double tmax); |
| 196 |
|
| 197 |
void Sound_draw (constSound me, Graphics g, |
| 198 |
double tmin, double tmax, double minimum, double maximum, bool garnish, conststring32 method); |
| 199 |
/* For method, see Vector_draw. */ |
| 200 |
|
| 201 |
autoMatrix Sound_to_Matrix (constSound me); |
| 202 |
/* |
| 203 |
Create a Matrix from a Sound, |
| 204 |
with deep copy of all its Matrix attributes, except class information and methods. |
| 205 |
*/ |
| 206 |
|
| 207 |
autoSound Matrix_to_Sound (constMatrix me); |
| 208 |
|
| 209 |
autoSound Matrix_to_Sound_mono (constMatrix me, integer row); |
| 210 |
/* |
| 211 |
Function: |
| 212 |
create a Sound from one row of a Matrix. |
| 213 |
"row" is forced inside the segment [1, my ny]; |
| 214 |
negative values count from the last row; |
| 215 |
e.g., if "row" is -1, the last row is taken. |
| 216 |
Postconditions: |
| 217 |
thy xmin == my xmin; |
| 218 |
thy xmax == my xmax; |
| 219 |
thy nx == my nx; |
| 220 |
thy dx == my dx; |
| 221 |
thy x1 == my x1; |
| 222 |
thy ymin ymax ny dy y1 == 1; |
| 223 |
thy z [1] [...] == my z [row] [...]; |
| 224 |
*/ |
| 225 |
|
| 226 |
inline autoSound Sound_clipboard; |
| 227 |
|
| 228 |
/********** Sound_audio.cpp **********/ |
| 229 |
|
| 230 |
autoSound Sound_record_fixedTime (int inputSource, |
| 231 |
double gain, double balance, double samplingFrequency, double duration); |
| 232 |
/* |
| 233 |
Function: |
| 234 |
record a sound without user interaction, synchronously (i.e., return when recording finsishes). |
| 235 |
Arguments: |
| 236 |
inputSource: |
| 237 |
<=0 = do not change (may be set by Audio Control Panel). |
| 238 |
1 = microphone. |
| 239 |
2 = line. |
| 240 |
3 = digital. |
| 241 |
If the input source selection fails, the microphone is used. |
| 242 |
gain: |
| 243 |
<0.0 = do not change. |
| 244 |
0.0 = minimum. |
| 245 |
1.0 = maximum. |
| 246 |
balance: |
| 247 |
<0.0 = do not change. |
| 248 |
0.0 = left channel only. |
| 249 |
0.5 = mid balance. |
| 250 |
1.0 = right channel only. |
| 251 |
samplingFrequency: |
| 252 |
<=0.0 = do not change. |
| 253 |
must be one of the sampling frequencies supported by the hardware. |
| 254 |
duration: |
| 255 |
positive time in seconds. |
| 256 |
Return value: |
| 257 |
the resulting Sound. |
| 258 |
Failures: |
| 259 |
Hardware does not support sampling frequency. |
| 260 |
Duration must be positive. |
| 261 |
Duration too long (out of memory). |
| 262 |
Usage: |
| 263 |
batch; in an interactive environment, it is better to use SoundRecorder_create, |
| 264 |
and wait for broadcastPublication(). |
| 265 |
*/ |
| 266 |
|
| 267 |
typedef MelderCallback <int, structThing /* boss */, int /* phase */, double /* tmin */, double /* tmax */, double /* t */> Sound_PlayCallback; |
| 268 |
|
| 269 |
void Sound_playPart (constSound me, double tmin, double tmax, Sound_PlayCallback playCallback, Thing playBoss); |
| 270 |
/* |
| 271 |
* Play a sound. The playing can be interrupted with the Escape key (also Command-period on the Mac). |
| 272 |
* If playCallback is not null, Sound_play will call it repeatedly, with five parameters: |
| 273 |
* 1. playBoss: the same value as was supplied as the last argument to Sound_playPart. |
| 274 |
* 2. phase: 1 at the start, 2 while playing, 3 at the end. |
| 275 |
* 3. tmin: the same tmin that was supplied as the second argument to Sound_playPart. |
| 276 |
* 4. tmax: the same tmax that was supplied as the second argument to Sound_playPart. |
| 277 |
* 5. t: the time (probably between tmin and tmax) at which the sound is playing. |
| 278 |
* The usage of playCallback is as follows. Suppose we are an editor that wants to show a moving |
| 279 |
* cursor while we play a sound: |
| 280 |
* Sound_playPart (my sound, my startSelection, my endSelection, thePlayCallback, me); |
| 281 |
* We gave ourselves as the playClosure, so that the message will arrive with us: |
| 282 |
* int thePlayCallback (SoundEditor me, int phase, double tmin, double tmax, double t) { |
| 283 |
* if (phase == 1) { |
| 284 |
* Melder_assert (t == tmin); |
| 285 |
* drawPlayCursor (me, my playCursor = t); |
| 286 |
* } else if (phase == 2) { |
| 287 |
* undrawPlayCursor (me, my playCursor); |
| 288 |
* drawPlayCursor (me, my playCursor = t); |
| 289 |
* } else { |
| 290 |
* undrawPlayCursor (me, t); |
| 291 |
* moveCursor (me, t); |
| 292 |
* if (t < tmax) { Melder_casual (U"Sound play interrupted."); } |
| 293 |
* } |
| 294 |
* return 1; |
| 295 |
* } |
| 296 |
* The playCallback procedure usually returns 1, because the sound should continue playing. |
| 297 |
* If playCallback returns 0 instead, Sound_play will interrupt the play and return. |
| 298 |
* Sound_playPart returns the time at which playing stopped. If playing was not interrupted, it returns tmax. |
| 299 |
* |
| 300 |
* Sound_playPart () usually runs asynchronously, and kills an already playing sound. |
| 301 |
*/ |
| 302 |
void Sound_play (constSound me, Sound_PlayCallback playCallback, Thing playBoss); |
| 303 |
/* The same as Sound_playPart (me, my xmin, my xmax, playCallback, playBoss); */ |
| 304 |
|
| 305 |
/********** Sound_files.cpp **********/ |
| 306 |
|
| 307 |
/* To avoid clipping, keep the absolute amplitude below 1.000. */ |
| 308 |
/* All are mono or stereo PCM. */ |
| 309 |
void Sound_saveAsAudioFile (constSound me, MelderFile file, int audioFileType, int numberOfBitsPerSamplePoint); |
| 310 |
void Sound_saveAsKayFile (constSound me, MelderFile file); // 16-bit |
| 311 |
void Sound_saveAsSesamFile (constSound me, MelderFile file); // 12-bit SESAM/LVS |
| 312 |
|
| 313 |
autoSound Sound_readFromSoundFile (MelderFile file); // AIFF, WAV, NeXT/Sun, or NIST |
| 314 |
autoDaata Sound_readFromAnyKayFile (MelderFile file); // 16-bit |
| 315 |
autoSound Sound_readFromSesamFile (MelderFile file); // 12-bit SESAM/LVS |
| 316 |
autoSound Sound_readFromBellLabsFile (MelderFile file); // 16-bit |
| 317 |
autoSound Sound_readFromRawAlawFile (MelderFile file); |
| 318 |
autoSound Sound_readFromMovieFile (MelderFile file); |
| 319 |
|
| 320 |
autoSound Sound_readFromRawSoundFile (MelderFile file, int encoding, int numberOfChannels, double sampleRate); |
| 321 |
/* |
| 322 |
'encoding' is any of the following: |
| 323 |
Melder_LINEAR_8_SIGNED |
| 324 |
Melder_LINEAR_8_UNSIGNED |
| 325 |
Melder_LINEAR_16_BIG_ENDIAN |
| 326 |
Melder_LINEAR_16_LITTLE_ENDIAN |
| 327 |
Melder_MULAW |
| 328 |
Melder_ALAW |
| 329 |
'numberOfChannels' is 1 (mono) or 2 (stereo) |
| 330 |
'sampleRate' is in hertz |
| 331 |
*/ |
| 332 |
void Sound_saveAsRawSoundFile (constSound me, MelderFile file, int encoding); |
| 333 |
/* |
| 334 |
'encoding' is any of the following: |
| 335 |
Melder_LINEAR_8_SIGNED |
| 336 |
Melder_LINEAR_8_UNSIGNED |
| 337 |
Melder_LINEAR_16_BIG_ENDIAN |
| 338 |
Melder_LINEAR_16_LITTLE_ENDIAN |
| 339 |
'me' must exist |
| 340 |
*/ |
| 341 |
|
| 342 |
/********** Sound_enhance.cpp **********/ |
| 343 |
|
| 344 |
autoSound Sound_lengthen_overlapAdd (constSound me, double fmin, double fmax, double factor); |
| 345 |
autoSound Sound_deepenBandModulation (constSound me, double enhancement_dB, |
| 346 |
double flow, double fhigh, double slowModulation, double fastModulation, double bandSmoothing); |
| 347 |
|
| 348 |
/* |
| 349 |
Abstract. |
| 350 |
*/ |
| 351 |
Collection_define (SoundList, OrderedOf, Sound) { |
| 352 |
}; |
| 353 |
|
| 354 |
autoSound Sounds_concatenate (SoundList list, double overlapTime); |
| 355 |
void SoundList_play (SoundList me, Sound_PlayCallback playCallback, Thing playClosure); |
| 356 |
|
| 357 |
/* End of file Sound.h */ |
| 358 |
#endif |