#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,0";
int res;
playBack.resetParams();
if ((res=playBack.setFormat(SND_PCM_FORMAT_S32_LE))<0)
#ifndef USE_INTERLEAVED
cout<<"\n\nsetting non interleaved"<<endl;
if ((res=playBack.setAccess(SND_PCM_ACCESS_RW_NONINTERLEAVED))<0)
#else
cout<<"\n\nsetting interleaved"<<endl;
if ((res=playBack.setAccess(SND_PCM_ACCESS_RW_INTERLEAVED))<0)
#endif
unsigned int fs;
if (sox.
getFSIn()!=playBack.getSampleRate()){
cout<<
"sample rate mismatch, file = "<<sox.
getFSIn()<<
" Hz and ALSA = "<<playBack.getSampleRate()<<endl;
if ((res=playBack.setSampleRate(sox.
getFSIn()))<0)
fs=playBack.getSampleRate();
}
cout<<
"rates are now, file = "<<sox.
getFSIn()<<
" Hz and ALSA = "<<fs<<endl;
int latency=2048;
if ((res=playBack.setBufSize(latency))<0)
cout<<"latency = "<<(float)latency/(float)fs<<" s"<<endl;
playBack.setParams();
snd_pcm_format_t format;
playBack.getFormat(format);
cout<<"format "<<playBack.getFormatName(format)<<endl;
cout<<"channels "<<playBack.getChannels()<<endl;
snd_pcm_uframes_t pSize;
playBack.getPeriodSize(&pSize);
cout<<"period size "<<pSize<<endl;
if (!playBack.prepared()){
cout<<"should be prepared, but isn't"<<endl;
return -1;
}
Eigen::Array<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> buffer;
size_t totalWritten=0;
while (sox.
read(buffer, pSize)>=0){
if (buffer.rows()==0)
break;
#ifndef USE_INTERLEAVED
playBack.writeBufN(buffer);
#else
playBack<<buffer;
#endif
totalWritten+=buffer.rows();
}
return 0;
}