| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | #include "dsp/filesink.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | #include "dsp/dspcommands.h"
 | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | #include "util/simpleserializer.h"
 | 
					
						
							|  |  |  | #include "util/messagequeue.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(FileSink::MsgConfigureFileSink, Message) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FileSink::FileSink() : | 
					
						
							|  |  |  | 	SampleSink(), | 
					
						
							|  |  |  |     m_fileName(std::string("test.sdriq")), | 
					
						
							|  |  |  |     m_sampleRate(0), | 
					
						
							|  |  |  |     m_centerFrequency(0), | 
					
						
							|  |  |  | 	m_recordOn(false), | 
					
						
							|  |  |  |     m_recordStart(false), | 
					
						
							|  |  |  |     m_byteCount(0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-12 09:03:02 +02:00
										 |  |  | 	setObjectName("FileSink"); | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FileSink::~FileSink() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     stopRecording(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | void FileSink::configure(MessageQueue* msgQueue, const std::string& filename) | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | 	Message* cmd = MsgConfigureFileSink::create(filename); | 
					
						
							|  |  |  | 	msgQueue->push(cmd); | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-25 08:24:23 +02:00
										 |  |  | void FileSink::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly) | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // if no recording is active, send the samples to /dev/null
 | 
					
						
							|  |  |  |     if(!m_recordOn) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (begin < end) // if there is something to put out
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_recordStart) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             writeHeader(); | 
					
						
							|  |  |  |             m_recordStart = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_sampleFile.write(reinterpret_cast<const char*>(&*(begin)), (end - begin)*sizeof(Sample)); | 
					
						
							|  |  |  |         m_byteCount += end - begin; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSink::start() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSink::stop() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     stopRecording(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSink::startRecording() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!m_sampleFile.is_open()) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  |     	qDebug() << "FileSink::startRecording"; | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  |         m_sampleFile.open(m_fileName.c_str(), std::ios::binary); | 
					
						
							|  |  |  |         m_recordOn = true; | 
					
						
							|  |  |  |         m_recordStart = true; | 
					
						
							|  |  |  |         m_byteCount = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSink::stopRecording() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_sampleFile.is_open()) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  |     	qDebug() << "FileSink::stopRecording"; | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  |         m_sampleFile.close(); | 
					
						
							|  |  |  |         m_recordOn = false; | 
					
						
							|  |  |  |         m_recordStart = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | bool FileSink::handleMessage(const Message& message) | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	qDebug() << "FileSink::handleMessage"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (DSPSignalNotification::match(message)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		DSPSignalNotification& notif = (DSPSignalNotification&) message; | 
					
						
							|  |  |  | 		m_sampleRate = notif.getSampleRate(); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:52 +02:00
										 |  |  | 		m_centerFrequency = notif.getCenterFrequency(); | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  | 		qDebug() << "FileSink::handleMessage: DSPSignalNotification: m_inputSampleRate: " << m_sampleRate | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 				<< " m_centerFrequency: " << m_centerFrequency; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (MsgConfigureFileSink::match(message)) | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  |         MsgConfigureFileSink& conf = (MsgConfigureFileSink&) message; | 
					
						
							|  |  |  |         handleConfigure(conf.getFileName()); | 
					
						
							| 
									
										
										
										
											2015-08-24 00:51:27 +02:00
										 |  |  |         qDebug() << "FileSink::handleMessage: MsgConfigureFileSink: fileName: " << m_fileName.c_str(); | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  | void FileSink::handleConfigure(const std::string& fileName) | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-14 05:00:28 +02:00
										 |  |  |     if (fileName != m_fileName) | 
					
						
							| 
									
										
										
										
											2015-07-28 23:54:17 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         stopRecording(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | 	m_fileName = fileName; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSink::writeHeader() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_sampleFile.write((const char *) &m_sampleRate, sizeof(int)); | 
					
						
							|  |  |  |     m_sampleFile.write((const char *) &m_centerFrequency, sizeof(quint64)); | 
					
						
							|  |  |  |     std::time_t ts = time(0); | 
					
						
							|  |  |  |     m_sampleFile.write((const char *) &ts, sizeof(std::time_t)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FileSink::readHeader(std::ifstream& sampleFile, Header& header) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     sampleFile.read((char *) &(header.sampleRate), sizeof(int)); | 
					
						
							|  |  |  |     sampleFile.read((char *) &(header.centerFrequency), sizeof(quint64)); | 
					
						
							|  |  |  |     sampleFile.read((char *) &(header.startTimeStamp), sizeof(std::time_t)); | 
					
						
							|  |  |  | } |