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