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 | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | Read wave files and speed them up or slow them down. |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 27 | #include <string.h> |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 28 | #include "sonic.h" |
| 29 | #include "wave.h" |
| 30 | |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 31 | #define BUFFER_SIZE 2048 |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 32 | |
| 33 | /* Run sonic. */ |
| 34 | static void runSonic( |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 35 | waveFile inFile, |
| 36 | waveFile outFile, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 37 | float speed, |
| 38 | float pitch, |
| 39 | float volume, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 40 | int sampleRate, |
| 41 | int numChannels) |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 42 | { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 43 | sonicStream stream = sonicCreateStream(sampleRate, numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 44 | short inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE]; |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 45 | int samplesRead, samplesWritten; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 46 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 47 | sonicSetSpeed(stream, speed); |
| 48 | sonicSetPitch(stream, pitch); |
| 49 | sonicSetVolume(stream, volume); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 50 | do { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 51 | samplesRead = readFromWaveFile(inFile, inBuffer, BUFFER_SIZE/numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 52 | if(samplesRead == 0) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 53 | sonicFlushStream(stream); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 54 | } else { |
| 55 | sonicWriteShortToStream(stream, inBuffer, samplesRead); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 56 | } |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 57 | do { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 58 | samplesWritten = sonicReadShortFromStream(stream, outBuffer, |
| 59 | BUFFER_SIZE/numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 60 | if(samplesWritten > 0) { |
| 61 | writeToWaveFile(outFile, outBuffer, samplesWritten); |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 62 | } |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 63 | } while(samplesWritten > 0); |
| 64 | } while(samplesRead > 0); |
| 65 | sonicDestroyStream(stream); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* Print the usage. */ |
| 69 | static void usage(void) |
| 70 | { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 71 | fprintf(stderr, "Usage: sonic [-s speed] [-v volume] infile outfile\n" |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 72 | " -p -- Set pitch scaling factor. 1.3 means 30%% higher.\n" |
| 73 | " -s -- Set speed up factor. 1.0 means no change, 2.0 means 2X faster.\n" |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 74 | " -v -- Scale volume as percentage of maximum allowed. 100 uses full range.\n"); |
Bill Cox | 3a7abf9 | 2010-11-06 15:18:49 -0400 | [diff] [blame] | 75 | exit(1); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int main( |
| 79 | int argc, |
| 80 | char **argv) |
| 81 | { |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 82 | waveFile inFile, outFile; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 83 | char *inFileName, *outFileName; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 84 | float speed = 1.0; |
| 85 | float pitch = 1.0; |
| 86 | float volume = 1.0; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 87 | int sampleRate, numChannels; |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 88 | int xArg = 1; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 89 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 90 | if(argc < 2 || *(argv[xArg]) != '-') { |
| 91 | fprintf(stderr, "You must provide at least one option to change speed," |
| 92 | "pitch, or volume.\n"); |
| 93 | usage(); |
| 94 | return 1; |
| 95 | } |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 96 | while(xArg < argc && *(argv[xArg]) == '-') { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 97 | if(!strcmp(argv[xArg], "-p")) { |
| 98 | xArg++; |
| 99 | if(xArg < argc) { |
| 100 | pitch = atof(argv[xArg]); |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame^] | 101 | printf("Setting pitch to %0.2f%%\n", pitch*100.0f); |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 102 | } |
| 103 | } else if(!strcmp(argv[xArg], "-s")) { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 104 | xArg++; |
| 105 | if(xArg < argc) { |
| 106 | speed = atof(argv[xArg]); |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame^] | 107 | printf("Setting speed to %0.2f%%\n", speed*100.0f); |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 108 | } |
| 109 | } else if(!strcmp(argv[xArg], "-v")) { |
| 110 | xArg++; |
| 111 | if(xArg < argc) { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 112 | volume = atof(argv[xArg]); |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame^] | 113 | printf("Setting volume to %0.2f%%\n", volume*100.0f); |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | xArg++; |
| 117 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 118 | if(argc - xArg != 2) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 119 | usage(); |
| 120 | } |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 121 | inFileName = argv[xArg]; |
| 122 | outFileName = argv[xArg + 1]; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 123 | inFile = openInputWaveFile(inFileName, &sampleRate, &numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 124 | if(inFile == NULL) { |
| 125 | return 1; |
| 126 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 127 | outFile = openOutputWaveFile(outFileName, sampleRate, numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 128 | if(outFile == NULL) { |
| 129 | closeWaveFile(inFile); |
| 130 | return 1; |
| 131 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 132 | runSonic(inFile, outFile, speed, pitch, volume, sampleRate, numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 133 | closeWaveFile(inFile); |
| 134 | closeWaveFile(outFile); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 135 | return 0; |
| 136 | } |