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, |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 23 | float rate, |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 24 | float volume, |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 25 | int emulateChordPitch, |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 26 | int quality, |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 27 | int sampleRate, |
| 28 | int numChannels) |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 29 | { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 30 | sonicStream stream = sonicCreateStream(sampleRate, numChannels); |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 31 | short inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE]; |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 32 | int samplesRead, samplesWritten; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 33 | |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 34 | sonicSetSpeed(stream, speed); |
| 35 | sonicSetPitch(stream, pitch); |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 36 | sonicSetRate(stream, rate); |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 37 | sonicSetVolume(stream, volume); |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 38 | sonicSetChordPitch(stream, emulateChordPitch); |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 39 | sonicSetQuality(stream, quality); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 40 | do { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 41 | samplesRead = readFromWaveFile(inFile, inBuffer, BUFFER_SIZE/numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 42 | if(samplesRead == 0) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 43 | sonicFlushStream(stream); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 44 | } else { |
| 45 | sonicWriteShortToStream(stream, inBuffer, samplesRead); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 46 | } |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 47 | do { |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 48 | samplesWritten = sonicReadShortFromStream(stream, outBuffer, |
| 49 | BUFFER_SIZE/numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 50 | if(samplesWritten > 0) { |
| 51 | writeToWaveFile(outFile, outBuffer, samplesWritten); |
Bill Cox | 882fb1d | 2010-11-02 16:27:20 -0400 | [diff] [blame] | 52 | } |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 53 | } while(samplesWritten > 0); |
| 54 | } while(samplesRead > 0); |
| 55 | sonicDestroyStream(stream); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Print the usage. */ |
| 59 | static void usage(void) |
| 60 | { |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 61 | fprintf(stderr, "Usage: sonic [OPTION]... infile outfile\n" |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 62 | " -c -- Modify pitch by emulating vocal chords vibrating\n" |
| 63 | " faster or slower.\n" |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 64 | " -p pitch -- Set pitch scaling factor. 1.3 means 30%% higher.\n" |
| 65 | " -q -- Disable speed-up heuristics. May increase quality.\n" |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 66 | " -r rate -- Set playback rate. 2.0 means 2X faster, and 2X pitch.\n" |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 67 | " -s speed -- Set speed up factor. 2.0 means 2X faster.\n" |
| 68 | " -v volume -- Scale volume by a constant factor.\n"); |
Bill Cox | 3a7abf9 | 2010-11-06 15:18:49 -0400 | [diff] [blame] | 69 | exit(1); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int main( |
| 73 | int argc, |
| 74 | char **argv) |
| 75 | { |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 76 | waveFile inFile, outFile; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 77 | char *inFileName, *outFileName; |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 78 | float speed = 1.0f; |
| 79 | float pitch = 1.0f; |
| 80 | float rate = 1.0f; |
| 81 | float volume = 1.0f; |
| 82 | int emulateChordPitch = 0; |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 83 | int quality = 0; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 84 | int sampleRate, numChannels; |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 85 | int xArg = 1; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 86 | |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 87 | while(xArg < argc && *(argv[xArg]) == '-') { |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 88 | if(!strcmp(argv[xArg], "-c")) { |
| 89 | emulateChordPitch = 1; |
| 90 | printf("Scaling pitch linearly.\n"); |
| 91 | } else if(!strcmp(argv[xArg], "-p")) { |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 92 | xArg++; |
| 93 | if(xArg < argc) { |
| 94 | pitch = atof(argv[xArg]); |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 95 | printf("Setting pitch to %0.2fX\n", pitch); |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 96 | } |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 97 | } else if(!strcmp(argv[xArg], "-q")) { |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 98 | quality = 1; |
| 99 | printf("Disabling speed-up heuristics\n"); |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 100 | } else if(!strcmp(argv[xArg], "-r")) { |
| 101 | xArg++; |
| 102 | if(xArg < argc) { |
| 103 | rate = atof(argv[xArg]); |
| 104 | printf("Setting rate to %0.2fX\n", rate); |
| 105 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 106 | } else if(!strcmp(argv[xArg], "-s")) { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 107 | xArg++; |
| 108 | if(xArg < argc) { |
| 109 | speed = atof(argv[xArg]); |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 110 | printf("Setting speed to %0.2fX\n", speed); |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 111 | } |
| 112 | } else if(!strcmp(argv[xArg], "-v")) { |
| 113 | xArg++; |
| 114 | if(xArg < argc) { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 115 | volume = atof(argv[xArg]); |
Bill Cox | c978c39 | 2010-12-17 05:04:06 -0500 | [diff] [blame] | 116 | printf("Setting volume to %0.2f\n", volume); |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | xArg++; |
| 120 | } |
Bill Cox | d544fdb | 2010-11-23 14:13:46 -0500 | [diff] [blame] | 121 | if(argc - xArg != 2) { |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 122 | usage(); |
| 123 | } |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 124 | inFileName = argv[xArg]; |
| 125 | outFileName = argv[xArg + 1]; |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 126 | inFile = openInputWaveFile(inFileName, &sampleRate, &numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 127 | if(inFile == NULL) { |
| 128 | return 1; |
| 129 | } |
Bill Cox | 1a299bb | 2010-11-19 15:07:17 -0500 | [diff] [blame] | 130 | outFile = openOutputWaveFile(outFileName, sampleRate, numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 131 | if(outFile == NULL) { |
| 132 | closeWaveFile(inFile); |
| 133 | return 1; |
| 134 | } |
Bill Cox | 3276bb0 | 2011-01-11 07:39:26 -0500 | [diff] [blame] | 135 | runSonic(inFile, outFile, speed, pitch, rate, volume, emulateChordPitch, quality, |
| 136 | sampleRate, numChannels); |
Bill Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 137 | closeWaveFile(inFile); |
| 138 | closeWaveFile(outFile); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 139 | return 0; |
| 140 | } |