summaryrefslogtreecommitdiff
path: root/soundout.cpp
diff options
context:
space:
mode:
authorIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
committerIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
commitd63dd8d081a6fcd0715cd13f26df59fb831769d2 (patch)
treeb9c1f6245147a310aac3762630bad389bbb57335 /soundout.cpp
parentc720a3d55c5ec832c29cb7ce50b6fb4b70c55b11 (diff)
Imported Upstream version 1.5.0upstream/1.5.0
Diffstat (limited to 'soundout.cpp')
-rw-r--r--soundout.cpp385
1 files changed, 155 insertions, 230 deletions
diff --git a/soundout.cpp b/soundout.cpp
index 8693237c..9e5843e9 100644
--- a/soundout.cpp
+++ b/soundout.cpp
@@ -1,270 +1,195 @@
#include "soundout.h"
-//#define FRAMES_PER_BUFFER 1024
-
-extern "C" {
-#include <portaudio.h>
-}
-
-extern float gran(); //Noise generator (for tests only)
-extern int itone[126]; //Audio tones for all Tx symbols
-extern int icw[250]; //Dits for CW ID
-extern int outBufSize;
-extern bool btxok;
-extern bool btxMute;
-extern double outputLatency;
-
-typedef struct //Parameters sent to or received from callback function
-{
- double txsnrdb;
- double dnsps;
- int ntrperiod;
- int ntxfreq;
- int xit;
- int ncall;
- int nsym;
- bool txMute;
- bool bRestart;
- bool btune;
-} paUserData;
+#include <QDateTime>
+#include <QAudioDeviceInfo>
+#include <QAudioOutput>
+#include <qmath.h>
+#include <QDebug>
+
+#include "moc_soundout.cpp"
+
+#if defined (WIN32)
+# define MS_BUFFERED 1000u
+#else
+# define MS_BUFFERED 2000u
+#endif
-//--------------------------------------------------------------- d2aCallback
-extern "C" int d2aCallback(const void *inputBuffer, void *outputBuffer,
- unsigned long framesToProcess,
- const PaStreamCallbackTimeInfo* timeInfo,
- PaStreamCallbackFlags statusFlags,
- void *userData )
+bool SoundOutput::audioError () const
{
- paUserData *udata=(paUserData*)userData;
- short *wptr = (short*)outputBuffer;
-
- static double twopi=2.0*3.141592653589793238462;
- static double baud;
- static double phi=0.0;
- static double dphi;
- static double freq;
- static double snr;
- static double fac;
- static double amp;
- static int ic=0,j=0;
- static int isym0=-999;
- static short int i2;
- int isym,nspd;
-
- udata->ncall++;
- if(udata->bRestart) {
- // Time according to this computer
- qint64 ms = QDateTime::currentMSecsSinceEpoch() % 86400000;
- int mstr = ms % (1000*udata->ntrperiod );
- if(mstr<1000) return paContinue;
- ic=(mstr-1000)*48;
- udata->bRestart=false;
- srand(mstr); //Initialize random seed
- }
- isym=ic/(4.0*udata->dnsps); //Actual fsample=48000
- if(udata->btune) isym=0; //If tuning, send pure tone
- if(udata->txsnrdb < 0.0) {
- snr=pow(10.0,0.05*(udata->txsnrdb-6.0));
- fac=3000.0;
- if(snr>1.0) fac=3000.0/snr;
- }
-
- if(isym>=udata->nsym and icw[0]>0) { //Output the CW ID
- freq=udata->ntxfreq - udata->xit;
- dphi=twopi*freq/48000.0;
-
-// float wpm=20.0;
-// int nspd=1.2*48000.0/wpm;
-// nspd=3072; //18.75 WPM
- nspd=2048 + 512; //22.5 WPM
- int ic0=udata->nsym*4*udata->dnsps;
- for(uint i=0 ; i<framesToProcess; i++ ) {
- phi += dphi;
- if(phi>twopi) phi -= twopi;
- i2=32767.0*sin(phi);
- j=(ic-ic0)/nspd + 1;
- if(icw[j]==0) i2=0;
- if(udata->txsnrdb < 0.0) {
- int i4=fac*(gran() + i2*snr/32768.0);
- if(i4>32767) i4=32767;
- if(i4<-32767) i4=-32767;
- i2=i4;
+ bool result (true);
+
+ Q_ASSERT_X (m_stream, "SoundOutput", "programming error");
+ if (m_stream) {
+ switch (m_stream->error ())
+ {
+ case QAudio::OpenError:
+ Q_EMIT error (tr ("An error opening the audio output device has occurred."));
+ break;
+
+ case QAudio::IOError:
+ Q_EMIT error (tr ("An error occurred during write to the audio output device."));
+ break;
+
+ case QAudio::UnderrunError:
+ Q_EMIT error (tr ("Audio data not being fed to the audio output device fast enough."));
+ break;
+
+ case QAudio::FatalError:
+ Q_EMIT error (tr ("Non-recoverable error, audio output device not usable at this time."));
+ break;
+
+ case QAudio::NoError:
+ result = false;
+ break;
}
- if(!btxok or btxMute) i2=0;
- *wptr++ = i2; //left
-#ifdef UNIX
- *wptr++ = i2; //right
-#endif
- ic++;
- }
- if(j>icw[0]) return paComplete;
- if(statusFlags==999999 and timeInfo==NULL and
- inputBuffer==NULL) return paContinue; //Silence compiler warning:
- return paContinue;
- }
-
- baud=12000.0/udata->dnsps;
- amp=32767.0;
- int i0=(udata->nsym-0.017)*4.0*udata->dnsps;
- int i1=udata->nsym*4.0*udata->dnsps;
- if(udata->btune) { //If tuning, no ramp down
- i0=999*udata->dnsps;
- i1=i0;
- }
- for(uint i=0 ; i<framesToProcess; i++ ) {
- isym=ic/(4.0*udata->dnsps); //Actual fsample=48000
- if(udata->btune) isym=0; //If tuning, send pure tone
- if(isym!=isym0) {
- freq=udata->ntxfreq + itone[isym]*baud - udata->xit;
- dphi=twopi*freq/48000.0;
- isym0=isym;
- }
- phi += dphi;
- if(phi>twopi) phi -= twopi;
- if(ic>i0) amp=0.98*amp;
- if(ic>i1) amp=0.0;
- i2=amp*sin(phi);
- if(udata->txsnrdb < 0.0) {
- int i4=fac*(gran() + i2*snr/32768.0);
- if(i4>32767) i4=32767;
- if(i4<-32767) i4=-32767;
- i2=i4;
- }
- if(!btxok or btxMute) i2=0;
- *wptr++ = i2; //left
-#ifdef UNIX
- *wptr++ = i2; //right
-#endif
- ic++;
}
- if(amp==0.0) {
- if(icw[0]==0) return paComplete;
- phi=0.0;
- }
- return paContinue;
+ return result;
}
-void SoundOutThread::run()
+void SoundOutput::setFormat (QAudioDeviceInfo const& device, unsigned channels, unsigned msBuffered)
{
- PaError paerr;
- PaStreamParameters outParam;
- PaStream *outStream;
- paUserData udata;
- quitExecution = false;
+ Q_ASSERT (0 < channels && channels < 3);
- outParam.device=m_nDevOut; //Output device number
- outParam.channelCount=1; //Number of analog channels
-#ifdef UNIX
- outParam.channelCount=2; //Number of analog channels
-#endif
- outParam.sampleFormat=paInt16; //Send short ints to PortAudio
- outParam.suggestedLatency=0.05;
- outParam.hostApiSpecificStreamInfo=NULL;
+ m_msBuffered = msBuffered;
- paerr=Pa_IsFormatSupported(NULL,&outParam,48000.0);
- if(paerr<0) {
- qDebug() << "PortAudio says requested output format not supported.";
- qDebug() << paerr << m_nDevOut;
- return;
- }
+ QAudioFormat format (device.preferredFormat ());
- udata.txsnrdb=99.0;
- udata.dnsps=m_nsps;
- udata.nsym=85;
- if(m_modeTx=="JT65") {
- udata.dnsps=4096.0*12000.0/11025.0;
- udata.nsym=126;
- }
- udata.ntrperiod=m_TRperiod;
- udata.ntxfreq=m_txFreq;
- udata.xit=m_xit;
- udata.ncall=0;
- udata.txMute=m_txMute;
- udata.bRestart=true;
- udata.btune=m_tune;
+ format.setChannelCount (channels);
+ format.setCodec ("audio/pcm");
+ format.setSampleRate (48000);
+ format.setSampleType (QAudioFormat::SignedInt);
+ format.setSampleSize (16);
+ if (!format.isValid ())
+ {
+ Q_EMIT error (tr ("Requested output audio format is not valid."));
+ }
+ if (!device.isFormatSupported (format))
+ {
+ Q_EMIT error (tr ("Requested output audio format is not supported on device."));
+ }
- paerr=Pa_OpenStream(&outStream, //Output stream
- NULL, //No input parameters
- &outParam, //Output parameters
- 48000.0, //Sample rate
- outBufSize, //Frames per buffer
- paClipOff, //No clipping
- d2aCallback, //output callbeck routine
- &udata); //userdata
+ m_stream.reset (new QAudioOutput (device, format));
+ audioError ();
+ m_stream->setVolume (m_volume);
+ m_stream->setNotifyInterval(100);
- paerr=Pa_StartStream(outStream);
- if(paerr<0) {
- qDebug() << "Failed to start audio output stream.";
- return;
- }
- const PaStreamInfo* p=Pa_GetStreamInfo(outStream);
- outputLatency = p->outputLatency;
- bool qe = quitExecution;
- qint64 ms0 = QDateTime::currentMSecsSinceEpoch();
+ connect (m_stream.data(), &QAudioOutput::stateChanged, this, &SoundOutput::handleStateChanged);
-//---------------------------------------------- Soundcard output loop
- while (!qe) {
- qe = quitExecution;
- if (qe) break;
+ // qDebug() << "A" << m_volume << m_stream->notifyInterval();
+}
- udata.txsnrdb=m_txsnrdb;
- udata.dnsps=m_nsps;
- udata.nsym=85;
- if(m_modeTx=="JT65") {
- udata.dnsps=4096.0*12000.0/11025.0;
- udata.nsym=126;
- }
- udata.ntrperiod=m_TRperiod;
- udata.ntxfreq=m_txFreq;
- udata.xit=m_xit;
- udata.txMute=m_txMute;
- udata.btune=m_tune;
+void SoundOutput::restart (QIODevice * source)
+{
+ Q_ASSERT (m_stream);
+
+ //
+ // This buffer size is critical since for proper sound streaming. If
+ // it is too short; high activity levels on the machine can starve
+ // the audio buffer. On the other hand the Windows implementation
+ // seems to take the length of the buffer in time to stop the audio
+ // stream even if reset() is used.
+ //
+ // 2 seconds seems a reasonable compromise except for Windows
+ // where things are probably broken.
+ //
+ // we have to set this before every start on the stream because the
+ // Windows implementation seems to forget the buffer size after a
+ // stop.
+ m_stream->setBufferSize (m_stream->format().bytesForDuration((m_msBuffered ? m_msBuffered : MS_BUFFERED) * 1000));
+ // qDebug() << "B" << m_stream->bufferSize() <<
+ // m_stream->periodSize() << m_stream->notifyInterval();
+
+ m_stream->start (source);
+}
- m_SamFacOut=1.0;
- if(udata.ncall>400) {
- qint64 ms = QDateTime::currentMSecsSinceEpoch();
- m_SamFacOut=udata.ncall*outBufSize*1000.0/(48000.0*(ms-ms0-50));
+void SoundOutput::suspend ()
+{
+ if (m_stream && QAudio::ActiveState == m_stream->state ())
+ {
+ m_stream->suspend ();
+ audioError ();
}
- msleep(100);
- }
- Pa_StopStream(outStream);
- Pa_CloseStream(outStream);
}
-void SoundOutThread::setOutputDevice(int n) //setOutputDevice()
+void SoundOutput::resume ()
{
- if (isRunning()) return;
- this->m_nDevOut=n;
+ if (m_stream && QAudio::SuspendedState == m_stream->state ())
+ {
+ m_stream->resume ();
+ audioError ();
+ }
}
-void SoundOutThread::setPeriod(int ntrperiod, int nsps)
+void SoundOutput::reset ()
{
- m_TRperiod=ntrperiod;
- m_nsps=nsps;
+ if (m_stream)
+ {
+ m_stream->reset ();
+ audioError ();
+ }
}
-void SoundOutThread::setTxFreq(int n)
+void SoundOutput::stop ()
{
- m_txFreq=n;
+ if (m_stream)
+ {
+ m_stream->stop ();
+ audioError ();
+ }
}
-void SoundOutThread::setXIT(int n)
+qreal SoundOutput::attenuation () const
{
- m_xit=n;
-
+ return -(10. * qLn (m_volume) / qLn (10.));
}
-void SoundOutThread::setTxSNR(double snr)
+void SoundOutput::setAttenuation (qreal a)
{
- m_txsnrdb=snr;
+ Q_ASSERT (0. <= a && a <= 999.);
+ m_volume = qPow (10., -a / 10.);
+ // qDebug () << "SoundOut: attn = " << a << ", vol = " << m_volume;
+ if (m_stream)
+ {
+ m_stream->setVolume (m_volume);
+ }
}
-void SoundOutThread::setTune(bool b)
+void SoundOutput::resetAttenuation ()
{
- m_tune=b;
+ m_volume = 1.;
+ if (m_stream)
+ {
+ m_stream->setVolume (m_volume);
+ }
}
-double SoundOutThread::samFacOut()
+void SoundOutput::handleStateChanged (QAudio::State newState)
{
- return m_SamFacOut;
+ // qDebug () << "SoundOutput::handleStateChanged: newState:" << newState;
+
+ switch (newState)
+ {
+ case QAudio::IdleState:
+ Q_EMIT status (tr ("Idle"));
+ break;
+
+ case QAudio::ActiveState:
+ Q_EMIT status (tr ("Sending"));
+ break;
+
+ case QAudio::SuspendedState:
+ Q_EMIT status (tr ("Suspended"));
+ break;
+
+ case QAudio::StoppedState:
+ if (audioError ())
+ {
+ Q_EMIT status (tr ("Error"));
+ }
+ else
+ {
+ Q_EMIT status (tr ("Stopped"));
+ }
+ break;
+ }
}