summaryrefslogtreecommitdiff
path: root/soundin.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 /soundin.cpp
parentc720a3d55c5ec832c29cb7ce50b6fb4b70c55b11 (diff)
Imported Upstream version 1.5.0upstream/1.5.0
Diffstat (limited to 'soundin.cpp')
-rw-r--r--soundin.cpp316
1 files changed, 135 insertions, 181 deletions
diff --git a/soundin.cpp b/soundin.cpp
index 332e5d37..a93fb49d 100644
--- a/soundin.cpp
+++ b/soundin.cpp
@@ -1,208 +1,162 @@
#include "soundin.h"
-#include <stdexcept>
-
-#define FRAMES_PER_BUFFER 1024
-//#define NSMAX 1365
-#define NSMAX 6827
-#define NTMAX 120
-
-extern "C" {
-#include <portaudio.h>
-extern struct {
- float ss[184*NSMAX]; //This is "common/jt9com/..." in fortran
- float savg[NSMAX];
-// float c0[2*NTMAX*1500];
- short int d2[NTMAX*12000];
- int nutc; //UTC as integer, HHMM
- int ndiskdat; //1 ==> data read from *.wav file
- int ntrperiod; //TR period (seconds)
- int mousefqso; //User-selected QSO freq (kHz)
- int newdat; //1 ==> new data, must do long FFT
- int npts8; //npts in c0() array
- int nfa; //Low decode limit (Hz)
- int nfb; //High decode limit (Hz)
- int ntol; //+/- decoding range around fQSO (Hz)
- int kin;
- int nzhsym;
- int nsave;
- int nagain;
- int ndepth;
- int ntxmode;
- int nmode;
- char datetime[20];
-} jt9com_;
-}
-typedef struct
-{
- int kin; //Parameters sent to/from the portaudio callback function
- int ncall;
- bool bzero;
- bool monitoring;
-} 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().
+#include <QAudioDeviceInfo>
+#include <QAudioFormat>
+#include <QAudioInput>
+#include <QDebug>
+#include "moc_soundin.cpp"
+
+bool SoundInput::audioError () const
{
- paUserData *udata=(paUserData*)userData;
- (void) outputBuffer; //Prevent unused variable warnings.
- (void) timeInfo;
- (void) userData;
- int nbytes,k;
-
- udata->ncall++;
- if( (statusFlags&paInputOverflow) != 0) {
- qDebug() << "Input Overflow in a2dCallback";
- }
- if(udata->bzero) { //Start of a new Rx sequence
- udata->kin=0; //Reset buffer pointer
- udata->bzero=false;
- }
-
- nbytes=2*framesToProcess; //Bytes per frame
- k=udata->kin;
- if(udata->monitoring) {
- memcpy(&jt9com_.d2[k],inputBuffer,nbytes); //Copy all samples to d2
- }
- udata->kin += framesToProcess;
- jt9com_.kin=udata->kin;
-
- return paContinue;
+ bool result (true);
+
+ Q_ASSERT_X (m_stream, "SoundInput", "programming error");
+ if (m_stream)
+ {
+ switch (m_stream->error ())
+ {
+ case QAudio::OpenError:
+ Q_EMIT error (tr ("An error opening the audio input device has occurred."));
+ break;
+
+ case QAudio::IOError:
+ Q_EMIT error (tr ("An error occurred during read from the audio input device."));
+ break;
+
+ case QAudio::UnderrunError:
+ Q_EMIT error (tr ("Audio data not being fed to the audio input device fast enough."));
+ break;
+
+ case QAudio::FatalError:
+ Q_EMIT error (tr ("Non-recoverable error, audio input device not usable at this time."));
+ break;
+
+ case QAudio::NoError:
+ result = false;
+ break;
+ }
+ }
+ return result;
}
-void SoundInThread::run() //SoundInThread::run()
+void SoundInput::start(QAudioDeviceInfo const& device, int framesPerBuffer, AudioDevice * sink, unsigned downSampleFactor, AudioDevice::Channel channel)
{
- quitExecution = false;
-
-//---------------------------------------------------- Soundcard Setup
- PaError paerr;
- PaStreamParameters inParam;
- PaStream *inStream;
- paUserData udata;
-
- udata.kin=0; //Buffer pointer
- udata.ncall=0; //Number of callbacks
- udata.bzero=false; //Flag to request reset of kin
- udata.monitoring=m_monitoring;
-
- inParam.device=m_nDevIn; //### Input Device Number ###
- inParam.channelCount=1; //Number of analog channels
- inParam.sampleFormat=paInt16; //Get i*2 from Portaudio
- inParam.suggestedLatency=0.05;
- inParam.hostApiSpecificStreamInfo=NULL;
-
- paerr=Pa_IsFormatSupported(&inParam,NULL,12000.0);
- if(paerr<0) {
- emit error("PortAudio says requested soundcard format not supported.");
-// return;
- }
- qDebug() << "";
- paerr=Pa_OpenStream(&inStream, //Input stream
- &inParam, //Input parameters
- NULL, //No output parameters
- 12000.0, //Sample rate
- FRAMES_PER_BUFFER, //Frames per buffer
-// paClipOff+paDitherOff, //No clipping or dithering
- paClipOff, //No clipping
- a2dCallback, //Input callback routine
- &udata); //userdata
-
- paerr=Pa_StartStream(inStream);
- if(paerr<0) {
- emit error("Failed to start audio input stream.");
- return;
- }
-
- bool qe = quitExecution;
- static int ntr0=99;
- int k=0;
- int nsec;
- int ntr;
- int nBusy=0;
- int nstep0=0;
- int nsps0=0;
- qint64 ms0 = QDateTime::currentMSecsSinceEpoch();
-
-//---------------------------------------------- Soundcard input loop
- while (!qe) {
- qe = quitExecution;
- if (qe) break;
- udata.monitoring=m_monitoring;
- qint64 ms = QDateTime::currentMSecsSinceEpoch();
- m_SamFacIn=1.0;
- if(udata.ncall>100) {
- m_SamFacIn=udata.ncall*FRAMES_PER_BUFFER*1000.0/(12000.0*(ms-ms0-50));
+ Q_ASSERT (sink);
+
+ stop ();
+
+ m_sink = sink;
+
+ QAudioFormat format (device.preferredFormat());
+ format.setChannelCount (AudioDevice::Mono == channel ? 1 : 2);
+ format.setCodec ("audio/pcm");
+ format.setSampleRate (12000 * downSampleFactor);
+ format.setSampleType (QAudioFormat::SignedInt);
+ format.setSampleSize (16);
+
+ if (!format.isValid ())
+ {
+ Q_EMIT error (tr ("Requested input audio format is not valid."));
+ return;
}
- ms=ms % 86400000;
- nsec = ms/1000; // Time according to this computer
- ntr = nsec % m_TRperiod;
-
-// Reset buffer pointer and symbol number at start of minute
- if(ntr < ntr0 or !m_monitoring or m_nsps!=nsps0) {
- nstep0=0;
- nsps0=m_nsps;
- udata.bzero=true;
+
+ // this function lies!
+ // if (!device.isFormatSupported (format))
+ // {
+ // Q_EMIT error (tr ("Requested input audio format is not supported on device."));
+ // return;
+ // }
+
+ m_stream.reset (new QAudioInput {device, format});
+ if (audioError ())
+ {
+ return;
}
- k=udata.kin;
- if(m_monitoring) {
- int kstep=m_nsps/2;
-// m_step=k/kstep;
- m_step=(k-1)/kstep;
- if(m_step != nstep0) {
- if(m_dataSinkBusy) {
- nBusy++;
- } else {
-// m_dataSinkBusy=true;
-// emit readyForFFT(k); //Signal to compute new FFTs
- emit readyForFFT(k-1); //Signal to compute new FFTs
- }
- nstep0=m_step;
- }
+
+ connect (m_stream.data(), &QAudioInput::stateChanged, this, &SoundInput::handleStateChanged);
+
+ m_stream->setBufferSize (m_stream->format ().bytesForFrames (framesPerBuffer));
+ if (sink->initialize (QIODevice::WriteOnly, channel))
+ {
+ m_stream->start (sink);
+ audioError ();
+ }
+ else
+ {
+ Q_EMIT error (tr ("Failed to initialize audio sink device"));
}
- msleep(100);
- ntr0=ntr;
- }
- Pa_StopStream(inStream);
- Pa_CloseStream(inStream);
}
-void SoundInThread::setInputDevice(int n) //setInputDevice()
+void SoundInput::suspend ()
{
- if (isRunning()) return;
- this->m_nDevIn=n;
+ if (m_stream)
+ {
+ m_stream->suspend ();
+ audioError ();
+ }
}
-void SoundInThread::quit() //quit()
+void SoundInput::resume ()
{
- quitExecution = true;
-}
+ if (m_sink)
+ {
+ m_sink->reset ();
+ }
-void SoundInThread::setMonitoring(bool b) //setMonitoring()
-{
- m_monitoring = b;
+ if (m_stream)
+ {
+ m_stream->resume ();
+ audioError ();
+ }
}
-void SoundInThread::setPeriod(int ntrperiod, int nsps)
+void SoundInput::handleStateChanged (QAudio::State newState) const
{
- m_TRperiod=ntrperiod;
- m_nsps=nsps;
+ // qDebug () << "SoundInput::handleStateChanged: newState:" << newState;
+
+ switch (newState)
+ {
+ case QAudio::IdleState:
+ Q_EMIT status (tr ("Idle"));
+ break;
+
+ case QAudio::ActiveState:
+ Q_EMIT status (tr ("Receiving"));
+ 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;
+ }
}
-int SoundInThread::mstep()
+void SoundInput::stop()
{
- return m_step;
+ if (m_stream)
+ {
+ m_stream->stop ();
+ }
+ m_stream.reset ();
+
+ if (m_sink)
+ {
+ m_sink->close ();
+ }
}
-double SoundInThread::samFacIn()
+SoundInput::~SoundInput ()
{
- return m_SamFacIn;
+ stop ();
}