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