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

📄 unit1.~pas

📁 Delphi Microphone real time Show , and Waveform Viewer
💻 ~PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes,graphics, Forms,
  Dialogs,mmsystem, StdCtrls,shellapi, Controls;

const tmpn=1024;
      tampon=tmpn-1;
type
TArrayBuf = array[0..tampon]of byte;
PArrayBuf = ^TArrayBuf;

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);

  private
    { Private declarations }
  public

  WaveFormat:TWaveFormatEx;
  WaveIn:PHWaveIn;
  IsOn:boolean;

Procedure WNDPROC(var msg:TMessage);override;
Procedure InitWaveIn(ac_kapa:boolean);

    { Public declarations }

end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

Procedure Tform1.WNDPROC(var msg:TMessage);
var Hdr:PWaveHdr;
    i,y:integer;
    r:real;
begin
     inherited;
case msg.Msg of
MM_WIM_OPEN: begin
             Button1.Enabled:=false;
             Button2.Enabled:=true;
             form2.Show;
             end;

MM_WIM_CLOSE: begin
              Button1.Enabled:=true;
              Button2.Enabled:=false;
              end;
MM_WIM_DATA: begin
             Hdr:=PWaveHdr(msg.LParam);
             if hdr^.dwBytesRecorded>0 then r:=form2.clientWidth/hdr^.dwBytesRecorded else r:=0;
         //  form2.Canvas.Brush.Color:=clGreen;
             PatBlt(form2.Canvas.Handle,0,0,form2.ClientWidth,form2.ClientHeight,BLACKNESS);
             y:=form2.ClientHeight div 2;
             form2.Canvas.Pen.Color:=clLime;
             form2.Canvas.MoveTo(0,y-127+PArrayBuf(hdr.lpData)^[0]);
             for i:=0 to hdr^.dwBytesRecorded-1 do
             form2.Canvas.lineTo(round(r*i),y-127+PArrayBuf(hdr.lpData)^[i]);

             WaveInUnprepareHeader(WaveIn^,hdr,Sizeof(TWaveHdr));
             Dispose(hdr.lpData);
             DisPose(hdr);
             if isOn=false then EXIT;

             Hdr:=new(PWaveHdr);
             Hdr^.lpData:=pointer(new(PArrayBuf));
             Hdr^.dwBufferLength:=tmpn;
             Hdr^.dwBytesRecorded:=0;
             Hdr^.dwUser:=0;
             Hdr^.dwFlags:=0;
             Hdr^.dwLoops:=0;

             WaveInPrepareHeader(WaveIn^,Hdr,Sizeof(TWaveHdr));
             WaveInAddBuffer(WaveIn^,hdr,Sizeof(TWaveHdr));
             end;
end;
end;

Procedure Tform1.InitWaveIn(ac_kapa:boolean);
var i:integer;
WaveHdr:PWaveHdr;
DaBuffer:PArrayBuf;
openError,PrepareError,BufferAddError,StartError:integer;
begin
if ac_kapa then
begin
WaveFormat.wFormatTag:=WAVE_FORMAT_PCM;
WaveFormat.nChannels:=1; //MONO
WaveFormat.nSamplesPerSec:=8000;
WaveFormat.wBitsPerSample:=8;
WaveFormat.nBlockAlign:=(WaveFormat.nChannels * WaveFormat.wBitsPerSample) div 8;
WaveFormat.nAvgBytesPerSec:=WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;

WaveIn:=new(PHWaveIn);//鰊emli
openError:=WaveInOpen(WaveIn,0,@WaveFormat,handle,0,CALLBACK_WINDOW);
for i:=1 to 8 do
begin
DaBuffer:=new(PArrayBuf);//鰊emli
WaveHdr:=new(PWaveHdr);//鰊emli
with WaveHdr^ do
begin
lpData:=pointer(DaBuffer);
dwBufferLength:=sizeof(DaBuffer);//tampon kadar
dwBytesRecorded:=0;
dwUser:=0;
dwFlags:=0;
dwLoops:=0;
end;
PrepareError:=WaveInPrepareHeader(WaveIn^,WaveHdr,sizeOf(TWaveHdr));
BufferAddError:=WaveInAddBuffer(WaveIn^,WaveHdr,Sizeof(TWaveHdr));
end;
StartError:=WaveInStart(WaveIn^);

if (openError<>0)or(PrepareError<>0)or(BufferAddError<>0)or(StartError<>0)then
Showmessage(
'WaveInOpen :'+inttostr(openError)(*Error 32 = Falsches Format*)+#13+//#13 = Zeilenumbruch
'WaveInPrepareHeader :'+inttostr(PrepareError)(*Error 5 = Nicht Ge鰂fnet*)+#13+
'WaveInAddBuffer :'+inttostr(BufferAddError)(*Error 5 = Nicht Ge鰂fnet*)+#13+
'StartError :'+inttostr(StartError)(*Error 5 = Nicht Ge鰂fnet*)+#13);

end else
begin
WaveInStop(WaveIn^);
if WaveIn<>nil then WaveInReset(WaveIn^);
if WaveIn<>nil then WaveInClose(WaveIn^);
Dispose(WaveIn);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
IsOn:=true;
InitWaveIn(true); //Kayda ba

⌨️ 快捷键说明

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