diff options
Diffstat (limited to 'Modulator.cpp')
| -rw-r--r-- | Modulator.cpp | 207 |
1 files changed, 120 insertions, 87 deletions
diff --git a/Modulator.cpp b/Modulator.cpp index 30b275b6..9155e588 100644 --- a/Modulator.cpp +++ b/Modulator.cpp @@ -4,6 +4,7 @@ #include <QDateTime> #include <QDebug> #include "mainwindow.h" +#include "soundout.h" #include "moc_Modulator.cpp" @@ -17,16 +18,15 @@ extern float gran(); // Noise generator (for tests only) # define SOFT_KEYING 1 #endif -double const Modulator::m_twoPi = 2.0 * 3.141592653589793238462; +double constexpr Modulator::m_twoPi; // float wpm=20.0; // unsigned m_nspd=1.2*48000.0/wpm; // m_nspd=3072; //18.75 WPM -unsigned const Modulator::m_nspd = 2048 + 512; // 22.5 WPM -Modulator::Modulator (unsigned frameRate, unsigned periodLengthInSeconds, QObject * parent) +Modulator::Modulator (unsigned frameRate, unsigned periodLengthInSeconds, + QObject * parent) : AudioDevice {parent} - , m_stream {nullptr} , m_quickClose {false} , m_phi {0.0} , m_toneSpacing {0.0} @@ -36,20 +36,20 @@ Modulator::Modulator (unsigned frameRate, unsigned periodLengthInSeconds, QObjec , m_state {Idle} , m_tuning {false} , m_cwLevel {false} + , m_j0 {-1} + , m_toneFrequency0 {1500.0} { - qsrand (QDateTime::currentMSecsSinceEpoch()); // Initialize random - // seed } -void Modulator::start (unsigned symbolsLength, double framesPerSymbol, unsigned frequency, double toneSpacing, SoundOutput * stream, Channel channel, bool synchronize, double dBSNR) +void Modulator::start (unsigned symbolsLength, double framesPerSymbol, + double frequency, double toneSpacing, + SoundOutput * stream, Channel channel, + bool synchronize, bool fastMode, double dBSNR, int TRperiod) { Q_ASSERT (stream); - - // Time according to this computer which becomes our base time +// Time according to this computer which becomes our base time qint64 ms0 = QDateTime::currentMSecsSinceEpoch() % 86400000; - // qDebug () << "Modulator: Using soft keying for CW is " << SOFT_KEYING;; - if (m_state != Idle) { stop (); @@ -58,14 +58,17 @@ void Modulator::start (unsigned symbolsLength, double framesPerSymbol, unsigned m_quickClose = false; m_symbolsLength = symbolsLength; - m_isym0 = std::numeric_limits<unsigned>::max (); // Arbitrary big - // number + m_isym0 = std::numeric_limits<unsigned>::max (); // big number m_frequency0 = 0.; + m_phi = 0.; m_addNoise = dBSNR < 0.; m_nsps = framesPerSymbol; m_frequency = frequency; m_amp = std::numeric_limits<qint16>::max (); m_toneSpacing = toneSpacing; + m_bFastMode=fastMode; + m_TRperiod=TRperiod; + unsigned delay_ms = 1920 == m_nsps && 15 == m_period ? 500 : 1000; // noise generator parameters if (m_addNoise) { @@ -75,34 +78,30 @@ void Modulator::start (unsigned symbolsLength, double framesPerSymbol, unsigned } unsigned mstr = ms0 % (1000 * m_period); // ms in period - m_ic = (mstr / 1000) * m_frameRate; // we start exactly N seconds - // into period where N is the next whole second + + // round up to an exact portion of a second that allows for startup + // delays + m_ic = (mstr / delay_ms) * m_frameRate * delay_ms / 1000; + + if(m_bFastMode) m_ic=0; m_silentFrames = 0; // calculate number of silent frames to send - if (synchronize && !m_tuning) { - m_silentFrames = m_ic + m_frameRate - (mstr * m_frameRate / 1000); + if (synchronize && !m_tuning && !m_bFastMode) { + m_silentFrames = m_ic + m_frameRate / (1000 / delay_ms) - (mstr * (m_frameRate / 1000)); } - // qDebug () << "Modulator: starting at " << m_ic / m_frameRate << " sec, sending " << m_silentFrames << " silent frames"; - initialize (QIODevice::ReadOnly, channel); Q_EMIT stateChanged ((m_state = (synchronize && m_silentFrames) ? Synchronizing : Active)); m_stream = stream; - if (m_stream) - { - m_stream->restart (this); - } + if (m_stream) m_stream->restart (this); } void Modulator::tune (bool newState) { m_tuning = newState; - if (!m_tuning) - { - stop (true); - } + if (!m_tuning) stop (true); } void Modulator::stop (bool quick) @@ -133,10 +132,12 @@ void Modulator::close () qint64 Modulator::readData (char * data, qint64 maxSize) { - static int j0=-1; - static double toneFrequency0; - double toneFrequency; - + double toneFrequency=1500.0; + if(m_nsps==6) { + toneFrequency=1000.0; + m_frequency=1000.0; + m_frequency0=1000.0; + } if(maxSize==0) return 0; Q_ASSERT (!(maxSize % qint64 (bytesPerFrame ()))); // no torn frames Q_ASSERT (isOpen ()); @@ -146,8 +147,6 @@ qint64 Modulator::readData (char * data, qint64 maxSize) qint16 * end (samples + numFrames * (bytesPerFrame () / sizeof (qint16))); qint64 framesGenerated (0); - // qDebug () << "Modulator: " << numFrames << " requested, m_ic = " << m_ic << ", tune mode is " << m_tuning; - // qDebug() << "C" << maxSize << numFrames << bytesPerFrame(); switch (m_state) { case Synchronizing: @@ -168,83 +167,117 @@ qint64 Modulator::readData (char * data, qint64 maxSize) case Active: { - unsigned isym (m_tuning ? 0 : m_ic / (4.0 * m_nsps)); // Actual fsample=48000 - if (isym >= m_symbolsLength && icw[0] > 0) { // start CW condition - // Output the CW ID - m_dphi = m_twoPi * m_frequency / m_frameRate; - unsigned const ic0 = m_symbolsLength * 4 * m_nsps; - unsigned j (0); + unsigned int isym=0; + if(!m_tuning) isym=m_ic/(4.0*m_nsps); // Actual fsample=48000 + bool slowCwId=((isym >= m_symbolsLength) && (icw[0] > 0)) && (!m_bFastMode); + if(m_TRperiod==3) slowCwId=false; + bool fastCwId=false; + static bool bCwId=false; + qint64 ms = QDateTime::currentMSecsSinceEpoch(); + float tsec=0.001*(ms % (1000*m_TRperiod)); + if(m_bFastMode and (icw[0]>0) and (tsec>(m_TRperiod-5.0))) fastCwId=true; + if(!m_bFastMode) m_nspd=2560; // 22.5 WPM + + if(slowCwId or fastCwId) { // Transmit CW ID? + m_dphi = m_twoPi*m_frequency/m_frameRate; + if(m_bFastMode and !bCwId) { + m_frequency=1500; // Set params for CW ID + m_dphi = m_twoPi*m_frequency/m_frameRate; + m_symbolsLength=126; + m_nsps=4096.0*12000.0/11025.0; + m_ic=2246949; + m_nspd=2560; // 22.5 WPM + if(icw[0]*m_nspd/48000.0 > 4.0) m_nspd=4.0*48000.0/icw[0]; //Faster CW for long calls + } + bCwId=true; + unsigned ic0 = m_symbolsLength * 4 * m_nsps; + unsigned j(0); while (samples != end) { - j = (m_ic - ic0) / m_nspd + 1; // symbol of this sample + j = (m_ic - ic0)/m_nspd + 1; // symbol of this sample bool level {bool (icw[j])}; - m_phi += m_dphi; if (m_phi > m_twoPi) m_phi -= m_twoPi; - - qint16 sample ((SOFT_KEYING ? qAbs (m_ramp - 1) : - (m_ramp ? 32767 : 0)) * qSin (m_phi)); - - if (int (j) <= icw[0] && j < NUM_CW_SYMBOLS) // stop condition - { - samples = load (postProcessSample (sample), samples); - ++framesGenerated; - ++m_ic; - } - else - { - Q_EMIT stateChanged ((m_state = Idle)); - return framesGenerated * bytesPerFrame (); + qint16 sample=0; + float amp=32767.0; + float x=0; + if(m_ramp!=0) { + x=qSin(float(m_phi)); + if(SOFT_KEYING) { + amp=qAbs(qint32(m_ramp)); + if(amp>32767.0) amp=32767.0; } + sample=round(amp*x); + } + if(m_bFastMode) { + sample=0; + if(level) sample=32767.0*x; + } + if (int (j) <= icw[0] && j < NUM_CW_SYMBOLS) { // stop condition + samples = load (postProcessSample (sample), samples); + ++framesGenerated; + ++m_ic; + } else { + Q_EMIT stateChanged ((m_state = Idle)); + return framesGenerated * bytesPerFrame (); + } // adjust ramp - if ((m_ramp != 0 && m_ramp != std::numeric_limits<qint16>::min ()) || level != m_cwLevel) - { - // either ramp has terminated at max/min or direction - // has changed - m_ramp += RAMP_INCREMENT; // ramp - } - - // if (m_cwLevel != level) - // { - // qDebug () << "@m_ic:" << m_ic << "icw[" << j << "] =" << icw[j] << "@" << framesGenerated << "in numFrames:" << numFrames; - // } - + if ((m_ramp != 0 && m_ramp != std::numeric_limits<qint16>::min ()) || level != m_cwLevel) { + // either ramp has terminated at max/min or direction has changed + m_ramp += RAMP_INCREMENT; // ramp + } m_cwLevel = level; } - return framesGenerated * bytesPerFrame (); - } + } else { + bCwId=false; + } //End of code for CW ID double const baud (12000.0 / m_nsps); // fade out parameters (no fade out for tuning) - unsigned const i0 = m_tuning ? 999 * m_nsps : - (m_symbolsLength - 0.017) * 4.0 * m_nsps; - unsigned const i1 = m_tuning ? 999 * m_nsps : - m_symbolsLength * 4.0 * m_nsps; + unsigned int i0,i1; + if(m_tuning) { + i1 = i0 = (m_bFastMode ? 999999 : 9999) * m_nsps; + } else { + i0=(m_symbolsLength - 0.017) * 4.0 * m_nsps; + i1= m_symbolsLength * 4.0 * m_nsps; + } + if(m_bFastMode and !m_tuning) { + i1=m_TRperiod*48000 - 24000; + i0=i1-816; + } + for (unsigned i = 0; i < numFrames && m_ic <= i1; ++i) { - isym = m_tuning ? 0 : m_ic / (4.0 * m_nsps); //Actual fsample=48000 + isym=0; + if(!m_tuning and m_TRperiod!=3) isym=m_ic / (4.0 * m_nsps); //Actual + //fsample=48000 + if(m_bFastMode) isym=isym%m_symbolsLength; if (isym != m_isym0 || m_frequency != m_frequency0) { - // qDebug () << "@m_ic:" << m_ic << "itone[" << isym << "] =" << itone[isym] << "@" << i << "in numFrames:" << numFrames; - - if(m_toneSpacing==0.0) { - toneFrequency0=m_frequency + itone[isym]*baud; + if(itone[0]>=100) { + m_toneFrequency0=itone[0]; } else { - toneFrequency0=m_frequency + itone[isym]*m_toneSpacing; + if(m_toneSpacing==0.0) { + m_toneFrequency0=m_frequency + itone[isym]*baud; + } else { + m_toneFrequency0=m_frequency + itone[isym]*m_toneSpacing; + } } - m_dphi = m_twoPi * toneFrequency0 / m_frameRate; +// qDebug() << "B" << m_bFastMode << m_ic << numFrames << isym << itone[isym] +// << m_toneFrequency0 << m_nsps; + m_dphi = m_twoPi * m_toneFrequency0 / m_frameRate; m_isym0 = isym; - m_frequency0 = m_frequency; + m_frequency0 = m_frequency; //??? } int j=m_ic/480; - if(m_fSpread>0.0 and j!=j0) { - float x1=(float)rand()/RAND_MAX; - float x2=(float)rand()/RAND_MAX; - toneFrequency = toneFrequency0 + 0.5*m_fSpread*(x1+x2-1.0); + if(m_fSpread>0.0 and j!=m_j0) { + float x1=(float)qrand()/RAND_MAX; + float x2=(float)qrand()/RAND_MAX; + toneFrequency = m_toneFrequency0 + 0.5*m_fSpread*(x1+x2-1.0); m_dphi = m_twoPi * toneFrequency / m_frameRate; - j0=j; + m_j0=j; } m_phi += m_dphi; @@ -263,10 +296,10 @@ qint64 Modulator::readData (char * data, qint64 maxSize) Q_EMIT stateChanged ((m_state = Idle)); return framesGenerated * bytesPerFrame (); } - m_phi = 0.0; } + m_frequency0 = m_frequency; // done for this chunk - continue on next call return framesGenerated * bytesPerFrame (); } |
