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