Chinaunix首页 | 论坛 | 博客
  • 博客访问: 265427
  • 博文数量: 49
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-08 10:33
文章分类

全部博文(49)

文章存档

2010年(2)

2009年(30)

2008年(17)

我的朋友

分类: C/C++

2009-05-03 13:59:08

前一段时间在进行c++ Builder开发时,需要用到麦克风录音并大致显示音量的大小,网上搜了一半天,关于这方面的也不是很多。程序如下,希望对大家有用:
音量大小用Label来显示,Button1是开始按钮,Button2是结束按钮。
wave_bo.h文件
 
#ifndef wave_boH
#define wave_boH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TButton *Button1;
        TButton *Button2;
        TLabel *Label1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall FormShow(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
private: // User declarations
       void DrawWave(WAVEHDR *whdr,HDC hdc);
       void _fastcall CallBackPlay(TMessage*Message);
public:  // User declarations
      BEGIN_MESSAGE_MAP
      MESSAGE_HANDLER(MM_WIM_DATA,TMessage* ,CallBackPlay)
      END_MESSAGE_MAP(TComponent)
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
 
wave_bo.cpp文件
 
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "wave_bo.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define MAX_INQUEU 2
#define BUFSIZE    1024
#define DEBUG 1
#define as_and(value,a,b)  (value

HWND hwndMain;          //Main window handle
HDC whdc;
HPEN hpenB,hpenG,hpenOld;
MMRESULT num;
HWAVEIN hwi;
WAVEHDR *pwhi,whis[MAX_INQUEU];
 
unsigned char waveBufferRecord[MAX_INQUEU][BUFSIZE];
long currWavHdr=0;
DWORD sqrtPoints;
#if DEBUG
FILE  *fp;
#endif
WAVEFORMATEX wfx;
bool  flag;
 
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    long k;
    memset(&wfx,0,sizeof(WAVEFORMATEX));
    wfx.wFormatTag=WAVE_FORMAT_PCM;
    wfx.nChannels= 1;
    wfx.wBitsPerSample= 8;
    wfx.nSamplesPerSec= 22050L;
    wfx.nBlockAlign= 1;
    wfx.nAvgBytesPerSec= 22050L;
    wfx.cbSize=0;
    waveInOpen(&hwi,0,&wfx,(unsigned long)Handle,0,CALLBACK_WINDOW|WAVE_MAPPED);
    for(k=0;k    {
        pwhi=&whis[k];
        pwhi->dwFlags=0;
        pwhi->dwLoops=0;
        pwhi->dwBytesRecorded=0;
        pwhi->dwBufferLength=BUFSIZE;
        pwhi->lpData=waveBufferRecord[k];
    }
    pwhi=&whis[currWavHdr];
    waveInPrepareHeader(hwi,pwhi,sizeof(WAVEHDR));
    waveInAddBuffer(hwi,pwhi,sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

#if DEBUG
       fp = fopen("data.txt","w+");
#endif
       if(flag == false)
       {
         waveInOpen(&hwi,0,&wfx,(unsigned long)Handle,0,CALLBACK_WINDOW|WAVE_MAPPED);
         pwhi=&whis[currWavHdr];
         waveInPrepareHeader(hwi,pwhi,sizeof(WAVEHDR));
         waveInAddBuffer(hwi,pwhi,sizeof(WAVEHDR));
         waveInStart(hwi);
       }
       waveInStart(hwi);
}
//---------------------------------------------------------------------------
void TForm1::DrawWave(WAVEHDR *whdr,HDC hdc)
{
   static unsigned char prevBuf[BUFSIZE];
   int i,SumCount = 0;
   long sum = 0 ;
   unsigned char Max = 0;
   unsigned char Min = 255;
   hpenOld = SelectObject(hdc, hpenB);
   memcpy(prevBuf,waveBufferRecord[1-currWavHdr], BUFSIZE);
   MoveToEx(hdc,0,prevBuf[0],NULL);
   for(i=0;i   {
     LineTo(hdc,i,(unsigned int)prevBuf[i]);
#if DEBUG
     fprintf(fp,"i%d=%d\n",i,(unsigned int)prevBuf[i]);
#endif
    if (prevBuf[i]>Max)
         Max = prevBuf[i];
    if(prevBuf[i]         Min = prevBuf[i];
     sum+= prevBuf[i];
     SumCount++;
     Label1->Width = prevBuf[i];
   }
    Label1->Width = Max - Min;
   SelectObject(hdc, hpenOld);
#if DEBUG
     fprintf(fp,"sum=%d\n",sum/BUFSIZE + 200);
#endif
}
void _fastcall TForm1::CallBackPlay(TMessage *Message)
{
    WAVEHDR  *pwhi_tmp;
    pwhi_tmp = pwhi;
    whdc=GetDC(hwndMain);
    waveInUnprepareHeader(hwi,pwhi,sizeof(WAVEHDR));
    currWavHdr=1-currWavHdr;
    pwhi=&whis[currWavHdr];

    waveInPrepareHeader(hwi,pwhi,sizeof(WAVEHDR));
    waveInAddBuffer(hwi,pwhi,sizeof(WAVEHDR));
      TRect  Clr;
      Clr.Left = 0;
      Clr.Top = 0;
      Clr.Right = Screen->Monitors[0]->Width;
      Clr.Bottom = Screen->Monitors[0]->Height/2;
      Brush->Color = clWhite;
      Canvas->Brush->Style = bsSolid;
      Canvas->FillRect(Clr);
      DrawWave(pwhi_tmp,whdc);
      ReleaseDC(hwndMain,whdc);
}
void __fastcall TForm1::FormShow(TObject *Sender)
{
    hwndMain  = (HWND)Form1->Handle;
    hpenG = CreatePen(PS_SOLID, 0, RGB(0, 255, 0));
    hpenB = CreatePen(PS_SOLID, 0, RGB(0, 0, 0));
    flag = true;
    Label1->Left = 0;
    Label1->Height = 30;
    Label1->Top = Screen->Monitors[0]->Height - Label1->Height - 150;
    Label1->Width = 120;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  int i;
  waveInStop(hwi);
  waveInUnprepareHeader(hwi,pwhi,sizeof(WAVEHDR));
  DeleteObject(hpenB);
  DeleteObject(hpenG);
  DeleteObject(hpenOld);
  waveInClose(hwi);
#if DEBUG
  fclose(fp);
#endif
  flag = false;
}

 
阅读(2332) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~