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

📄 unit1.cpp

📁 xiao cheng xu yiduan ,da jia yikuai fen xiang yi xia
💻 CPP
字号:
/*==============================================================================

       Simple Audio Recording with the MCIWnd* functions
       Pankhurst Algorithmics (www.pankhurst.com)

==============================================================================*/
#include <vcl.h>
#pragma hdrstop

#include <vfw.h>

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
extern HINSTANCE g_hInstance;
//---------------------------------------------------------------------------
HANDLE m_hMCIWnd  =NULL;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  //
  // open our MCI window along with the program
  //
  m_hMCIWnd=MCIWndCreate(Handle,
                         g_hInstance,
//
// uncomment 'WS_VISIBLE' line to display the MCI window
//
//                         WS_VISIBLE |
                         WS_CHILD | WS_OVERLAPPED | WS_CAPTION | WS_BORDER |
                         MCIWNDF_RECORD | MCIWNDF_SHOWALL,
                         NULL );
  if ( NULL==m_hMCIWnd ) // error?
  {
    MessageBox(Handle,"Error Creating MCIWnd Window!",NULL, MB_OK);
    return;
  }
  //
  // enable record button
  //
  Button1->Enabled=true;
  Label1->Caption="IDLE";
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1(void)
{
  //
  // close our MCI program as we close the form
  //
  if ( Button2->Enabled ) // enabled stop button means we are still recording!
    Button2Click(NULL); // stop and save file before closing
  //
  // close our MCI window now
  //
  MCIWndDestroy(m_hMCIWnd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  //
  // start recording
  //
  // create new .WAV file (internal - not yet on disk)
  MCIWndNew(m_hMCIWnd, "waveaudio");
  // change the default audio setting (which is 11khz 8bit mono)
  // to do this successfully, we need to change all of the following:
  // MCI_WAVE_SET_PARMS members: wFormatTag, wBitsPerSample,
  // nChannels, nSamplesPerSec, nAvgBytesPerSec, and nBlockAlign
  MCI_WAVE_SET_PARMS set_parms;
  set_parms.wFormatTag      = WAVE_FORMAT_PCM;
  set_parms.wBitsPerSample  = 16;
  set_parms.nChannels       = 1;
  set_parms.nBlockAlign     = (set_parms.nChannels*set_parms.wBitsPerSample)/8;
  set_parms.nSamplesPerSec  = 44100;
  set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample) *
                                set_parms.nChannels *
                                set_parms.nSamplesPerSec)/8;
  // now send the format changes with MCI_SET
  int deviceID=MCIWndGetDeviceID(m_hMCIWnd);
  int result = mciSendCommand( deviceID, MCI_SET,
                               MCI_WAIT
                             | MCI_WAVE_SET_FORMATTAG
                             | MCI_WAVE_SET_BITSPERSAMPLE
                             | MCI_WAVE_SET_CHANNELS
                             | MCI_WAVE_SET_SAMPLESPERSEC
                             | MCI_WAVE_SET_AVGBYTESPERSEC
                             | MCI_WAVE_SET_BLOCKALIGN,
                               (DWORD)(LPVOID)&set_parms);
  if ( result ) // failed?
  {
    char buffer[100];
    mciGetErrorString(result, buffer, sizeof(buffer));
    MessageBox( NULL, buffer, "MCI_WAVE_SET_1", MB_OK);
    return;
  }
  // now we can record at our audio setting
  MCIWndRecord(m_hMCIWnd);
  //
  // set GUI buttons so we know we can stop
  //
  Button1->Enabled=false;
  Button2->Enabled=true;
  Label1->Caption="RECORDING";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  //
  // stop recording and save as unique local file
  //
  // generate unique filename from timer
  char file[260]="";
  wsprintf(file,"aud%05d.wav",GetTickCount()%100000L); // random filename
  //
  // now stop audio and save to disk
  MCIWndStop(m_hMCIWnd);
  MCIWndSave(m_hMCIWnd,file);
  MCIWndClose(m_hMCIWnd);
  //
  // now set buttons so we know we can stop
  //
  Button1->Enabled=true;
  Button2->Enabled=false;
  Label1->Caption="IDLE";
}
//---------------------------------------------------------------------------











⌨️ 快捷键说明

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