#include <iostream>
#define USE_INTERLEAVED
int main(
int argc,
char *argv[]) {
if (argc<2){
cout<<"Usage:\n"<<argv[0]<<" audioFileName"<<endl;
return -1;
}
const string deviceName="hw:0";
Capture capture(deviceName.c_str());
int chCnt=2;
int fs=44100;
float duration=2.1;
int res;
capture.resetParams();
snd_pcm_format_t format=SND_PCM_FORMAT_S16_LE;
if ((res=capture.setFormat(format))<0)
#ifndef USE_INTERLEAVED
cout<<"\n\nsetting non interleaved"<<endl;
if ((res=capture.setAccess(SND_PCM_ACCESS_RW_NONINTERLEAVED))<0)
#else
cout<<"\n\nsetting interleaved"<<endl;
if ((res=capture.setAccess(SND_PCM_ACCESS_RW_INTERLEAVED))<0)
#endif
if ((res=capture.setChannels(chCnt))<0)
res=sox.
openWrite(argv[1], fs, capture.getChannels(), pow(2.,(
double)snd_pcm_format_width(format)));
if (res<0)
if ((res=capture.setSampleRate(fs))<0)
cout<<
"rates are now, file = "<<sox.
getFSIn()<<
" Hz and ALSA = "<<capture.getSampleRate()<<endl;
int latency=2048;
if ((res=capture.setBufSize(latency))<0)
cout<<"latency = "<<(float)latency/(float)fs<<" s"<<endl;
capture.setParams();
capture.getFormat(format);
cout<<"format "<<capture.getFormatName(format)<<endl;
cout<<"channels "<<capture.getChannels()<<endl;
snd_pcm_uframes_t pSize;
capture.getPeriodSize(&pSize);
cout<<"period size "<<pSize<<endl;
if (!capture.prepared()){
cout<<"should be prepared, but isn't"<<endl;
return -1;
}
Eigen::Array<short int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> buffer(latency,chCnt);
if ((res=capture.start())<0)
int N=(int)floor(duration*(
float)fs);
while (N>0){
if (latency>N)
if (buffer.rows()==0)
break;
#ifndef USE_INTERLEAVED
capture.readBufN(buffer);
#else
if (latency<buffer.rows())
capture>>buffer.block(0, 0, latency, chCnt);
else
capture>>buffer;
#endif
if (latency<buffer.rows())
sox.
write(buffer.block(0, 0, latency, chCnt));
else
N-=latency;
}
return 0;
}