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