blob: a1043dd96852c1784b8211b8a1f7c9abf82e24f9 [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
29#define BUFFER_SIZE 8192
30
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);
41 float inBuffer[BUFFER_SIZE], outBuffer[BUFFER_SIZE];
42 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 }
53 sonicWriteToStream(stream, inBuffer, numSamples);
Bill Cox882fb1d2010-11-02 16:27:20 -040054 do {
55 numSamples = sonicReadFromStream(stream, outBuffer, BUFFER_SIZE);
56 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{
66 fprintf(stderr, "Usage: sonic speed infile outfile");
67}
68
69int main(
70 int argc,
71 char **argv)
72{
73 char *inFileName, *outFileName;
74 double speed;
75
76 if(argc != 4) {
77 usage();
78 }
79 speed = atof(argv[1]);
80 inFileName = argv[2];
81 outFileName = argv[3];
82 runSonic(inFileName, outFileName, speed);
83 return 0;
84}