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 | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 20 | #include <stdio.h> |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Bill Cox | a33e3bd | 2010-11-16 19:57:43 -0500 | [diff] [blame] | 23 | #include <stdarg.h> |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 24 | #include "sonic.h" |
| 25 | |
| 26 | struct sonicStreamStruct { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 27 | short *inputBuffer; |
| 28 | short *outputBuffer; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 29 | short *pitchBuffer; |
| 30 | short *downSampleBuffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 31 | float speed; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 32 | float volume; |
| 33 | float pitch; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 34 | int numChannels; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 35 | int inputBufferSize; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 36 | int pitchBufferSize; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 37 | int outputBufferSize; |
| 38 | int numInputSamples; |
| 39 | int numOutputSamples; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 40 | int numPitchSamples; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 41 | int minPeriod; |
| 42 | int maxPeriod; |
| 43 | int maxRequired; |
| 44 | int remainingInputToCopy; |
| 45 | int sampleRate; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 46 | int prevPeriod; |
| 47 | int prevMaxDiff; |
| 48 | int prevMinDiff; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 51 | /* Just used for debugging */ |
Bill Cox | a33e3bd | 2010-11-16 19:57:43 -0500 | [diff] [blame] | 52 | void sonicMSG(char *format, ...) |
| 53 | { |
| 54 | char buffer[4096]; |
| 55 | va_list ap; |
| 56 | FILE *file; |
| 57 | |
| 58 | va_start(ap, format); |
| 59 | vsprintf((char *)buffer, (char *)format, ap); |
| 60 | va_end(ap); |
| 61 | file=fopen("/tmp/sonic.log", "a"); |
| 62 | fprintf(file, "%s", buffer); |
| 63 | fclose(file); |
| 64 | } |
| 65 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 66 | /* Scale the samples by the factor. */ |
| 67 | static void scaleSamples( |
| 68 | short *samples, |
| 69 | int numSamples, |
| 70 | float volume) |
| 71 | { |
| 72 | int fixedPointVolume = volume*4096.0f; |
| 73 | int value; |
| 74 | |
| 75 | while(numSamples--) { |
| 76 | value = (*samples*fixedPointVolume) >> 12; |
| 77 | if(value > 32767) { |
| 78 | value = 32767; |
| 79 | } else if(value < -32767) { |
| 80 | value = -32767; |
| 81 | } |
| 82 | *samples++ = value; |
| 83 | } |
| 84 | } |
| 85 | |
Bill Cox | af9a624 | 2010-11-08 09:32:27 -0500 | [diff] [blame] | 86 | /* Get the speed of the stream. */ |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 87 | float sonicGetSpeed( |
Bill Cox | af9a624 | 2010-11-08 09:32:27 -0500 | [diff] [blame] | 88 | sonicStream stream) |
| 89 | { |
| 90 | return stream->speed; |
| 91 | } |
| 92 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 93 | /* Set the speed of the stream. */ |
| 94 | void sonicSetSpeed( |
| 95 | sonicStream stream, |
| 96 | float speed) |
| 97 | { |
| 98 | stream->speed = speed; |
| 99 | } |
| 100 | |
| 101 | /* Get the pitch of the stream. */ |
| 102 | float sonicGetPitch( |
| 103 | sonicStream stream) |
| 104 | { |
| 105 | return stream->pitch; |
| 106 | } |
| 107 | |
| 108 | /* Set the pitch of the stream. */ |
| 109 | void sonicSetPitch( |
| 110 | sonicStream stream, |
| 111 | float pitch) |
| 112 | { |
| 113 | stream->pitch = pitch; |
| 114 | } |
| 115 | |
| 116 | /* Get the scaling factor of the stream. */ |
| 117 | float sonicGetVolume( |
| 118 | sonicStream stream) |
| 119 | { |
| 120 | return stream->volume; |
| 121 | } |
| 122 | |
| 123 | /* Set the scaling factor of the stream. */ |
| 124 | void sonicSetVolume( |
| 125 | sonicStream stream, |
| 126 | float volume) |
| 127 | { |
| 128 | stream->volume = volume; |
| 129 | } |
| 130 | |
Bill Cox | af9a624 | 2010-11-08 09:32:27 -0500 | [diff] [blame] | 131 | /* Get the sample rate of the stream. */ |
| 132 | int sonicGetSampleRate( |
| 133 | sonicStream stream) |
| 134 | { |
| 135 | return stream->sampleRate; |
| 136 | } |
| 137 | |
Bill Cox | 527b4e8 | 2010-11-24 17:42:58 -0500 | [diff] [blame] | 138 | /* Get the number of channels. */ |
| 139 | int sonicGetNumChannels( |
| 140 | sonicStream stream) |
| 141 | { |
| 142 | return stream->numChannels; |
| 143 | } |
| 144 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 145 | /* Destroy the sonic stream. */ |
| 146 | void sonicDestroyStream( |
| 147 | sonicStream stream) |
| 148 | { |
| 149 | if(stream->inputBuffer != NULL) { |
| 150 | free(stream->inputBuffer); |
| 151 | } |
| 152 | if(stream->outputBuffer != NULL) { |
| 153 | free(stream->outputBuffer); |
| 154 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 155 | if(stream->pitchBuffer != NULL) { |
| 156 | free(stream->pitchBuffer); |
| 157 | } |
| 158 | if(stream->downSampleBuffer != NULL) { |
| 159 | free(stream->downSampleBuffer); |
| 160 | } |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 161 | free(stream); |
| 162 | } |
| 163 | |
| 164 | /* Create a sonic stream. Return NULL only if we are out of memory and cannot |
| 165 | allocate the stream. */ |
| 166 | sonicStream sonicCreateStream( |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 167 | int sampleRate, |
| 168 | int numChannels) |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 169 | { |
| 170 | sonicStream stream = (sonicStream)calloc(1, sizeof(struct sonicStreamStruct)); |
| 171 | int minPeriod = sampleRate/SONIC_MAX_PITCH; |
| 172 | int maxPeriod = sampleRate/SONIC_MIN_PITCH; |
| 173 | int maxRequired = 2*maxPeriod; |
| 174 | |
| 175 | if(stream == NULL) { |
| 176 | return NULL; |
| 177 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 178 | stream->inputBufferSize = maxRequired; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 179 | stream->inputBuffer = (short *)calloc(maxRequired, sizeof(short)*numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 180 | if(stream->inputBuffer == NULL) { |
| 181 | sonicDestroyStream(stream); |
| 182 | return NULL; |
| 183 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 184 | stream->outputBufferSize = maxRequired; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 185 | stream->outputBuffer = (short *)calloc(maxRequired, sizeof(short)*numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 186 | if(stream->outputBuffer == NULL) { |
| 187 | sonicDestroyStream(stream); |
| 188 | return NULL; |
| 189 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 190 | stream->pitchBufferSize = maxRequired; |
| 191 | stream->pitchBuffer = (short *)calloc(maxRequired, sizeof(short)*numChannels); |
| 192 | if(stream->pitchBuffer == NULL) { |
| 193 | sonicDestroyStream(stream); |
| 194 | return NULL; |
| 195 | } |
| 196 | stream->downSampleBuffer = (short *)calloc(maxRequired, sizeof(short)); |
| 197 | stream->speed = 1.0f; |
| 198 | stream->pitch = 1.0f; |
| 199 | stream->volume = 1.0f; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 200 | stream->sampleRate = sampleRate; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 201 | stream->numChannels = numChannels; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 202 | stream->minPeriod = minPeriod; |
| 203 | stream->maxPeriod = maxPeriod; |
| 204 | stream->maxRequired = maxRequired; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 205 | return stream; |
| 206 | } |
| 207 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 208 | /* Enlarge the output buffer if needed. */ |
| 209 | static int enlargeOutputBufferIfNeeded( |
| 210 | sonicStream stream, |
| 211 | int numSamples) |
| 212 | { |
| 213 | if(stream->numOutputSamples + numSamples > stream->outputBufferSize) { |
| 214 | stream->outputBufferSize += (stream->outputBufferSize >> 1) + numSamples; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 215 | stream->outputBuffer = (short *)realloc(stream->outputBuffer, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 216 | stream->outputBufferSize*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 217 | if(stream->outputBuffer == NULL) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | return 1; |
| 222 | } |
| 223 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 224 | /* Enlarge the input buffer if needed. */ |
| 225 | static int enlargeInputBufferIfNeeded( |
| 226 | sonicStream stream, |
| 227 | int numSamples) |
| 228 | { |
| 229 | if(stream->numInputSamples + numSamples > stream->inputBufferSize) { |
| 230 | stream->inputBufferSize += (stream->inputBufferSize >> 1) + numSamples; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 231 | stream->inputBuffer = (short *)realloc(stream->inputBuffer, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 232 | stream->inputBufferSize*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 233 | if(stream->inputBuffer == NULL) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | /* Add the input samples to the input buffer. */ |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 241 | static int addFloatSamplesToInputBuffer( |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 242 | sonicStream stream, |
| 243 | float *samples, |
| 244 | int numSamples) |
| 245 | { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 246 | short *buffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 247 | int count = numSamples*stream->numChannels; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 248 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 249 | if(numSamples == 0) { |
| 250 | return 1; |
| 251 | } |
| 252 | if(!enlargeInputBufferIfNeeded(stream, numSamples)) { |
| 253 | return 0; |
| 254 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 255 | buffer = stream->inputBuffer + stream->numInputSamples*stream->numChannels; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 256 | while(count--) { |
| 257 | *buffer++ = (*samples++)*32767.0f; |
| 258 | } |
Bill Cox | 14efa44 | 2010-11-02 15:43:58 -0400 | [diff] [blame] | 259 | stream->numInputSamples += numSamples; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 260 | return 1; |
| 261 | } |
| 262 | |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 263 | /* Add the input samples to the input buffer. */ |
| 264 | static int addShortSamplesToInputBuffer( |
| 265 | sonicStream stream, |
| 266 | short *samples, |
| 267 | int numSamples) |
| 268 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 269 | if(numSamples == 0) { |
| 270 | return 1; |
| 271 | } |
| 272 | if(!enlargeInputBufferIfNeeded(stream, numSamples)) { |
| 273 | return 0; |
| 274 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 275 | memcpy(stream->inputBuffer + stream->numInputSamples*stream->numChannels, samples, |
| 276 | numSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 277 | stream->numInputSamples += numSamples; |
| 278 | return 1; |
| 279 | } |
| 280 | |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 281 | /* Add the input samples to the input buffer. */ |
| 282 | static int addUnsignedCharSamplesToInputBuffer( |
| 283 | sonicStream stream, |
| 284 | unsigned char *samples, |
| 285 | int numSamples) |
| 286 | { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 287 | short *buffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 288 | int count = numSamples*stream->numChannels; |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 289 | |
| 290 | if(numSamples == 0) { |
| 291 | return 1; |
| 292 | } |
| 293 | if(!enlargeInputBufferIfNeeded(stream, numSamples)) { |
| 294 | return 0; |
| 295 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 296 | buffer = stream->inputBuffer + stream->numInputSamples*stream->numChannels; |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 297 | while(count--) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 298 | *buffer++ = (*samples++ - 128) << 8; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 299 | } |
| 300 | stream->numInputSamples += numSamples; |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | /* Remove input samples that we have already processed. */ |
| 305 | static void removeInputSamples( |
| 306 | sonicStream stream, |
| 307 | int position) |
| 308 | { |
| 309 | int remainingSamples = stream->numInputSamples - position; |
| 310 | |
| 311 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 312 | memmove(stream->inputBuffer, stream->inputBuffer + position*stream->numChannels, |
| 313 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 314 | } |
| 315 | stream->numInputSamples = remainingSamples; |
| 316 | } |
| 317 | |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 318 | /* Just copy from the array to the output buffer */ |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 319 | static int copyToOutput( |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 320 | sonicStream stream, |
| 321 | short *samples, |
| 322 | int numSamples) |
| 323 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 324 | if(!enlargeOutputBufferIfNeeded(stream, numSamples)) { |
| 325 | return 0; |
| 326 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 327 | memcpy(stream->outputBuffer + stream->numOutputSamples*stream->numChannels, |
| 328 | samples, numSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 329 | stream->numOutputSamples += numSamples; |
| 330 | return numSamples; |
| 331 | } |
| 332 | |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 333 | /* Just copy from the input buffer to the output buffer. Return 0 if we fail to |
| 334 | resize the output buffer. Otherwise, return numSamples */ |
| 335 | static int copyInputToOutput( |
| 336 | sonicStream stream, |
| 337 | int position) |
| 338 | { |
| 339 | int numSamples = stream->remainingInputToCopy; |
| 340 | |
| 341 | if(numSamples > stream->maxRequired) { |
| 342 | numSamples = stream->maxRequired; |
| 343 | } |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 344 | if(!copyToOutput(stream, stream->inputBuffer + position*stream->numChannels, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 345 | numSamples)) { |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 346 | return 0; |
| 347 | } |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 348 | stream->remainingInputToCopy -= numSamples; |
| 349 | return numSamples; |
| 350 | } |
| 351 | |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 352 | /* Read data out of the stream. Sometimes no data will be available, and zero |
| 353 | is returned, which is not an error condition. */ |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 354 | int sonicReadFloatFromStream( |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 355 | sonicStream stream, |
| 356 | float *samples, |
| 357 | int maxSamples) |
| 358 | { |
| 359 | int numSamples = stream->numOutputSamples; |
| 360 | int remainingSamples = 0; |
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; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 363 | |
| 364 | if(numSamples == 0) { |
| 365 | return 0; |
| 366 | } |
| 367 | if(numSamples > maxSamples) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 368 | remainingSamples = numSamples - maxSamples; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 369 | numSamples = maxSamples; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 370 | } |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 371 | buffer = stream->outputBuffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 372 | count = numSamples*stream->numChannels; |
| 373 | while(count--) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 374 | *samples++ = (*buffer++)/32767.0f; |
| 375 | } |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 376 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 377 | memmove(stream->outputBuffer, stream->outputBuffer + numSamples*stream->numChannels, |
| 378 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 379 | } |
| 380 | stream->numOutputSamples = remainingSamples; |
| 381 | return numSamples; |
| 382 | } |
| 383 | |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 384 | /* Read short data out of the stream. Sometimes no data will be available, and zero |
| 385 | is returned, which is not an error condition. */ |
| 386 | int sonicReadShortFromStream( |
| 387 | sonicStream stream, |
| 388 | short *samples, |
| 389 | int maxSamples) |
| 390 | { |
| 391 | int numSamples = stream->numOutputSamples; |
| 392 | int remainingSamples = 0; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 393 | |
| 394 | if(numSamples == 0) { |
| 395 | return 0; |
| 396 | } |
| 397 | if(numSamples > maxSamples) { |
| 398 | remainingSamples = numSamples - maxSamples; |
| 399 | numSamples = maxSamples; |
| 400 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 401 | memcpy(samples, stream->outputBuffer, numSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 402 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 403 | memmove(stream->outputBuffer, stream->outputBuffer + numSamples*stream->numChannels, |
| 404 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 405 | } |
| 406 | stream->numOutputSamples = remainingSamples; |
| 407 | return numSamples; |
| 408 | } |
| 409 | |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 410 | /* Read unsigned char data out of the stream. Sometimes no data will be available, and zero |
| 411 | is returned, which is not an error condition. */ |
| 412 | int sonicReadUnsignedCharFromStream( |
| 413 | sonicStream stream, |
| 414 | unsigned char *samples, |
| 415 | int maxSamples) |
| 416 | { |
| 417 | int numSamples = stream->numOutputSamples; |
| 418 | int remainingSamples = 0; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 419 | short *buffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 420 | int count; |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 421 | |
| 422 | if(numSamples == 0) { |
| 423 | return 0; |
| 424 | } |
| 425 | if(numSamples > maxSamples) { |
| 426 | remainingSamples = numSamples - maxSamples; |
| 427 | numSamples = maxSamples; |
| 428 | } |
| 429 | buffer = stream->outputBuffer; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 430 | count = numSamples*stream->numChannels; |
| 431 | while(count--) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 432 | *samples++ = (char)((*buffer++) >> 8) + 128; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 433 | } |
| 434 | if(remainingSamples > 0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 435 | memmove(stream->outputBuffer, stream->outputBuffer + numSamples*stream->numChannels, |
| 436 | remainingSamples*sizeof(short)*stream->numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 437 | } |
| 438 | stream->numOutputSamples = remainingSamples; |
| 439 | return numSamples; |
| 440 | } |
| 441 | |
| 442 | /* Force the sonic stream to generate output using whatever data it currently |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 443 | has. No extra delay will be added to the output, but flushing in the middle of |
| 444 | words could introduce distortion. */ |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 445 | int sonicFlushStream( |
| 446 | sonicStream stream) |
| 447 | { |
| 448 | int maxRequired = stream->maxRequired; |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 449 | int remainingSamples = stream->numInputSamples; |
| 450 | float speed = stream->speed/stream->pitch; |
| 451 | int expectedOutputSamples = stream->numOutputSamples + |
| 452 | (int)(remainingSamples/speed + stream->numPitchSamples/stream->pitch + 0.5f); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 453 | |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 454 | /* Add enough silence to flush both input and pitch buffers. */ |
| 455 | if(!enlargeInputBufferIfNeeded(stream, remainingSamples + 2*maxRequired)) { |
| 456 | return 0; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 457 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 458 | memset(stream->inputBuffer + remainingSamples*stream->numChannels, 0, |
| 459 | 2*maxRequired*sizeof(short)*stream->numChannels); |
| 460 | stream->numInputSamples += 2*maxRequired; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 461 | if(!sonicWriteShortToStream(stream, NULL, 0)) { |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | /* 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^] | 465 | if(stream->numOutputSamples > expectedOutputSamples) { |
| 466 | stream->numOutputSamples = expectedOutputSamples; |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 467 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 468 | /* Empty input and pitch buffers */ |
| 469 | stream->numInputSamples = 0; |
| 470 | stream->remainingInputToCopy = 0; |
| 471 | stream->numPitchSamples = 0; |
Bill Cox | 4bbbbcc | 2010-11-09 05:32:38 -0500 | [diff] [blame] | 472 | return 1; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* Return the number of samples in the output buffer */ |
Bill Cox | 3a7abf9 | 2010-11-06 15:18:49 -0400 | [diff] [blame] | 476 | int sonicSamplesAvailable( |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 477 | sonicStream stream) |
| 478 | { |
| 479 | return stream->numOutputSamples; |
| 480 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 481 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 482 | /* If skip is greater than one, average skip samples togther and write them to |
| 483 | the down-sample buffer. If numChannels is greater than one, mix the channels |
| 484 | together as we down sample. */ |
| 485 | static void downSampleInput( |
| 486 | sonicStream stream, |
| 487 | short *samples, |
| 488 | int skip) |
| 489 | { |
| 490 | int numSamples = stream->maxRequired/skip; |
| 491 | int samplesPerValue = stream->numChannels*skip; |
| 492 | int i, j; |
| 493 | int value; |
| 494 | short *downSamples = stream->downSampleBuffer; |
| 495 | |
| 496 | for(i = 0; i < numSamples; i++) { |
| 497 | value = 0; |
| 498 | for(j = 0; j < samplesPerValue; j++) { |
| 499 | value += *samples++; |
| 500 | } |
| 501 | value /= samplesPerValue; |
| 502 | *downSamples++ = value; |
| 503 | } |
| 504 | } |
| 505 | |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 506 | /* Find the best frequency match in the range, and given a sample skip multiple. |
| 507 | For now, just find the pitch of the first channel. */ |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 508 | static int findPitchPeriodInRange( |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 509 | short *samples, |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 510 | int minPeriod, |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 511 | int maxPeriod, |
| 512 | int *retMinDiff, |
| 513 | int *retMaxDiff) |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 514 | { |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 515 | int period, bestPeriod = 0, worstPeriod = 255; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 516 | short *s, *p, sVal, pVal; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 517 | unsigned long diff, minDiff = 1, maxDiff = 0; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 518 | int i; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 519 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 520 | for(period = minPeriod; period <= maxPeriod; period++) { |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 521 | diff = 0; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 522 | s = samples; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 523 | p = samples + period; |
| 524 | for(i = 0; i < period; i++) { |
| 525 | sVal = *s++; |
| 526 | pVal = *p++; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 527 | diff += sVal >= pVal? (unsigned short)(sVal - pVal) : |
| 528 | (unsigned short)(pVal - sVal); |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 529 | } |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 530 | /* Note that the highest number of samples we add into diff will be less |
| 531 | than 256, since we skip samples. Thus, diff is a 24 bit number, and |
| 532 | we can safely multiply by numSamples without overflow */ |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 533 | if(diff*bestPeriod < minDiff*period) { |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 534 | minDiff = diff; |
| 535 | bestPeriod = period; |
| 536 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 537 | if(diff*worstPeriod > maxDiff*period) { |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 538 | maxDiff = diff; |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 539 | worstPeriod = period; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 540 | } |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 541 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 542 | *retMinDiff = minDiff/bestPeriod; |
| 543 | *retMaxDiff = maxDiff/worstPeriod; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 544 | return bestPeriod; |
| 545 | } |
| 546 | |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 547 | /* At abrupt ends of voiced words, we can have pitch periods that are better |
| 548 | aproximated by the previous pitch period estimate. Try to detect this case. */ |
| 549 | static int prevPeriodBetter( |
| 550 | sonicStream stream, |
| 551 | int period, |
| 552 | int minDiff, |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 553 | int maxDiff, |
| 554 | int preferNewPeriod) |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 555 | { |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 556 | if(minDiff == 0) { |
| 557 | return 0; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 558 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 559 | if(preferNewPeriod) { |
| 560 | if(maxDiff > minDiff*3) { |
| 561 | /* Got a reasonable match this period */ |
| 562 | return 0; |
| 563 | } |
| 564 | if(minDiff*2 <= stream->prevMinDiff*3) { |
| 565 | /* Mismatch is not that much greater this period */ |
| 566 | return 0; |
| 567 | } |
| 568 | } else { |
| 569 | if(minDiff <= stream->prevMinDiff) { |
| 570 | return 0; |
| 571 | } |
| 572 | } |
| 573 | return 1; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 574 | } |
| 575 | |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 576 | /* 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] | 577 | multiple ways to get a good answer. This version uses AMDF. To improve |
| 578 | speed, we down sample by an integer factor get in the 11KHz range, and then |
| 579 | do it again with a narrower frequency range without down sampling */ |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 580 | static int findPitchPeriod( |
| 581 | sonicStream stream, |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 582 | short *samples, |
| 583 | int preferNewPeriod) |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 584 | { |
| 585 | int minPeriod = stream->minPeriod; |
| 586 | int maxPeriod = stream->maxPeriod; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 587 | int sampleRate = stream->sampleRate; |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 588 | int minDiff, maxDiff, retPeriod; |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 589 | int skip = 1; |
| 590 | int period; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 591 | |
Bill Cox | 0cd49c8 | 2010-11-03 10:46:22 -0400 | [diff] [blame] | 592 | if(sampleRate > SONIC_AMDF_FREQ) { |
| 593 | skip = sampleRate/SONIC_AMDF_FREQ; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 594 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 595 | if(stream->numChannels == 1 && skip == 1) { |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 596 | period = findPitchPeriodInRange(samples, minPeriod, maxPeriod, &minDiff, &maxDiff); |
| 597 | } else { |
| 598 | downSampleInput(stream, samples, skip); |
| 599 | period = findPitchPeriodInRange(stream->downSampleBuffer, minPeriod/skip, |
| 600 | maxPeriod/skip, &minDiff, &maxDiff); |
| 601 | if(skip != 1) { |
| 602 | period *= skip; |
| 603 | minPeriod = period - (skip << 2); |
| 604 | maxPeriod = period + (skip << 2); |
| 605 | if(minPeriod < stream->minPeriod) { |
| 606 | minPeriod = stream->minPeriod; |
| 607 | } |
| 608 | if(maxPeriod > stream->maxPeriod) { |
| 609 | maxPeriod = stream->maxPeriod; |
| 610 | } |
| 611 | if(stream->numChannels == 1) { |
| 612 | period = findPitchPeriodInRange(samples, minPeriod, maxPeriod, |
| 613 | &minDiff, &maxDiff); |
| 614 | } else { |
| 615 | downSampleInput(stream, samples, 1); |
| 616 | period = findPitchPeriodInRange(stream->downSampleBuffer, minPeriod, |
| 617 | maxPeriod, &minDiff, &maxDiff); |
| 618 | } |
| 619 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 620 | } |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 621 | if(prevPeriodBetter(stream, period, minDiff, maxDiff, preferNewPeriod)) { |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 622 | retPeriod = stream->prevPeriod; |
| 623 | } else { |
| 624 | retPeriod = period; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 625 | } |
Bill Cox | c17208e | 2010-11-26 11:09:15 -0500 | [diff] [blame] | 626 | stream->prevMinDiff = minDiff; |
| 627 | stream->prevMaxDiff = maxDiff; |
| 628 | stream->prevPeriod = period; |
| 629 | return retPeriod; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 630 | } |
| 631 | |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 632 | /* Overlap two sound segments, ramp the volume of one down, while ramping the |
| 633 | other one from zero up, and add them, storing the result at the output. */ |
| 634 | static void overlapAdd( |
| 635 | int numSamples, |
| 636 | int numChannels, |
| 637 | short *out, |
| 638 | short *rampDown, |
| 639 | short *rampUp) |
| 640 | { |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 641 | short *o, *u, *d; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 642 | int i, t; |
| 643 | |
| 644 | for(i = 0; i < numChannels; i++) { |
| 645 | o = out + i; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 646 | u = rampUp + i; |
| 647 | d = rampDown + i; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 648 | for(t = 0; t < numSamples; t++) { |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 649 | *o = (*d*(numSamples - t) + *u*t)/numSamples; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 650 | o += numChannels; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 651 | d += numChannels; |
| 652 | u += numChannels; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* Overlap two sound segments, ramp the volume of one down, while ramping the |
| 658 | other one from zero up, and add them, storing the result at the output. */ |
| 659 | static void overlapAddWithSeparation( |
| 660 | int numSamples, |
| 661 | int numChannels, |
| 662 | int separation, |
| 663 | short *out, |
| 664 | short *rampDown, |
| 665 | short *rampUp) |
| 666 | { |
| 667 | short *o, *u, *d; |
| 668 | int i, t; |
| 669 | |
| 670 | for(i = 0; i < numChannels; i++) { |
| 671 | o = out + i; |
| 672 | u = rampUp + i; |
| 673 | d = rampDown + i; |
| 674 | for(t = 0; t < numSamples + separation; t++) { |
| 675 | if(t < separation) { |
| 676 | *o = *d*(numSamples - t)/numSamples; |
| 677 | d += numChannels; |
| 678 | } else if(t < numSamples) { |
| 679 | *o = (*d*(numSamples - t) + *u*(t - separation))/numSamples; |
| 680 | d += numChannels; |
| 681 | u += numChannels; |
| 682 | } else { |
| 683 | *o = *u*(t - separation)/numSamples; |
| 684 | u += numChannels; |
| 685 | } |
| 686 | o += numChannels; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* Just move the new samples in the output buffer to the pitch bufer */ |
| 692 | static int moveNewSamplesToPitchBuffer( |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 693 | sonicStream stream, |
| 694 | int originalNumOutputSamples) |
| 695 | { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 696 | int numSamples = stream->numOutputSamples - originalNumOutputSamples; |
| 697 | int numChannels = stream->numChannels; |
| 698 | |
| 699 | if(stream->numPitchSamples + numSamples > stream->pitchBufferSize) { |
| 700 | stream->pitchBufferSize += (stream->pitchBufferSize >> 1) + numSamples; |
| 701 | stream->pitchBuffer = (short *)realloc(stream->pitchBuffer, |
| 702 | stream->pitchBufferSize*sizeof(short)*numChannels); |
| 703 | if(stream->pitchBuffer == NULL) { |
| 704 | return 0; |
| 705 | } |
| 706 | } |
| 707 | memcpy(stream->pitchBuffer + stream->numPitchSamples*numChannels, |
| 708 | stream->outputBuffer + originalNumOutputSamples*numChannels, |
| 709 | numSamples*sizeof(short)*numChannels); |
| 710 | stream->numOutputSamples = originalNumOutputSamples; |
| 711 | stream->numPitchSamples += numSamples; |
| 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | /* Remove processed samples from the pitch buffer. */ |
| 716 | static void removePitchSamples( |
| 717 | sonicStream stream, |
| 718 | int numSamples) |
| 719 | { |
| 720 | int numChannels = stream->numChannels; |
| 721 | short *source = stream->pitchBuffer + numSamples*numChannels; |
| 722 | |
| 723 | if(numSamples == 0) { |
| 724 | return; |
| 725 | } |
| 726 | if(numSamples != stream->numPitchSamples) { |
| 727 | memmove(stream->pitchBuffer, source, (stream->numPitchSamples - |
| 728 | numSamples)*sizeof(short)*numChannels); |
| 729 | } |
| 730 | stream->numPitchSamples -= numSamples; |
| 731 | } |
| 732 | |
| 733 | /* Change the pitch. The latency this introduces could be reduced by looking at |
| 734 | past samples to determine pitch, rather than future. */ |
| 735 | static int adjustPitch( |
| 736 | sonicStream stream, |
| 737 | int originalNumOutputSamples) |
| 738 | { |
| 739 | float pitch = stream->pitch; |
| 740 | int numChannels = stream->numChannels; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 741 | int period, newPeriod, separation; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 742 | int position = 0; |
| 743 | short *out, *rampDown, *rampUp; |
| 744 | |
| 745 | if(stream->numOutputSamples == originalNumOutputSamples) { |
| 746 | return 1; |
| 747 | } |
| 748 | if(!moveNewSamplesToPitchBuffer(stream, originalNumOutputSamples)) { |
| 749 | return 0; |
| 750 | } |
| 751 | while(stream->numPitchSamples - position >= stream->maxRequired) { |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 752 | period = findPitchPeriod(stream, stream->pitchBuffer + position*numChannels, 0); |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 753 | newPeriod = period/pitch; |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 754 | if(!enlargeOutputBufferIfNeeded(stream, newPeriod)) { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 755 | return 0; |
| 756 | } |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 757 | out = stream->outputBuffer + stream->numOutputSamples*numChannels; |
| 758 | if(pitch >= 1.0f) { |
| 759 | rampDown = stream->pitchBuffer + position*numChannels; |
| 760 | rampUp = stream->pitchBuffer + (position + period - newPeriod)*numChannels; |
| 761 | overlapAdd(newPeriod, numChannels, out, rampDown, rampUp); |
| 762 | } else { |
| 763 | rampDown = stream->pitchBuffer + position*numChannels; |
| 764 | rampUp = stream->pitchBuffer + position*numChannels; |
| 765 | separation = newPeriod - period; |
| 766 | overlapAddWithSeparation(period, numChannels, separation, out, rampDown, rampUp); |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 767 | } |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 768 | stream->numOutputSamples += newPeriod; |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 769 | position += period; |
| 770 | } |
| 771 | removePitchSamples(stream, position); |
| 772 | return 1; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 773 | } |
| 774 | |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 775 | /* Skip over a pitch period, and copy period/speed samples to the output */ |
| 776 | static int skipPitchPeriod( |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 777 | sonicStream stream, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 778 | short *samples, |
| 779 | float speed, |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 780 | int period) |
| 781 | { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 782 | long newSamples; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 783 | int numChannels = stream->numChannels; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 784 | |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 785 | if(speed >= 2.0f) { |
| 786 | newSamples = period/(speed - 1.0f); |
| 787 | } else if(speed > 1.0f) { |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 788 | newSamples = period; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 789 | stream->remainingInputToCopy = period*(2.0f - speed)/(speed - 1.0f); |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 790 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 791 | if(!enlargeOutputBufferIfNeeded(stream, newSamples)) { |
| 792 | return 0; |
| 793 | } |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 794 | overlapAdd(newSamples, numChannels, stream->outputBuffer + |
| 795 | stream->numOutputSamples*numChannels, samples, samples + period*numChannels); |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 796 | stream->numOutputSamples += newSamples; |
| 797 | return newSamples; |
| 798 | } |
| 799 | |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 800 | /* Insert a pitch period, and determine how much input to copy directly. */ |
| 801 | static int insertPitchPeriod( |
| 802 | sonicStream stream, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 803 | short *samples, |
| 804 | float speed, |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 805 | int period) |
| 806 | { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 807 | long newSamples; |
| 808 | short *out; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 809 | int numChannels = stream->numChannels; |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 810 | |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 811 | if(speed < 0.5f) { |
| 812 | newSamples = period*speed/(1.0f - speed); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 813 | } else { |
| 814 | newSamples = period; |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 815 | stream->remainingInputToCopy = period*(2.0f*speed - 1.0f)/(1.0f - speed); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 816 | } |
| 817 | if(!enlargeOutputBufferIfNeeded(stream, period + newSamples)) { |
| 818 | return 0; |
| 819 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 820 | out = stream->outputBuffer + stream->numOutputSamples*numChannels; |
| 821 | memcpy(out, samples, period*sizeof(short)*numChannels); |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 822 | out = stream->outputBuffer + (stream->numOutputSamples + period)*numChannels; |
| 823 | overlapAdd(newSamples, numChannels, out, samples + period*numChannels, samples); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 824 | stream->numOutputSamples += period + newSamples; |
| 825 | return newSamples; |
| 826 | } |
| 827 | |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 828 | /* 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] | 829 | we fail to resize an input or output buffer. Also scale the output by the volume. */ |
| 830 | static int changeSpeed( |
| 831 | sonicStream stream, |
| 832 | float speed) |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 833 | { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 834 | short *samples; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 835 | int numSamples = stream->numInputSamples; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 836 | int position = 0, period, newSamples; |
| 837 | int maxRequired = stream->maxRequired; |
| 838 | |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 839 | if(stream->numInputSamples < maxRequired) { |
| 840 | return 1; |
| 841 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 842 | do { |
| 843 | if(stream->remainingInputToCopy > 0) { |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 844 | newSamples = copyInputToOutput(stream, position); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 845 | position += newSamples; |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 846 | } else { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 847 | samples = stream->inputBuffer + position*stream->numChannels; |
Bill Cox | 0283c34 | 2010-11-30 11:47:50 -0500 | [diff] [blame^] | 848 | period = findPitchPeriod(stream, samples, 1); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 849 | if(speed > 1.0) { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 850 | newSamples = skipPitchPeriod(stream, samples, speed, period); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 851 | position += period + newSamples; |
| 852 | } else { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 853 | newSamples = insertPitchPeriod(stream, samples, speed, period); |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 854 | position += newSamples; |
| 855 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 856 | } |
| 857 | if(newSamples == 0) { |
| 858 | return 0; /* Failed to resize output buffer */ |
| 859 | } |
Bill Cox | 9bf11b5 | 2010-11-03 05:33:09 -0400 | [diff] [blame] | 860 | } while(position + maxRequired <= numSamples); |
| 861 | removeInputSamples(stream, position); |
| 862 | return 1; |
| 863 | } |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 864 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 865 | /* Resample as many pitch periods as we have buffered on the input. Return 0 if |
| 866 | we fail to resize an input or output buffer. Also scale the output by the volume. */ |
| 867 | static int processStreamInput( |
| 868 | sonicStream stream) |
| 869 | { |
| 870 | int originalNumOutputSamples = stream->numOutputSamples; |
| 871 | float speed = stream->speed/stream->pitch; |
| 872 | |
| 873 | if(speed > 1.00001 || speed < 0.99999) { |
| 874 | changeSpeed(stream, speed); |
| 875 | } else { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 876 | if(!copyToOutput(stream, stream->inputBuffer, stream->numInputSamples)) { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | stream->numInputSamples = 0; |
| 880 | } |
| 881 | if(stream->pitch != 1.0f) { |
Bill Cox | 68e2aee | 2010-11-23 19:24:41 -0500 | [diff] [blame] | 882 | if(!adjustPitch(stream, originalNumOutputSamples)) { |
| 883 | return 0; |
| 884 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 885 | } |
| 886 | if(stream->volume != 1.0f) { |
| 887 | /* Adjust output volume. */ |
| 888 | scaleSamples(stream->outputBuffer + originalNumOutputSamples*stream->numChannels, |
| 889 | (stream->numOutputSamples - originalNumOutputSamples)*stream->numChannels, |
| 890 | stream->volume); |
| 891 | } |
| 892 | return 1; |
| 893 | } |
| 894 | |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 895 | /* Write floating point data to the input buffer and process it. */ |
| 896 | int sonicWriteFloatToStream( |
| 897 | sonicStream stream, |
| 898 | float *samples, |
| 899 | int numSamples) |
| 900 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 901 | if(!addFloatSamplesToInputBuffer(stream, samples, numSamples)) { |
| 902 | return 0; |
| 903 | } |
| 904 | return processStreamInput(stream); |
| 905 | } |
| 906 | |
| 907 | /* Simple wrapper around sonicWriteFloatToStream that does the short to float |
| 908 | conversion for you. */ |
| 909 | int sonicWriteShortToStream( |
| 910 | sonicStream stream, |
| 911 | short *samples, |
| 912 | int numSamples) |
| 913 | { |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 914 | if(!addShortSamplesToInputBuffer(stream, samples, numSamples)) { |
| 915 | return 0; |
| 916 | } |
| 917 | return processStreamInput(stream); |
| 918 | } |
| 919 | |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 920 | /* Simple wrapper around sonicWriteFloatToStream that does the unsigned char to float |
| 921 | conversion for you. */ |
| 922 | int sonicWriteUnsignedCharToStream( |
| 923 | sonicStream stream, |
| 924 | unsigned char *samples, |
| 925 | int numSamples) |
| 926 | { |
Bill Cox | 8a23d2f | 2010-11-16 18:49:36 -0500 | [diff] [blame] | 927 | if(!addUnsignedCharSamplesToInputBuffer(stream, samples, numSamples)) { |
| 928 | return 0; |
| 929 | } |
| 930 | return processStreamInput(stream); |
| 931 | } |
| 932 | |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 933 | /* This is a non-stream oriented interface to just change the speed of a sound sample */ |
| 934 | int sonicChangeFloatSpeed( |
| 935 | float *samples, |
| 936 | int numSamples, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 937 | float speed, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 938 | float pitch, |
| 939 | float volume, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 940 | int sampleRate, |
| 941 | int numChannels) |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 942 | { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 943 | sonicStream stream = sonicCreateStream(sampleRate, numChannels); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 944 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 945 | sonicSetSpeed(stream, speed); |
| 946 | sonicSetPitch(stream, pitch); |
| 947 | sonicSetVolume(stream, volume); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 948 | sonicWriteFloatToStream(stream, samples, numSamples); |
| 949 | sonicFlushStream(stream); |
| 950 | numSamples = sonicSamplesAvailable(stream); |
| 951 | sonicReadFloatFromStream(stream, samples, numSamples); |
| 952 | sonicDestroyStream(stream); |
| 953 | return numSamples; |
| 954 | } |
| 955 | |
| 956 | /* This is a non-stream oriented interface to just change the speed of a sound sample */ |
| 957 | int sonicChangeShortSpeed( |
| 958 | short *samples, |
| 959 | int numSamples, |
Bill Cox | 6a1bbb1 | 2010-11-19 11:14:28 -0500 | [diff] [blame] | 960 | float speed, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 961 | float pitch, |
| 962 | float volume, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 963 | int sampleRate, |
| 964 | int numChannels) |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 965 | { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 966 | sonicStream stream = sonicCreateStream(sampleRate, numChannels); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 967 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 968 | sonicSetSpeed(stream, speed); |
| 969 | sonicSetPitch(stream, pitch); |
| 970 | sonicSetVolume(stream, volume); |
Bill Cox | 036d732 | 2010-11-09 09:29:24 -0500 | [diff] [blame] | 971 | sonicWriteShortToStream(stream, samples, numSamples); |
| 972 | sonicFlushStream(stream); |
| 973 | numSamples = sonicSamplesAvailable(stream); |
| 974 | sonicReadShortFromStream(stream, samples, numSamples); |
| 975 | sonicDestroyStream(stream); |
| 976 | return numSamples; |
| 977 | } |