FCDProPlus: use Qt for FCD audio device handling. Fixes start up error

This commit is contained in:
f4exb 2018-11-19 02:33:44 +01:00
parent 865d26cc15
commit a10b29d6d3
7 changed files with 98 additions and 131 deletions

View File

@ -1,15 +1,27 @@
/* ///////////////////////////////////////////////////////////////////////////////////
* fcdtraits.cpp // Copyright (C) 2015-2018 Edouard Griffiths, F4EXB //
* // //
* Created on: Sep 5, 2015 // This program is free software; you can redistribute it and/or modify //
* Author: f4exb // it under the terms of the GNU General Public License as published by //
*/ // the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "fcdtraits.h" #include "fcdtraits.h"
const char *fcd_traits<Pro>::alsaDeviceName = "hw:CARD=V10"; const char *fcd_traits<Pro>::alsaDeviceName = "hw:CARD=V10";
const char *fcd_traits<ProPlus>::alsaDeviceName = "hw:CARD=V20"; const char *fcd_traits<ProPlus>::alsaDeviceName = "hw:CARD=V20";
const char *fcd_traits<Pro>::qtDeviceName = "FUNcube_Dongle_V1.0";
const char *fcd_traits<ProPlus>::qtDeviceName = "FUNcube_Dongle_V2.0";
const char *fcd_traits<Pro>::hardwareID = "FCDPro"; const char *fcd_traits<Pro>::hardwareID = "FCDPro";
const char *fcd_traits<ProPlus>::hardwareID = "FCDPro+"; const char *fcd_traits<ProPlus>::hardwareID = "FCDPro+";
@ -23,7 +35,7 @@ const char *fcd_traits<Pro>::pluginDisplayedName = "FunCube Pro Input";
const char *fcd_traits<ProPlus>::pluginDisplayedName = "FunCube Pro+ Input"; const char *fcd_traits<ProPlus>::pluginDisplayedName = "FunCube Pro+ Input";
const char *fcd_traits<Pro>::pluginVersion = "4.0.0"; const char *fcd_traits<Pro>::pluginVersion = "4.0.0";
const char *fcd_traits<ProPlus>::pluginVersion = "4.0.0"; const char *fcd_traits<ProPlus>::pluginVersion = "4.3.0";
const int64_t fcd_traits<Pro>::loLowLimitFreq = 64000000L; const int64_t fcd_traits<Pro>::loLowLimitFreq = 64000000L;
const int64_t fcd_traits<ProPlus>::loLowLimitFreq = 150000L; const int64_t fcd_traits<ProPlus>::loLowLimitFreq = 150000L;

View File

@ -1,9 +1,18 @@
/* ///////////////////////////////////////////////////////////////////////////////////
* fcdtraits.h // Copyright (C) 2015-2018 Edouard Griffiths, F4EXB //
* // //
* Created on: 3 Sep 2015 // This program is free software; you can redistribute it and/or modify //
* Author: egriffiths // it under the terms of the GNU General Public License as published by //
*/ // the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef FCDLIB_FCDTRAITS_H_ #ifndef FCDLIB_FCDTRAITS_H_
#define FCDLIB_FCDTRAITS_H_ #define FCDLIB_FCDTRAITS_H_
@ -25,6 +34,7 @@ struct fcd_traits
static const int convBufSize = (1<<11); static const int convBufSize = (1<<11);
static const int fcdBufSize = (1<<12); static const int fcdBufSize = (1<<12);
static const char *alsaDeviceName; static const char *alsaDeviceName;
static const char *qtDeviceName;
static const char *hardwareID; static const char *hardwareID;
static const char *interfaceIID; static const char *interfaceIID;
static const char *displayedName; static const char *displayedName;
@ -43,6 +53,7 @@ struct fcd_traits<Pro>
static const int convBufSize = (1<<11); static const int convBufSize = (1<<11);
static const int fcdBufSize = (1<<12); static const int fcdBufSize = (1<<12);
static const char *alsaDeviceName; static const char *alsaDeviceName;
static const char *qtDeviceName;
static const char *hardwareID; static const char *hardwareID;
static const char *interfaceIID; static const char *interfaceIID;
static const char *displayedName; static const char *displayedName;
@ -58,9 +69,10 @@ struct fcd_traits<ProPlus>
static const uint16_t vendorId = 0x04D8; static const uint16_t vendorId = 0x04D8;
static const uint16_t productId = 0xFB31; static const uint16_t productId = 0xFB31;
static const int sampleRate = 192000; static const int sampleRate = 192000;
static const int convBufSize = (1<<12); static const int convBufSize = (1<<10);
static const int fcdBufSize = (1<<18); static const int fcdBufSize = (1<<18); // used in FCDProPlusReader
static const char *alsaDeviceName; static const char *alsaDeviceName;
static const char *qtDeviceName;
static const char *hardwareID; static const char *hardwareID;
static const char *interfaceIID; static const char *interfaceIID;
static const char *displayedName; static const char *displayedName;

View File

@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB // // Copyright (C) 2016-2018 Edouard Griffiths, F4EXB //
// // // //
// This program is free software; you can redistribute it and/or modify // // This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by // // it under the terms of the GNU General Public License as published by //
@ -14,8 +14,6 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // // along with this program. If not, see <http://www.gnu.org/licenses/>. //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// FIXME: FCD is handled very badly!
#include <QDebug> #include <QDebug>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -47,6 +45,7 @@ FCDProPlusInput::FCDProPlusInput(DeviceSourceAPI *deviceAPI) :
m_deviceDescription(fcd_traits<ProPlus>::displayedName), m_deviceDescription(fcd_traits<ProPlus>::displayedName),
m_running(false) m_running(false)
{ {
m_fcdFIFO.setSize(4*fcd_traits<ProPlus>::convBufSize);
openDevice(); openDevice();
m_fileSink = new FileRecord(QString("test_%1.sdriq").arg(m_deviceAPI->getDeviceUID())); m_fileSink = new FileRecord(QString("test_%1.sdriq").arg(m_deviceAPI->getDeviceUID()));
m_deviceAPI->addSink(m_fileSink); m_deviceAPI->addSink(m_fileSink);
@ -113,17 +112,17 @@ bool FCDProPlusInput::start()
return false; return false;
} }
if (!openSource(fcd_traits<ProPlus>::alsaDeviceName)) if (!openFCDAudio(fcd_traits<ProPlus>::qtDeviceName))
{ {
qCritical("FCDProPlusInput::start: could not open source"); qCritical("FCDProPlusInput::start: could not open FCD audio source");
return false; return false;
} }
else else
{ {
qDebug("FCDProPlusInput::start: source opened"); qDebug("FCDProPlusInput::start: FCD audio source opened");
} }
m_FCDThread = new FCDProPlusThread(&m_sampleFifo, fcd_handle); m_FCDThread = new FCDProPlusThread(&m_sampleFifo, &m_fcdFIFO);
m_FCDThread->startWork(); m_FCDThread->startWork();
// mutexLocker.unlock(); // mutexLocker.unlock();
@ -145,73 +144,33 @@ void FCDProPlusInput::closeDevice()
m_dev = 0; m_dev = 0;
} }
bool FCDProPlusInput::openSource(const char* cardname) bool FCDProPlusInput::openFCDAudio(const char* cardname)
{ {
bool fail = false; AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager();
snd_pcm_hw_params_t* params; const QList<QAudioDeviceInfo>& audioList = audioDeviceManager->getInputDevices();
//fcd_rate = FCDPP_RATE;
//fcd_channels =2;
//fcd_format = SND_PCM_SFMT_U16_LE;
snd_pcm_stream_t fcd_stream = SND_PCM_STREAM_CAPTURE;
if (fcd_handle) for (const auto &itAudio : audioList)
{ {
qDebug("FCDProPlusInput::OpenSource: already opened"); if (itAudio.deviceName().contains(QString(cardname)))
return false;
}
if (snd_pcm_open(&fcd_handle, cardname, fcd_stream, 0) < 0)
{
qCritical("FCDProPlusInput::OpenSource: cannot open %s", cardname);
return false;
}
snd_pcm_hw_params_alloca(&params);
if (snd_pcm_hw_params_any(fcd_handle, params) < 0)
{
qCritical("FCDProPlusInput::OpenSource: snd_pcm_hw_params_any failed");
fail = true;
}
else if (snd_pcm_hw_params(fcd_handle, params) < 0)
{
qCritical("FCDProPlusInput::OpenSource: snd_pcm_hw_params failed");
fail = true;
// TODO: check actual samplerate, may be crippled firmware
}
else
{
if (snd_pcm_start(fcd_handle) < 0)
{ {
qCritical("FCDProPlusInput::OpenSource: snd_pcm_start failed"); int fcdDeviceIndex = audioDeviceManager->getInputDeviceIndex(itAudio.deviceName());
fail = true; m_fcdAudioInput.start(fcdDeviceIndex, fcd_traits<ProPlus>::sampleRate);
} int fcdSampleRate = m_fcdAudioInput.getRate();
else qDebug("FCDProPlusInput::openFCDAudio: %s index %d at %d S/s",
{ itAudio.deviceName().toStdString().c_str(), fcdDeviceIndex, fcdSampleRate);
qDebug("FCDProPlusInput::OpenSource: snd_pcm_start OK"); m_fcdAudioInput.addFifo(&m_fcdFIFO);
return true;
} }
} }
if (fail) qCritical("FCDProPlusInput::openFCDAudio: device with name %s not found", cardname);
{ return false;
snd_pcm_close(fcd_handle);
return false;
}
else
{
qDebug("FCDProPlusInput::OpenSource: Funcube stream started");
}
return true;
} }
void FCDProPlusInput::closeSource() void FCDProPlusInput::closeFCDAudio()
{ {
if (fcd_handle) { m_fcdAudioInput.removeFifo(&m_fcdFIFO);
snd_pcm_close(fcd_handle); m_fcdAudioInput.stop();
}
fcd_handle = nullptr;
} }
void FCDProPlusInput::stop() void FCDProPlusInput::stop()
@ -224,7 +183,7 @@ void FCDProPlusInput::stop()
// wait for thread to quit ? // wait for thread to quit ?
delete m_FCDThread; delete m_FCDThread;
m_FCDThread = nullptr; m_FCDThread = nullptr;
closeSource(); closeFCDAudio();
} }
m_running = false; m_running = false;

View File

@ -23,7 +23,10 @@
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <dsp/devicesamplesource.h> #include "dsp/devicesamplesource.h"
#include "audio/audioinput.h"
#include "audio/audiofifo.h"
#include "fcdproplussettings.h" #include "fcdproplussettings.h"
#include "fcdhid.h" #include "fcdhid.h"
@ -149,13 +152,15 @@ public:
private: private:
bool openDevice(); bool openDevice();
void closeDevice(); void closeDevice();
bool openSource(const char *filename); bool openFCDAudio(const char *filename);
void closeSource(); void closeFCDAudio();
void applySettings(const FCDProPlusSettings& settings, bool force); void applySettings(const FCDProPlusSettings& settings, bool force);
void webapiFormatDeviceSettings(SWGSDRangel::SWGDeviceSettings& response, const FCDProPlusSettings& settings); void webapiFormatDeviceSettings(SWGSDRangel::SWGDeviceSettings& response, const FCDProPlusSettings& settings);
DeviceSourceAPI *m_deviceAPI; DeviceSourceAPI *m_deviceAPI;
hid_device *m_dev; hid_device *m_dev;
AudioInput m_fcdAudioInput;
AudioFifo m_fcdFIFO;
snd_pcm_t* fcd_handle; snd_pcm_t* fcd_handle;
QMutex m_mutex; QMutex m_mutex;
FCDProPlusSettings m_settings; FCDProPlusSettings m_settings;

View File

@ -1,6 +1,5 @@
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany // // Copyright (C) 2016-2018 Edouard Griffiths, F4EXB //
// written by Christian Daniel //
// // // //
// This program is free software; you can redistribute it and/or modify // // This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by // // it under the terms of the GNU General Public License as published by //
@ -21,13 +20,13 @@
#include "fcdproplusthread.h" #include "fcdproplusthread.h"
#include "dsp/samplesinkfifo.h" #include "dsp/samplesinkfifo.h"
#include "fcdtraits.h" #include "audio/audiofifo.h"
FCDProPlusThread::FCDProPlusThread(SampleSinkFifo* sampleFifo, snd_pcm_t *fcd_handle, QObject* parent) : FCDProPlusThread::FCDProPlusThread(SampleSinkFifo* sampleFifo, AudioFifo *fcdFIFO, QObject* parent) :
QThread(parent), QThread(parent),
m_fcd_handle(fcd_handle), m_fcdFIFO(fcdFIFO),
m_running(false), m_running(false),
m_convertBuffer(fcd_traits<ProPlus>::convBufSize), m_convertBuffer(fcd_traits<ProPlus>::convBufSize), // nb samples
m_sampleFifo(sampleFifo) m_sampleFifo(sampleFifo)
{ {
start(); start();
@ -62,43 +61,18 @@ void FCDProPlusThread::run()
m_running = true; m_running = true;
qDebug("FCDThread::run: start running loop"); qDebug("FCDThread::run: start running loop");
while (m_running) while (m_running) {
{ work(fcd_traits<ProPlus>::convBufSize);
if (work(fcd_traits<ProPlus>::convBufSize) < 0) {
break;
}
} }
qDebug("FCDThread::run: running loop stopped"); qDebug("FCDThread::run: running loop stopped");
m_running = false;
} }
int FCDProPlusThread::work(int n_items) void FCDProPlusThread::work(unsigned int n_items)
{ {
int l; uint32_t nbRead = m_fcdFIFO->read((unsigned char *) m_buf, n_items); // number of samples
SampleVector::iterator it; SampleVector::iterator it = m_convertBuffer.begin();
void *out; m_decimators.decimate1(&it, m_buf, 2*nbRead);
m_sampleFifo->write(m_convertBuffer.begin(), it);
it = m_convertBuffer.begin();
out = (void *)&it[0];
l = snd_pcm_mmap_readi(m_fcd_handle, out, (snd_pcm_uframes_t)n_items);
if (l > 0)
{
m_sampleFifo->write(it, it + l);
}
else
{
if (l == -EPIPE) {
qDebug("FCDProPlusThread::work: Overrun detected");
} else if (l < 0) {
qDebug("FCDProPlusThread::work: snd_pcm_mmap_readi failed with code %d", l);
} else {
qDebug("FCDProPlusThread::work: snd_pcm_mmap_readi empty");
}
return 0;
}
return l;
} }

View File

@ -21,32 +21,36 @@
#include <QThread> #include <QThread>
#include <QMutex> #include <QMutex>
#include <QWaitCondition> #include <QWaitCondition>
#include <alsa/asoundlib.h>
#include "dsp/inthalfbandfilter.h"
#include "dsp/samplesinkfifo.h" #include "dsp/samplesinkfifo.h"
#include "dsp/decimators.h"
#include "fcdtraits.h"
class AudioFifo;
class FCDProPlusThread : public QThread { class FCDProPlusThread : public QThread {
Q_OBJECT Q_OBJECT
public: public:
FCDProPlusThread(SampleSinkFifo* sampleFifo, snd_pcm_t *fcd_handle, QObject* parent = nullptr); FCDProPlusThread(SampleSinkFifo* sampleFifo, AudioFifo *fcdFIFO, QObject* parent = nullptr);
~FCDProPlusThread(); ~FCDProPlusThread();
void startWork(); void startWork();
void stopWork(); void stopWork();
private: private:
snd_pcm_t* m_fcd_handle; AudioFifo* m_fcdFIFO;
QMutex m_startWaitMutex; QMutex m_startWaitMutex;
QWaitCondition m_startWaiter; QWaitCondition m_startWaiter;
bool m_running; bool m_running;
qint16 m_buf[fcd_traits<ProPlus>::convBufSize*2]; // stereo (I, Q)
SampleVector m_convertBuffer; SampleVector m_convertBuffer;
SampleSinkFifo* m_sampleFifo; SampleSinkFifo* m_sampleFifo;
Decimators<qint32, qint16, SDR_RX_SAMP_SZ, 16> m_decimators;
void run(); void run();
int work(int n_items); void work(unsigned int n_items);
}; };
#endif // INCLUDE_FCDTHREAD_H #endif // INCLUDE_FCDTHREAD_H

View File

@ -64,12 +64,12 @@ bool AudioInput::start(int device, int rate)
if (device < devicesInfo.size()) if (device < devicesInfo.size())
{ {
devInfo = devicesInfo[device]; devInfo = devicesInfo[device];
qWarning("AudioInput::start: using audio device #%d: %s", device, qPrintable(devInfo.defaultInputDevice().deviceName())); qWarning("AudioInput::start: using audio device #%d: %s", device, qPrintable(devInfo.deviceName()));
} }
else else
{ {
devInfo = QAudioDeviceInfo::defaultInputDevice(); devInfo = QAudioDeviceInfo::defaultInputDevice();
qWarning("AudioInput::start: audio device #%d does not exist. Using default device %s", device, qPrintable(devInfo.defaultInputDevice().deviceName())); qWarning("AudioInput::start: audio device #%d does not exist. Using default device %s", device, qPrintable(devInfo.deviceName()));
} }
} }
@ -80,12 +80,13 @@ bool AudioInput::start(int device, int rate)
m_audioFormat.setSampleSize(16); m_audioFormat.setSampleSize(16);
m_audioFormat.setCodec("audio/pcm"); m_audioFormat.setCodec("audio/pcm");
m_audioFormat.setByteOrder(QAudioFormat::LittleEndian); m_audioFormat.setByteOrder(QAudioFormat::LittleEndian);
m_audioFormat.setSampleType(QAudioFormat::SignedInt); m_audioFormat.setSampleType(QAudioFormat::SignedInt); // Unknown, SignedInt, UnSignedInt, Float
if (!devInfo.isFormatSupported(m_audioFormat)) if (!devInfo.isFormatSupported(m_audioFormat))
{ {
m_audioFormat = devInfo.nearestFormat(m_audioFormat); m_audioFormat = devInfo.nearestFormat(m_audioFormat);
qWarning("AudioInput::start: %d Hz S16_LE audio format not supported. New rate: %d", rate, m_audioFormat.sampleRate()); qWarning("AudioInput::start: %d Hz S16_LE audio format not supported. Nearest is sampleRate: %d channelCount: %d sampleSize: %d sampleType: %d",
rate, m_audioFormat.sampleRate(), m_audioFormat.channelCount(), m_audioFormat.sampleSize(), (int) m_audioFormat.sampleType());
} }
else else
{ {