mirror of
				https://github.com/f4exb/sdrangel.git
				synced 2025-11-03 21:20:31 -05:00 
			
		
		
		
	FileRecord improvement: CRC check and sample size fix
This commit is contained in:
		
							parent
							
								
									6269125d2c
								
							
						
					
					
						commit
						cc49d5c266
					
				@ -16,6 +16,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <QDebug>
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "SWGDeviceSettings.h"
 | 
					#include "SWGDeviceSettings.h"
 | 
				
			||||||
@ -87,14 +88,22 @@ void FileSourceInput::openFileStream()
 | 
				
			|||||||
        // TODO: add CRC
 | 
					        // TODO: add CRC
 | 
				
			||||||
	    m_ifstream.seekg(0,std::ios_base::beg);
 | 
						    m_ifstream.seekg(0,std::ios_base::beg);
 | 
				
			||||||
	    FileRecord::Header header;
 | 
						    FileRecord::Header header;
 | 
				
			||||||
	    FileRecord::readHeader(m_ifstream, header);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	    m_sampleRate = header.sampleRate;
 | 
						    if (FileRecord::readHeader(m_ifstream, header)) // CRC OK
 | 
				
			||||||
	    m_centerFrequency = header.centerFrequency;
 | 
						    {
 | 
				
			||||||
	    m_startingTimeStamp = header.startTimeStamp;
 | 
						        m_sampleRate = header.sampleRate;
 | 
				
			||||||
	    m_sampleSize = header.sampleSize;
 | 
						        m_centerFrequency = header.centerFrequency;
 | 
				
			||||||
 | 
						        m_startingTimeStamp = header.startTimeStamp;
 | 
				
			||||||
 | 
						        m_sampleSize = header.sampleSize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						        m_recordLength = (fileSize - sizeof(FileRecord::Header)) / ((m_sampleSize == 24 ? 8 : 4) * m_sampleRate);
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
						    else
 | 
				
			||||||
 | 
						    {
 | 
				
			||||||
 | 
						        qCritical("FileSourceInput::openFileStream: bad CRC header");
 | 
				
			||||||
 | 
						        m_recordLength = 0;
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	    m_recordLength = (fileSize - sizeof(FileRecord::Header)) / (4 * m_sampleRate);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
				
			|||||||
@ -1,10 +1,30 @@
 | 
				
			|||||||
#include <dsp/filerecord.h>
 | 
					///////////////////////////////////////////////////////////////////////////////////
 | 
				
			||||||
 | 
					// 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 <boost/crc.hpp>
 | 
				
			||||||
 | 
					#include <boost/cstdint.hpp>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					#include <QDateTime>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "dsp/dspcommands.h"
 | 
					#include "dsp/dspcommands.h"
 | 
				
			||||||
#include "util/simpleserializer.h"
 | 
					#include "util/simpleserializer.h"
 | 
				
			||||||
#include "util/message.h"
 | 
					#include "util/message.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <QDebug>
 | 
					#include "filerecord.h"
 | 
				
			||||||
#include <QDateTime>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
FileRecord::FileRecord() :
 | 
					FileRecord::FileRecord() :
 | 
				
			||||||
	BasebandSampleSink(),
 | 
						BasebandSampleSink(),
 | 
				
			||||||
@ -128,21 +148,24 @@ void FileRecord::handleConfigure(const QString& fileName)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void FileRecord::writeHeader()
 | 
					void FileRecord::writeHeader()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    m_sampleFile.write((const char *) &m_sampleRate, sizeof(qint32));         // 4 bytes
 | 
					    Header header;
 | 
				
			||||||
    m_sampleFile.write((const char *) &m_centerFrequency, sizeof(quint64));   // 8 bytes
 | 
					    header.sampleRate = m_sampleRate;
 | 
				
			||||||
 | 
					    header.centerFrequency = m_centerFrequency;
 | 
				
			||||||
    std::time_t ts = time(0);
 | 
					    std::time_t ts = time(0);
 | 
				
			||||||
    m_sampleFile.write((const char *) &ts, sizeof(std::time_t));              // 8 bytes
 | 
					    header.startTimeStamp = ts;
 | 
				
			||||||
    quint32 sampleSize = SDR_RX_SAMP_SZ;
 | 
					    header.sampleSize = SDR_RX_SAMP_SZ;
 | 
				
			||||||
    m_sampleFile.write((const char *) &sampleSize, sizeof(int));              // 4 bytes
 | 
					    header.filler = 0;
 | 
				
			||||||
 | 
					    boost::crc_32_type crc32;
 | 
				
			||||||
 | 
					    crc32.process_bytes(&header, 28);
 | 
				
			||||||
 | 
					    header.crc32 = crc32.checksum();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    m_sampleFile.write((const char *) &header, sizeof(Header));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void FileRecord::readHeader(std::ifstream& sampleFile, Header& header)
 | 
					bool FileRecord::readHeader(std::ifstream& sampleFile, Header& header)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    sampleFile.read((char *) &(header.sampleRate), sizeof(qint32));
 | 
					    sampleFile.read((char *) &header, sizeof(Header));
 | 
				
			||||||
    sampleFile.read((char *) &(header.centerFrequency), sizeof(quint64));
 | 
					    boost::crc_32_type crc32;
 | 
				
			||||||
    sampleFile.read((char *) &(header.startTimeStamp), sizeof(std::time_t));
 | 
					    crc32.process_bytes(&header, 28);
 | 
				
			||||||
    sampleFile.read((char *) &(header.sampleSize), sizeof(quint32));
 | 
					    return header.crc32 == crc32.checksum();
 | 
				
			||||||
    if ((header.sampleSize != 16) && (header.sampleSize != 24)) { // assume 16 bits if garbage (old I/Q file)
 | 
					 | 
				
			||||||
    	header.sampleSize = 16;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,21 @@
 | 
				
			|||||||
#ifndef INCLUDE_FILESINK_H
 | 
					///////////////////////////////////////////////////////////////////////////////////
 | 
				
			||||||
#define INCLUDE_FILESINK_H
 | 
					// 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 INCLUDE_FILERECORD_H
 | 
				
			||||||
 | 
					#define INCLUDE_FILERECORD_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <dsp/basebandsamplesink.h>
 | 
					#include <dsp/basebandsamplesink.h>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
@ -16,10 +32,12 @@ public:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    struct Header
 | 
					    struct Header
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
    	qint32      sampleRate;
 | 
					    	quint32  sampleRate;
 | 
				
			||||||
        quint64     centerFrequency;
 | 
					        quint64 centerFrequency;
 | 
				
			||||||
        std::time_t startTimeStamp;
 | 
					        quint64 startTimeStamp;
 | 
				
			||||||
        quint32     sampleSize;
 | 
					        quint32 sampleSize;
 | 
				
			||||||
 | 
					        quint32 filler;
 | 
				
			||||||
 | 
					        quint32 crc32;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	FileRecord();
 | 
						FileRecord();
 | 
				
			||||||
@ -37,11 +55,11 @@ public:
 | 
				
			|||||||
	virtual bool handleMessage(const Message& message);
 | 
						virtual bool handleMessage(const Message& message);
 | 
				
			||||||
    void startRecording();
 | 
					    void startRecording();
 | 
				
			||||||
    void stopRecording();
 | 
					    void stopRecording();
 | 
				
			||||||
    static void readHeader(std::ifstream& samplefile, Header& header);
 | 
					    static bool readHeader(std::ifstream& samplefile, Header& header); //!< returns true if CRC checksum is correct else false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	QString m_fileName;
 | 
						QString m_fileName;
 | 
				
			||||||
	qint32 m_sampleRate;
 | 
						quint32 m_sampleRate;
 | 
				
			||||||
	quint64 m_centerFrequency;
 | 
						quint64 m_centerFrequency;
 | 
				
			||||||
	bool m_recordOn;
 | 
						bool m_recordOn;
 | 
				
			||||||
    bool m_recordStart;
 | 
					    bool m_recordStart;
 | 
				
			||||||
@ -52,4 +70,4 @@ private:
 | 
				
			|||||||
    void writeHeader();
 | 
					    void writeHeader();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // INCLUDE_FILESINK_H
 | 
					#endif // INCLUDE_FILERECORD_H
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user