Bill Cox | 9d14021 | 2011-01-09 11:45:41 -0500 | [diff] [blame] | 1 | /* This file, main.c, was written by Bill Cox in 2010, and placed into the public domain. |
| 2 | Feel free to copy and paste code from this file into your application. Note, |
| 3 | however, that the other source files, sonic.c and sonic.h, are LGPL. |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 4 | |
Bill Cox | 9d14021 | 2011-01-09 11:45:41 -0500 | [diff] [blame] | 5 | This file is meant as a simple example for how to use libsonic. It is also a |
| 6 | useful utility on it's own, which can speed up or slow down wav files, change |
| 7 | pitch, and scale volume. */ |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 11 | #include <string.h> |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 12 | #include "sonic.h" |
| 13 | #include "wave.h" |
| 14 | |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 15 | #define BUFFER_SIZE 2048 |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 16 | |
| 17 | /* Run sonic. */ |
| 18 | static void runSonic( |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 19 | waveFile inFile, |
| 20 | waveFile outFile, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 21 | float speed, |
| 22 | float pitch, |
| 23 | float volume, |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 24 | int quality, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 25 | int sampleRate, |
| 26 | int numChannels) |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 27 | { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 28 | sonicStream stream = sonicCreateStream(sampleRate, numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 29 | short inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE]; |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 30 | int samplesRead, samplesWritten; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 31 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 32 | sonicSetSpeed(stream, speed); |
| 33 | sonicSetPitch(stream, pitch); |
| 34 | sonicSetVolume(stream, volume); |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 35 | sonicSetQuality(stream, quality); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 36 | do { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 37 | samplesRead = readFromWaveFile(inFile, inBuffer, BUFFER_SIZE/numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 38 | if(samplesRead == 0) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 39 | sonicFlushStream(stream); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 40 | } else { |
| 41 | sonicWriteShortToStream(stream, inBuffer, samplesRead); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 42 | } |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 43 | do { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 44 | samplesWritten = sonicReadShortFromStream(stream, outBuffer, |
| 45 | BUFFER_SIZE/numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 46 | if(samplesWritten > 0) { |
| 47 | writeToWaveFile(outFile, outBuffer, samplesWritten); |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 48 | } |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 49 | } while(samplesWritten > 0); |
| 50 | } while(samplesRead > 0); |
| 51 | sonicDestroyStream(stream); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* Print the usage. */ |
| 55 | static void usage(void) |
| 56 | { |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 57 | fprintf(stderr, "Usage: sonic [OPTION]... infile outfile\n" |
| 58 | " -p pitch -- Set pitch scaling factor. 1.3 means 30%% higher.\n" |
| 59 | " -q -- Disable speed-up heuristics. May increase quality.\n" |
| 60 | " -s speed -- Set speed up factor. 2.0 means 2X faster.\n" |
| 61 | " -v volume -- Scale volume by a constant factor.\n"); |
Bill Cox | 3a7abf9 | 2010-11-06 15:18:49 -0400 | [diff] [blame] | 62 | exit(1); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int main( |
| 66 | int argc, |
| 67 | char **argv) |
| 68 | { |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 69 | waveFile inFile, outFile; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 70 | char *inFileName, *outFileName; |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 71 | float speed = 1.0; |
| 72 | float pitch = 1.0; |
| 73 | float volume = 1.0; |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 74 | int quality = 0; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 75 | int sampleRate, numChannels; |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 76 | int xArg = 1; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 77 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 78 | if(argc < 2 || *(argv[xArg]) != '-') { |
| 79 | fprintf(stderr, "You must provide at least one option to change speed," |
| 80 | "pitch, or volume.\n"); |
| 81 | usage(); |
| 82 | return 1; |
| 83 | } |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 84 | while(xArg < argc && *(argv[xArg]) == '-') { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 85 | if(!strcmp(argv[xArg], "-p")) { |
| 86 | xArg++; |
| 87 | if(xArg < argc) { |
| 88 | pitch = atof(argv[xArg]); |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 89 | printf("Setting pitch to %0.2f%%\n", pitch*100.0f); |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 90 | } |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 91 | } else if(!strcmp(argv[xArg], "-q")) { |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 92 | quality = 1; |
| 93 | printf("Disabling speed-up heuristics\n"); |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 94 | } else if(!strcmp(argv[xArg], "-s")) { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 95 | xArg++; |
| 96 | if(xArg < argc) { |
| 97 | speed = atof(argv[xArg]); |
Bill Cox | d76d222 | 2010-11-24 11:42:29 -0500 | [diff] [blame] | 98 | printf("Setting speed to %0.2f%%\n", speed*100.0f); |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 99 | } |
| 100 | } else if(!strcmp(argv[xArg], "-v")) { |
| 101 | xArg++; |
| 102 | if(xArg < argc) { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 103 | volume = atof(argv[xArg]); |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 104 | printf("Setting volume to %0.2f\n", volume); |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | xArg++; |
| 108 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 109 | if(argc - xArg != 2) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 110 | usage(); |
| 111 | } |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 112 | inFileName = argv[xArg]; |
| 113 | outFileName = argv[xArg + 1]; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 114 | inFile = openInputWaveFile(inFileName, &sampleRate, &numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 115 | if(inFile == NULL) { |
| 116 | return 1; |
| 117 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 118 | outFile = openOutputWaveFile(outFileName, sampleRate, numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 119 | if(outFile == NULL) { |
| 120 | closeWaveFile(inFile); |
| 121 | return 1; |
| 122 | } |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 123 | runSonic(inFile, outFile, speed, pitch, volume, quality, sampleRate, numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 124 | closeWaveFile(inFile); |
| 125 | closeWaveFile(outFile); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 126 | return 0; |
| 127 | } |