gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
MixerTester.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 
18 /*
19 Author: Matt Flax <flatmax@flatmax.org>
20 Date: 2013.08.05
21 */
22 
23 #include "JackClient.H"
24 #include <iostream>
25 #include <fstream>
26 using namespace std;
27 #include "OptionParser.H"
28 //#include "Sox.H"
29 #include "DSP/MixerTestAudio.H"
30 #include "Octave.H"
31 #include <unistd.h>
32 
33 int printUsage(string name) {
34  cout<<"\nUseage: \n"<<endl;
35  cout<<name<<" [-t duration] [-o num] [-i num] [-O num] [-g num] "<<endl;
36  cout<<name<<" -t num : duration in seconds"<<endl;
37  cout<<name<<" -o num : number of output channels to open at the same time on the audio device"<<endl;
38  cout<<name<<" -O num : total number of test output channels to record"<<endl;
39  cout<<name<<" -i num : number of input channels to open at the same time on the audio device"<<endl;
40  cout<<name<<" -g num : the output gain"<<endl;
41  return 0;
42 }
43 
44 int main(int argc, char *argv[]) {
45  OptionParser op;
46 
47  int i=0;
48  string help;
49  if (op.getArg<string>("h", argc, argv, help, i=0)!=0)
50  return printUsage(argv[0]);
51 
52  MixerTestAudio mixerAudio; // init the crossover tester
53 
54  float t=10.0;
55  op.getArg<float>("t", argc, argv, t, i=0);
56  cout<<"running for "<<t<<" seconds"<<endl;
57  mixerAudio.setDuration(t);
58 
59  float g=1.0;
60  op.getArg<float>("g", argc, argv, g, i=0);
61  cout<<"using a gain="<<g<<endl;
62  mixerAudio.setGain(g);
63 
64  int inChCnt=2;
65  op.getArg<int>("i", argc, argv, inChCnt, i=0);
66  cout<<"recording "<<inChCnt<<" channels of physical input audio to record each time"<<endl;
67 
68  int outChCnt=1;
69  op.getArg<int>("o", argc, argv, outChCnt, i=0);
70  cout<<"playing "<<outChCnt<<" channels of output random noise"<<endl;
71 
72  int outTestChCnt=2;
73  op.getArg<int>("O", argc, argv, outTestChCnt, i=0);
74  cout<<"recording "<<outTestChCnt<<" channels of output audio (mixer inputs) total, maximum of "<<outChCnt<<" channels each run."<<endl;
75 
76  mixerAudio.setChannels(outChCnt, inChCnt, outTestChCnt);
77 
78  cout<<"Jack : sample rate set to : "<<mixerAudio.getSampleRate()<<" Hz"<<endl;
79  cout<<"Jack : block size set to : "<<mixerAudio.getBlockSize()<<" samples"<<endl;
80 
81  ofstream mFileOut("/tmp/MixerTester.m");
82  mFileOut<<"function MixerTester(data)"<<endl;
83  mFileOut<<"sampleRate="<<mixerAudio.getSampleRate()<<';'<<endl;
84  mFileOut<<"[N,M]=size(data); figure(1); f=linspace(0,sampleRate,N);"<<endl;
85  mFileOut<<"for i=2:M"<<endl;
86  mFileOut<<" subplot(M-1,1,i-1); loglog(f, abs(fft(data(:,i))));"<<endl;
87  mFileOut<<"end"<<endl;
88  mFileOut<<"xlabel('f (Hz)')"<<endl;
89  mFileOut<<"figure(2); t=(0:N-1)/sampleRate;"<<endl;
90  mFileOut<<"for i=2:M"<<endl;
91  mFileOut<<" subplot(M-1,1,i-1); plot(t,data(:,i));"<<endl;
92  mFileOut<<" set(gca,'ylim',[-1.5 1.5]);"<<endl;
93  mFileOut<<"end"<<endl;
94  mFileOut<<"xlabel('t (s)')"<<endl;
95  mFileOut<<"disp('paused'); pause \nend"<<endl;
96  mFileOut.close();
97 
98  vector<string> args(3); args[0]=string("--silent"); args[1]=string("--path"); args[2]="/tmp";
99  Octave octave(args);
100 
101  while (1) {
102  mixerAudio.reset();
103  while (mixerAudio.getNumberOfRecordedChannels()<outTestChCnt){
104  // cout<<"\r"<<mixerAudio.getNumberOfRecordedChannels()<<" channels already recorded, recroding the "<<mixerAudio.getNumberOfRecordedChannels()+1<<" channel, device under test input channel."<<endl;
105  cout<<mixerAudio.getNumberOfRecordedChannels()<<" channels already recorded, recroding the "<<mixerAudio.getNumberOfRecordedChannels()+1<<" channel, device under test input channel."<<endl;
106 
107  // string userInput;
108  // getline(cin, userInput);
109  int ret=mixerAudio.recordNextChannelSet();
110  if (ret!=NO_ERROR)
111  exit(ret);
112  sleep(t+1); // sleep for duration seconds
113  while (mixerAudio.isRecording()) // whilst still recording, sleep for half a second, allowing time to finish.
114  sleep(1);
115  }
116 
117  // cout<<"\rDone, now going to write to the file "<<argv[argc-1]<<endl;
118 
119  // // open sox
120  // Sox sox;
121  // int ret=sox.openWrite(argv[argc-1], mixerAudio.getSampleRate(), inTestChCnt+outChCnt+1, 1.0);
122  // if (ret!=NO_ERROR)
123  // return SoxDebug().evaluateError(ret);
124  //
125  // int written=sox.write(mixerAudio.audio);
126  // if (written!=mixerAudio.audio.rows()*mixerAudio.audio.cols()) {
127  // cout<<SoxDebug().evaluateError(written)<<endl;
128  // cout<<"written "<<written<<endl;
129  // cout<<"Output matrix size (rows, cols) = ("<<mixerAudio.audio.rows()<<", "<<mixerAudio.audio.cols()<<")"<<endl;
130  // cout<<"Matrix is a total of "<<mixerAudio.audio.rows()*mixerAudio.audio.cols()<<" samples"<<endl;
131  // cout<<"Error writing, exiting."<<endl;
132  // }
133  //
134  // sox.closeWrite();
135 
136  cout<<"\r launching octave to view results "<<endl;
137 
138  vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > input(1), output;
139  input[0]=mixerAudio.audio;
140  octave.runM("MixerTester",input, output);
141  }
142  return 0;
143 }
144 
vector< vector< vector< TYPE > > > & runM(const char *commandName, const vector< vector< vector< TYPE > > > &in, vector< vector< vector< TYPE > > > &out)
Definition: Octave.C:104
int getArg(string key, int argc, char *argv[], TYPE &ret, int i)
Definition: OptionParser.H:43
Octave&#39;s octave_value_list class.
Definition: Octave.H:95
virtual int getBlockSize()
Definition: JackClient.H:140
virtual int getNumberOfRecordedChannels()
int setDuration(float d)
STL namespace.
#define NO_ERROR
There is no error.
Definition: Debug.H:33
int getSampleRate(void) const
Definition: JackBase.H:266
int setChannels(int outCnt, int inCnt, int testOutCnt)
int main(int argc, char *argv[])
Definition: MixerTester.C:44
virtual int reset()
void setGain(float g)
int recordNextChannelSet()
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic > audio
The first channel is the same data sent over each output channel, then the output channels...
int printUsage(string name)
Definition: MixerTester.C:33
gtkIOStream: /tmp/gtkiostream/applications/MixerTester.C Source File
GTK+ IOStream  Beta