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 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program 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 |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 19 | |
| 20 | /* |
| 21 | Read wave files and speed them up or slow them down. |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include "sonic.h" |
| 27 | #include "wave.h" |
| 28 | |
| 29 | #define BUFFER_SIZE 8192 |
| 30 | |
| 31 | /* Run sonic. */ |
| 32 | static void runSonic( |
| 33 | char *inFileName, |
| 34 | char *outFileName, |
| 35 | double speed) |
| 36 | { |
| 37 | int sampleRate; |
| 38 | waveFile inFile = openInputWaveFile(inFileName, &sampleRate); |
| 39 | waveFile outFile = openOutputWaveFile(outFileName, sampleRate); |
| 40 | sonicStream stream = sonicCreateStream(speed, sampleRate); |
| 41 | float inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE]; |
| 42 | int numSamples; |
| 43 | |
| 44 | while(1) { |
| 45 | numSamples = readFromWaveFile(inFile, inBuffer, BUFFER_SIZE); |
| 46 | if(numSamples == 0) { |
| 47 | sonicFlushStream(stream); |
| 48 | sonicDestroyStream(stream); |
| 49 | closeWaveFile(inFile); |
| 50 | closeWaveFile(outFile); |
| 51 | return; |
| 52 | } |
| 53 | sonicWriteToStream(stream, inBuffer, numSamples); |
| 54 | numSamples = sonicReadFromStream(stream, outBuffer, BUFFER_SIZE); |
| 55 | if(numSamples > 0) { |
| 56 | writeToWaveFile(outFile, outBuffer, numSamples); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* Print the usage. */ |
| 62 | static void usage(void) |
| 63 | { |
| 64 | fprintf(stderr, "Usage: sonic speed infile outfile"); |
| 65 | } |
| 66 | |
| 67 | int main( |
| 68 | int argc, |
| 69 | char **argv) |
| 70 | { |
| 71 | char *inFileName, *outFileName; |
| 72 | double speed; |
| 73 | |
| 74 | if(argc != 4) { |
| 75 | usage(); |
| 76 | } |
| 77 | speed = atof(argv[1]); |
| 78 | inFileName = argv[2]; |
| 79 | outFileName = argv[3]; |
| 80 | runSonic(inFileName, outFileName, speed); |
| 81 | return 0; |
| 82 | } |