{ ------------------------------------------------------------------------------ }
{ }
{ Wave Audio Package - Recorder / Player Demo }
{ by Kambiz R. Khojasteh }
{ }
{ kambiz@delphiarea.com }
{ http://www.delphiarea.com }
{ }
{ ------------------------------------------------------------------------------ }

unit uRecorderPlayer;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, WaveIn, WaveRecorders, WaveIO, WaveOut,
  WavePlayers, StdCtrls, Buttons, ExtCtrls, mmSystem, WaveUtils,
  WaveStorage;

type
  TfrmRecorderPlayer = class(TForm)
    btnRecord: TBitBtn;
    btnPlay: TBitBtn;
    btnPause: TBitBtn;
    btnStop: TBitBtn;
    lblWaveFile: TLabel;
    pbAudioLevel: TProgressBar;
    lblPosition: TLabel;
    StockAudioPlayer: TStockAudioPlayer;
    StockAudioRecorder: TStockAudioRecorder;
    lblPositionID: TLabel;
    cbFormat: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure cbFormatChange(Sender: TObject);
    procedure btnRecordClick(Sender: TObject);
    procedure btnPlayClick(Sender: TObject);
    procedure btnPauseClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure StockAudioPlayerActivate(Sender: TObject);
    procedure StockAudioPlayerDeactivate(Sender: TObject);
    procedure StockAudioPlayerLevel(Sender: TObject; Level: Integer);
    procedure StockAudioRecorderActivate(Sender: TObject);
    procedure StockAudioRecorderDeactivate(Sender: TObject);
    procedure StockAudioRecorderLevel(Sender: TObject; Level: Integer);
  end;

var
  frmRecorderPlayer: TfrmRecorderPlayer;
 FileName:String;
implementation

{$R *.dfm}

procedure TfrmRecorderPlayer.FormCreate(Sender: TObject);
var
  pcm: TPCMFormat;
  WaveFormatEx: TWaveFormatEx;
begin
   FileName := 'Test.WAV';
  for pcm := Succ(Low(TPCMFormat)) to High(TPCMFormat) do
  begin
    SetPCMAudioFormatS(@WaveFormatEx, pcm);
    cbFormat.Items.Append(GetWaveAudioFormat(@WaveFormatEx));
  end;
  cbFormat.ItemIndex := Ord(StockAudioRecorder.PCMFormat) - 1;
  lblWaveFile.Caption := FileName;
  btnRecord.Enabled := True;
  btnPlay.Enabled := FileExists(FileName);

end;

procedure TfrmRecorderPlayer.cbFormatChange(Sender: TObject);
begin
  StockAudioRecorder.PCMFormat := TPCMFormat(cbFormat.ItemIndex + 1);
end;

procedure TfrmRecorderPlayer.btnRecordClick(Sender: TObject);
begin
  StockAudioRecorder.RecordToFile(FileName);
end;

procedure TfrmRecorderPlayer.btnPlayClick(Sender: TObject);
begin
  StockAudioPlayer.PlayFile(FileName);
end;

procedure TfrmRecorderPlayer.btnPauseClick(Sender: TObject);
begin
  if StockAudioPlayer.Active then
    StockAudioPlayer.Paused := not StockAudioPlayer.Paused
  else
    StockAudioRecorder.Paused := not StockAudioRecorder.Paused;
end;

procedure TfrmRecorderPlayer.btnStopClick(Sender: TObject);
begin
  if StockAudioPlayer.Active then
    StockAudioPlayer.Active := False
  else
    StockAudioRecorder.Active := False;
end;

procedure TfrmRecorderPlayer.StockAudioPlayerActivate(Sender: TObject);
begin
  cbFormat.Enabled := False;

  btnPause.Enabled := True;
  btnStop.Enabled := True;
  btnRecord.Enabled := False;
  btnPlay.Enabled := False;
  btnStop.SetFocus;
end;

procedure TfrmRecorderPlayer.StockAudioPlayerDeactivate(Sender: TObject);
begin
  cbFormat.Enabled := True;

  btnRecord.Enabled := True;
  btnPlay.Enabled := True;
  btnPause.Enabled := False;
  btnStop.Enabled := False;
  btnPlay.SetFocus;
end;

procedure TfrmRecorderPlayer.StockAudioPlayerLevel(Sender: TObject; Level: Integer);
begin
  lblPosition.Caption := MS2Str(StockAudioPlayer.Position, msAh);
  pbAudioLevel.Position := Level;
end;

procedure TfrmRecorderPlayer.StockAudioRecorderActivate(Sender: TObject);
begin
  cbFormat.Enabled := False;

  btnPause.Enabled := True;
  btnStop.Enabled := True;
  btnRecord.Enabled := False;
  btnPlay.Enabled := False;
  btnStop.SetFocus;
end;

procedure TfrmRecorderPlayer.StockAudioRecorderDeactivate(Sender: TObject);
begin
  cbFormat.Enabled := True;

  btnRecord.Enabled := True;
  btnPlay.Enabled := True;
  btnPause.Enabled := False;
  btnStop.Enabled := False;
  btnPlay.SetFocus;
  lblPosition.Caption := '0.00';
end;

procedure TfrmRecorderPlayer.StockAudioRecorderLevel(Sender: TObject; Level: Integer);
begin
  lblPosition.Caption := MS2Str(StockAudioRecorder.Position, msAh);
  pbAudioLevel.Position := Level;
end;

end.
