31 int printUsage(
string name,
int N,
string chip,
int chCnt,
float T,
float fs,
int periodCount) {
32 cout<<name<<
" : An application to stream input from IIO devices to file."<<endl;
34 cout<<
"\t "<<name<<
" [options] outFileName"<<endl;
35 cout<<
"\t -p : The number of samples to read each time from the IIO devices : (-p "<<N<<
")"<<endl;
36 cout<<
"\t -C : The name of the chip to look for in the available IIO devices : (-C "<<chip<<
")"<<endl;
37 cout<<
"\t -i : The number of channels to open, if the available number is less, then it is reduced to the available : (-i "<<chCnt<<
")"<<endl;
38 cout<<
"\t -t : The duration to sample for : (-t "<<T<<
")"<<endl;
39 cout<<
"\t -f : The sample rate to use : (-f "<<fixed<<setprecision(2)<<fs<<
")"<<endl;
40 cout<<
"\t -n : The number of periods : (-n "<<periodCount<<
")"<<endl;
41 cout<<resetiosflags(ios::showbase);
44 cout<<
"The known output file extensions (output file formats) are the following :"<<endl;
45 for (
int i=0; i<formats.size(); i++)
46 cout<<formats[i]<<
' ';
51 int main(
int argc,
char *argv[]) {
52 struct sched_param param;
53 param.sched_priority = 96;
54 if (sched_setscheduler(0, SCHED_FIFO, & param) == -1) {
55 perror(
"sched_setscheduler");
61 string chip(
"AD7476A");
71 if (op.
getArg<
string>(
"h", argc, argv, help, i=0)!=0)
72 return printUsage(argv[0], N, chip, chCnt, T, fs, periodCount);
73 if (op.
getArg<
string>(
"help", argc, argv, help, i=0)!=0)
74 return printUsage(argv[0], N, chip, chCnt, T, fs, periodCount);
76 return printUsage(argv[0], N, chip, chCnt, T, fs, periodCount);
78 if (op.
getArg<
int>(
"p", argc, argv, N, i=0)!=0)
81 if (op.
getArg<
string>(
"C", argc, argv, chip, i=0)!=0)
84 if (op.
getArg<
int>(
"i", argc, argv, chCnt, i=0)!=0)
87 if (op.
getArg<
float>(
"t", argc, argv, T, i=0)!=0)
90 if (op.
getArg<
float>(
"f", argc, argv, fs, i=0)!=0)
93 if (op.
getArg<
int>(
"n", argc, argv, periodCount, i=0)!=0)
108 cout<<
"Number of samples p="<<N<<
"\nNumber of channels available i="<<chCnt<<
"\nChip name C="<<chip<<endl;
109 cout<<
"Reading n="<<periodCount<<
" period buffers of p samples each"<<endl;
110 cout<<
"Duration t="<<T<<
"\nSample rate f="<<fs<<endl;
113 cout<<
"Reading "<<M<<
" times "<<N<<
" samples, resulting in a processing time of "<<M*N/1e6<<
" or "<<M*N<<
" samples per channel."<<endl;
115 int colCnt=(int)ceil((
float)chCnt/(float)iio[0].getChCnt());
122 int expectedWriteCnt=N*chCnt;
129 Eigen::Array<unsigned short int, Eigen::Dynamic, Eigen::Dynamic> data;
133 if (data.cols()>colCnt)
134 data.resize(data.rows(), colCnt);
137 cout<<
"maxDelay = "<<maxDelay<<endl;
141 ret=sox.
openWrite(argv[argc-1], fs, colCnt, MAXSHORT);
145 struct timespec start, stop, lockStart, lockStop;
147 if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
148 cout<<
"clock start get time error"<<endl;
153 for (
int i=0; i<M; i++) {
156 if( clock_gettime( CLOCK_REALTIME, &lockStart) == -1 ) {
157 cout<<
"clock lockStart get time error"<<endl;
167 ret=iio.
read(N, data);
169 cout<<
"error with i="<<i<<
" out of M="<<M<<
" loops."<<endl;
174 int written=sox.
write(data);
175 if (written!=expectedWriteCnt) {
177 cout<<
"Attempted to write "<<N<<
" samples (per channel) to the audio file, however only "<<written<<
" samples were written. Exiting!"<<endl;
180 cout<<
"Output matrix size (rows, cols) = ("<<data.rows()<<
", "<<data.cols()<<
")"<<endl;
181 cout<<
"Error writing, exiting."<<endl;
186 if( clock_gettime( CLOCK_REALTIME, &lockStop) == -1 ) {
187 cout<<
"clock lockStop get time error"<<endl;
190 durations[i] = 1.e3*( lockStop.tv_sec - lockStart.tv_sec ) + (
double)( lockStop.tv_nsec - lockStart.tv_nsec )/1.e6;
191 if (durations[i]>maxDelay*1.e3)
192 cout<<
"Safe lock duration of "<<maxDelay<<
" exceeded with a lag of "<<durations[i]<<
" ms, possible sample drop.\n";
196 cout<<
"closing the devices"<<endl;
199 if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
200 cout<<
"clock stop get time error"<<endl;
207 ofstream outputd(
"/tmp/durations.txt");
208 for (
int i=0; i<M; i++)
209 outputd<<durations[i]<<
'\n';
212 double duration = 1.e3*( stop.tv_sec - start.tv_sec ) + (
double)( stop.tv_nsec - start.tv_nsec )/1.e6;
213 cout<<
"Duration "<<duration<<
" ms\n";
215 double expected = (float)(N*M)/1.e6*1.e3;
216 cout<<
"Expected "<<expected<<
" ms\n";
218 cout<<
"Difference "<<duration-expected<<
" ms"<<endl;
virtual int openWrite(const string &fileName, double fs, int channels, double maxVal)
int getArg(string key, int argc, char *argv[], TYPE &ret, int i)
virtual int evaluateError(int errorNum)
int printUsage(string name, int N, string chip, int chCnt, float T, float fs, int periodCount)
vector< string > availableFormats(void)
int findDevicesByChipName(std::string chipName)
#define NO_ERROR
There is no error.
int read(uint N, const Eigen::Array< TYPE, Eigen::Dynamic, Eigen::Dynamic > &array)
int main(int argc, char *argv[])
int getReadArray(uint N, Eigen::Array< TYPE, Eigen::Dynamic, Eigen::Dynamic > &array)
double getMaxDelay(float fs)
virtual int write(const vector< vector< FP_TYPE_ > > &audioData)