2012-05-22 13:09:48 -04:00
|
|
|
#include "soundin.h"
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#define NFFT 32768
|
|
|
|
#define FRAMES_PER_BUFFER 1024
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <portaudio.h>
|
|
|
|
extern struct {
|
2012-07-07 14:10:39 -04:00
|
|
|
short int d2[30*48000]; //This is "common/mscom/..." in fortran
|
2012-07-13 09:37:50 -04:00
|
|
|
float s1[215];
|
|
|
|
float s2[215];
|
2012-07-04 12:27:57 -04:00
|
|
|
int kin;
|
2012-07-13 09:37:50 -04:00
|
|
|
int ndiskdat;
|
|
|
|
int kline;
|
2012-07-07 14:10:39 -04:00
|
|
|
} mscom_;
|
2012-05-22 13:09:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int kin; //Parameters sent to/from the portaudio callback function
|
|
|
|
bool bzero;
|
|
|
|
} paUserData;
|
|
|
|
|
|
|
|
//--------------------------------------------------------------- a2dCallback
|
|
|
|
extern "C" int a2dCallback( const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesToProcess,
|
|
|
|
const PaStreamCallbackTimeInfo* timeInfo,
|
|
|
|
PaStreamCallbackFlags statusFlags,
|
|
|
|
void *userData )
|
|
|
|
|
|
|
|
// This routine called by the PortAudio engine when samples are available.
|
|
|
|
// It may be called at interrupt level, so don't do anything
|
|
|
|
// that could mess up the system like calling malloc() or free().
|
|
|
|
|
|
|
|
{
|
|
|
|
paUserData *udata=(paUserData*)userData;
|
|
|
|
(void) outputBuffer; //Prevent unused variable warnings.
|
|
|
|
(void) timeInfo;
|
|
|
|
(void) userData;
|
2012-07-04 13:48:57 -04:00
|
|
|
int nbytes,k;
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2012-07-04 12:27:57 -04:00
|
|
|
// if(framesToProcess != -99) return paContinue; //###
|
2012-07-03 16:07:37 -04:00
|
|
|
|
2012-05-22 13:09:48 -04:00
|
|
|
if( (statusFlags&paInputOverflow) != 0) {
|
|
|
|
qDebug() << "Input Overflow";
|
|
|
|
}
|
2012-07-04 12:27:57 -04:00
|
|
|
if(udata->bzero) { //Start of a new Rx sequence
|
2012-05-22 13:09:48 -04:00
|
|
|
udata->kin=0; //Reset buffer pointer
|
|
|
|
udata->bzero=false;
|
|
|
|
}
|
|
|
|
|
2012-07-05 12:16:03 -04:00
|
|
|
nbytes=2*framesToProcess; //Bytes per frame
|
2012-07-04 12:27:57 -04:00
|
|
|
k=udata->kin;
|
2012-07-07 14:10:39 -04:00
|
|
|
memcpy(&mscom_.d2[k],inputBuffer,nbytes); //Copy all samples to d2
|
2012-05-22 13:09:48 -04:00
|
|
|
udata->kin += framesToProcess;
|
2012-07-07 14:10:39 -04:00
|
|
|
mscom_.kin=udata->kin;
|
2012-07-04 12:27:57 -04:00
|
|
|
|
2012-05-22 13:09:48 -04:00
|
|
|
return paContinue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundInThread::run() //SoundInThread::run()
|
|
|
|
{
|
|
|
|
quitExecution = false;
|
|
|
|
|
|
|
|
//---------------------------------------------------- Soundcard Setup
|
|
|
|
PaError paerr;
|
|
|
|
PaStreamParameters inParam;
|
|
|
|
PaStream *inStream;
|
|
|
|
paUserData udata;
|
|
|
|
|
|
|
|
udata.kin=0; //Buffer pointer
|
|
|
|
udata.bzero=false; //Flag to request reset of kin
|
|
|
|
|
|
|
|
inParam.device=m_nDevIn; //### Input Device Number ###
|
2012-07-04 12:27:57 -04:00
|
|
|
inParam.channelCount=1; //Number of analog channels
|
2012-07-05 12:16:03 -04:00
|
|
|
inParam.sampleFormat=paInt16; //Get i*2 from Portaudio
|
2012-05-22 13:09:48 -04:00
|
|
|
inParam.suggestedLatency=0.05;
|
|
|
|
inParam.hostApiSpecificStreamInfo=NULL;
|
|
|
|
|
2012-07-03 16:07:37 -04:00
|
|
|
paerr=Pa_IsFormatSupported(&inParam,NULL,48000.0);
|
2012-05-22 13:09:48 -04:00
|
|
|
if(paerr<0) {
|
|
|
|
emit error("PortAudio says requested soundcard format not supported.");
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
paerr=Pa_OpenStream(&inStream, //Input stream
|
|
|
|
&inParam, //Input parameters
|
|
|
|
NULL, //No output parameters
|
2012-07-03 16:07:37 -04:00
|
|
|
48000.0, //Sample rate
|
2012-05-22 13:09:48 -04:00
|
|
|
FRAMES_PER_BUFFER, //Frames per buffer
|
2012-07-05 12:16:03 -04:00
|
|
|
// paClipOff+paDitherOff, //No clipping or dithering
|
2012-05-22 13:09:48 -04:00
|
|
|
paClipOff, //No clipping
|
|
|
|
a2dCallback, //Input callbeck routine
|
|
|
|
&udata); //userdata
|
|
|
|
|
|
|
|
paerr=Pa_StartStream(inStream);
|
|
|
|
if(paerr<0) {
|
|
|
|
emit error("Failed to start audio input stream.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool qe = quitExecution;
|
2012-07-03 16:07:37 -04:00
|
|
|
int n30z=99;
|
2012-05-22 13:09:48 -04:00
|
|
|
int k=0;
|
|
|
|
int nsec;
|
2012-07-03 16:07:37 -04:00
|
|
|
int n30;
|
2012-05-22 13:09:48 -04:00
|
|
|
int nBusy=0;
|
2012-07-04 12:27:57 -04:00
|
|
|
int nstep0=0;
|
2012-05-22 13:09:48 -04:00
|
|
|
|
|
|
|
//---------------------------------------------- Soundcard input loop
|
|
|
|
while (!qe) {
|
|
|
|
qe = quitExecution;
|
|
|
|
if (qe) break;
|
|
|
|
qint64 ms = QDateTime::currentMSecsSinceEpoch() % 86400000;
|
|
|
|
nsec = ms/1000; // Time according to this computer
|
2012-07-03 16:07:37 -04:00
|
|
|
n30 = nsec % 30;
|
2012-05-22 13:09:48 -04:00
|
|
|
|
|
|
|
// Reset buffer pointer and symbol number at start of minute
|
2012-07-03 16:07:37 -04:00
|
|
|
if(n30 < n30z or !m_monitoring) {
|
2012-07-04 12:27:57 -04:00
|
|
|
nstep0=0;
|
2012-05-22 13:09:48 -04:00
|
|
|
udata.bzero=true;
|
|
|
|
}
|
|
|
|
k=udata.kin;
|
|
|
|
if(m_monitoring) {
|
2012-07-07 14:10:39 -04:00
|
|
|
m_step=k/2048;
|
2012-07-04 12:27:57 -04:00
|
|
|
if(m_step != nstep0) {
|
2012-05-22 13:09:48 -04:00
|
|
|
if(m_dataSinkBusy) {
|
|
|
|
nBusy++;
|
|
|
|
} else {
|
2012-07-04 12:27:57 -04:00
|
|
|
// m_dataSinkBusy=true;
|
|
|
|
emit readyForFFT(k); //Signal to compute new FFTs
|
2012-05-22 13:09:48 -04:00
|
|
|
}
|
2012-07-04 12:27:57 -04:00
|
|
|
nstep0=m_step;
|
2012-05-22 13:09:48 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-07 14:10:39 -04:00
|
|
|
msleep(10);
|
2012-07-03 16:07:37 -04:00
|
|
|
n30z=n30;
|
2012-05-22 13:09:48 -04:00
|
|
|
}
|
|
|
|
Pa_StopStream(inStream);
|
|
|
|
Pa_CloseStream(inStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundInThread::setInputDevice(int n) //setInputDevice()
|
|
|
|
{
|
|
|
|
if (isRunning()) return;
|
|
|
|
this->m_nDevIn=n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundInThread::quit() //quit()
|
|
|
|
{
|
|
|
|
quitExecution = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundInThread::setMonitoring(bool b) //setMonitoring()
|
|
|
|
{
|
|
|
|
m_monitoring = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-04 12:27:57 -04:00
|
|
|
int SoundInThread::mstep()
|
2012-05-22 13:09:48 -04:00
|
|
|
{
|
2012-07-04 12:27:57 -04:00
|
|
|
return m_step;
|
2012-05-22 13:09:48 -04:00
|
|
|
}
|