⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pathsimdoc.cpp

📁 信道仿真源代码
💻 CPP
字号:
// PathSimDoc.cpp : implementation of the CPathSimDoc class
//////////////////////////////////////////////////////////////////////
// Copyright 2000.    Moe Wheatley AE4JY  <ae4jy@mindspring.com>
//
//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; either version 2
//of the License, or any later version.
//
//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 for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
////

#include "stdafx.h"
#include "PathSim.h"
#include "PathSimDoc.h"
#include "Wave.h"
#include <direct.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define SETUP_SAVEINPUT (1<<0)
#define SETUP_SAVEOUTPUT (1<<1)
#define SETUP_APPENDTITLE (1<<2)
#define SETUP_SAVERUNTIME (1<<3)
#define SETUP_SAVEAWGN (1<<4)

/////////////////////////////////////////////////////////////////////////////
// CPathSimDoc

IMPLEMENT_DYNCREATE(CPathSimDoc, CDocument)

BEGIN_MESSAGE_MAP(CPathSimDoc, CDocument)
	//{{AFX_MSG_MAP(CPathSimDoc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPathSimDoc construction/destruction

CPathSimDoc::CPathSimDoc()
{
	m_SetupBools = 0;
}

CPathSimDoc::~CPathSimDoc()
{
}

BOOL CPathSimDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	m_SimState = SIMSTATE_OFF;
	if(!ReadFromRegistry())		// if registry read fails, load defaults
	{
		m_InputSelect = USE_SOUNDCARD;
		m_OutputSelect = USE_SOUNDCARD;
		m_InFileName = _T("");
		m_InFilePath = _T("");
		m_OutFileName = _T("");
		m_OutFilePath = _T("");
		m_InFileInfo = _T("");
		m_SimTitle = _T("Untitled");
		m_TimeLimit = 60;
		m_UseDelayStart = FALSE;
		m_AWGNOn = FALSE;
		m_Path0On = FALSE;
		m_Path1On = FALSE;
		m_Path2On = FALSE;
		m_DelayStartHour = 0;
		m_DelayStartMin = 0;
		m_Offset0 = 0.0;
		m_Offset1 = 0.0;
		m_Offset2 = 0.0;
		m_Spread0 = 1.0;
		m_Spread1 = 1.0;
		m_Spread2 = 1.0;
		m_Delay1 = 0.0;
		m_Delay2 = 0.0;
		m_SNRSetValue = 10;
		m_InFileLength = 0;
		m_SetupBools = 0;
	}
	if( (m_InputSelect==USE_WAVEFILE) && (m_OutputSelect==USE_WAVEFILE))
		m_WaveToWave = TRUE;
	else
		m_WaveToWave = FALSE;
	
	if(m_SetupBools & SETUP_SAVEINPUT)
		m_SaveInputs = TRUE;
	else
		m_SaveInputs = FALSE;
	if(m_SetupBools & SETUP_SAVEOUTPUT)
		m_SaveOutputs = TRUE;
	else
		m_SaveOutputs = FALSE;
	if(m_SetupBools & SETUP_APPENDTITLE)
		m_AppendTitle = TRUE;
	else
		m_AppendTitle = FALSE;
	if(m_SetupBools & SETUP_SAVERUNTIME)
		m_SaveRunTime = TRUE;
	else
		m_SaveRunTime = FALSE;
	if(m_SetupBools & SETUP_SAVEAWGN)
		m_SaveAWGN = TRUE;
	else
		m_SaveAWGN = FALSE;

	
	if(	m_InputSelect == USE_WAVEFILE )
		m_InFileLength = GetInputFileLength();

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CPathSimDoc serialization
//  Stores and retrieves *.sim simulation setup data
void CPathSimDoc::Serialize(CArchive& ar)
{
BOOL bSpare = FALSE;
INT iSpare = 0;
double dSpare = 0.0;
CString sSpare = _T("");
	if (ar.IsStoring())
	{
		ar << m_AWGNOn;			//BOOL's
		ar << m_Path0On;
		ar << m_Path1On;
		ar << m_Path2On;
		ar << m_UseDelayStart;
		ar << bSpare;
		ar << bSpare;
		ar << bSpare;
		//
		ar << m_InputSelect;	//INT's
		ar << m_OutputSelect;
		ar << m_TimeLimit;
		ar << m_DelayStartHour;
		ar << m_DelayStartMin;
		ar << m_SNRSetValue;
		ar << m_InFileLength;
		ar << iSpare;
		ar << iSpare;
		ar << iSpare;
		//
		ar << m_Delay1;			//double's
		ar << m_Delay2;
		ar << m_Offset0;
		ar << m_Offset1;
		ar << m_Offset2;
		ar << m_Spread0;
		ar << m_Spread1;
		ar << m_Spread2;
		ar << dSpare;
		ar << dSpare;
		ar << dSpare;
		//
		ar << m_InFileName;		//CStrings
		ar << m_InFilePath;
		ar << m_InFileInfo;
		ar << m_OutFileName;
		ar << m_OutFilePath;
		ar << m_SimTitle;
		ar << sSpare;
		ar << sSpare;
		ar << sSpare;
	}
	else
	{
		if(m_SaveAWGN)
			ar >> m_AWGNOn;			//BOOL's
		else
			ar >> bSpare;
		ar >> m_Path0On;
		ar >> m_Path1On;
		ar >> m_Path2On;
		ar >> m_UseDelayStart;
		ar >> bSpare;
		ar >> bSpare;
		ar >> bSpare;
		//					//INT's
		if(m_SaveInputs)
			ar >> m_InputSelect;
		else
			ar >> iSpare;
		if(m_SaveOutputs)
			ar >> m_OutputSelect;
		else
			ar >> iSpare;
		if(m_SaveRunTime)
			ar >> m_TimeLimit;
		else
			ar >> iSpare;
		ar >> m_DelayStartHour;
		ar >> m_DelayStartMin;
		if(m_SaveAWGN)
			ar >> m_SNRSetValue;
		else
			ar >> iSpare;
		ar >> m_InFileLength;
		ar >> iSpare;
		ar >> iSpare;
		ar >> iSpare;
		//
		ar >> m_Delay1;			//double's
		ar >> m_Delay2;
		ar >> m_Offset0;
		ar >> m_Offset1;
		ar >> m_Offset2;
		ar >> m_Spread0;
		ar >> m_Spread1;
		ar >> m_Spread2;
		ar >> dSpare;
		ar >> dSpare;
		ar >> dSpare;
		//						//CStrings
		if(m_SaveInputs)
		{
			ar >> m_InFileName;
			ar >> m_InFilePath;
			ar >> m_InFileInfo;
		}
		else
		{
			ar >> sSpare;
			ar >> sSpare;
			ar >> sSpare;
		}
		if(m_SaveOutputs)
		{
			ar >> m_OutFileName;
			ar >> m_OutFilePath;
		}
		else
		{
			ar >> sSpare;		
			ar >> sSpare;
		}
		ar >> m_SimTitle;
		ar >> sSpare;
		ar >> sSpare;
		ar >> sSpare;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CPathSimDoc diagnostics

#ifdef _DEBUG
void CPathSimDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CPathSimDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPathSimDoc commands

UINT CPathSimDoc::GetDiskSpace()
{
CString strFreeDiskSpace;
CString strFmt;
char drive;
CWave tWave;
	// Fill disk free information
	drive = _getdrive()-1 + _T('A');
	CString sDrive = drive;
	sDrive = sDrive + ":\\"; // use current default drive
	LONGLONG size = tWave.GetDriveFreeSpace( &sDrive);
	size /= 1000000;
	return (UINT)size;	
}

INT CPathSimDoc::GetInputFileLength()
{
CWave tWave;
WAVEFORMATEX wfx;
DWORD length = 0;

	if( tWave.InOpen( &m_InFilePath, &wfx, 0, &length) )
		return 0;
	if( (wfx.nChannels != 1) || (wfx.nSamplesPerSec != 8000) ||
		(wfx.wBitsPerSample != 16) )
		return 0;
	return length;
}

void CPathSimDoc::OnCloseDocument() 
{
	if( m_SaveInputs )
		m_SetupBools |= SETUP_SAVEINPUT;
	else
		m_SetupBools &= ~SETUP_SAVEINPUT;

	if( m_SaveOutputs )
		m_SetupBools |= SETUP_SAVEOUTPUT;
	else
		m_SetupBools &= ~SETUP_SAVEOUTPUT;
	
	if( m_AppendTitle )
		m_SetupBools |= SETUP_APPENDTITLE;
	else
		m_SetupBools &= ~SETUP_APPENDTITLE;

	if( m_SaveRunTime )
		m_SetupBools |= SETUP_SAVERUNTIME;
	else
		m_SetupBools &= ~SETUP_SAVERUNTIME;

	if( m_SaveAWGN )
		m_SetupBools |= SETUP_SAVEAWGN;
	else
		m_SetupBools &= ~SETUP_SAVEAWGN;

	WriteToRegistry();
	CDocument::OnCloseDocument();
}

BOOL CPathSimDoc::WriteToRegistry()
{
BOOL bSpare = FALSE;
INT iSpare = 0;
double dSpare = 0.0;
CString sSpare = _T("");
CWinApp* pApp = AfxGetApp();
CString str;
//save BOOL's
	str.Format("%i:%i:%i:%i:%i:%i:%i:%i:",
		m_AWGNOn,
		m_Path0On,
		m_Path1On,
		m_Path2On,
		m_UseDelayStart,
		bSpare,
		bSpare,
		bSpare);
	if(!pApp->WriteProfileString("Settings\\BOOLs", "BOOLs", str))
		return FALSE;
//save INT's
	str.Format("%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:",
		m_InputSelect,
		m_OutputSelect,
		m_TimeLimit,
		m_DelayStartHour,
		m_DelayStartMin,
		m_SNRSetValue,
		m_InFileLength,
		m_SetupBools,
		iSpare,
		iSpare);
	if(!pApp->WriteProfileString("Settings\\INTs", "INTs", str))
		return FALSE;

//save doubles's
	str.Format("%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:",
		m_Delay1,
		m_Delay2,
		m_Offset0,
		m_Offset1,
		m_Offset2,
		m_Spread0,
		m_Spread1,
		m_Spread2,
		dSpare,
		dSpare,
		dSpare);
	if(!pApp->WriteProfileString("Settings\\DOUBLEs", "DOUBLEs", str))
		return FALSE;

//save strings
	if(!pApp->WriteProfileString("Settings\\Strings", "InName", m_InFileName))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "InPath", m_InFilePath))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "InInfo", m_InFileInfo))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "OutName", m_OutFileName))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "OutPath", m_OutFilePath))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "Title", m_SimTitle))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "Spare0", sSpare))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "Spare1", sSpare))
		return FALSE;
	if(!pApp->WriteProfileString("Settings\\Strings", "Spare2", sSpare))
		return FALSE;
	return TRUE;
}

BOOL CPathSimDoc::ReadFromRegistry()
{
BOOL bSpare = FALSE;
INT iSpare = 0;
double dSpare = 0.0;
CString sSpare = _T("");
CWinApp* pApp = AfxGetApp();
CString str;
INT cRead;
	str = pApp->GetProfileString ("Settings\\BOOLs", "BOOLs");
	if(str.IsEmpty ())
		return FALSE;
// Get BOOLs
	cRead = _stscanf (str, "%i:%i:%i:%i:%i:%i:%i:%i:",
		&m_AWGNOn,
		&m_Path0On,
		&m_Path1On,
		&m_Path2On,
		&m_UseDelayStart,
		&bSpare,
		&bSpare,
		&bSpare);
	if(cRead != 8)
		return FALSE;
//Get INT's
	str = pApp->GetProfileString ("Settings\\INTs", "INTs");
	if(str.IsEmpty ())
		return FALSE;
	cRead = _stscanf(str, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:",
		&m_InputSelect,
		&m_OutputSelect,
		&m_TimeLimit,
		&m_DelayStartHour,
		&m_DelayStartMin,
		&m_SNRSetValue,
		&m_InFileLength,
		&m_SetupBools,
		&iSpare,
		&iSpare);
	if(cRead != 10)
		return FALSE;
//get doubles's
	str = pApp->GetProfileString("Settings\\DOUBLEs", "DOUBLEs");
	if(str.IsEmpty ())
		return FALSE;
	cRead = _stscanf(str, "%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:%lg:",
		&m_Delay1,
		&m_Delay2,
		&m_Offset0,
		&m_Offset1,
		&m_Offset2,
		&m_Spread0,
		&m_Spread1,
		&m_Spread2,
		&dSpare,
		&dSpare,
		&dSpare);
	if(cRead != 11)
		return FALSE;
//get strings
	m_InFileName = pApp->GetProfileString("Settings\\Strings", "InName",_T(""));
	m_InFilePath = pApp->GetProfileString("Settings\\Strings", "InPath",_T(""));
	m_InFileInfo = pApp->GetProfileString("Settings\\Strings", "InInfo",_T(""));
	m_OutFileName = pApp->GetProfileString("Settings\\Strings", "OutName",_T(""));
	m_OutFilePath = pApp->GetProfileString("Settings\\Strings", "OutPath",_T(""));
	m_SimTitle = pApp->GetProfileString("Settings\\Strings", "Title",_T("Untitled"));
	sSpare = pApp->GetProfileString("Settings\\Strings", "Spare0",_T(""));
	sSpare = pApp->GetProfileString("Settings\\Strings", "Spare1",_T(""));
	sSpare = pApp->GetProfileString("Settings\\Strings", "Spare2",_T(""));
	return TRUE;
}

INT CPathSimDoc::GetDiskInfo(CString &sFree, CString &sNeeded)
{
INT free;
INT needed;
LONGLONG temp;
	free = GetDiskSpace()-25;		//give 25 meg margin
	if(free<0)
		free = 0;
	needed = 1+(m_TimeLimit*8000*2)/1000000;
	if( needed >= free )
	{
		temp = (LONGLONG)(free*1000000);
		m_TimeLimit = (INT)(temp/(2*8000) );
		needed = (m_TimeLimit*8000*2)/1000000;
	}
	if(m_OutputSelect==USE_WAVEFILE)
	{
		sFree.Format( _T("Free Space = %ld MB"), free);
		sNeeded.Format( _T("Needed Space = %ld MB"), needed);
	}
	else
	{
		sFree = _T("");
		sNeeded = _T("");
	}
	return m_TimeLimit;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -