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

📄 soundgeneratordlg.cpp

📁 This source can emulate function generator using sound card. This source is very useful for sound c
💻 CPP
📖 第 1 页 / 共 2 页
字号:

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}	
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSoundGeneratorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSoundGeneratorDlg::OnActiveShape1() 
{
	UpdateData(1);

	if( m_shape1Active )
	{
		m_pShapeActive[0] = true;
	}
	else
	{
		m_pShapeActive[0] = false;

	}	
}

void CSoundGeneratorDlg::OnActiveShape2() 
{
	UpdateData(1);

	if( m_shape2Active )
	{
		m_pShapeActive[1] = true;
	}
	else
	{
		m_pShapeActive[1] = false;

	}	
}

void CSoundGeneratorDlg::OnActiveShape3() 
{
	UpdateData(1);

	if( m_shape3Active )
	{
		m_pShapeActive[2] = true;
	}
	else
	{
		m_pShapeActive[2] = false;

	}	
}

void CSoundGeneratorDlg::OnApplyShape1() 
{
	UpdateData(1);
	ScopeGuardMutex guard(&m_exprMutex);
	
	try
	{
		m_pShapeExpr[0].setExpression(m_shape1.GetBuffer(0));
		m_msg = _T("");
	}
	catch( MATExceptions &e )
	{
		m_msg = (_T("Shape1 apply error: ") + e.getAllExceptionStr()).c_str();
		
	}	
	UpdateData(0);
	
	GetDlgItem(IDC_APPLY_SHAPE1)->EnableWindow(FALSE);
}

void CSoundGeneratorDlg::OnApplyShape2() 
{
	UpdateData(1);
	ScopeGuardMutex guard(&m_exprMutex);
	
	try
	{
		m_pShapeExpr[1].setExpression(m_shape2.GetBuffer(0));
		m_msg = _T("");
	}
	catch( MATExceptions &e )
	{
		m_msg = (_T("Shape2 apply error: ") + e.getAllExceptionStr()).c_str();		
	}	

	UpdateData(0);
	
	GetDlgItem(IDC_APPLY_SHAPE2)->EnableWindow(FALSE);
	
}

void CSoundGeneratorDlg::OnApplyShape3() 
{
	UpdateData(1);
	ScopeGuardMutex guard(&m_exprMutex);
	
	try
	{
		m_pShapeExpr[2].setExpression(m_shape3.GetBuffer(0));
		m_msg = _T("");
	}
	catch( MATExceptions &e )
	{
		m_msg = (_T("Shape3 apply error: ") + e.getAllExceptionStr()).c_str();
		
	}	
	
	UpdateData(0);
	GetDlgItem(IDC_APPLY_SHAPE3)->EnableWindow(FALSE);
	
}

void CSoundGeneratorDlg::OnChangeShape1() 
{
	GetDlgItem(IDC_APPLY_SHAPE1)->EnableWindow(TRUE);
}

void CSoundGeneratorDlg::OnChangeShape2() 
{
	GetDlgItem(IDC_APPLY_SHAPE2)->EnableWindow(TRUE);
	
}

void CSoundGeneratorDlg::OnChangeShape3() 
{
	GetDlgItem(IDC_APPLY_SHAPE3)->EnableWindow(TRUE);	
}

void CSoundGeneratorDlg::OnApplyVarx() 
{
	UpdateData(1);
	ScopeGuardMutex guard(&m_exprMutex);
	try
	{
		m_pVarExpr[0].setExpression(m_varx.GetBuffer(0));
		m_msg = _T("");
	}
	catch( MATExceptions &e )
	{
		m_msg = (_T("VarX apply error: ") + e.getAllExceptionStr()).c_str();
		
	}

	UpdateData(0);
	GetDlgItem(IDC_APPLY_VARX)->EnableWindow(FALSE);
	
}

void CSoundGeneratorDlg::OnApplyVary() 
{
	UpdateData(1);
	ScopeGuardMutex guard(&m_exprMutex);
	try
	{
		m_pVarExpr[1].setExpression(m_vary.GetBuffer(0));
		m_msg = _T("");
	}
	catch( MATExceptions &e )
	{
		m_msg = (_T("VarY apply error: ") + e.getAllExceptionStr()).c_str();		
	}
	
	UpdateData(0);
	GetDlgItem(IDC_APPLY_VARY)->EnableWindow(FALSE);
	
}

void CSoundGeneratorDlg::OnApplyVarz() 
{
	UpdateData(1);
	ScopeGuardMutex guard(&m_exprMutex);
	try
	{
		m_pVarExpr[2].setExpression(m_varz.GetBuffer(0));
		m_msg = _T("");
	}
	catch( MATExceptions &e )
	{
		m_msg = (_T("VarZ apply error: ") + e.getAllExceptionStr()).c_str();		
	}

	UpdateData(0);
	GetDlgItem(IDC_APPLY_VARZ)->EnableWindow(FALSE);
	
}

void CSoundGeneratorDlg::OnChangeVarx() 
{
	GetDlgItem(IDC_APPLY_VARX)->EnableWindow(TRUE);
	
}

void CSoundGeneratorDlg::OnChangeVary() 
{
	GetDlgItem(IDC_APPLY_VARY)->EnableWindow(TRUE);
	
}

void CSoundGeneratorDlg::OnChangeVarz() 
{
	GetDlgItem(IDC_APPLY_VARZ)->EnableWindow(TRUE);
}

void CSoundGeneratorDlg::OnSoundPlayerNotify(int eventNumber)
{
	ScopeGuardMutex guard(&m_exprMutex);

	// compute the next sound samples...

	double sample;

	DWORD begin = GetTickCount();
	try
	{
		for( int s=0; s<m_soundPlayerEventNbSamples; s++ )
		{
			sample = 0;

			for( int t=0; t<m_nbVars; t++ )
			{
				m_pUserVar[t] = m_pVarExpr[t].evaluate();		
			}

			for( t=0; t<m_nbShapes; t++ )
			{
				if( m_pShapeActive[t] )
				{
					sample += m_pShapeExpr[t].evaluate() * m_pShapeVolume[t];		
				}
			}

			m_pSamples[s] = sample*m_sampleScaleVal;	

			m_t+=m_tStep;	// increment the time			
		}
	}
	catch( MATExceptions &e )
	{
		OutputDebugString(e.getAllExceptionStr().c_str());		// math expression evaluation error...
	}
	
	try
	{
		m_pSoundPlayer->Write(((eventNumber+1)%m_nbSoudPlayerEvents)*m_soundPlayerEventSize, (unsigned char*)m_pSamples, m_soundPlayerEventSize);
	}
	catch( MATExceptions &e )
	{
		OutputDebugString(e.getAllExceptionStr().c_str());		// it would be better to stop the program...
	}			
	
	drawWave();

	DWORD end = GetTickCount();
	DWORD elapsed = end-begin;	

	
	if( elapsed > m_maxEventComputeTime )
	{
		m_warningMsg.Format(_T("Warning! compute time: %dms"), elapsed);
	}
	else
	{
		m_warningMsg.Format(_T("compute time: %dms"), elapsed);
	}

	
}

void CSoundGeneratorDlg::drawWave()
{
	int nbSteps = m_soundPlayerEventNbSamples / m_graphStep;
	::SelectObject(m_hdc, m_hBgPen);
	::SelectObject(m_hdc, m_hBgBrush);
	
	int end = m_graphRect.left + m_graphCursor + nbSteps;
	int remaining = 0;
	if( end > m_graphRect.right )
	{
		remaining = end - m_graphRect.right;
		end = m_graphRect.right;
	}
	Rectangle(m_hdc,	m_graphRect.left + m_graphCursor,
						m_graphRect.top,
						end,
						m_graphRect.bottom );

	if( remaining > 0 )
	{
		Rectangle(m_hdc,	m_graphRect.left,
							m_graphRect.top,
							m_graphRect.left+remaining,
							m_graphRect.bottom );

	}

	int pos = m_graphCursor;
	
	int middle = (m_graphRect.bottom - m_graphRect.top)/2;
	double scaleY = middle / (double)m_sampleScaleVal;

	::SelectObject(m_hdc, m_hRedPen);
	MoveToEx(m_hdc, pos, m_graphLastVal, NULL);
	unsigned int val;

	for( int s=0; s<m_soundPlayerEventNbSamples; s+=m_graphStep )
	{
		val = -m_pSamples[s] * scaleY+middle;
		pos++;
		if( pos >= m_graphRect.right )
		{
			pos = m_graphRect.left;			
			MoveToEx(m_hdc, pos, val, NULL);				
		}
		
		LineTo(m_hdc, pos, val);		
	}

	m_graphCursor = pos;
	m_graphLastVal = val;
}

void CSoundGeneratorDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	m_pSoundPlayer->Stop();	
}

void CSoundGeneratorDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// volume scroll bars...

	CSliderCtrl *pCtrl = (CSliderCtrl*)pScrollBar;

	if( pCtrl == &m_volumeShape1 )
	{
		m_pShapeVolume[0] = m_volumeShape1.GetPos() / 100.0;

	}
	else if( pCtrl == &m_volumeShape2 )
	{
		m_pShapeVolume[1] = m_volumeShape2.GetPos() / 100.0;

	}
	else if( pCtrl == &m_volumeShape3 )
	{
		m_pShapeVolume[2] = m_volumeShape3.GetPos() / 100.0;
	}
	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}


void CSoundGeneratorDlg::OnTimer(UINT nIDEvent) 
{

	GetDlgItem(IDC_MSGWARNING)->SetWindowText(m_warningMsg);
	m_warningMsg = _T("");
	
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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