| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-18 09:58:55 +01:00
										 |  |  | // Copyright (C) 2016-2017, 2019-2020 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
 | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 06:39:30 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 <stdio.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2017-01-03 23:22:06 +01:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  | #include "dsp/samplesourcefifo.h"
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | #include "fileoutputworker.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | FileOutputWorker::FileOutputWorker(std::ofstream *samplesStream, SampleSourceFifo* sampleFifo, QObject* parent) : | 
					
						
							| 
									
										
										
										
											2020-07-12 10:22:24 +02:00
										 |  |  | 	QObject(parent), | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | 	m_running(false), | 
					
						
							|  |  |  | 	m_ofstream(samplesStream), | 
					
						
							|  |  |  | 	m_bufsize(0), | 
					
						
							|  |  |  | 	m_samplesChunkSize(0), | 
					
						
							|  |  |  | 	m_sampleFifo(sampleFifo), | 
					
						
							| 
									
										
										
										
											2016-10-19 22:32:14 +02:00
										 |  |  | 	m_samplesCount(0), | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  |     m_samplerate(0), | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  |     m_log2Interpolation(0), | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  |     m_throttlems(FILEOUTPUT_THROTTLE_MS), | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  |     m_maxThrottlems(50), | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  |     m_throttleToggle(false), | 
					
						
							| 
									
										
										
										
											2020-07-12 10:22:24 +02:00
										 |  |  |     m_buf(nullptr) | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | FileOutputWorker::~FileOutputWorker() | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (m_running) { | 
					
						
							|  |  |  | 		stopWork(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_buf) delete[] m_buf; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | void FileOutputWorker::startWork() | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | 	qDebug() << "FileOutputWorker::startWork: "; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_ofstream->is_open()) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  |         qDebug() << "FileOutputWorker::startWork: file stream open, starting..."; | 
					
						
							| 
									
										
										
										
											2016-12-20 00:26:43 +01:00
										 |  |  |         m_maxThrottlems = 0; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  |         m_elapsedTimer.start(); | 
					
						
							| 
									
										
										
										
											2020-07-12 10:22:24 +02:00
										 |  |  |         m_running = true; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  |         qDebug() << "FileOutputWorker::startWork: file stream closed, not starting."; | 
					
						
							| 
									
										
										
										
											2020-07-12 10:22:24 +02:00
										 |  |  |         m_running = false; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | void FileOutputWorker::stopWork() | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	m_running = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | void FileOutputWorker::setSamplerate(int samplerate) | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (samplerate != m_samplerate) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | 	    qDebug() << "FileOutputWorker::setSamplerate:" | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  | 	            << " new:" << samplerate | 
					
						
							|  |  |  | 	            << " old:" << m_samplerate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	    bool wasRunning = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (m_running) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | 			stopWork(); | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  | 			wasRunning = true; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 02:34:29 +02:00
										 |  |  | 		// resize sample FIFO
 | 
					
						
							|  |  |  | 		if (m_sampleFifo) { | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  | 		    m_sampleFifo->resize(SampleSourceFifo::getSizePolicy(samplerate)); // 1s buffer
 | 
					
						
							| 
									
										
										
										
											2016-10-25 02:34:29 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  |         // resize output buffer
 | 
					
						
							|  |  |  |         if (m_buf) delete[] m_buf; | 
					
						
							|  |  |  |         m_buf = new int16_t[samplerate*(1<<m_log2Interpolation)*2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_samplerate = samplerate; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  |         m_samplesChunkSize = (m_samplerate * m_throttlems) / 1000; | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (wasRunning) { | 
					
						
							|  |  |  |             startWork(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | void FileOutputWorker::setLog2Interpolation(int log2Interpolation) | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if ((log2Interpolation < 0) || (log2Interpolation > 6)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (log2Interpolation != m_log2Interpolation) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  |         qDebug() << "FileOutputWorker::setLog2Interpolation:" | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  |                 << " new:" << log2Interpolation | 
					
						
							|  |  |  |                 << " old:" << m_log2Interpolation; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool wasRunning = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_running) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             stopWork(); | 
					
						
							|  |  |  |             wasRunning = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // resize output buffer
 | 
					
						
							|  |  |  |         if (m_buf) delete[] m_buf; | 
					
						
							|  |  |  |         m_buf = new int16_t[m_samplerate*(1<<log2Interpolation)*2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_log2Interpolation = log2Interpolation; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (wasRunning) { | 
					
						
							|  |  |  |             startWork(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | void FileOutputWorker::connectTimer(const QTimer& timer) | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | 	qDebug() << "FileOutputWorker::connectTimer"; | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | 	connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | void FileOutputWorker::tick() | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (m_running) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  |         qint64 throttlems = m_elapsedTimer.restart(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (throttlems != m_throttlems) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             m_throttlems = throttlems; | 
					
						
							|  |  |  |             m_samplesChunkSize = (m_samplerate * (m_throttlems+(m_throttleToggle ? 1 : 0))) / 1000; | 
					
						
							|  |  |  |             m_throttleToggle = !m_throttleToggle; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |         unsigned int iPart1Begin, iPart1End, iPart2Begin, iPart2End; | 
					
						
							|  |  |  |         SampleVector& data = m_sampleFifo->getData(); | 
					
						
							|  |  |  |         m_sampleFifo->read(m_samplesChunkSize, iPart1Begin, iPart1End, iPart2Begin, iPart2End); | 
					
						
							| 
									
										
										
										
											2016-10-19 22:32:14 +02:00
										 |  |  |         m_samplesCount += m_samplesChunkSize; | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |         if (iPart1Begin != iPart1End) { | 
					
						
							|  |  |  |             callbackPart(data, iPart1Begin, iPart1End); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (iPart2Begin != iPart2End) { | 
					
						
							|  |  |  |             callbackPart(data, iPart2Begin, iPart2End); | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 00:29:15 +02:00
										 |  |  | void FileOutputWorker::callbackPart(SampleVector& data, unsigned int iBegin, unsigned int iEnd) | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     SampleVector::iterator beginRead = data.begin() + iBegin; | 
					
						
							|  |  |  |     unsigned int chunkSize = iEnd - iBegin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_log2Interpolation == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_ofstream->write(reinterpret_cast<char*>(&(*beginRead)), chunkSize*sizeof(Sample)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         switch (m_log2Interpolation) | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |         case 1: | 
					
						
							|  |  |  |             m_interpolators.interpolate2_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 2: | 
					
						
							|  |  |  |             m_interpolators.interpolate4_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 3: | 
					
						
							|  |  |  |             m_interpolators.interpolate8_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 4: | 
					
						
							|  |  |  |             m_interpolators.interpolate16_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 5: | 
					
						
							|  |  |  |             m_interpolators.interpolate32_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 6: | 
					
						
							|  |  |  |             m_interpolators.interpolate64_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2017-01-02 03:14:46 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 01:04:24 +01:00
										 |  |  |         m_ofstream->write(reinterpret_cast<char*>(m_buf), chunkSize*(1<<m_log2Interpolation)*2*sizeof(int16_t)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-19 18:42:57 +02:00
										 |  |  | } |