blob: b48bd9c16a4133cc53a635f306f4cca39c00a57d [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(
33 char *inFileName,
34 char *outFileName,
35 double speed)
36{
37 int sampleRate;
38 waveFile inFile = openInputWaveFile(inFileName, &sampleRate);
39 waveFile outFile = openOutputWaveFile(outFileName, sampleRate);
40 sonicStream stream = sonicCreateStream(speed, sampleRate);
Bill Cox0c4c0602010-11-08 11:46:30 -050041 short inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE];
Bill Coxca02d872010-11-02 15:10:52 -040042 int numSamples;
43
44 while(1) {
45 numSamples = readFromWaveFile(inFile, inBuffer, BUFFER_SIZE);
46 if(numSamples == 0) {
47 sonicFlushStream(stream);
48 sonicDestroyStream(stream);
49 closeWaveFile(inFile);
50 closeWaveFile(outFile);
51 return;
52 }
Bill Cox0c4c0602010-11-08 11:46:30 -050053 sonicWriteShortToStream(stream, inBuffer, numSamples);
Bill Cox882fb1d2010-11-02 16:27:20 -040054 do {
Bill Cox0c4c0602010-11-08 11:46:30 -050055 numSamples = sonicReadShortFromStream(stream, outBuffer, BUFFER_SIZE);
Bill Cox882fb1d2010-11-02 16:27:20 -040056 if(numSamples > 0) {
57 writeToWaveFile(outFile, outBuffer, numSamples);
58 }
59 } while(numSamples > 0);
Bill Coxca02d872010-11-02 15:10:52 -040060 }
61}
62
63/* Print the usage. */
64static void usage(void)
65{
Bill Cox3a7abf92010-11-06 15:18:49 -040066 fprintf(stderr, "Usage: sonic speed infile outfile\n");
67 exit(1);
Bill Coxca02d872010-11-02 15:10:52 -040068}
69
70int main(
71 int argc,
72 char **argv)
73{
74 char *inFileName, *outFileName;
75 double speed;
76
77 if(argc != 4) {
78 usage();
79 }
80 speed = atof(argv[1]);
81 inFileName = argv[2];
82 outFileName = argv[3];
83 runSonic(inFileName, outFileName, speed);
84 return 0;
85}