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