首页  编辑  

录音机的程序

Tags: /超级猛料/Multi-Media.多媒体相关/   Date Created:

]、。·ˉˇ¨〃々—~‖…’”〕〉》」』〗】∶!"'),.:;?]` 我这里给你贴一个我写的程序段,看不明白就算了。

// 全局变量

var

 BufferFinished : boolean; // indicates current record buffer is full

.

 UserStopRecord: boolean;

 Recording: boolean;

 WaveInHandle: HWaveIn;

 CurrentUsingBuffer: integer;

 WaveInHeader1: TWaveHdr;

 WaveInHeader2: TWaveHdr;

 RecordBufferSize: integer;

 TotalRecordedSize: DWORD;

//进行录音

procedure TDlgRecord.BtnStartClick(Sender: TObject);

var

 TempInt, TempCode: integer;

 TempFileName: string;

 MMR: MMResult;

 MyWaveFormat: TWaveFormatEx;

begin

 PrgsBarSpace.Position := 0;

 Recording := false;

 OldDiskFree := DiskFree (RecordDrive);

 with RecordWaveFormat do

   begin

   if RdoBtnMono.Checked = true then

     Channels := 1

   else

     Channels := 2;

   if RdoBtn8Bit.Checked = true then

     BitsPerSample := 8

   else

     BitsPerSample := 16;

   Val (EditFrequency.Text, TempInt, TempCode);

   if TempCode <> 0 then

     begin

     Application.MessageBox ('无效数值', '错误', MB_Ok);

     exit;

     end

   else if TempInt > 48000 then

     begin

     Application.MessageBox('不能大于48000赫兹', '错误', MB_Ok);

     exit;

     end

   else

     begin

     SamplesPerSecond := TempInt;

     end;

   BytesPerSecond := SamplesPerSecond * Channels * BitsPerSample div  

8;

   BlockAlign := BitsPerSample * Channels div 8 ;

   end;

 TempFileName := TempFileNameGenerator (Self.TempPath, 'WM_', 'TMP',  

5);

 AssignFile (RecordFile, TempFileName);

 Rewrite (RecordFile, 1);

 if IOResult <> 0 then

   begin

   Application.Messagebox ('无法创建暂时文件,请检查临时目录设定。',  

'严重错误', MB_OK);

   exit;

   end;

 RecordFileName := TempFileName;

 RecordSuccess := false; // 由于文件名已经改变,所以在这里修改该变量。

 // Preparation jobs here

 SttTextElapsedTime.Caption := '00:00:00';

 ElapsedHours := 0;

 ElapsedMinutes := 0;

 ElapsedSeconds := 0;

 CurrentUsingBuffer := 1;

 TotalRecordedSize := 0;

 BufferFinished := false;

 UserStopRecord := false;

 RecordBufferSize := RecordWaveFormat.BytesPerSecond * 2; // for 2 se

conds

 Buffer1 := nil;

 Buffer2 := nil;

 try

   GetMem (Buffer1, RecordBufferSize);

 except

   Application.MessageBox ('内存不足,分配第一缓冲区失败。', '错误',  

MB_OK);

 end;

 try

   GetMem (Buffer2, RecordBufferSize);

 except

   Application.MessageBox ('内存不足,分配第二缓冲区失败。', '错误',  

MB_OK);

 end;

 if (Buffer1 = nil) or (Buffer2 = nil) then

   begin

   CloseFile (RecordFile);

   exit;

   end;

 MyWaveFormat.wFormatTag := WAVE_FORMAT_PCM;

 MyWaveFormat.nChannels := RecordWaveFormat.Channels;

 MyWaveFormat.nSamplesPerSec := RecordWaveFormat.SamplesPerSecond;

 MyWaveFormat.nAvgBytesPerSec := RecordWaveFormat.BytesPerSecond;

 MyWaveFormat.nBlockAlign := RecordWaveFormat.BlockAlign;

 MyWaveFormat.wBitsPerSample := RecordWaveFormat.BitsPerSample;

 MyWaveFormat.cbSize := 0;

 MMR := waveInOpen (Addr (waveInHandle),

                    SelectedDevice,

                    Addr (MyWaveFormat),

                    DWORD (Addr (WaveInCallBack)),

                    0,

                    CALLBACK_FUNCTION);

 if MMR <> MMSYSERR_NOERROR then

   begin

   FreeMem (Buffer1); FreeMem (Buffer2);

   RecordBufferSize := 0;

   CloseFile (RecordFile);

   Application.MessageBox ('无法打开输入设备,请检查设定。', '错误',  

MB_OK);

   exit;

   end;

 waveInHeader1.lpData := Buffer1;

 waveInHeader1.dwBufferLength := RecordBufferSize;

 waveInHeader1.dwBytesRecorded := 0;

 waveInHeader1.dwUser := 0; // 32 bits of user data

 waveInHeader1.dwFlags := 0;

 waveInHeader1.dwLoops := 0;

 waveInHeader1.lpNext := nil; // reserved; must be zero

 waveInHeader1.reserved := 0; // reserved; must be zero

 MMR := waveInPrepareHeader (waveInHandle, Addr (waveInHeader1),

                             sizeof (waveInHeader1));

 if MMR <> MMSYSERR_NOERROR then

   begin

   FreeMem (Buffer1); FreeMem (Buffer2);

   RecordBufferSize := 0;

   CloseFile (RecordFile);

   Application.MessageBox ('第一录音缓冲区准备出错。', '错误', MB_OK)

;

   exit;

   end;

 waveInHeader2.lpData := Buffer2;

 waveInHeader2.dwBufferLength := RecordBufferSize;

 waveInHeader2.dwBytesRecorded := 0;

 waveInHeader2.dwUser := 0; // 32 bits of user data

 waveInHeader2.dwFlags := 0;

 waveInHeader2.dwLoops := 0;

 waveInHeader2.lpNext := nil; // reserved; must be zero

 waveInHeader2.reserved := 0; // reserved; must be zero

 MMR := waveInPrepareHeader (waveInHandle, Addr (waveInHeader2),

                             sizeof (waveInHeader2));

 if MMR <> MMSYSERR_NOERROR then

   begin

   waveInUnprepareHeader (waveInHandle, Addr (waveInHeader1), sizeof(

waveInHeader1));

   FreeMem (Buffer1); FreeMem (Buffer2);

   RecordBufferSize := 0;

   CloseFile (RecordFile);

   Application.MessageBox ('第二录音缓冲区准备出错。', '错误', MB_OK)

;

   exit;

   end;

 MMR := waveInAddBuffer (WaveInHandle, Addr (waveInHeader1), sizeof (

waveInHeader1));

 if MMR <> MMSYSERR_NOERROR then

   begin

   waveInUnprepareHeader (waveInHandle, Addr (waveInHeader1), sizeof(

waveInHeader1));

   waveInUnprepareHeader (waveInHandle, Addr (waveInHeader2), sizeof(

waveInHeader2));

   FreeMem (Buffer1); FreeMem (Buffer2);

   RecordBufferSize := 0;

   CloseFile (RecordFile);

   Application.MessageBox ('添加第一缓冲区出错。', '错误', MB_OK);

   exit;

   end;

 MMR := waveInAddBuffer (WaveInHandle, Addr (waveInHeader2), sizeof (

waveInHeader2));

 if MMR <> MMSYSERR_NOERROR then

   begin

   waveInUnprepareHeader (waveInHandle, Addr (waveInHeader1), sizeof(

waveInHeader1));

   waveInUnprepareHeader (waveInHandle, Addr (waveInHeader2), sizeof(

waveInHeader2));

   FreeMem (Buffer1); FreeMem (Buffer2);

   RecordBufferSize := 0;

   CloseFile (RecordFile);

   Application.MessageBox ('添加第二缓冲区出错。', '错误', MB_OK);

   exit;

   end;

 TimerRecording.Enabled := true;

 // Record has begun.

 Recording := true;

 MMR := waveInStart (waveInHandle);

 if MMR <> MMSYSERR_NOERROR then

   begin

   waveInUnprepareHeader (waveInHandle, Addr (waveInHeader1), sizeof(

waveInHeader1));

   waveInUnprepareHeader (waveInHandle, Addr (waveInHeader2), sizeof(

waveInHeader2));

   FreeMem (Buffer1); FreeMem (Buffer2);

   RecordBufferSize := 0;

   CloseFile (RecordFile);

   Recording := false;

   TimerRecording.Enabled := false;

   Application.MessageBox ('未能开始录音。', '错误', MB_OK);

   exit;

   end;

 // 应该在这里开始创建线程.

 // 需要传递参数: RecordFile, Buffer1, Buffer2,

 with TMediaRecordThread.Create (Addr(RecordFile), Buffer1, Buffer2)  

do

   OnTerminate := ThreadRecordStop;

 // disable/enable controls

 BtnCancel.Enabled := false;

 BtnStart.Enabled := false;

 GrpBoxChannel.Enabled := false;

 LBFrequency.Enabled := false;

 EditFrequency.Enabled := false;

 LstBoxFrequency.Enabled := false;

 GrpBoxSample.Enabled := false;

 BtnStop.Enabled := true;

end;