简单处理音频输入数据的类

处理简单的音频输入数据

SoundIn类见我的另一篇博客 http://blog.csdn.net/zgl7903/article/details/5669770

 

#pragma once
//Microphone_Measure.h
#include "SoundIn.h"

class CMicrophone_Measure
{
public:
  CMicrophone_Measure();
  virtual ~CMicrophone_Measure();

public:
  BOOL StartMeasure();
  void StopMeansure();

public:
  typedef struct _WAVEMEASUMERESULT_TAG
  {
    BOOL bValid;
    DWORD dwPtSize; //点数
    double fWave_Frame_Avg; //平均值
    double fWave_Frame_RMS; //有效值
    double fWave_Frame_Max; //最大值
    double fWave_Frame_Min; //最小值
    double fWave_Frame_AbsMax; //绝对最大值
    double fWave_Frame_AbsMin; //绝对最小值

  }WAVEMEASUMERESULT;

  WAVEMEASUMERESULT m_WaveMeasureResult;

  BOOL IsOpened() { return bMicrophone_Opened; }

protected:
  BOOL bMicrophone_Opened;
  CSoundIn m_SoundIn;
  static BOOL WaveInProc(LPVOID pUser, SHORT *pData, DWORD dwSize);

};


 

 

#include "stdafx.h"
#include "Microphone_Measure.h"

CMicrophone_Measure::CMicrophone_Measure()
{
  bMicrophone_Opened = FALSE; //置关闭状态  
  memset(&m_WaveMeasureResult, 0, sizeof(m_WaveMeasureResult)); //初始化结构体
}

CMicrophone_Measure::~CMicrophone_Measure()
{
  StopMeansure();//停止测量
}

BOOL CMicrophone_Measure::StartMeasure()
{
  if(!bMicrophone_Opened)//非打开状态
  {
    if( m_SoundIn.OpenMic(this, WaveInProc) == MMSYSERR_NOERROR)
      bMicrophone_Opened = TRUE;
  }
  return bMicrophone_Opened;
}

void CMicrophone_Measure::StopMeansure()
{
  m_SoundIn.CloseMic();
  bMicrophone_Opened = FALSE;
}

BOOL CMicrophone_Measure::WaveInProc(LPVOID pUser, SHORT *pData, DWORD dwSize)
{
  CMicrophone_Measure *pThis = (CMicrophone_Measure *)pUser;

  WAVEMEASUMERESULT tmpResult={0};

  double dbVal = pData[0]; //第一个点
  tmpResult.dwPtSize = dwSize; //点数
  tmpResult.fWave_Frame_Avg = dbVal; //平均值
  tmpResult.fWave_Frame_RMS = pow(dbVal, 2); //有效值
  tmpResult.fWave_Frame_Max = dbVal; //最大值
  tmpResult.fWave_Frame_Min = dbVal; //最小值
  tmpResult.fWave_Frame_AbsMax = fabs(dbVal); //绝对最大值
  tmpResult.fWave_Frame_AbsMin = fabs(dbVal); //绝对最小值

  for(DWORD i=1; i<dwSize; i++)
  {
    dbVal = pData[i];//单声道
    tmpResult.fWave_Frame_Avg += dbVal; //平均值
    tmpResult.fWave_Frame_RMS += pow(dbVal, 2); //有效值
    tmpResult.fWave_Frame_Max = max(dbVal, tmpResult.fWave_Frame_Max); //最大值
    tmpResult.fWave_Frame_Min = min(dbVal, tmpResult.fWave_Frame_Min); //最小值
    tmpResult.fWave_Frame_AbsMax = max(fabs(dbVal), tmpResult.fWave_Frame_AbsMax); //绝对最大值
    tmpResult.fWave_Frame_AbsMin = min(fabs(dbVal), tmpResult.fWave_Frame_AbsMin); //绝对最小值
  }
  tmpResult.fWave_Frame_Avg /= dwSize; //平均值
  tmpResult.fWave_Frame_RMS = sqrt(tmpResult.fWave_Frame_RMS / dwSize);//有效值

  tmpResult.bValid = TRUE;

  pThis->m_WaveMeasureResult = tmpResult;

  return TRUE;
}


 

//简单的应用举例

 

#pragma once

#include "Microphone_Measure.h"

// CTestDlg 对话框
class CTestDlg : public CDialog
{
public:

  CMicrophone_Measure m_Microphone_Measure;
  afx_msg void OnTimer(UINT_PTR nIDEvent);
  CProgressCtrl m_ProgressCtrl;


 

BOOL CTestDlg::OnInitDialog()
{
  CDialog::OnInitDialog();

  // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  //  执行此操作
  SetIcon(m_hIcon, TRUE);			// 设置大图标
  SetIcon(m_hIcon, FALSE);		// 设置小图标

  // TODO: 在此添加额外的初始化代码

  if(m_Microphone_Measure.StartMeasure())
    SetTimer(1, 100, NULL);


  return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CTestDlg::OnTimer(UINT_PTR nIDEvent)
{
  // TODO: 在此添加消息处理程序代码和/或调用默认值
  if(m_Microphone_Measure.m_WaveMeasureResult.bValid)
  {
    CString itemStr;
    itemStr.Format(
      _T("点数    %ld\r\n")
      _T("平均值   %g\r\n")
      _T("有效值   %g\r\n")
      _T("最大值   %g\r\n")
      _T("最小值   %g\r\n")
      _T("绝对最大值 %g\r\n")
      _T("绝对最小值 %g\r\n"),
      m_Microphone_Measure.m_WaveMeasureResult.dwPtSize, //点数
      m_Microphone_Measure.m_WaveMeasureResult.fWave_Frame_Avg, //平均值
      m_Microphone_Measure.m_WaveMeasureResult.fWave_Frame_RMS, //有效值
      m_Microphone_Measure.m_WaveMeasureResult.fWave_Frame_Max, //最大值
      m_Microphone_Measure.m_WaveMeasureResult.fWave_Frame_Min, //最小值
      m_Microphone_Measure.m_WaveMeasureResult.fWave_Frame_AbsMax, //绝对最大值
      m_Microphone_Measure.m_WaveMeasureResult.fWave_Frame_AbsMin, //绝对最小值
      0);
    SetDlgItemText(IDC_EDIT1, itemStr);

  }

  CDialog::OnTimer(nIDEvent);
}


 

 

 

 

你可能感兴趣的:(简单处理音频输入数据的类)