mirror of
				https://github.com/f4exb/sdrangel.git
				synced 2025-10-30 12:30:20 -04:00 
			
		
		
		
	FCDProPlus: use Qt for FCD audio device handling. Fixes start up error
This commit is contained in:
		
							parent
							
								
									865d26cc15
								
							
						
					
					
						commit
						a10b29d6d3
					
				| @ -1,15 +1,27 @@ | ||||
| /*
 | ||||
|  * fcdtraits.cpp | ||||
|  * | ||||
|  *  Created on: Sep 5, 2015 | ||||
|  *      Author: f4exb | ||||
|  */ | ||||
| ///////////////////////////////////////////////////////////////////////////////////
 | ||||
| // Copyright (C) 2015-2018 Edouard Griffiths, F4EXB                              //
 | ||||
| //                                                                               //
 | ||||
| // 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          //
 | ||||
| // 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" | ||||
| 
 | ||||
| const char *fcd_traits<Pro>::alsaDeviceName = "hw:CARD=V10"; | ||||
| 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<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<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<ProPlus>::loLowLimitFreq = 150000L; | ||||
|  | ||||
| @ -1,9 +1,18 @@ | ||||
| /*
 | ||||
|  * fcdtraits.h | ||||
|  * | ||||
|  *  Created on: 3 Sep 2015 | ||||
|  *      Author: egriffiths | ||||
|  */ | ||||
| ///////////////////////////////////////////////////////////////////////////////////
 | ||||
| // Copyright (C) 2015-2018 Edouard Griffiths, F4EXB                              //
 | ||||
| //                                                                               //
 | ||||
| // 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          //
 | ||||
| // 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_ | ||||
| #define FCDLIB_FCDTRAITS_H_ | ||||
| @ -25,6 +34,7 @@ struct fcd_traits | ||||
| 	static const int convBufSize = (1<<11); | ||||
| 	static const int fcdBufSize = (1<<12); | ||||
| 	static const char *alsaDeviceName; | ||||
| 	static const char *qtDeviceName; | ||||
|     static const char *hardwareID; | ||||
|     static const char *interfaceIID; | ||||
|     static const char *displayedName; | ||||
| @ -43,6 +53,7 @@ struct fcd_traits<Pro> | ||||
| 	static const int convBufSize = (1<<11); | ||||
| 	static const int fcdBufSize = (1<<12); | ||||
| 	static const char *alsaDeviceName; | ||||
|     static const char *qtDeviceName; | ||||
|     static const char *hardwareID; | ||||
|     static const char *interfaceIID; | ||||
|     static const char *displayedName; | ||||
| @ -58,9 +69,10 @@ struct fcd_traits<ProPlus> | ||||
| 	static const uint16_t vendorId = 0x04D8; | ||||
| 	static const uint16_t productId = 0xFB31; | ||||
| 	static const int sampleRate = 192000; | ||||
| 	static const int convBufSize = (1<<12); | ||||
| 	static const int fcdBufSize = (1<<18); | ||||
| 	static const int convBufSize = (1<<10); | ||||
| 	static const int fcdBufSize = (1<<18); // used in FCDProPlusReader
 | ||||
| 	static const char *alsaDeviceName; | ||||
|     static const char *qtDeviceName; | ||||
|     static const char *hardwareID; | ||||
|     static const char *interfaceIID; | ||||
|     static const char *displayedName; | ||||
|  | ||||
| @ -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          //
 | ||||
| // 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/>.          //
 | ||||
| ///////////////////////////////////////////////////////////////////////////////////
 | ||||
| 
 | ||||
| // FIXME: FCD is handled very badly!
 | ||||
| 
 | ||||
| #include <QDebug> | ||||
| #include <string.h> | ||||
| #include <errno.h> | ||||
| @ -47,6 +45,7 @@ FCDProPlusInput::FCDProPlusInput(DeviceSourceAPI *deviceAPI) : | ||||
| 	m_deviceDescription(fcd_traits<ProPlus>::displayedName), | ||||
| 	m_running(false) | ||||
| { | ||||
|     m_fcdFIFO.setSize(4*fcd_traits<ProPlus>::convBufSize); | ||||
|     openDevice(); | ||||
|     m_fileSink = new FileRecord(QString("test_%1.sdriq").arg(m_deviceAPI->getDeviceUID())); | ||||
|     m_deviceAPI->addSink(m_fileSink); | ||||
| @ -113,17 +112,17 @@ bool FCDProPlusInput::start() | ||||
| 		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; | ||||
| 	} | ||||
|     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(); | ||||
| 
 | ||||
| //	mutexLocker.unlock();
 | ||||
| @ -145,73 +144,33 @@ void FCDProPlusInput::closeDevice() | ||||
|     m_dev = 0; | ||||
| } | ||||
| 
 | ||||
| bool FCDProPlusInput::openSource(const char* cardname) | ||||
| bool FCDProPlusInput::openFCDAudio(const char* cardname) | ||||
| { | ||||
|     bool fail = false; | ||||
|     snd_pcm_hw_params_t* params; | ||||
|     //fcd_rate = FCDPP_RATE;
 | ||||
|     //fcd_channels =2;
 | ||||
|     //fcd_format = SND_PCM_SFMT_U16_LE;
 | ||||
|     snd_pcm_stream_t fcd_stream = SND_PCM_STREAM_CAPTURE; | ||||
|     AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager(); | ||||
|     const QList<QAudioDeviceInfo>& audioList = audioDeviceManager->getInputDevices(); | ||||
| 
 | ||||
|     if (fcd_handle) | ||||
|     for (const auto &itAudio : audioList) | ||||
|     { | ||||
|         qDebug("FCDProPlusInput::OpenSource: already opened"); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     if (snd_pcm_open(&fcd_handle, cardname, fcd_stream, 0) < 0) | ||||
|         if (itAudio.deviceName().contains(QString(cardname))) | ||||
|         { | ||||
|         qCritical("FCDProPlusInput::OpenSource: cannot open %s", cardname); | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     snd_pcm_hw_params_alloca(¶ms); | ||||
| 
 | ||||
|     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"); | ||||
|             fail = true; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             qDebug("FCDProPlusInput::OpenSource: snd_pcm_start OK"); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     if (fail) | ||||
|     { | ||||
|         snd_pcm_close(fcd_handle); | ||||
|         return false; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         qDebug("FCDProPlusInput::OpenSource: Funcube stream started"); | ||||
|     } | ||||
| 
 | ||||
|             int fcdDeviceIndex = audioDeviceManager->getInputDeviceIndex(itAudio.deviceName()); | ||||
|             m_fcdAudioInput.start(fcdDeviceIndex, fcd_traits<ProPlus>::sampleRate); | ||||
|             int fcdSampleRate = m_fcdAudioInput.getRate(); | ||||
|             qDebug("FCDProPlusInput::openFCDAudio: %s index %d at %d S/s", | ||||
|                     itAudio.deviceName().toStdString().c_str(), fcdDeviceIndex, fcdSampleRate); | ||||
|             m_fcdAudioInput.addFifo(&m_fcdFIFO); | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
| void FCDProPlusInput::closeSource() | ||||
| { | ||||
|     if (fcd_handle) { | ||||
|         snd_pcm_close(fcd_handle); | ||||
|     } | ||||
| 
 | ||||
|     fcd_handle = nullptr; | ||||
|     qCritical("FCDProPlusInput::openFCDAudio: device with name %s not found", cardname); | ||||
|     return false; | ||||
| } | ||||
| 
 | ||||
| void FCDProPlusInput::closeFCDAudio() | ||||
| { | ||||
|     m_fcdAudioInput.removeFifo(&m_fcdFIFO); | ||||
|     m_fcdAudioInput.stop(); | ||||
| } | ||||
| 
 | ||||
| void FCDProPlusInput::stop() | ||||
| @ -224,7 +183,7 @@ void FCDProPlusInput::stop() | ||||
| 		// wait for thread to quit ?
 | ||||
| 		delete m_FCDThread; | ||||
| 		m_FCDThread = nullptr; | ||||
| 		closeSource(); | ||||
| 		closeFCDAudio(); | ||||
| 	} | ||||
| 
 | ||||
| 	m_running = false; | ||||
|  | ||||
| @ -23,7 +23,10 @@ | ||||
| 
 | ||||
| #include <alsa/asoundlib.h> | ||||
| 
 | ||||
| #include <dsp/devicesamplesource.h> | ||||
| #include "dsp/devicesamplesource.h" | ||||
| #include "audio/audioinput.h" | ||||
| #include "audio/audiofifo.h" | ||||
| 
 | ||||
| #include "fcdproplussettings.h" | ||||
| #include "fcdhid.h" | ||||
| 
 | ||||
| @ -149,13 +152,15 @@ public: | ||||
| private: | ||||
|     bool openDevice(); | ||||
|     void closeDevice(); | ||||
|     bool openSource(const char *filename); | ||||
|     void closeSource(); | ||||
|     bool openFCDAudio(const char *filename); | ||||
|     void closeFCDAudio(); | ||||
| 	void applySettings(const FCDProPlusSettings& settings, bool force); | ||||
|     void webapiFormatDeviceSettings(SWGSDRangel::SWGDeviceSettings& response, const FCDProPlusSettings& settings); | ||||
| 
 | ||||
| 	DeviceSourceAPI *m_deviceAPI; | ||||
| 	hid_device *m_dev; | ||||
| 	AudioInput m_fcdAudioInput; | ||||
| 	AudioFifo m_fcdFIFO; | ||||
| 	snd_pcm_t* fcd_handle; | ||||
| 	QMutex m_mutex; | ||||
| 	FCDProPlusSettings m_settings; | ||||
|  | ||||
| @ -1,6 +1,5 @@ | ||||
| ///////////////////////////////////////////////////////////////////////////////////
 | ||||
| // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
 | ||||
| // written by Christian Daniel                                                   //
 | ||||
| // Copyright (C) 2016-2018 Edouard Griffiths, F4EXB                              //
 | ||||
| //                                                                               //
 | ||||
| // 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          //
 | ||||
| @ -21,13 +20,13 @@ | ||||
| #include "fcdproplusthread.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), | ||||
| 	m_fcd_handle(fcd_handle), | ||||
| 	m_fcdFIFO(fcdFIFO), | ||||
| 	m_running(false), | ||||
| 	m_convertBuffer(fcd_traits<ProPlus>::convBufSize), | ||||
| 	m_convertBuffer(fcd_traits<ProPlus>::convBufSize), // nb samples
 | ||||
| 	m_sampleFifo(sampleFifo) | ||||
| { | ||||
| 	start(); | ||||
| @ -62,43 +61,18 @@ void FCDProPlusThread::run() | ||||
| 	m_running = true; | ||||
| 	qDebug("FCDThread::run: start running loop"); | ||||
| 
 | ||||
| 	while (m_running) | ||||
| 	{ | ||||
| 		if (work(fcd_traits<ProPlus>::convBufSize) < 0) { | ||||
| 			break; | ||||
| 		} | ||||
| 	while (m_running) { | ||||
| 	    work(fcd_traits<ProPlus>::convBufSize); | ||||
| 	} | ||||
| 
 | ||||
| 	qDebug("FCDThread::run: running loop stopped"); | ||||
| 	m_running = false; | ||||
| } | ||||
| 
 | ||||
| int FCDProPlusThread::work(int n_items) | ||||
| void FCDProPlusThread::work(unsigned int n_items) | ||||
| { | ||||
| 	int l; | ||||
| 	SampleVector::iterator it; | ||||
| 	void *out; | ||||
| 
 | ||||
| 	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; | ||||
|     uint32_t nbRead = m_fcdFIFO->read((unsigned char *) m_buf, n_items); // number of samples
 | ||||
|     SampleVector::iterator it = m_convertBuffer.begin(); | ||||
|     m_decimators.decimate1(&it, m_buf, 2*nbRead); | ||||
|     m_sampleFifo->write(m_convertBuffer.begin(), it); | ||||
| } | ||||
|  | ||||
| @ -21,32 +21,36 @@ | ||||
| #include <QThread> | ||||
| #include <QMutex> | ||||
| #include <QWaitCondition> | ||||
| #include <alsa/asoundlib.h> | ||||
| 
 | ||||
| #include "dsp/inthalfbandfilter.h" | ||||
| #include "dsp/samplesinkfifo.h" | ||||
| #include "dsp/decimators.h" | ||||
| #include "fcdtraits.h" | ||||
| 
 | ||||
| class AudioFifo; | ||||
| 
 | ||||
| class FCDProPlusThread : public QThread { | ||||
| 	Q_OBJECT | ||||
| 
 | ||||
| public: | ||||
| 	FCDProPlusThread(SampleSinkFifo* sampleFifo, snd_pcm_t *fcd_handle, QObject* parent = nullptr); | ||||
| 	FCDProPlusThread(SampleSinkFifo* sampleFifo, AudioFifo *fcdFIFO, QObject* parent = nullptr); | ||||
| 	~FCDProPlusThread(); | ||||
| 
 | ||||
| 	void startWork(); | ||||
| 	void stopWork(); | ||||
| 
 | ||||
| private: | ||||
| 	snd_pcm_t* m_fcd_handle; | ||||
| 	AudioFifo* m_fcdFIFO; | ||||
| 
 | ||||
| 	QMutex m_startWaitMutex; | ||||
| 	QWaitCondition m_startWaiter; | ||||
| 	bool m_running; | ||||
| 
 | ||||
| 	qint16 m_buf[fcd_traits<ProPlus>::convBufSize*2]; // stereo (I, Q)
 | ||||
| 	SampleVector m_convertBuffer; | ||||
| 	SampleSinkFifo* m_sampleFifo; | ||||
| 	Decimators<qint32, qint16, SDR_RX_SAMP_SZ, 16> m_decimators; | ||||
| 
 | ||||
| 	void run(); | ||||
| 	int work(int n_items); | ||||
| 	void work(unsigned int n_items); | ||||
| }; | ||||
| #endif // INCLUDE_FCDTHREAD_H
 | ||||
|  | ||||
| @ -64,12 +64,12 @@ bool AudioInput::start(int device, int rate) | ||||
|             if (device < devicesInfo.size()) | ||||
|             { | ||||
|                 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 | ||||
|             { | ||||
|                 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.setCodec("audio/pcm"); | ||||
|         m_audioFormat.setByteOrder(QAudioFormat::LittleEndian); | ||||
|         m_audioFormat.setSampleType(QAudioFormat::SignedInt); | ||||
|         m_audioFormat.setSampleType(QAudioFormat::SignedInt); // Unknown, SignedInt, UnSignedInt, Float
 | ||||
| 
 | ||||
|         if (!devInfo.isFormatSupported(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 | ||||
|         { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user