summaryrefslogtreecommitdiff
path: root/map65/soundout.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'map65/soundout.cpp')
-rw-r--r--map65/soundout.cpp309
1 files changed, 181 insertions, 128 deletions
diff --git a/map65/soundout.cpp b/map65/soundout.cpp
index e74d0549..0828afe4 100644
--- a/map65/soundout.cpp
+++ b/map65/soundout.cpp
@@ -4,13 +4,10 @@
#include <windows.h>
#endif
-#define FRAMES_PER_BUFFER 256
-
#include <portaudio.h>
-extern float gran(); //Noise generator (for tests only)
-
-extern short int iwave[2*60*11025]; //Wave file for Tx audio
+extern float gran();
+extern short int (&iwave)[2*60*12000];
extern int nwave;
extern bool btxok;
extern bool bTune;
@@ -20,9 +17,11 @@ extern int iqPhase;
extern int txPower;
extern double outputLatency;
-typedef struct //Parameters sent to or received from callback function
+typedef struct
{
int nTRperiod;
+ int actualChannelCount;
+ double streamSampleRate; // Added to help callback calculate pitch
} paUserData;
//--------------------------------------------------------------- d2aCallback
@@ -32,94 +31,125 @@ extern "C" int d2aCallback(const void * /*inputBuffer*/, void *outputBuffer,
PaStreamCallbackFlags /*statusFlags*/,
void *userData )
{
- paUserData *udata=(paUserData*)userData;
+ paUserData *udata = (paUserData*)userData;
short *wptr = (short*)outputBuffer;
- unsigned int i;
- static int n;
- static int ic=0;
- static bool btxok0=false;
- static bool bTune0=false;
- static int nStart=0;
- static double phi=0.;
- double tsec,tstart,dphi;
+ int channels = udata->actualChannelCount;
+
+ // step is ~0.229 for 48k ASIO, and exactly 1.0 for 11k MME
+ // Calculate step once outside the loop
+ double step = 11025.0 / udata->streamSampleRate;
+
+ // Debug: Check this value in your console once to ensure it's ~0.229 for ASIO
+ static bool debugOnce = true;
+ if(debugOnce) {
+ qDebug() << "Stream Rate:" << udata->streamSampleRate << "Step:" << step;
+ qDebug() << "channels:" << channels;
+ debugOnce = false;
+ }
+
+
+ static double ic_exact = 0.0;
+ static bool btxok0 = false;
+ static bool bTune0 = false;
+ static int nStart = 0;
+ static double phi = 0.;
+
+ double tsec, tstart, dphi;
int nsec;
- int nTRperiod=udata->nTRperiod;
+ int nTRperiod = udata->nTRperiod;
- // Get System time
qint64 ms = QDateTime::currentMSecsSinceEpoch() % 86400000;
- tsec = 0.001*ms;
- nsec = ms/1000;
- qreal dPhase=iqPhase/5729.57795131;
- qreal amp=1.0 + 0.0001*iqAmp;
- qreal xAmp=txPower*295.00*qSqrt(2.0 - amp*amp);
- qreal yAmp=txPower*295.00*amp;
- static int nsec0=0;
-
- if(bTune) {
- ic=0;
- dphi=6.28318530718*1270.46/11025.0;
- }
- if(bTune0 and !bTune) btxok=false;
- bTune0=bTune;
-
- if(nsec!=nsec0) {
-// qDebug() << txPower << iqAmp << iqPhase << amp << xAmp << yAmp << dPhase << bTune;
-// qDebug() << "A" << nsec%60 << bTune << btxok;
-// ic=0;
- nsec0=nsec;
+ tsec = 0.001 * ms;
+ nsec = ms / 1000;
+
+ qreal dPhase = iqPhase / 5729.57795131;
+ qreal amp = 1.0 + 0.0001 * iqAmp;
+ qreal xAmp = txPower * 295.00 * qSqrt(2.0 - amp * amp);
+ qreal yAmp = txPower * 295.00 * amp;
+ static int nsec0 = 0;
+
+ if(bTune) {
+ ic_exact = 0;
+ // CHANGE: Use the actual stream sample rate, not 11025
+ dphi = 6.28318530718 * 1270.46 / udata->streamSampleRate;
}
- if(btxok and !btxok0) { //Start (or re-start) a transmission
- n=nsec/nTRperiod;
- tstart=tsec - n*nTRperiod - 1.0;
+ if(bTune0 && !bTune) btxok = false;
+ bTune0 = bTune;
+ if(nsec != nsec0) nsec0 = nsec;
- if(tstart<1.0) {
- ic=0; //Start of Tx cycle, set starting index to 0
- nStart=n;
+ // Handle Tx Start/Restart logic
+ if(btxok && !btxok0) {
+ int n = nsec / nTRperiod;
+ tstart = tsec - n * nTRperiod - 1.0;
+ if(tstart < 1.0) {
+ ic_exact = 0;
+ nStart = n;
} else {
- if(n != nStart) { //Late start in new Tx cycle: compute starting index
- ic=(int)(tstart*11025.0);
- ic=2*ic;
- nStart=n;
+ if(n != nStart) {
+ ic_exact = (tstart * 11025.0);
+ nStart = n;
}
}
}
- btxok0=btxok;
-
- if(btxok) {
- for(i=0 ; i<framesToProcess; i++ ) {
- short int i2a=iwave[ic++];
- short int i2b=iwave[ic++];
- if(ic > nwave) {i2a=0; i2b=0;}
-// i2 = 500.0*(i2/32767.0 + 5.0*gran()); //Add noise (tests only!)
-// if(bIQxt) {
- if(1) {
- if(bTune) {
- phi += dphi;
- } else {
- phi=qAtan2(qreal(i2b),qreal(i2a));
- }
- i2a=xAmp*qCos(phi);
- i2b=yAmp*qSin(phi + dPhase);
-// qDebug() << xAmp << yAmp << phi << i2a << i2b;
- }
-// i2a=0.01*txPower*i2a;
-// i2b=0.01*txPower*i2b;
- *wptr++ = i2b; //left
- *wptr++ = i2a; //right
+ btxok0 = btxok;
+
+
+ for(unsigned int i = 0; i < framesToProcess; i++) {
+ // 1. Get current fractional position
+ int i0 = (int)ic_exact;
+ int i1 = i0 + 1;
+ double frac = ic_exact - (double)i0;
+
+ short int i2a = 0;
+ short int i2b = 0;
+
+ // 2. Linear Interpolation to clean up the noise
+if (btxok && (2 * i1 + 1) < 1440000) {
+
+ // Interpolate message waveform
+ qreal I = (1.0 - frac) * iwave[2*i0] + frac * iwave[2*i1];
+ qreal Q = (1.0 - frac) * iwave[2*i0 + 1] + frac * iwave[2*i1 + 1];
+
+ if (bTune) {
+ // --- TUNE MODE (unchanged) ---
+ phi += dphi;
+ I = xAmp * qCos(phi);
+ Q = yAmp * qSin(phi + dPhase);
+
+ } else {
+ // --- MESSAGE TX: scale using the Tune slider percentage ---
+ qreal gain = txPower / 100.0; // 0.0 ? 1.0
+
+ I *= gain;
+ Q *= gain;
}
- } else {
- for(i=0 ; i<framesToProcess; i++ ) {
- *wptr++ = 0;
+
+ // Clamp to 16-bit
+ i2a = short(qBound(-32768.0, I, 32767.0));
+ i2b = short(qBound(-32768.0, Q, 32767.0));
+}
+
+
+
+ *wptr++ = i2b; // Left
+ *wptr++ = i2a; // Right
+
+ // Pad remaining 6 channels for Voicemeeter
+ for(int c = 2; c < channels; c++) {
*wptr++ = 0;
- ic++; ic++;
+ }
+
+ // Advance index by the ratio (0.229)
+ ic_exact += step;
+
+ if((2 * (int)ic_exact) >= nwave) {
+ btxok = false;
+ ic_exact = 0;
}
}
- if(ic > nwave) {
- btxok=0;
- ic=0;
- }
- return 0;
+
+ return paContinue;
}
namespace
@@ -128,14 +158,15 @@ namespace
{
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
}
@@ -145,63 +176,85 @@ namespace
void SoundOutThread::run()
{
COMWrapper c;
-
PaError paerr;
PaStreamParameters outParam;
PaStream *outStream;
paUserData udata;
quitExecution = false;
- auto device_info = Pa_GetDeviceInfo (m_nDevOut);
+ // --- SAFETY CHECK 1: Ensure device index is valid ---
+ auto device_info = Pa_GetDeviceInfo(m_nDevOut);
+ if (!device_info) {
+ qDebug() << "ASIO Error: Could not get info for device index" << m_nDevOut;
+ return;
+ }
+
+ // --- SAFETY CHECK 2: Ensure Host API is valid ---
+ auto host_api_info = Pa_GetHostApiInfo(device_info->hostApi);
+ if (!host_api_info) {
+ qDebug() << "ASIO Error: Could not get host API info for device" << m_nDevOut;
+ return;
+ }
+
+ bool isASIO = (host_api_info->type == paASIO);
+
+ // Setup defaults for non-ASIO (MME, DirectSound, etc)
+ double outRate = 11025.0;
+ udata.actualChannelCount = 2;
+ unsigned long framesPerBuffer = 256;
+
+ if(isASIO) {
+ outRate = 48000.0;
+ udata.actualChannelCount = 8;
+ framesPerBuffer = paFramesPerBufferUnspecified;
+ }
+
+ udata.nTRperiod = m_TRperiod;
+ udata.streamSampleRate = outRate;
- outParam.device=m_nDevOut; //Output device number
- outParam.channelCount=2; //Number of analog channels
- outParam.sampleFormat=paInt16; //Send short ints to PortAudio
- outParam.suggestedLatency=device_info->defaultLowOutputLatency;
- outParam.hostApiSpecificStreamInfo=NULL;
+ outParam.device = m_nDevOut;
+ outParam.channelCount = udata.actualChannelCount;
+ outParam.sampleFormat = paInt16;
+
+ // --- SAFETY CHECK 3: Guard latency access ---
+ outParam.suggestedLatency = device_info->defaultLowOutputLatency;
+ outParam.hostApiSpecificStreamInfo = NULL;
- udata.nTRperiod=m_TRperiod;
- paerr=Pa_IsFormatSupported(NULL,&outParam,11025.0);
- if(paerr<0) {
- qDebug() << "PortAudio says requested output format not supported.";
- qDebug() << paerr;
- return;
+ // ASIO Fallback Logic
+ paerr = Pa_IsFormatSupported(NULL, &outParam, outRate);
+ if(paerr < 0 && isASIO) {
+ udata.actualChannelCount = 2;
+ outParam.channelCount = 2;
+ paerr = Pa_IsFormatSupported(NULL, &outParam, outRate);
}
- paerr=Pa_OpenStream(&outStream, //Output stream
- NULL, //No input parameters
- &outParam, //Output parameters
- 11025.0, //Sample rate
- FRAMES_PER_BUFFER, //Frames per buffer
- paClipOff, //No clipping
- d2aCallback, //output callbeck routine
- &udata); //userdata
-
- paerr=Pa_StartStream(outStream);
- if(paerr<0) {
- qDebug() << "Failed to start audio output stream.";
- return;
+
+ // --- SAFETY CHECK 4: Final guard before opening ---
+ paerr = Pa_OpenStream(&outStream, NULL, &outParam, outRate,
+ framesPerBuffer, paClipOff,
+ d2aCallback, &udata);
+
+ if(paerr != paNoError) {
+ qDebug() << "Pa_OpenStream failed:" << Pa_GetErrorText(paerr);
+ return;
}
- const PaStreamInfo* p=Pa_GetStreamInfo(outStream);
- outputLatency = p->outputLatency;
- bool qe = quitExecution;
-
-//---------------------------------------------- Soundcard output loop
- while (!qe) {
- qe = quitExecution;
- if (qe) break;
- msleep(100);
+
+ paerr = Pa_StartStream(outStream);
+ if(paerr != paNoError) {
+ qDebug() << "Pa_StartStream failed:" << Pa_GetErrorText(paerr);
+ return;
}
+
+ // --- SAFETY CHECK 5: Final stream info guard ---
+ const PaStreamInfo* p = Pa_GetStreamInfo(outStream);
+ if (p) {
+ outputLatency = p->outputLatency;
+ }
+
+ while (!quitExecution) { msleep(100); }
+
Pa_StopStream(outStream);
Pa_CloseStream(outStream);
}
-void SoundOutThread::setOutputDevice(int n) //setOutputDevice()
-{
- if (isRunning()) return;
- this->m_nDevOut=n;
-}
-
-void SoundOutThread::setPeriod(int n)
-{
- m_TRperiod=n;
-}
+void SoundOutThread::setOutputDevice(int n) { this->m_nDevOut = n; }
+void SoundOutThread::setPeriod(int n) { m_TRperiod = n; }