blob: c77db47f9266ead13a3aa489d138aabc5a77035e [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);
54 numSamples = sonicReadFromStream(stream, outBuffer, BUFFER_SIZE);
55 if(numSamples > 0) {
56 writeToWaveFile(outFile, outBuffer, numSamples);
57 }
58 }
59}
60
61/* Print the usage. */
62static void usage(void)
63{
64 fprintf(stderr, "Usage: sonic speed infile outfile");
65}
66
67int main(
68 int argc,
69 char **argv)
70{
71 char *inFileName, *outFileName;
72 double speed;
73
74 if(argc != 4) {
75 usage();
76 }
77 speed = atof(argv[1]);
78 inFileName = argv[2];
79 outFileName = argv[3];
80 runSonic(inFileName, outFileName, speed);
81 return 0;
82}