1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135:
| unit TAudioInputDemo;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, AudioIO, ExtCtrls, Buttons, ComCtrls, MMSYSTEM;
type TForm1 = class(TForm) StartButton: TButton; Timer1: TTimer; AudioIn1: TAudioIn; PaintBox1: TPaintBox; CheckBox1: TCheckBox; Label1: TLabel; Label2: TLabel; Label3: TLabel; procedure StartButtonClick(Sender: TObject); function AudioIn1BufferFilled(Buffer: PChar; var Size: Integer): Boolean; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } count : integer; end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject); begin count := 0; end;
procedure TForm1.StartButtonClick(Sender: TObject); begin if StartButton.Caption = 'Start' then begin If Not AudioIn1.Start Then ShowMessage(AudioIn1.ErrorMessage); StartButton.Caption := 'Stop'; end else begin AudioIn1.StopGracefully; StartButton.Caption := 'Start'; end; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin AudioIn1.StopGracefully; end;
// feste Einstellung: Puffergröße: 512 // Fenster: 512 x 256
function TForm1.AudioIn1BufferFilled(Buffer: PChar; var Size: Integer): Boolean; type puffer = array[0..511] of SmallInt; var zeiger : ^puffer; i, x, N : Integer; tm, dx : extended; vor, nach : SmallInt; crossings : integer; begin dx := AudioIn1.FrameRate*0.001; // vgl. Anzeige Label2: "1 ms/div" with PaintBox1.Canvas do begin FillRect(ClientRect); // t-Achse zeichnen: Pen.Color := clRed; MoveTo(0,128); LineTo(Width,128); // Tickmarks zeichnen: tm := 0; while tm < Width do begin MoveTo(round(tm),128-4); LineTo(round(tm),128+4); tm := tm + dx; end; Pen.Color := clLime; end; N := Size Div 2; zeiger := Pointer(Buffer); // zähle Nulldurchgänge: crossings := 0; vor := zeiger^[0] AND $8000; i := 1; while i < N do begin nach := vor; vor := zeiger^[i] AND $8000; if vor <> nach then inc(crossings); inc(i); end; // zeige Frequenz an: Label3.Caption := Format('%8.2f Hz', [crossings / 2 * AudioIn1.FrameRate / N]); // zeichne: i := 0; x := 0; // falls Triggerung eingeschaltet: if CheckBox1.Checked then // warte auf positiven Nulldurchgang: begin while zeiger^[i] < 0 do inc(i); while zeiger^[i] > 0 do inc(i); end; PaintBox1.Canvas.MoveTo(x,128+zeiger^[i] div 256); // jetzt zeichne wirklich: while i < N do begin PaintBox1.Canvas.LineTo(x,128+zeiger^[i] div 256); inc(i); inc(x); end; inc(count); Result := TRUE; end;
procedure TForm1.Timer1Timer(Sender: TObject); begin Label1.Caption := IntToStr(count)+' Bilder/sec'; count := 0; end;
end. |