blob: 03525b991e298c0ab91451e605b70b08b020a803 [file] [log] [blame]
Bill Coxca02d872010-11-02 15:10:52 -04001/* Sonic library
2 Copyright 2010
3 Bill Cox
4 This file is part of the Sonic Library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
19
20/*
21Read 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 Cox59e65122010-11-03 10:06:29 -040029#define BUFFER_SIZE 1024
Bill Coxca02d872010-11-02 15:10:52 -040030
31/* Run sonic. */
32static void runSonic(
Bill Cox0c4cade2010-11-09 05:54:54 -050033 waveFile inFile,
34 waveFile outFile,
35 double speed,
36 int sampleRate)
Bill Coxca02d872010-11-02 15:10:52 -040037{
Bill Coxca02d872010-11-02 15:10:52 -040038 sonicStream stream = sonicCreateStream(speed, sampleRate);
Bill Cox0c4c0602010-11-08 11:46:30 -050039 short inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE];
Bill Cox0c4cade2010-11-09 05:54:54 -050040 int samplesRead, samplesWritten;
Bill Coxca02d872010-11-02 15:10:52 -040041
Bill Cox0c4cade2010-11-09 05:54:54 -050042 do {
43 samplesRead = readFromWaveFile(inFile, inBuffer, BUFFER_SIZE);
44 if(samplesRead == 0) {
Bill Coxca02d872010-11-02 15:10:52 -040045 sonicFlushStream(stream);
Bill Cox0c4cade2010-11-09 05:54:54 -050046 } else {
47 sonicWriteShortToStream(stream, inBuffer, samplesRead);
Bill Coxca02d872010-11-02 15:10:52 -040048 }
Bill Cox882fb1d2010-11-02 16:27:20 -040049 do {
Bill Cox0c4cade2010-11-09 05:54:54 -050050 samplesWritten = sonicReadShortFromStream(stream, outBuffer, BUFFER_SIZE);
51 if(samplesWritten > 0) {
52 writeToWaveFile(outFile, outBuffer, samplesWritten);
Bill Cox882fb1d2010-11-02 16:27:20 -040053 }
Bill Cox0c4cade2010-11-09 05:54:54 -050054 } while(samplesWritten > 0);
55 } while(samplesRead > 0);
56 sonicDestroyStream(stream);
Bill Coxca02d872010-11-02 15:10:52 -040057}
58
59/* Print the usage. */
60static void usage(void)
61{
Bill Cox3a7abf92010-11-06 15:18:49 -040062 fprintf(stderr, "Usage: sonic speed infile outfile\n");
63 exit(1);
Bill Coxca02d872010-11-02 15:10:52 -040064}
65
66int main(
67 int argc,
68 char **argv)
69{
Bill Cox0c4cade2010-11-09 05:54:54 -050070 waveFile inFile, outFile;
Bill Coxca02d872010-11-02 15:10:52 -040071 char *inFileName, *outFileName;
72 double speed;
Bill Cox0c4cade2010-11-09 05:54:54 -050073 int sampleRate;
Bill Coxca02d872010-11-02 15:10:52 -040074
75 if(argc != 4) {
76 usage();
77 }
78 speed = atof(argv[1]);
79 inFileName = argv[2];
80 outFileName = argv[3];
Bill Cox0c4cade2010-11-09 05:54:54 -050081 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 Coxca02d872010-11-02 15:10:52 -040093 return 0;
94}