diff options
Diffstat (limited to 'map65/soundin.cpp')
| -rw-r--r-- | map65/soundin.cpp | 339 |
1 files changed, 164 insertions, 175 deletions
diff --git a/map65/soundin.cpp b/map65/soundin.cpp index 46433d4e..ee1886e8 100644 --- a/map65/soundin.cpp +++ b/map65/soundin.cpp @@ -1,5 +1,8 @@ #include "soundin.h" +#include "commons.h" #include <math.h> +#include <complex> +#include "mainwindow.h" #ifdef Q_OS_WIN32 #include <windows.h> @@ -7,48 +10,11 @@ #include <sys/socket.h> #endif -#define NFFT 32768 #define FRAMES_PER_BUFFER 1024 #include <portaudio.h> -extern "C" -{ - struct - { - double d8[2*60*96000]; //This is "common/datcom/..." in fortran - float ss[4*322*NFFT]; - float savg[4*NFFT]; - double fcenter; - int nutc; - int idphi; //Phase correction for Y pol'n, degrees - int mousedf; //User-selected DF - int mousefqso; //User-selected QSO freq (kHz) - int nagain; //1 ==> decode only at fQSO +/- Tol - int ndepth; //How much hinted decoding to do? - int ndiskdat; //1 ==> data read from *.tf2 or *.iq file - int neme; //Hinted decoding tries only for EME calls - int newdat; //1 ==> new data, must do long FFT - int nfa; //Low decode limit (kHz) - int nfb; //High decode limit (kHz) - int nfcal; //Frequency correction, for calibration (Hz) - int nfshift; //Shift of displayed center freq (kHz) - int mcall3; //1 ==> CALL3.TXT has been modified - int ntimeout; //Max for timeouts in Messages and BandMap - int ntol; //+/- decoding range around fQSO (Hz) - int nxant; //1 ==> add 45 deg to measured pol angle - int map65RxLog; //Flags to control log files - int nfsample; //Input sample rate - int nxpol; //1 if using xpol antennas, 0 otherwise - int mode65; //JT65 sub-mode: A=1, B=2, C=4 - int nfast; //1No longer used - int nsave; //Number of s3(64,63) spectra saved - char mycall[12]; - char mygrid[6]; - char hiscall[12]; - char hisgrid[6]; - char datetime[20]; - } datcom_; -} +#include <QDebug> + typedef struct { @@ -60,113 +26,133 @@ typedef struct } 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(). - +extern "C" int a2dCallback(const void* inputBuffer, void* outputBuffer, + unsigned long framesToProcess, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void* userData) { - paUserData *udata=(paUserData*)userData; - (void) outputBuffer; //Prevent unused variable warnings. - (void) timeInfo; - (void) userData; - int nbytes,i,j; - float d4[4*FRAMES_PER_BUFFER]; - float d4a[4*FRAMES_PER_BUFFER]; - float tmp; - float fac; - - if( (statusFlags&paInputOverflow) != 0) { - qDebug() << "Input Overflow"; - } - if(udata->bzero) { //Start of a new minute - udata->kin=0; //Reset buffer pointer - udata->bzero=false; - } + paUserData* udata = static_cast<paUserData*>(userData); - nbytes=udata->nrx*8*framesToProcess; //Bytes per frame - memcpy(d4,inputBuffer,nbytes); //Copy all samples to d4 + (void) outputBuffer; + (void) timeInfo; + (void) userData; - fac=32767.0 * pow(10.0,0.05*udata->dB); + int nbytes,i,j; + float d4[4 * FRAMES_PER_BUFFER]; + float d4a[4 * FRAMES_PER_BUFFER]; + float tmp; + float fac; - if(udata->nrx==2) { - for(i=0; i<4*int(framesToProcess); i++) { //Negate odd-numbered frames - d4[i]=fac*d4[i]; - j=i/4; - if((j%2)==1) d4[i]=-d4[i]; + if ((statusFlags & paInputOverflow) != 0) { + qDebug() << "Input Overflow"; } - if(!udata->iqswap) { - for(i=0; i<int(framesToProcess); i++) { - j=4*i; - tmp=d4[j]; - d4[j]=d4[j+1]; - d4[j+1]=tmp; - tmp=d4[j+2]; - d4[j+2]=d4[j+3]; - d4[j+3]=tmp; - } - } - memcpy(&datcom_.d8[2*udata->kin],d4,nbytes); //Copy from d4 to dd() - } else { - int k=0; - for(i=0; i<2*int(framesToProcess); i+=2) { //Negate odd-numbered frames - j=i/2; - if(j%2==0) { - d4a[k++]=fac*d4[i]; - d4a[k++]=fac*d4[i+1]; - } else { - d4a[k++]=-fac*d4[i]; - d4a[k++]=-fac*d4[i+1]; - } - d4a[k++]=0.0; - d4a[k++]=0.0; + + if (udata->bzero) { // Start of a new minute + advance_live_input_generation(); + udata->kin = 0; // Reset buffer pointer + udata->bzero = false; } - if(!udata->iqswap) { - for(i=0; i<int(framesToProcess); i++) { - j=4*i; - tmp=d4a[j]; - d4a[j]=d4a[j+1]; - d4a[j+1]=tmp; - } + + // Bytes per frame, mirroring legacy intent but for float32 + // legacy used: nbytes = udata->nrx * 8 * framesToProcess; + // here: nrx * 8 bytes per frame (4 floats per frame, each 4 bytes) + nbytes = udata->nrx * 8 * framesToProcess; + + memcpy(d4, inputBuffer, nbytes); + + fac = 32767.0f * pow(10.0f, 0.05f * float(udata->dB)); + + if (udata->nrx == 2) { + // Two RF channels, r*4 data (complex I/Q for X and Y) + for (i = 0; i < 4 * int(framesToProcess); i++) { + d4[i] = fac * d4[i]; + j = i / 4; + if ((j % 2) == 1) d4[i] = -d4[i]; + } + + if (!udata->iqswap) { + for (i = 0; i < int(framesToProcess); i++) { + j = 4 * i; + tmp = d4[j]; d4[j] = d4[j+1]; d4[j+1] = tmp; + tmp = d4[j+2]; d4[j+2] = d4[j+3]; d4[j+3] = tmp; + } + } + + memcpy(&dd[4 * udata->kin], d4, nbytes); + + } else { + // One RF channel + int k = 0; + for (i = 0; i < 2 * int(framesToProcess); i += 2) { + j = i / 2; + if (j % 2 == 0) { + d4a[k++] = fac * d4[i]; + d4a[k++] = fac * d4[i+1]; + } else { + d4a[k++] = -fac * d4[i]; + d4a[k++] = -fac * d4[i+1]; + } + d4a[k++] = 0.0; + d4a[k++] = 0.0; + } + + if (!udata->iqswap) { + for (i = 0; i < int(framesToProcess); i++) { + j = 4 * i; + tmp = d4a[j]; d4a[j] = d4a[j+1]; d4a[j+1] = tmp; + } + } + + // Legacy wrote 2*nbytes into datcom_.d8 for this case (4 floats per frame) + memcpy(&dd[4 * udata->kin], d4a, 2 * nbytes); } - memcpy(&datcom_.d8[2*udata->kin],d4a,2*nbytes); //Copy from d4a to dd() - } - udata->kin += framesToProcess; - return paContinue; + + udata->kin += framesToProcess; + return paContinue; } namespace { + constexpr int symspecLookaheadSamples = 512; + + int symspecReadyPointer(int acquiredSamples) + { + // symspec's final 1024-sample window extends one 512-sample hop past k. + return acquiredSamples - symspecLookaheadSamples; + } + struct COMWrapper { explicit COMWrapper () { -#ifdef Q_OS_WIN32 - // required because Qt only does this for GUI thread - CoInitializeEx (nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); +#ifdef _WIN32 + CoInitializeEx (nullptr, + COINIT_APARTMENTTHREADED | + COINIT_DISABLE_OLE1DDE); #endif } ~COMWrapper () { -#ifdef Q_OS_WIN32 +#ifdef _WIN32 CoUninitialize (); #endif } }; } +//SoundInThread::SoundInThread(MainWindow* mw, QObject* parent) +// : QThread(parent), m_mainWindow(mw) {} + void SoundInThread::run() //SoundInThread::run() { quitExecution = false; - + if (m_net) { // qDebug() << "Start inputUDP()"; inputUDP(); + return; + // qDebug() << "Finished inputUDP()"; return; } @@ -174,13 +160,13 @@ void SoundInThread::run() //SoundInThread::run() COMWrapper c; //---------------------------------------------------- Soundcard Setup - // qDebug() << "Start souncard input"; + qDebug() << "START SOUNDCARD INPUT "; PaError paerr; PaStreamParameters inParam; PaStream *inStream; paUserData udata; - + udata.kin=0; //Buffer pointer udata.bzero=false; //Flag to request reset of kin udata.nrx=m_nrx; //Number of polarizations @@ -195,7 +181,7 @@ void SoundInThread::run() //SoundInThread::run() inParam.suggestedLatency=device_info->defaultHighInputLatency; inParam.hostApiSpecificStreamInfo=NULL; - paerr=Pa_IsFormatSupported(&inParam,NULL,96000.0); + paerr=Pa_IsFormatSupported(&inParam,NULL,double(g_sampleRate)); if(paerr<0) { QString error_message; if (paUnanticipatedHostError == paerr) @@ -208,70 +194,66 @@ void SoundInThread::run() //SoundInThread::run() error_message = "PortAudio says requested soundcard format not supported."; } emit error(error_message); -// return; } - paerr=Pa_OpenStream(&inStream, //Input stream - &inParam, //Input parameters - NULL, //No output parameters - 96000.0, //Sample rate - FRAMES_PER_BUFFER, //Frames per buffer -// paClipOff+paDitherOff, //No clipping or dithering - 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; - } -// const PaStreamInfo* p=Pa_GetStreamInfo(inStream); + paerr = Pa_OpenStream( + &inStream, + &inParam, + NULL, + double(g_sampleRate), + FRAMES_PER_BUFFER, + paClipOff, + a2dCallback, + &udata); + + paerr=Pa_StartStream(inStream); + if(paerr<0) { + emit error("Failed to start audio input stream."); + return; + } bool qe = quitExecution; int ntr0=99; int k=0; int nsec; int ntr; - int nBusy=0; - int nhsym0=0; + Map65HalfSymbolScheduler spectrumScheduler; //---------------------------------------------- Soundcard input loop while (!qe) { qe = quitExecution; if (qe) break; + qint64 ms = QDateTime::currentMSecsSinceEpoch() % 86400000; - nsec = ms/1000; // Time according to this computer + nsec = ms/1000; ntr = nsec % m_TRperiod; -// Reset buffer pointer and symbol number at start of minute if(ntr < ntr0 or !m_monitoring or m_TRperiod!=m_TRperiod0) { - nhsym0=0; + spectrumScheduler.reset (); udata.bzero=true; m_TRperiod0=m_TRperiod; } + k=udata.kin; - udata.iqswap=m_IQswap; + + udata.iqswap= (m_IQswap != 0); udata.dB=m_dB; + if(m_monitoring) { - if(m_bForceCenterFreq) { - datcom_.fcenter=m_dForceCenterFreq; - } else { - datcom_.fcenter=144.125; - } - m_hsym=(k-2048)*11025.0/(2048.0*m_rate); - if(m_hsym != nhsym0) { - if(m_dataSinkBusy) { - nBusy++; - } else { - m_dataSinkBusy=true; - emit readyForFFT(k); //Signal to compute new FFTs - } - nhsym0=m_hsym; + double fcenter = m_bForceCenterFreq ? m_dForceCenterFreq : 144.125; + set_fcenter(fcenter); + + int const fftPointer=symspecReadyPointer(k); + m_hsym=(fftPointer-2048)*11025.0/(2048.0*m_rate); + + if (spectrumScheduler.claim_next (m_hsym, m_dataSinkBusy)) { + emit readyForFFT(fftPointer); } } + msleep(100); ntr0=ntr; } + Pa_StopStream(inStream); Pa_CloseStream(inStream); } @@ -314,7 +296,6 @@ void SoundInThread::setFadd(double x) m_fAdd=x; } - void SoundInThread::quit() //quit() { quitExecution = true; @@ -328,12 +309,15 @@ void SoundInThread::setNetwork(bool b) //setNetwork() void SoundInThread::setMonitoring(bool b) //setMonitoring() { m_monitoring = b; + qDebug() << "m_monitoring set to" << m_monitoring << "at" << Q_FUNC_INFO; + } void SoundInThread::setForceCenterFreqBool(bool b) { m_bForceCenterFreq=b; + } void SoundInThread::setForceCenterFreqMHz(double d) @@ -344,10 +328,12 @@ void SoundInThread::setForceCenterFreqMHz(double d) void SoundInThread::setNrx(int n) //setNrx() { m_nrx = n; + //qDebug() << "soundin line 333 SET m_nrx = " << n; } int SoundInThread::nrx() { + //qDebug() << "soundin line 338 RETURNED m_nrx = " << m_nrx; return m_nrx; } @@ -380,7 +366,7 @@ void SoundInThread::inputUDP() int msec; float userfreq; int iptr; - quint16 iblk; + quint16 iblk; //was quint16 qint8 nrx; char iusb; double d8[174]; @@ -390,9 +376,8 @@ void SoundInThread::inputUDP() int k=0; int nsec; int ntr; - int nhsym0=0; + Map65HalfSymbolScheduler spectrumScheduler; int iz=174; - int nBusy=0; // Main loop for input of UDP packets over the network: while (!qe) { @@ -401,8 +386,11 @@ void SoundInThread::inputUDP() if (!udpSocket->hasPendingDatagrams()) { msleep(2); // Sleep if no packet available } else { - int nBytesRead = udpSocket->readDatagram((char *)&b,1416); - if (nBytesRead != 1416) qDebug() << "UDP Read Error:" << nBytesRead; + int nBytesRead = udpSocket->readDatagram(reinterpret_cast<char*>(&b), 1416); + if (nBytesRead != 1416) { + qDebug() << "UDP Read Error:" << nBytesRead; + } + qint64 ms = QDateTime::currentMSecsSinceEpoch() % 86400000; nsec = ms/1000; // Time according to this computer @@ -410,8 +398,9 @@ void SoundInThread::inputUDP() // Reset buffer pointer and symbol number at start of minute if(ntr < ntr0 or !m_monitoring or m_TRperiod!=m_TRperiod0) { + advance_live_input_generation(); k=0; - nhsym0=0; + spectrumScheduler.reset (); m_TRperiod0=m_TRperiod; } ntr0=ntr; @@ -424,28 +413,28 @@ void SoundInThread::inputUDP() if(m_nrx == -2) iz=87; // Two RF channels, r*4 data // If buffer will not overflow, move data into datcom_ - if ((k+iz) <= 60*96000) { + if ((k + iz) <= 60 * m_rate) { int nsam=-1; recvpkt_(&nsam, &b.iblk, &b.nrx, &k, b.d8, b.d8, b.d8); + double fcenter; if(m_bForceCenterFreq) { - datcom_.fcenter=m_dForceCenterFreq; + fcenter = m_dForceCenterFreq; } else { - datcom_.fcenter=b.cfreq + m_fAdd; + fcenter = b.cfreq + m_fAdd; } + set_fcenter(fcenter); } - m_hsym=(k-2048)*11025.0/(2048.0*m_rate); - if(m_hsym != nhsym0) { - if(m_dataSinkBusy) { - nBusy++; - } else { - m_dataSinkBusy=true; - emit readyForFFT(k); //Signal to compute new FFTs - } - nhsym0=m_hsym; - } +double hsym = 2048.0 * m_rate / 11025.0; +int const fftPointer=symspecReadyPointer(k); +m_hsym = (fftPointer - 2048) / hsym; +if (spectrumScheduler.claim_next (m_hsym, m_dataSinkBusy)) { + emit readyForFFT(fftPointer); +} + } } } delete udpSocket; + udpSocket = nullptr; } |
