Delphi method of making WAV file _delphi

Source: Internet
Author: User
Tags reserved

This article describes the Delphi method of making WAV files. Share to everyone for your reference. Specifically as follows:

Here Delphi with WaveIn ... function to make WAV files

The specific code is as follows:

Use a window to accept messages from an audio device: unit Unit1; 
Interface uses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls; 
  Type TFORM1 = Class (Tform) Button1:tbutton; 
  Button2:tbutton; 
  Button3:tbutton; 
  Procedure Formcreate (Sender:tobject); 
  Procedure Button1Click (Sender:tobject); 
  Procedure Button2click (Sender:tobject); 
 Procedure Button3click (Sender:tobject); Protected procedure WndProc (Var m:tmessage); 
 Override 
End 
var Form1:tform1; 
Implementation {$R *.DFM} uses MMSystem; 
 var whin1,whin2, Whout:twavehdr; 
 Hwavein,hwaveout:hwave; 
 Fmt:twaveformatex; 
Buf1,buf2,savebuf:tbytes; 
Procedure Tform1.formcreate (Sender:tobject); 
 Begin button1.caption: = ' start recording '; 
 Button2.caption: = ' stop recording '; 
Button3.caption: = ' play the recording '; 
End 
Start recording procedure Tform1.button1click (Sender:tobject); 
 Begin {Specifies the format to be recorded} Fmt.wformattag: = WAVE_FORMAT_PCM; 
 Fmt.nchannels: = 2; 
 Fmt.nsamplespersec: = 22050; Fmt.navgbytespersec: = 88200; 
 Fmt.nblockalign: = 4; 
 Fmt.wbitspersample: = 16; 
 Fmt.cbsize: = 0; Savebuf: = nil; {Erase recorded content} if Waveinopen (@hWaveIn, Wave_mapper, @fmt, Handle, 0, Callback_window) = 0 THEN BEGIN SetLength (BUF1, 10 
  24*8); 
  SetLength (Buf2, 1024*8); 
  Whin1.lpdata: = Pansichar (BUF1); 
  Whin1.dwbufferlength: = Length (BUF1); 
  Whin1.dwbytesrecorded: = 0; 
  Whin1.dwuser: = 0; 
  Whin1.dwflags: = 0; 
  Whin1.dwloops: = 0; 
  Whin1.lpnext: = nil; 
  Whin1.reserved: = 0; 
  Whin2.lpdata: = Pansichar (BUF2); 
  Whin2.dwbufferlength: = Length (BUF2); 
  Whin2.dwbytesrecorded: = 0; 
  Whin2.dwuser: = 0; 
  Whin2.dwflags: = 0; 
  Whin2.dwloops: = 0; 
  Whin2.lpnext: = nil; 
  Whin2.reserved: = 0; 
  Waveinprepareheader (Hwavein, @whIn1, SizeOf (TWAVEHDR)); 
  Waveinprepareheader (Hwavein, @whIn2, SizeOf (TWAVEHDR)); 
  Waveinaddbuffer (Hwavein, @whIn1, SizeOf (TWAVEHDR)); 
  Waveinaddbuffer (Hwavein, @whIn2, SizeOf (TWAVEHDR)); 
 Waveinstart (Hwavein); 
End 
End Stop Recording Procedure Tform1.buttOn2click (Sender:tobject); 
 Begin Waveinstop (Hwavein); 
 Waveinunprepareheader (Hwavein, @whIn1, SizeOf (TWAVEHDR)); 
 Waveinunprepareheader (Hwavein, @whIn2, SizeOf (TWAVEHDR)); 
Waveinclose (Hwavein); 
End 
Play the recording procedure Tform1.button3click (Sender:tobject); 
 Begin Whout.lpdata: = Pansichar (SAVEBUF); 
 Whout.dwbufferlength: = Length (SAVEBUF); 
 Whout.dwbytesrecorded: = 0; 
 Whout.dwuser: = 0; 
 Whout.dwflags: = 0; 
 Whout.dwloops: = 1; 
 Whout.lpnext: = nil; 
 Whout.reserved: = 0; 
 Waveoutopen (@hWaveOut, Wave_mapper, @fmt, Handle, 0, Callback_window); 
 Waveoutprepareheader (Hwaveout, @whOut, SizeOf (TWAVEHDR)); 
Waveoutwrite (Hwaveout, @whOut, SizeOf (TWAVEHDR)); 
End 
Procedure Tform1.wndproc (var m:tmessage); 
var Ordlen:integer; 
 Begin inherited;   Case M.msg of {processing audio messages} Mm_wim_open:; 
  {This message only carries a device handle}  Mm_wim_close:; 
  {This message only carries a device handle} 
   Mm_wim_data:begin {This message carries the device handle and Wavehdr pointer (LParam)} {save recorded data} Ordlen: = Length (SAVEBUF); SetLength (savebuf, Ordlen + PWAVEHDR (m.LParam). dwbytesrecorded); 
   CopyMemory (PTR (SAVEBUF) +ordlen), Pwavehdr (m.LParam). Lpdata, Pwavehdr (m.LParam). dwbytesrecorded);
  {Continue recording} waveinaddbuffer (Hwavein, Pwavehdr (m.LParam), SizeOf (TWAVEHDR));
  End 
  {Processing playback messages}   Mm_wom_open:; 
  {This message only carries a device handle}  Mm_wom_close:; 
  {This message only carries a device handle} Mm_wom_done:begin {This message carries device handles and WAVEHDR pointers (LParam)} waveoutunprepareheader (Hwaveout, Pwavehdr (m.LParam), SizeOf ( 
   TWAVEHDR)); 
  Waveoutclose (hwaveout); 
 End 
End 
End

 End.

I hope this article will help you with your Delphi program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.