| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2015 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 <stdio.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  | #include <assert.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 10:02:36 +02:00
										 |  |  | #include "dsp/filerecord.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | #include "filesourcethread.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-08 10:02:36 +02:00
										 |  |  | #include "dsp/samplesinkfifo.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 19:18:02 +02:00
										 |  |  | FileSourceThread::FileSourceThread(std::ifstream *samplesStream, SampleSinkFifo* sampleFifo, QObject* parent) : | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 	QThread(parent), | 
					
						
							|  |  |  | 	m_running(false), | 
					
						
							|  |  |  | 	m_ifstream(samplesStream), | 
					
						
							|  |  |  | 	m_buf(0), | 
					
						
							|  |  |  | 	m_bufsize(0), | 
					
						
							|  |  |  | 	m_chunksize(0), | 
					
						
							|  |  |  | 	m_sampleFifo(sampleFifo), | 
					
						
							| 
									
										
										
										
											2015-08-06 01:14:44 +02:00
										 |  |  | 	m_samplesCount(0), | 
					
						
							| 
									
										
										
										
											2016-03-14 01:43:21 +01:00
										 |  |  |     m_samplerate(0), | 
					
						
							|  |  |  |     m_throttlems(FILESOURCE_THROTTLE_MS), | 
					
						
							|  |  |  |     m_throttleToggle(false) | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |     assert(m_ifstream != 0); | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FileSourceThread::~FileSourceThread() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (m_running) { | 
					
						
							|  |  |  | 		stopWork(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (m_buf != 0) { | 
					
						
							|  |  |  | 		free(m_buf); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSourceThread::startWork() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | 	qDebug() << "FileSourceThread::startWork: "; | 
					
						
							| 
									
										
										
										
											2016-10-08 10:02:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |     if (m_ifstream->is_open()) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:07:39 +01:00
										 |  |  |         qDebug() << "FileSourceThread::startWork: file stream open, starting..."; | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |         m_startWaitMutex.lock(); | 
					
						
							| 
									
										
										
										
											2016-03-14 01:43:21 +01:00
										 |  |  |         m_elapsedTimer.start(); | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |         start(); | 
					
						
							|  |  |  |         while(!m_running) | 
					
						
							|  |  |  |             m_startWaiter.wait(&m_startWaitMutex, 100); | 
					
						
							|  |  |  |         m_startWaitMutex.unlock(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:07:39 +01:00
										 |  |  |         qDebug() << "FileSourceThread::startWork: file stream closed, not starting."; | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSourceThread::stopWork() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | 	qDebug() << "FileSourceThread::stopWork"; | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 	m_running = false; | 
					
						
							|  |  |  | 	wait(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSourceThread::setSamplerate(int samplerate) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | 	qDebug() << "FileSourceThread::setSamplerate:" | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  | 			<< " new:" << samplerate | 
					
						
							|  |  |  | 			<< " old:" << m_samplerate; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 	if (samplerate != m_samplerate) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (m_running) { | 
					
						
							|  |  |  | 			stopWork(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  | 		m_samplerate = samplerate; | 
					
						
							| 
									
										
										
										
											2016-03-14 01:43:21 +01:00
										 |  |  |         // TODO: implement FF and slow motion here. 4 corresponds to live. 2 is half speed, 8 is doulbe speed
 | 
					
						
							|  |  |  |         m_chunksize = (m_samplerate * 4 * m_throttlems) / 1000; | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 01:43:21 +01:00
										 |  |  |         setBuffer(m_chunksize); | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 00:10:23 +02:00
										 |  |  | 	//m_samplerate = samplerate;
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 08:21:06 +01:00
										 |  |  | void FileSourceThread::setBuffer(std::size_t chunksize) | 
					
						
							| 
									
										
										
										
											2016-03-14 01:43:21 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (chunksize > m_bufsize) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_bufsize = chunksize; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_buf == 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug() << "FileSourceThread::setBuffer: Allocate buffer"; | 
					
						
							|  |  |  |             m_buf = (quint8*) malloc(m_bufsize); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug() << "FileSourceThread::setBuffer: Re-allocate buffer"; | 
					
						
							|  |  |  |             m_buf = (quint8*) realloc((void*) m_buf, m_bufsize); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         qDebug() << "FileSourceThread::setBuffer: size: " << m_bufsize | 
					
						
							|  |  |  |                 << " #samples: " << (m_bufsize/4); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | void FileSourceThread::run() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_running = true; | 
					
						
							|  |  |  | 	m_startWaiter.wakeAll(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  | 	while(m_running) // actual work is in the tick() function
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		sleep(1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_running = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSourceThread::connectTimer(const QTimer& timer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | 	qDebug() << "FileSourceThread::connectTimer"; | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 	connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSourceThread::tick() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (m_running) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2016-03-14 01:43:21 +01:00
										 |  |  |         qint64 throttlems = m_elapsedTimer.restart(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (throttlems != m_throttlems) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             m_throttlems = throttlems; | 
					
						
							| 
									
										
										
										
											2016-10-07 18:16:17 +02:00
										 |  |  |             m_chunksize = 4 * ((m_samplerate * (m_throttlems+(m_throttleToggle ? 1 : 0))) / 1000); | 
					
						
							| 
									
										
										
										
											2016-03-14 01:43:21 +01:00
										 |  |  |             m_throttleToggle = !m_throttleToggle; | 
					
						
							|  |  |  |             setBuffer(m_chunksize); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 		// read samples directly feeding the SampleFifo (no callback)
 | 
					
						
							|  |  |  | 		m_ifstream->read(reinterpret_cast<char*>(m_buf), m_chunksize); | 
					
						
							| 
									
										
										
										
											2016-10-08 10:02:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |         if (m_ifstream->eof()) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             m_sampleFifo->write(m_buf, m_ifstream->gcount()); | 
					
						
							|  |  |  |             // TODO: handle loop playback situation
 | 
					
						
							| 
									
										
										
										
											2015-08-05 09:26:00 +02:00
										 |  |  |     		m_ifstream->clear(); | 
					
						
							| 
									
										
										
										
											2016-10-08 10:02:36 +02:00
										 |  |  |             m_ifstream->seekg(sizeof(FileRecord::Header), std::ios::beg); | 
					
						
							| 
									
										
										
										
											2015-08-06 01:14:44 +02:00
										 |  |  |     		m_samplesCount = 0; | 
					
						
							| 
									
										
										
										
											2015-08-05 09:26:00 +02:00
										 |  |  |             //stopWork();
 | 
					
						
							| 
									
										
										
										
											2015-08-05 03:25:06 +02:00
										 |  |  |             //m_ifstream->close();
 | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             m_sampleFifo->write(m_buf, m_chunksize); | 
					
						
							| 
									
										
										
										
											2015-08-06 01:14:44 +02:00
										 |  |  |     		m_samplesCount += m_chunksize / 4; | 
					
						
							| 
									
										
										
										
											2015-08-04 04:17:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-08-03 01:04:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |