Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 1 | /* Sonic library |
| 2 | Copyright 2010 |
| 3 | Bill Cox |
| 4 | This file is part of the Sonic Library. |
| 5 | |
Bill Cox | a999987 | 2010-11-11 14:36:59 -0500 | [diff] [blame] | 6 | The Sonic Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 10 | |
Bill Cox | a999987 | 2010-11-11 14:36:59 -0500 | [diff] [blame] | 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 15 | |
Bill Cox | a999987 | 2010-11-11 14:36:59 -0500 | [diff] [blame] | 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307 USA. */ |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 20 | |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 21 | #include <stdio.h> |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Bill Cox | a33e3bd | 2010-11-16 19:57:43 -0500 | [diff] [blame] | 24 | #include <stdarg.h> |
Bill Cox | 4e234d7 | 2010-12-17 05:44:02 -0500 | [diff] [blame] | 25 | #ifdef SONIC_USE_SIN |
| 26 | #include <math.h> |
| 27 | #ifndef M_PI |
| 28 | #define M_PI 3.14159265358979323846 |
| 29 | #endif |
| 30 | #endif |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 31 | #include "sonic.h" |
| 32 | |
| 33 | struct sonicStreamStruct { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 34 | short *inputBuffer; |
| 35 | short *outputBuffer; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 36 | short *pitchBuffer; |
| 37 | short *downSampleBuffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 38 | float speed; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 39 | float volume; |
| 40 | float pitch; |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 41 | float rate; |
| 42 | int oldRatePosition; |
| 43 | int newRatePosition; |
| 44 | int oldSampleRate; |
| 45 | int newSampleRate; |
| 46 | int useChordPitch; |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 47 | int quality; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 48 | int numChannels; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 49 | int inputBufferSize; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 50 | int pitchBufferSize; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 51 | int outputBufferSize; |
| 52 | int numInputSamples; |
| 53 | int numOutputSamples; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 54 | int numPitchSamples; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 55 | int minPeriod; |
| 56 | int maxPeriod; |
| 57 | int maxRequired; |
| 58 | int remainingInputToCopy; |
| 59 | int sampleRate; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 60 | int prevPeriod; |
| 61 | int prevMaxDiff; |
| 62 | int prevMinDiff; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 65 | /* Just used for debugging */ |
Bill Cox | 59af467 | 2010-12-17 16:57:41 -0500 | [diff] [blame] | 66 | /* |
Bill Cox | a33e3bd | 2010-11-16 19:57:43 -0500 | [diff] [blame] | 67 | void sonicMSG(char *format, ...) |
| 68 | { |
| 69 | char buffer[4096]; |
| 70 | va_list ap; |
| 71 | FILE *file; |
| 72 | |
| 73 | va_start(ap, format); |
| 74 | vsprintf((char *)buffer, (char *)format, ap); |
| 75 | va_end(ap); |
| 76 | file=fopen("/tmp/sonic.log", "a"); |
| 77 | fprintf(file, "%s", buffer); |
| 78 | fclose(file); |
| 79 | } |
Bill Cox | 59af467 | 2010-12-17 16:57:41 -0500 | [diff] [blame] | 80 | */ |
Bill Cox | a33e3bd | 2010-11-16 19:57:43 -0500 | [diff] [blame] | 81 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 82 | /* Scale the samples by the factor. */ |
| 83 | static void scaleSamples( |
| 84 | short *samples, |
| 85 | int numSamples, |
| 86 | float volume) |
| 87 | { |
| 88 | int fixedPointVolume = volume*4096.0f; |
| 89 | int value; |
| 90 | |
| 91 | while(numSamples--) { |
| 92 | value = (*samples*fixedPointVolume) >> 12; |
| 93 | if(value > 32767) { |
| 94 | value = 32767; |
| 95 | } else if(value < -32767) { |
| 96 | value = -32767; |
| 97 | } |
| 98 | *samples++ = value; |
| 99 | } |
| 100 | } |
| 101 | |
Bill Cox | af9a624 | 2010-11-08 09:32:27 -0500 | [diff] [blame] | 102 | /* Get the speed of the stream. */ |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 103 | float sonicGetSpeed( |
Bill Cox | af9a624 | 2010-11-08 09:32:27 -0500 | [diff] [blame] | 104 | sonicStream stream) |
| 105 | { |
| 106 | return stream->speed; |
| 107 | } |
| 108 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 109 | /* Set the speed of the stream. */ |
| 110 | void sonicSetSpeed( |
| 111 | sonicStream stream, |
| 112 | float speed) |
| 113 | { |
| 114 | stream->speed = speed; |
| 115 | } |
| 116 | |
| 117 | /* Get the pitch of the stream. */ |
| 118 | float sonicGetPitch( |
| 119 | sonicStream stream) |
| 120 | { |
| 121 | return stream->pitch; |
| 122 | } |
| 123 | |
| 124 | /* Set the pitch of the stream. */ |
| 125 | void sonicSetPitch( |
| 126 | sonicStream stream, |
| 127 | float pitch) |
| 128 | { |
| 129 | stream->pitch = pitch; |
| 130 | } |
| 131 | |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 132 | /* Get the rate of the stream. */ |
| 133 | float sonicGetRate( |
| 134 | sonicStream stream) |
| 135 | { |
| 136 | return stream->rate; |
| 137 | } |
| 138 | |
| 139 | /* Set the playback rate of the stream. This scales pitch and speed at the same time. */ |
| 140 | void sonicSetRate( |
| 141 | sonicStream stream, |
| 142 | float rate) |
| 143 | { |
| 144 | stream->rate = rate; |
| 145 | |
| 146 | stream->oldRatePosition = 0; |
| 147 | stream->newRatePosition = 0; |
| 148 | } |
| 149 | |
| 150 | /* Get the vocal chord pitch setting. */ |
| 151 | int sonicGetChordPitch( |
| 152 | sonicStream stream) |
| 153 | { |
| 154 | return stream->useChordPitch; |
| 155 | } |
| 156 | |
| 157 | /* Set the vocal chord mode for pitch computation. Default is off. */ |
| 158 | void sonicSetChordPitch( |
| 159 | sonicStream stream, |
| 160 | int useChordPitch) |
| 161 | { |
| 162 | stream->useChordPitch = useChordPitch; |
| 163 | } |
| 164 | |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 165 | /* Get the quality setting. */ |
| 166 | int sonicGetQuality( |
| 167 | sonicStream stream) |
| 168 | { |
| 169 | return stream->quality; |
| 170 | } |
| 171 | |
| 172 | /* Set the "quality". Default 0 is virtually as good as 1, but very much faster. */ |
| 173 | void sonicSetQuality( |
| 174 | sonicStream stream, |
| 175 | int quality) |
| 176 | { |
| 177 | stream->quality = quality; |
| 178 | } |
| 179 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 180 | /* Get the scaling factor of the stream. */ |
| 181 | float sonicGetVolume( |
| 182 | sonicStream stream) |
| 183 | { |
| 184 | return stream->volume; |
| 185 | } |
| 186 | |
| 187 | /* Set the scaling factor of the stream. */ |
| 188 | void sonicSetVolume( |
| 189 | sonicStream stream, |
| 190 | float volume) |
| 191 | { |
| 192 | stream->volume = volume; |
| 193 | } |
| 194 | |
Bill Cox | 69a864b | 2011-03-04 02:51:43 -0500 | [diff] [blame] | 195 | /* Free stream buffers. */ |
| 196 | static void freeStreamBuffers( |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 197 | sonicStream stream) |
| 198 | { |
| 199 | if(stream->inputBuffer != NULL) { |
| 200 | free(stream->inputBuffer); |
| 201 | } |
| 202 | if(stream->outputBuffer != NULL) { |
| 203 | free(stream->outputBuffer); |
| 204 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 205 | if(stream->pitchBuffer != NULL) { |
| 206 | free(stream->pitchBuffer); |
| 207 | } |
| 208 | if(stream->downSampleBuffer != NULL) { |
| 209 | free(stream->downSampleBuffer); |
| 210 | } |
Bill Cox | 69a864b | 2011-03-04 02:51:43 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* Destroy the sonic stream. */ |
| 214 | void sonicDestroyStream( |
| 215 | sonicStream stream) |
| 216 | { |
| 217 | freeStreamBuffers(stream); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 218 | free(stream); |
| 219 | } |
| 220 | |
Bill Cox | 69a864b | 2011-03-04 02:51:43 -0500 | [diff] [blame] | 221 | /* Allocate stream buffers. */ |
| 222 | static int allocateStreamBuffers( |
| 223 | sonicStream stream, |
| 224 | int sampleRate, |
| 225 | int numChannels) |
| 226 | { |
| 227 | int minPeriod = sampleRate/SONIC_MAX_PITCH; |
| 228 | int maxPeriod = sampleRate/SONIC_MIN_PITCH; |
| 229 | int maxRequired = 2*maxPeriod; |
| 230 | |
| 231 | stream->inputBufferSize = maxRequired; |
| 232 | stream->inputBuffer = (short *)calloc(maxRequired, sizeof(short)*numChannels); |
| 233 | if(stream->inputBuffer == NULL) { |
| 234 | sonicDestroyStream(stream); |
| 235 | return 0; |
| 236 | } |
| 237 | stream->outputBufferSize = maxRequired; |
| 238 | stream->outputBuffer = (short *)calloc(maxRequired, sizeof(short)*numChannels); |
| 239 | if(stream->outputBuffer == NULL) { |
| 240 | sonicDestroyStream(stream); |
| 241 | return 0; |
| 242 | } |
| 243 | stream->pitchBufferSize = maxRequired; |
| 244 | stream->pitchBuffer = (short *)calloc(maxRequired, sizeof(short)*numChannels); |
| 245 | if(stream->pitchBuffer == NULL) { |
| 246 | sonicDestroyStream(stream); |
| 247 | return 0; |
| 248 | } |
| 249 | stream->downSampleBuffer = (short *)calloc(maxRequired, sizeof(short)); |
| 250 | if(stream->downSampleBuffer == NULL) { |
| 251 | sonicDestroyStream(stream); |
| 252 | return 0; |
| 253 | } |
| 254 | stream->sampleRate = sampleRate; |
| 255 | stream->numChannels = numChannels; |
| 256 | stream->oldRatePosition = 0; |
| 257 | stream->newRatePosition = 0; |
| 258 | stream->minPeriod = minPeriod; |
| 259 | stream->maxPeriod = maxPeriod; |
| 260 | stream->maxRequired = maxRequired; |
| 261 | return 1; |
| 262 | } |
| 263 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 264 | /* Create a sonic stream. Return NULL only if we are out of memory and cannot |
| 265 | allocate the stream. */ |
| 266 | sonicStream sonicCreateStream( |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 267 | int sampleRate, |
| 268 | int numChannels) |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 269 | { |
| 270 | sonicStream stream = (sonicStream)calloc(1, sizeof(struct sonicStreamStruct)); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 271 | |
| 272 | if(stream == NULL) { |
| 273 | return NULL; |
| 274 | } |
Bill Cox | 69a864b | 2011-03-04 02:51:43 -0500 | [diff] [blame] | 275 | if(!allocateStreamBuffers(stream, sampleRate, numChannels)) { |
| 276 | return NULL; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 277 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 278 | stream->speed = 1.0f; |
| 279 | stream->pitch = 1.0f; |
| 280 | stream->volume = 1.0f; |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 281 | stream->rate = 1.0f; |
| 282 | stream->oldRatePosition = 0; |
| 283 | stream->newRatePosition = 0; |
| 284 | stream->useChordPitch = 0; |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 285 | stream->quality = 0; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 286 | return stream; |
| 287 | } |
| 288 | |
Bill Cox | 69a864b | 2011-03-04 02:51:43 -0500 | [diff] [blame] | 289 | /* Get the sample rate of the stream. */ |
| 290 | int sonicGetSampleRate( |
| 291 | sonicStream stream) |
| 292 | { |
| 293 | return stream->sampleRate; |
| 294 | } |
| 295 | |
| 296 | /* Set the sample rate of the stream. This will cause samples buffered in the stream to |
| 297 | be lost. */ |
| 298 | void sonicSetSampleRate( |
| 299 | sonicStream stream, |
| 300 | int sampleRate) |
| 301 | { |
| 302 | freeStreamBuffers(stream); |
| 303 | allocateStreamBuffers(stream, sampleRate, stream->numChannels); |
| 304 | } |
| 305 | |
| 306 | /* Get the number of channels. */ |
| 307 | int sonicGetNumChannels( |
| 308 | sonicStream stream) |
| 309 | { |
| 310 | return stream->numChannels; |
| 311 | } |
| 312 | |
| 313 | /* Set the num channels of the stream. This will cause samples buffered in the stream to |
| 314 | be lost. */ |
| 315 | void sonicSetNumChannels( |
| 316 | sonicStream stream, |
| 317 | int numChannels) |
| 318 | { |
| 319 | freeStreamBuffers(stream); |
| 320 | allocateStreamBuffers(stream, stream->sampleRate, numChannels); |
| 321 | } |
| 322 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 323 | /* Enlarge the output buffer if needed. */ |
| 324 | static int enlargeOutputBufferIfNeeded( |
| 325 | sonicStream stream, |
| 326 | int numSamples) |
| 327 | { |
| 328 | if(stream->numOutputSamples + numSamples > stream->outputBufferSize) { |
| 329 | stream->outputBufferSize += (stream->outputBufferSize >> 1) + numSamples; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 330 | stream->outputBuffer = (short *)realloc(stream->outputBuffer, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 331 | stream->outputBufferSize*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 332 | if(stream->outputBuffer == NULL) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | } |
| 336 | return 1; |
| 337 | } |
| 338 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 339 | /* Enlarge the input buffer if needed. */ |
| 340 | static int enlargeInputBufferIfNeeded( |
| 341 | sonicStream stream, |
| 342 | int numSamples) |
| 343 | { |
| 344 | if(stream->numInputSamples + numSamples > stream->inputBufferSize) { |
| 345 | stream->inputBufferSize += (stream->inputBufferSize >> 1) + numSamples; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 346 | stream->inputBuffer = (short *)realloc(stream->inputBuffer, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 347 | stream->inputBufferSize*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 348 | if(stream->inputBuffer == NULL) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | } |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | /* Add the input samples to the input buffer. */ |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 356 | static int addFloatSamplesToInputBuffer( |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 357 | sonicStream stream, |
| 358 | float *samples, |
| 359 | int numSamples) |
| 360 | { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 361 | short *buffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 362 | int count = numSamples*stream->numChannels; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 363 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 364 | if(numSamples == 0) { |
| 365 | return 1; |
| 366 | } |
| 367 | if(!enlargeInputBufferIfNeeded(stream, numSamples)) { |
| 368 | return 0; |
| 369 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 370 | buffer = stream->inputBuffer + stream->numInputSamples*stream->numChannels; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 371 | while(count--) { |
| 372 | *buffer++ = (*samples++)*32767.0f; |
| 373 | } |
Bill Cox | 14efa44 | 2010-11-02 15:43:58 -0400 | [diff] [blame] | 374 | stream->numInputSamples += numSamples; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 375 | return 1; |
| 376 | } |
| 377 | |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 378 | /* Add the input samples to the input buffer. */ |
| 379 | static int addShortSamplesToInputBuffer( |
| 380 | sonicStream stream, |
| 381 | short *samples, |
| 382 | int numSamples) |
| 383 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 384 | if(numSamples == 0) { |
| 385 | return 1; |
| 386 | } |
| 387 | if(!enlargeInputBufferIfNeeded(stream, numSamples)) { |
| 388 | return 0; |
| 389 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 390 | memcpy(stream->inputBuffer + stream->numInputSamples*stream->numChannels, samples, |
| 391 | numSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 392 | stream->numInputSamples += numSamples; |
| 393 | return 1; |
| 394 | } |
| 395 | |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 396 | /* Add the input samples to the input buffer. */ |
| 397 | static int addUnsignedCharSamplesToInputBuffer( |
| 398 | sonicStream stream, |
| 399 | unsigned char *samples, |
| 400 | int numSamples) |
| 401 | { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 402 | short *buffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 403 | int count = numSamples*stream->numChannels; |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 404 | |
| 405 | if(numSamples == 0) { |
| 406 | return 1; |
| 407 | } |
| 408 | if(!enlargeInputBufferIfNeeded(stream, numSamples)) { |
| 409 | return 0; |
| 410 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 411 | buffer = stream->inputBuffer + stream->numInputSamples*stream->numChannels; |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 412 | while(count--) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 413 | *buffer++ = (*samples++ - 128) << 8; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 414 | } |
| 415 | stream->numInputSamples += numSamples; |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | /* Remove input samples that we have already processed. */ |
| 420 | static void removeInputSamples( |
| 421 | sonicStream stream, |
| 422 | int position) |
| 423 | { |
| 424 | int remainingSamples = stream->numInputSamples - position; |
| 425 | |
| 426 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 427 | memmove(stream->inputBuffer, stream->inputBuffer + position*stream->numChannels, |
| 428 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 429 | } |
| 430 | stream->numInputSamples = remainingSamples; |
| 431 | } |
| 432 | |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 433 | /* Just copy from the array to the output buffer */ |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 434 | static int copyToOutput( |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 435 | sonicStream stream, |
| 436 | short *samples, |
| 437 | int numSamples) |
| 438 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 439 | if(!enlargeOutputBufferIfNeeded(stream, numSamples)) { |
| 440 | return 0; |
| 441 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 442 | memcpy(stream->outputBuffer + stream->numOutputSamples*stream->numChannels, |
| 443 | samples, numSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 444 | stream->numOutputSamples += numSamples; |
| 445 | return numSamples; |
| 446 | } |
| 447 | |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 448 | /* Just copy from the input buffer to the output buffer. Return 0 if we fail to |
| 449 | resize the output buffer. Otherwise, return numSamples */ |
| 450 | static int copyInputToOutput( |
| 451 | sonicStream stream, |
| 452 | int position) |
| 453 | { |
| 454 | int numSamples = stream->remainingInputToCopy; |
| 455 | |
| 456 | if(numSamples > stream->maxRequired) { |
| 457 | numSamples = stream->maxRequired; |
| 458 | } |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 459 | if(!copyToOutput(stream, stream->inputBuffer + position*stream->numChannels, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 460 | numSamples)) { |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 461 | return 0; |
| 462 | } |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 463 | stream->remainingInputToCopy -= numSamples; |
| 464 | return numSamples; |
| 465 | } |
| 466 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 467 | /* Read data out of the stream. Sometimes no data will be available, and zero |
| 468 | is returned, which is not an error condition. */ |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 469 | int sonicReadFloatFromStream( |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 470 | sonicStream stream, |
| 471 | float *samples, |
| 472 | int maxSamples) |
| 473 | { |
| 474 | int numSamples = stream->numOutputSamples; |
| 475 | int remainingSamples = 0; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 476 | short *buffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 477 | int count; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 478 | |
| 479 | if(numSamples == 0) { |
| 480 | return 0; |
| 481 | } |
| 482 | if(numSamples > maxSamples) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 483 | remainingSamples = numSamples - maxSamples; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 484 | numSamples = maxSamples; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 485 | } |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 486 | buffer = stream->outputBuffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 487 | count = numSamples*stream->numChannels; |
| 488 | while(count--) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 489 | *samples++ = (*buffer++)/32767.0f; |
| 490 | } |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 491 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 492 | memmove(stream->outputBuffer, stream->outputBuffer + numSamples*stream->numChannels, |
| 493 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 494 | } |
| 495 | stream->numOutputSamples = remainingSamples; |
| 496 | return numSamples; |
| 497 | } |
| 498 | |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 499 | /* Read short data out of the stream. Sometimes no data will be available, and zero |
| 500 | is returned, which is not an error condition. */ |
| 501 | int sonicReadShortFromStream( |
| 502 | sonicStream stream, |
| 503 | short *samples, |
| 504 | int maxSamples) |
| 505 | { |
| 506 | int numSamples = stream->numOutputSamples; |
| 507 | int remainingSamples = 0; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 508 | |
| 509 | if(numSamples == 0) { |
| 510 | return 0; |
| 511 | } |
| 512 | if(numSamples > maxSamples) { |
| 513 | remainingSamples = numSamples - maxSamples; |
| 514 | numSamples = maxSamples; |
| 515 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 516 | memcpy(samples, stream->outputBuffer, numSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 517 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 518 | memmove(stream->outputBuffer, stream->outputBuffer + numSamples*stream->numChannels, |
| 519 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 520 | } |
| 521 | stream->numOutputSamples = remainingSamples; |
| 522 | return numSamples; |
| 523 | } |
| 524 | |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 525 | /* Read unsigned char data out of the stream. Sometimes no data will be available, and zero |
| 526 | is returned, which is not an error condition. */ |
| 527 | int sonicReadUnsignedCharFromStream( |
| 528 | sonicStream stream, |
| 529 | unsigned char *samples, |
| 530 | int maxSamples) |
| 531 | { |
| 532 | int numSamples = stream->numOutputSamples; |
| 533 | int remainingSamples = 0; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 534 | short *buffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 535 | int count; |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 536 | |
| 537 | if(numSamples == 0) { |
| 538 | return 0; |
| 539 | } |
| 540 | if(numSamples > maxSamples) { |
| 541 | remainingSamples = numSamples - maxSamples; |
| 542 | numSamples = maxSamples; |
| 543 | } |
| 544 | buffer = stream->outputBuffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 545 | count = numSamples*stream->numChannels; |
| 546 | while(count--) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 547 | *samples++ = (char)((*buffer++) >> 8) + 128; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 548 | } |
| 549 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 550 | memmove(stream->outputBuffer, stream->outputBuffer + numSamples*stream->numChannels, |
| 551 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 552 | } |
| 553 | stream->numOutputSamples = remainingSamples; |
| 554 | return numSamples; |
| 555 | } |
| 556 | |
| 557 | /* Force the sonic stream to generate output using whatever data it currently |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 558 | has. No extra delay will be added to the output, but flushing in the middle of |
| 559 | words could introduce distortion. */ |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 560 | int sonicFlushStream( |
| 561 | sonicStream stream) |
| 562 | { |
| 563 | int maxRequired = stream->maxRequired; |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 564 | int remainingSamples = stream->numInputSamples; |
| 565 | float speed = stream->speed/stream->pitch; |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 566 | float rate = stream->rate*stream->pitch; |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 567 | int expectedOutputSamples = stream->numOutputSamples + |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 568 | (int)((remainingSamples/speed + stream->numPitchSamples)/rate + 0.5f); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 569 | |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 570 | /* Add enough silence to flush both input and pitch buffers. */ |
| 571 | if(!enlargeInputBufferIfNeeded(stream, remainingSamples + 2*maxRequired)) { |
| 572 | return 0; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 573 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 574 | memset(stream->inputBuffer + remainingSamples*stream->numChannels, 0, |
| 575 | 2*maxRequired*sizeof(short)*stream->numChannels); |
| 576 | stream->numInputSamples += 2*maxRequired; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 577 | if(!sonicWriteShortToStream(stream, NULL, 0)) { |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | /* Throw away any extra samples we generated due to the silence we added */ |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 581 | if(stream->numOutputSamples > expectedOutputSamples) { |
| 582 | stream->numOutputSamples = expectedOutputSamples; |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 583 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 584 | /* Empty input and pitch buffers */ |
| 585 | stream->numInputSamples = 0; |
| 586 | stream->remainingInputToCopy = 0; |
| 587 | stream->numPitchSamples = 0; |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 588 | return 1; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* Return the number of samples in the output buffer */ |
Bill Cox | 3a7abf9 | 2010-11-06 15:18:49 -0400 | [diff] [blame] | 592 | int sonicSamplesAvailable( |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 593 | sonicStream stream) |
| 594 | { |
| 595 | return stream->numOutputSamples; |
| 596 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 597 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 598 | /* If skip is greater than one, average skip samples togther and write them to |
| 599 | the down-sample buffer. If numChannels is greater than one, mix the channels |
| 600 | together as we down sample. */ |
| 601 | static void downSampleInput( |
| 602 | sonicStream stream, |
| 603 | short *samples, |
| 604 | int skip) |
| 605 | { |
| 606 | int numSamples = stream->maxRequired/skip; |
| 607 | int samplesPerValue = stream->numChannels*skip; |
| 608 | int i, j; |
| 609 | int value; |
| 610 | short *downSamples = stream->downSampleBuffer; |
| 611 | |
| 612 | for(i = 0; i < numSamples; i++) { |
| 613 | value = 0; |
| 614 | for(j = 0; j < samplesPerValue; j++) { |
| 615 | value += *samples++; |
| 616 | } |
| 617 | value /= samplesPerValue; |
| 618 | *downSamples++ = value; |
| 619 | } |
| 620 | } |
| 621 | |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 622 | /* Find the best frequency match in the range, and given a sample skip multiple. |
| 623 | For now, just find the pitch of the first channel. */ |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 624 | static int findPitchPeriodInRange( |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 625 | short *samples, |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 626 | int minPeriod, |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 627 | int maxPeriod, |
| 628 | int *retMinDiff, |
| 629 | int *retMaxDiff) |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 630 | { |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 631 | int period, bestPeriod = 0, worstPeriod = 255; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 632 | short *s, *p, sVal, pVal; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 633 | unsigned long diff, minDiff = 1, maxDiff = 0; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 634 | int i; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 635 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 636 | for(period = minPeriod; period <= maxPeriod; period++) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 637 | diff = 0; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 638 | s = samples; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 639 | p = samples + period; |
| 640 | for(i = 0; i < period; i++) { |
| 641 | sVal = *s++; |
| 642 | pVal = *p++; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 643 | diff += sVal >= pVal? (unsigned short)(sVal - pVal) : |
| 644 | (unsigned short)(pVal - sVal); |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 645 | } |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 646 | /* Note that the highest number of samples we add into diff will be less |
| 647 | than 256, since we skip samples. Thus, diff is a 24 bit number, and |
| 648 | we can safely multiply by numSamples without overflow */ |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 649 | if(diff*bestPeriod < minDiff*period) { |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 650 | minDiff = diff; |
| 651 | bestPeriod = period; |
| 652 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 653 | if(diff*worstPeriod > maxDiff*period) { |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 654 | maxDiff = diff; |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 655 | worstPeriod = period; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 656 | } |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 657 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 658 | *retMinDiff = minDiff/bestPeriod; |
| 659 | *retMaxDiff = maxDiff/worstPeriod; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 660 | return bestPeriod; |
| 661 | } |
| 662 | |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 663 | /* At abrupt ends of voiced words, we can have pitch periods that are better |
| 664 | aproximated by the previous pitch period estimate. Try to detect this case. */ |
| 665 | static int prevPeriodBetter( |
| 666 | sonicStream stream, |
| 667 | int period, |
| 668 | int minDiff, |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 669 | int maxDiff, |
| 670 | int preferNewPeriod) |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 671 | { |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 672 | if(minDiff == 0) { |
| 673 | return 0; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 674 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 675 | if(preferNewPeriod) { |
| 676 | if(maxDiff > minDiff*3) { |
| 677 | /* Got a reasonable match this period */ |
| 678 | return 0; |
| 679 | } |
| 680 | if(minDiff*2 <= stream->prevMinDiff*3) { |
| 681 | /* Mismatch is not that much greater this period */ |
| 682 | return 0; |
| 683 | } |
| 684 | } else { |
| 685 | if(minDiff <= stream->prevMinDiff) { |
| 686 | return 0; |
| 687 | } |
| 688 | } |
| 689 | return 1; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 690 | } |
| 691 | |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 692 | /* Find the pitch period. This is a critical step, and we may have to try |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 693 | multiple ways to get a good answer. This version uses AMDF. To improve |
| 694 | speed, we down sample by an integer factor get in the 11KHz range, and then |
| 695 | do it again with a narrower frequency range without down sampling */ |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 696 | static int findPitchPeriod( |
| 697 | sonicStream stream, |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 698 | short *samples, |
| 699 | int preferNewPeriod) |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 700 | { |
| 701 | int minPeriod = stream->minPeriod; |
| 702 | int maxPeriod = stream->maxPeriod; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 703 | int sampleRate = stream->sampleRate; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 704 | int minDiff, maxDiff, retPeriod; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 705 | int skip = 1; |
| 706 | int period; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 707 | |
Bill Cox | 4e234d7 | 2010-12-17 05:44:02 -0500 | [diff] [blame] | 708 | if(sampleRate > SONIC_AMDF_FREQ && stream->quality == 0) { |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 709 | skip = sampleRate/SONIC_AMDF_FREQ; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 710 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 711 | if(stream->numChannels == 1 && skip == 1) { |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 712 | period = findPitchPeriodInRange(samples, minPeriod, maxPeriod, &minDiff, &maxDiff); |
| 713 | } else { |
| 714 | downSampleInput(stream, samples, skip); |
| 715 | period = findPitchPeriodInRange(stream->downSampleBuffer, minPeriod/skip, |
| 716 | maxPeriod/skip, &minDiff, &maxDiff); |
| 717 | if(skip != 1) { |
| 718 | period *= skip; |
| 719 | minPeriod = period - (skip << 2); |
| 720 | maxPeriod = period + (skip << 2); |
| 721 | if(minPeriod < stream->minPeriod) { |
| 722 | minPeriod = stream->minPeriod; |
| 723 | } |
| 724 | if(maxPeriod > stream->maxPeriod) { |
| 725 | maxPeriod = stream->maxPeriod; |
| 726 | } |
| 727 | if(stream->numChannels == 1) { |
| 728 | period = findPitchPeriodInRange(samples, minPeriod, maxPeriod, |
| 729 | &minDiff, &maxDiff); |
| 730 | } else { |
| 731 | downSampleInput(stream, samples, 1); |
| 732 | period = findPitchPeriodInRange(stream->downSampleBuffer, minPeriod, |
| 733 | maxPeriod, &minDiff, &maxDiff); |
| 734 | } |
| 735 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 736 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 737 | if(prevPeriodBetter(stream, period, minDiff, maxDiff, preferNewPeriod)) { |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 738 | retPeriod = stream->prevPeriod; |
| 739 | } else { |
| 740 | retPeriod = period; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 741 | } |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 742 | stream->prevMinDiff = minDiff; |
| 743 | stream->prevMaxDiff = maxDiff; |
| 744 | stream->prevPeriod = period; |
| 745 | return retPeriod; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 746 | } |
| 747 | |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 748 | /* Overlap two sound segments, ramp the volume of one down, while ramping the |
| 749 | other one from zero up, and add them, storing the result at the output. */ |
| 750 | static void overlapAdd( |
| 751 | int numSamples, |
| 752 | int numChannels, |
| 753 | short *out, |
| 754 | short *rampDown, |
| 755 | short *rampUp) |
| 756 | { |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 757 | short *o, *u, *d; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 758 | int i, t; |
| 759 | |
| 760 | for(i = 0; i < numChannels; i++) { |
| 761 | o = out + i; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 762 | u = rampUp + i; |
| 763 | d = rampDown + i; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 764 | for(t = 0; t < numSamples; t++) { |
Bill Cox | 4e234d7 | 2010-12-17 05:44:02 -0500 | [diff] [blame] | 765 | #ifdef SONIC_USE_SIN |
| 766 | float ratio = sin(t*M_PI/(2*numSamples)); |
| 767 | *o = *d*(1.0f - ratio) + *u*ratio; |
| 768 | #else |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 769 | *o = (*d*(numSamples - t) + *u*t)/numSamples; |
Bill Cox | 4e234d7 | 2010-12-17 05:44:02 -0500 | [diff] [blame] | 770 | #endif |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 771 | o += numChannels; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 772 | d += numChannels; |
| 773 | u += numChannels; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* Overlap two sound segments, ramp the volume of one down, while ramping the |
| 779 | other one from zero up, and add them, storing the result at the output. */ |
| 780 | static void overlapAddWithSeparation( |
| 781 | int numSamples, |
| 782 | int numChannels, |
| 783 | int separation, |
| 784 | short *out, |
| 785 | short *rampDown, |
| 786 | short *rampUp) |
| 787 | { |
| 788 | short *o, *u, *d; |
| 789 | int i, t; |
| 790 | |
| 791 | for(i = 0; i < numChannels; i++) { |
| 792 | o = out + i; |
| 793 | u = rampUp + i; |
| 794 | d = rampDown + i; |
| 795 | for(t = 0; t < numSamples + separation; t++) { |
| 796 | if(t < separation) { |
| 797 | *o = *d*(numSamples - t)/numSamples; |
| 798 | d += numChannels; |
| 799 | } else if(t < numSamples) { |
| 800 | *o = (*d*(numSamples - t) + *u*(t - separation))/numSamples; |
| 801 | d += numChannels; |
| 802 | u += numChannels; |
| 803 | } else { |
| 804 | *o = *u*(t - separation)/numSamples; |
| 805 | u += numChannels; |
| 806 | } |
| 807 | o += numChannels; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /* Just move the new samples in the output buffer to the pitch bufer */ |
| 813 | static int moveNewSamplesToPitchBuffer( |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 814 | sonicStream stream, |
| 815 | int originalNumOutputSamples) |
| 816 | { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 817 | int numSamples = stream->numOutputSamples - originalNumOutputSamples; |
| 818 | int numChannels = stream->numChannels; |
| 819 | |
| 820 | if(stream->numPitchSamples + numSamples > stream->pitchBufferSize) { |
| 821 | stream->pitchBufferSize += (stream->pitchBufferSize >> 1) + numSamples; |
| 822 | stream->pitchBuffer = (short *)realloc(stream->pitchBuffer, |
| 823 | stream->pitchBufferSize*sizeof(short)*numChannels); |
| 824 | if(stream->pitchBuffer == NULL) { |
| 825 | return 0; |
| 826 | } |
| 827 | } |
| 828 | memcpy(stream->pitchBuffer + stream->numPitchSamples*numChannels, |
| 829 | stream->outputBuffer + originalNumOutputSamples*numChannels, |
| 830 | numSamples*sizeof(short)*numChannels); |
| 831 | stream->numOutputSamples = originalNumOutputSamples; |
| 832 | stream->numPitchSamples += numSamples; |
| 833 | return 1; |
| 834 | } |
| 835 | |
| 836 | /* Remove processed samples from the pitch buffer. */ |
| 837 | static void removePitchSamples( |
| 838 | sonicStream stream, |
| 839 | int numSamples) |
| 840 | { |
| 841 | int numChannels = stream->numChannels; |
| 842 | short *source = stream->pitchBuffer + numSamples*numChannels; |
| 843 | |
| 844 | if(numSamples == 0) { |
| 845 | return; |
| 846 | } |
| 847 | if(numSamples != stream->numPitchSamples) { |
| 848 | memmove(stream->pitchBuffer, source, (stream->numPitchSamples - |
| 849 | numSamples)*sizeof(short)*numChannels); |
| 850 | } |
| 851 | stream->numPitchSamples -= numSamples; |
| 852 | } |
| 853 | |
| 854 | /* Change the pitch. The latency this introduces could be reduced by looking at |
| 855 | past samples to determine pitch, rather than future. */ |
| 856 | static int adjustPitch( |
| 857 | sonicStream stream, |
| 858 | int originalNumOutputSamples) |
| 859 | { |
| 860 | float pitch = stream->pitch; |
| 861 | int numChannels = stream->numChannels; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 862 | int period, newPeriod, separation; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 863 | int position = 0; |
| 864 | short *out, *rampDown, *rampUp; |
| 865 | |
| 866 | if(stream->numOutputSamples == originalNumOutputSamples) { |
| 867 | return 1; |
| 868 | } |
| 869 | if(!moveNewSamplesToPitchBuffer(stream, originalNumOutputSamples)) { |
| 870 | return 0; |
| 871 | } |
| 872 | while(stream->numPitchSamples - position >= stream->maxRequired) { |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 873 | period = findPitchPeriod(stream, stream->pitchBuffer + position*numChannels, 0); |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 874 | newPeriod = period/pitch; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 875 | if(!enlargeOutputBufferIfNeeded(stream, newPeriod)) { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 876 | return 0; |
| 877 | } |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 878 | out = stream->outputBuffer + stream->numOutputSamples*numChannels; |
| 879 | if(pitch >= 1.0f) { |
| 880 | rampDown = stream->pitchBuffer + position*numChannels; |
| 881 | rampUp = stream->pitchBuffer + (position + period - newPeriod)*numChannels; |
| 882 | overlapAdd(newPeriod, numChannels, out, rampDown, rampUp); |
| 883 | } else { |
| 884 | rampDown = stream->pitchBuffer + position*numChannels; |
| 885 | rampUp = stream->pitchBuffer + position*numChannels; |
| 886 | separation = newPeriod - period; |
| 887 | overlapAddWithSeparation(period, numChannels, separation, out, rampDown, rampUp); |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 888 | } |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 889 | stream->numOutputSamples += newPeriod; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 890 | position += period; |
| 891 | } |
| 892 | removePitchSamples(stream, position); |
| 893 | return 1; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 894 | } |
| 895 | |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 896 | /* Interpolate the new output sample. */ |
| 897 | static short interpolate( |
| 898 | sonicStream stream, |
| 899 | short *in, |
| 900 | int oldSampleRate, |
| 901 | int newSampleRate) |
| 902 | { |
| 903 | short left = *in; |
| 904 | short right = in[stream->numChannels]; |
| 905 | int position = stream->newRatePosition*oldSampleRate; |
| 906 | int leftPosition = stream->oldRatePosition*newSampleRate; |
| 907 | int rightPosition = (stream->oldRatePosition + 1)*newSampleRate; |
| 908 | int ratio = rightPosition - position; |
| 909 | int width = rightPosition - leftPosition; |
| 910 | |
| 911 | return (ratio*left + (width - ratio)*right)/width; |
| 912 | } |
| 913 | |
| 914 | /* Change the rate. */ |
| 915 | static int adjustRate( |
| 916 | sonicStream stream, |
| 917 | float rate, |
| 918 | int originalNumOutputSamples) |
| 919 | { |
| 920 | int newSampleRate = stream->sampleRate/rate; |
| 921 | int oldSampleRate = stream->sampleRate; |
| 922 | int numChannels = stream->numChannels; |
| 923 | int position = 0; |
| 924 | short *in, *out; |
| 925 | int i; |
| 926 | |
| 927 | /* Set these values to help with the integer math */ |
| 928 | while(newSampleRate > (1 << 14) || oldSampleRate > (1 << 14)) { |
| 929 | newSampleRate >>= 1; |
| 930 | oldSampleRate >>= 1; |
| 931 | } |
| 932 | if(stream->numOutputSamples == originalNumOutputSamples) { |
| 933 | return 1; |
| 934 | } |
| 935 | if(!moveNewSamplesToPitchBuffer(stream, originalNumOutputSamples)) { |
| 936 | return 0; |
| 937 | } |
| 938 | /* Leave at least one pitch sample in the buffer */ |
| 939 | for(position = 0; position < stream->numPitchSamples - 1; position++) { |
| 940 | while((stream->oldRatePosition + 1)*newSampleRate > |
| 941 | stream->newRatePosition*oldSampleRate) { |
| 942 | if(!enlargeOutputBufferIfNeeded(stream, 1)) { |
| 943 | return 0; |
| 944 | } |
| 945 | out = stream->outputBuffer + stream->numOutputSamples*numChannels; |
| 946 | in = stream->pitchBuffer + position; |
| 947 | for(i = 0; i < numChannels; i++) { |
| 948 | *out++ = interpolate(stream, in, oldSampleRate, newSampleRate); |
| 949 | in++; |
| 950 | } |
| 951 | stream->newRatePosition++; |
| 952 | stream->numOutputSamples++; |
| 953 | } |
| 954 | stream->oldRatePosition++; |
| 955 | if(stream->oldRatePosition == oldSampleRate) { |
| 956 | stream->oldRatePosition = 0; |
| 957 | if(stream->newRatePosition != newSampleRate) { |
| 958 | fprintf(stderr, |
| 959 | "Assertion failed: stream->newRatePosition != newSampleRate\n"); |
| 960 | exit(1); |
| 961 | } |
| 962 | stream->newRatePosition = 0; |
| 963 | } |
| 964 | } |
| 965 | removePitchSamples(stream, position); |
| 966 | return 1; |
| 967 | } |
| 968 | |
| 969 | |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 970 | /* Skip over a pitch period, and copy period/speed samples to the output */ |
| 971 | static int skipPitchPeriod( |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 972 | sonicStream stream, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 973 | short *samples, |
| 974 | float speed, |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 975 | int period) |
| 976 | { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 977 | long newSamples; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 978 | int numChannels = stream->numChannels; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 979 | |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 980 | if(speed >= 2.0f) { |
| 981 | newSamples = period/(speed - 1.0f); |
| 982 | } else if(speed > 1.0f) { |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 983 | newSamples = period; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 984 | stream->remainingInputToCopy = period*(2.0f - speed)/(speed - 1.0f); |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 985 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 986 | if(!enlargeOutputBufferIfNeeded(stream, newSamples)) { |
| 987 | return 0; |
| 988 | } |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 989 | overlapAdd(newSamples, numChannels, stream->outputBuffer + |
| 990 | stream->numOutputSamples*numChannels, samples, samples + period*numChannels); |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 991 | stream->numOutputSamples += newSamples; |
| 992 | return newSamples; |
| 993 | } |
| 994 | |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 995 | /* Insert a pitch period, and determine how much input to copy directly. */ |
| 996 | static int insertPitchPeriod( |
| 997 | sonicStream stream, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 998 | short *samples, |
| 999 | float speed, |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1000 | int period) |
| 1001 | { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 1002 | long newSamples; |
| 1003 | short *out; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1004 | int numChannels = stream->numChannels; |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1005 | |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 1006 | if(speed < 0.5f) { |
| 1007 | newSamples = period*speed/(1.0f - speed); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1008 | } else { |
| 1009 | newSamples = period; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 1010 | stream->remainingInputToCopy = period*(2.0f*speed - 1.0f)/(1.0f - speed); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1011 | } |
| 1012 | if(!enlargeOutputBufferIfNeeded(stream, period + newSamples)) { |
| 1013 | return 0; |
| 1014 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1015 | out = stream->outputBuffer + stream->numOutputSamples*numChannels; |
| 1016 | memcpy(out, samples, period*sizeof(short)*numChannels); |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 1017 | out = stream->outputBuffer + (stream->numOutputSamples + period)*numChannels; |
| 1018 | overlapAdd(newSamples, numChannels, out, samples + period*numChannels, samples); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1019 | stream->numOutputSamples += period + newSamples; |
| 1020 | return newSamples; |
| 1021 | } |
| 1022 | |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1023 | /* Resample as many pitch periods as we have buffered on the input. Return 0 if |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1024 | we fail to resize an input or output buffer. Also scale the output by the volume. */ |
| 1025 | static int changeSpeed( |
| 1026 | sonicStream stream, |
| 1027 | float speed) |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1028 | { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1029 | short *samples; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 1030 | int numSamples = stream->numInputSamples; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1031 | int position = 0, period, newSamples; |
| 1032 | int maxRequired = stream->maxRequired; |
| 1033 | |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1034 | if(stream->numInputSamples < maxRequired) { |
| 1035 | return 1; |
| 1036 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1037 | do { |
| 1038 | if(stream->remainingInputToCopy > 0) { |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1039 | newSamples = copyInputToOutput(stream, position); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1040 | position += newSamples; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1041 | } else { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1042 | samples = stream->inputBuffer + position*stream->numChannels; |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame] | 1043 | period = findPitchPeriod(stream, samples, 1); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1044 | if(speed > 1.0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1045 | newSamples = skipPitchPeriod(stream, samples, speed, period); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1046 | position += period + newSamples; |
| 1047 | } else { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1048 | newSamples = insertPitchPeriod(stream, samples, speed, period); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 1049 | position += newSamples; |
| 1050 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1051 | } |
| 1052 | if(newSamples == 0) { |
| 1053 | return 0; /* Failed to resize output buffer */ |
| 1054 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 1055 | } while(position + maxRequired <= numSamples); |
| 1056 | removeInputSamples(stream, position); |
| 1057 | return 1; |
| 1058 | } |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 1059 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1060 | /* Resample as many pitch periods as we have buffered on the input. Return 0 if |
| 1061 | we fail to resize an input or output buffer. Also scale the output by the volume. */ |
| 1062 | static int processStreamInput( |
| 1063 | sonicStream stream) |
| 1064 | { |
| 1065 | int originalNumOutputSamples = stream->numOutputSamples; |
| 1066 | float speed = stream->speed/stream->pitch; |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 1067 | float rate = stream->rate; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1068 | |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 1069 | if(!stream->useChordPitch) { |
| 1070 | rate *= stream->pitch; |
| 1071 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1072 | if(speed > 1.00001 || speed < 0.99999) { |
| 1073 | changeSpeed(stream, speed); |
| 1074 | } else { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 1075 | if(!copyToOutput(stream, stream->inputBuffer, stream->numInputSamples)) { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1076 | return 0; |
| 1077 | } |
| 1078 | stream->numInputSamples = 0; |
| 1079 | } |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 1080 | if(stream->useChordPitch) { |
| 1081 | if(stream->pitch != 1.0f) { |
| 1082 | if(!adjustPitch(stream, originalNumOutputSamples)) { |
| 1083 | return 0; |
| 1084 | } |
| 1085 | } |
| 1086 | } else if(rate != 1.0f) { |
| 1087 | if(!adjustRate(stream, rate, originalNumOutputSamples)) { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1090 | } |
| 1091 | if(stream->volume != 1.0f) { |
| 1092 | /* Adjust output volume. */ |
| 1093 | scaleSamples(stream->outputBuffer + originalNumOutputSamples*stream->numChannels, |
| 1094 | (stream->numOutputSamples - originalNumOutputSamples)*stream->numChannels, |
| 1095 | stream->volume); |
| 1096 | } |
| 1097 | return 1; |
| 1098 | } |
| 1099 | |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 1100 | /* Write floating point data to the input buffer and process it. */ |
| 1101 | int sonicWriteFloatToStream( |
| 1102 | sonicStream stream, |
| 1103 | float *samples, |
| 1104 | int numSamples) |
| 1105 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 1106 | if(!addFloatSamplesToInputBuffer(stream, samples, numSamples)) { |
| 1107 | return 0; |
| 1108 | } |
| 1109 | return processStreamInput(stream); |
| 1110 | } |
| 1111 | |
| 1112 | /* Simple wrapper around sonicWriteFloatToStream that does the short to float |
| 1113 | conversion for you. */ |
| 1114 | int sonicWriteShortToStream( |
| 1115 | sonicStream stream, |
| 1116 | short *samples, |
| 1117 | int numSamples) |
| 1118 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 1119 | if(!addShortSamplesToInputBuffer(stream, samples, numSamples)) { |
| 1120 | return 0; |
| 1121 | } |
| 1122 | return processStreamInput(stream); |
| 1123 | } |
| 1124 | |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 1125 | /* Simple wrapper around sonicWriteFloatToStream that does the unsigned char to float |
| 1126 | conversion for you. */ |
| 1127 | int sonicWriteUnsignedCharToStream( |
| 1128 | sonicStream stream, |
| 1129 | unsigned char *samples, |
| 1130 | int numSamples) |
| 1131 | { |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 1132 | if(!addUnsignedCharSamplesToInputBuffer(stream, samples, numSamples)) { |
| 1133 | return 0; |
| 1134 | } |
| 1135 | return processStreamInput(stream); |
| 1136 | } |
| 1137 | |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 1138 | /* This is a non-stream oriented interface to just change the speed of a sound sample */ |
| 1139 | int sonicChangeFloatSpeed( |
| 1140 | float *samples, |
| 1141 | int numSamples, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 1142 | float speed, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1143 | float pitch, |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1144 | float rate, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1145 | float volume, |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1146 | int useChordPitch, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1147 | int sampleRate, |
| 1148 | int numChannels) |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 1149 | { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1150 | sonicStream stream = sonicCreateStream(sampleRate, numChannels); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 1151 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1152 | sonicSetSpeed(stream, speed); |
| 1153 | sonicSetPitch(stream, pitch); |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1154 | sonicSetRate(stream, rate); |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1155 | sonicSetVolume(stream, volume); |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1156 | sonicSetChordPitch(stream, useChordPitch); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 1157 | sonicWriteFloatToStream(stream, samples, numSamples); |
| 1158 | sonicFlushStream(stream); |
| 1159 | numSamples = sonicSamplesAvailable(stream); |
| 1160 | sonicReadFloatFromStream(stream, samples, numSamples); |
| 1161 | sonicDestroyStream(stream); |
| 1162 | return numSamples; |
| 1163 | } |
| 1164 | |
| 1165 | /* This is a non-stream oriented interface to just change the speed of a sound sample */ |
| 1166 | int sonicChangeShortSpeed( |
| 1167 | short *samples, |
| 1168 | int numSamples, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 1169 | float speed, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1170 | float pitch, |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1171 | float rate, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1172 | float volume, |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1173 | int useChordPitch, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 1174 | int sampleRate, |
| 1175 | int numChannels) |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 1176 | { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1177 | sonicStream stream = sonicCreateStream(sampleRate, numChannels); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 1178 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1179 | sonicSetSpeed(stream, speed); |
| 1180 | sonicSetPitch(stream, pitch); |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1181 | sonicSetRate(stream, rate); |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 1182 | sonicSetVolume(stream, volume); |
Bill Cox | d0380df | 2011-01-11 08:59:52 -0500 | [diff] [blame] | 1183 | sonicSetChordPitch(stream, useChordPitch); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 1184 | sonicWriteShortToStream(stream, samples, numSamples); |
| 1185 | sonicFlushStream(stream); |
| 1186 | numSamples = sonicSamplesAvailable(stream); |
| 1187 | sonicReadShortFromStream(stream, samples, numSamples); |
| 1188 | sonicDestroyStream(stream); |
| 1189 | return numSamples; |
| 1190 | } |