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 | |
Bill Cox | 59e6512 | 2010-11-03 10:06:29 -0400 | [diff] [blame] | 29 | #define BUFFER_SIZE 1024 |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 30 | |
| 31 | /* Run sonic. */ |
| 32 | static void runSonic( |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 33 | waveFile inFile, |
| 34 | waveFile outFile, |
| 35 | double speed, |
| 36 | int sampleRate) |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 37 | { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 38 | sonicStream stream = sonicCreateStream(speed, sampleRate); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 39 | short inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE]; |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 40 | int samplesRead, samplesWritten; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 41 | |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 42 | do { |
| 43 | samplesRead = readFromWaveFile(inFile, inBuffer, BUFFER_SIZE); |
| 44 | if(samplesRead == 0) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 45 | sonicFlushStream(stream); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 46 | } else { |
| 47 | sonicWriteShortToStream(stream, inBuffer, samplesRead); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 48 | } |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 49 | do { |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 50 | samplesWritten = sonicReadShortFromStream(stream, outBuffer, BUFFER_SIZE); |
| 51 | if(samplesWritten > 0) { |
| 52 | writeToWaveFile(outFile, outBuffer, samplesWritten); |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 53 | } |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 54 | } while(samplesWritten > 0); |
| 55 | } while(samplesRead > 0); |
| 56 | sonicDestroyStream(stream); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* Print the usage. */ |
| 60 | static void usage(void) |
| 61 | { |
Bill Cox | 3a7abf9 | 2010-11-06 15:18:49 -0400 | [diff] [blame] | 62 | fprintf(stderr, "Usage: sonic speed infile outfile\n"); |
| 63 | exit(1); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int main( |
| 67 | int argc, |
| 68 | char **argv) |
| 69 | { |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 70 | waveFile inFile, outFile; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 71 | char *inFileName, *outFileName; |
| 72 | double speed; |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 73 | int sampleRate; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 74 | |
| 75 | if(argc != 4) { |
| 76 | usage(); |
| 77 | } |
| 78 | speed = atof(argv[1]); |
| 79 | inFileName = argv[2]; |
| 80 | outFileName = argv[3]; |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame^] | 81 | inFile = openInputWaveFile(inFileName, &sampleRate); |
| 82 | if(inFile == NULL) { |
| 83 | return 1; |
| 84 | } |
| 85 | outFile = openOutputWaveFile(outFileName, sampleRate); |
| 86 | if(outFile == NULL) { |
| 87 | closeWaveFile(inFile); |
| 88 | return 1; |
| 89 | } |
| 90 | runSonic(inFile, outFile, speed, sampleRate); |
| 91 | closeWaveFile(inFile); |
| 92 | closeWaveFile(outFile); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 93 | return 0; |
| 94 | } |