gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
WSOLA.C
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #include "WSOLA.H"
18 #include "OptionParser.H"
19 
20 #ifndef _MSC_VER
21 #include "Sox.H"
22 #else
23 // Note : Microsoft doesn't understand the different between upper and lower case in file names.
24 // on microsoft, you have to manually rename Sox.H to SoxWindows.H
25 #include "SoxWindows.H"
26 #endif
27 
28 #include <iostream>
29 
30 template<typename Derived>
31 int readAudio(Sox<FP_TYPE> &sox, DenseBase<Derived> const &audioData, int sampleCount){
32  Matrix<FP_TYPE, Dynamic, Dynamic> output;
33  int ret=sox.read(output, sampleCount);
34  output.transposeInPlace();
35  const_cast< DenseBase<Derived>& >(audioData)=output;
36 // cout<<output<<endl;
37  return output.cols();
38 }
39 
40 void printUsage(const char *str){
41  cerr<<"Usage: "<<str<<" -h or --help"<<endl;
42  cerr<<"Usage: "<<str<<" fileName.wav rate"<<endl;
43  cerr<<"\t the fileName can be any readable audio file format."<<endl;
44  cerr<<"\t the rate can be any number within a reasonable range where 0 < rate < 5 or some reasonable speed."<<endl;
45  cerr<<"\n Outputs to the file fileName.wav.rate.wav"<<endl;
46  cerr<<"\n Author : Matt Flax <flatmax@>"<<endl;
47  exit(0);
48 }
49 
50 int main(int argc, char *argv[]){
51  if (argc<3)
52  printUsage(argv[0]);
53 
54  OptionParser op;
55  int i=0;
56  string help;
57  if (op.getArg<string>("h", argc, argv, help, i=0)!=0)
58  printUsage(argv[0]);
59  if (op.getArg<string>("help", argc, argv, help, i=0)!=0)
60  printUsage(argv[0]);
61 
62  FP_TYPE timeScale;
63  op.convertArg<FP_TYPE>(argv[argc-1], timeScale);
64  cout<<"using timescale = "<<timeScale<<endl;
65 
66  string fileName(argv[argc-2]);
67  cout<<"input file = "<<fileName<<endl;
68  Sox<FP_TYPE> sox;
69  int ret;
70  if ((ret=sox.openRead(fileName))<0 && ret!=SOX_READ_MAXSCALE_ERROR)
71  return WSOLADebug().evaluateError(ret, argv[argc-2]);
72  sox.setMaxVal(1.0);
73 
74  int chCnt=sox.getChCntIn(); // the channel count
75 
76  string fileNameOut;
77  fileNameOut=fileName+'.'+argv[argc-1]+".wav";
78  cout<<"output file = "<<fileNameOut<<endl;
79  if ((ret=sox.openWrite(fileNameOut, sox.getFSIn(), chCnt, 1.))<0)
80  return WSOLADebug().evaluateError(ret, fileNameOut);
81 
82  WSOLA wsola(chCnt);
83  wsola.setFS(sox.getFSIn());
84  Matrix<FP_TYPE, Dynamic, Dynamic> audioData;
85  int N=wsola.getSamplesRequired();
86  ret=readAudio(sox, audioData, N);
87  if (ret!=N)
88  cerr<<"couldn't read audio, wanted "<<N<<" got "<<ret<<endl;
89 
90  ret=NO_ERROR;
91  while (ret==NO_ERROR){
92  // process the audio data
93  N=wsola.process(timeScale, audioData);
94 
95  // write to file
96  ret=sox.writeTransposed(wsola.output.block(0,0,chCnt,wsola.getOutputSize()));
97  if (ret!=wsola.output.cols())
98  cerr<<"couldn't write audio, tried to write "<<N<<" wrote "<<ret<<endl;
99 
100  // read more audio data
101  ret=readAudio(sox, audioData, N);
102  if (ret!=N)
103  cerr<<"couldn't read audio, wanted "<<N<<" got "<<ret<<endl;
104  else
105  ret=NO_ERROR;
106  }
107 
108  sox.closeWrite();
109  sox.closeRead();
110 
111  return 0;
112 }
113 
void printUsage(const char *str)
Definition: WSOLA.C:40
#define SOX_READ_MAXSCALE_ERROR
Sox couldn&#39;t open the filename.max to read the rescale value for the audio file.
Definition: Sox.H:36
float FP_TYPE
Definition: audioMasker.C:25
virtual int openWrite(const string &fileName, double fs, int channels, double maxVal)
Definition: Sox.H:174
int N
int getArg(string key, int argc, char *argv[], TYPE &ret, int i)
Definition: OptionParser.H:43
int openRead(string fileName)
Definition: Sox.C:70
virtual int evaluateError(int errorNum)
Definition: Debug.H:132
int closeWrite(void)
Definition: Sox.C:232
#define NO_ERROR
There is no error.
Definition: Debug.H:33
int writeTransposed(const Eigen::DenseBase< Derived > &audioData)
Definition: Sox.H:264
void convertArg(const char *arg, TYPE &ret)
Definition: OptionParser.H:58
double getFSIn(void)
Definition: Sox.H:315
int getChCntIn(void)
Definition: Sox.H:333
int readAudio(Sox< FP_TYPE > &sox, DenseBase< Derived > const &audioData, int sampleCount)
Definition: WSOLA.C:31
int getSamplesRequired(void)
Definition: WSOLA.H:214
void setMaxVal(double newMax)
Definition: Sox.H:300
Array< FP_TYPE, Dynamic, Dynamic > output
The output vector, each row is a channel.
Definition: WSOLA.H:141
void setFS(float fsIn)
Definition: WSOLA.C:162
int closeRead(void)
Definition: Sox.C:226
int main(int argc, char *argv[])
Definition: WSOLA.C:50
int process(FP_TYPE timeScale, const DenseBase< Derived > &input)
Definition: WSOLA.H:169
Definition: WSOLA.H:82
int getOutputSize(void)
Definition: WSOLA.H:220
int read(Eigen::DenseBase< Derived > &audioData, int count=0)
Definition: Sox.H:135
gtkIOStream: /tmp/gtkiostream/applications/WSOLA.C Source File
GTK+ IOStream  Beta