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

📄 main.pas

📁 delphi下的声音记录源程序
💻 PAS
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, OleCtrls, AMIWAVEPROLib_TLB, reg, Menus;

type
  TFormamSoundRecorder = class(TForm)
    GroupBox1: TGroupBox;
    FrameVolumeControls: TGroupBox;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    GroupBox5: TGroupBox;
    CommandPlay: TButton;
    CommandPause: TButton;
    CommandStop: TButton;
    CommandRecord: TButton;
    CommandChangeConvertFormat: TButton;
    CommandConvert: TButton;
    CommandChangeFormat: TButton;
    ProgressBarLevel: TProgressBar;
    LabelPlayTag: TLabel;
    LabelPlayAttributes: TLabel;
    LabelLength: TLabel;
    LabelPlayVolume: TLabel;
    LabelRecordVolume: TLabel;
    SliderPlayVolume: TTrackBar;
    StatusBar: TStatusBar;
    amWavePro1: TamWavePro;
    LabelConvertTag: TLabel;
    LabelConvertAttributes: TLabel;
    LabelRecordTag: TLabel;
    LabelRecordAttributes: TLabel;
    OptionRecordFile: TRadioButton;
    OptionRecordNull: TRadioButton;
    CheckSilenceDeletion: TCheckBox;
    Label1: TLabel;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Options1: TMenuItem;
    Open1: TMenuItem;
    Save1: TMenuItem;
    Saveas1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    AudioDevices1: TMenuItem;
    CommonDialog1: TOpenDialog;
    SliderRecordVolume: TTrackBar;
    SaveDialog: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure AudioDevices1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Open1Click(Sender: TObject);
    procedure CommandPlayClick(Sender: TObject);
    procedure CommandPauseClick(Sender: TObject);
    procedure CommandStopClick(Sender: TObject);
    procedure CommandRecordClick(Sender: TObject);
    procedure CommandChangeConvertFormatClick(Sender: TObject);
    procedure CommandConvertClick(Sender: TObject);
    procedure CommandChangeFormatClick(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure SliderPlayVolumeChange(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Saveas1Click(Sender: TObject);
    procedure CheckSilenceDeletionClick(Sender: TObject);
    procedure amWavePro1ConvertDone(Sender: TObject);
    procedure amWavePro1ConvertError(Sender: TObject; ErrNumber: Integer;
      const ErrDescription: WideString);
    procedure amWavePro1DonePlay(Sender: TObject);
    procedure amWavePro1DoneRecord(Sender: TObject);
    procedure amWavePro1Silence(Sender: TObject; Silence: WordBool);
    procedure amWavePro1VUChange(Sender: TObject; PlayVolume,
      RecordLevel: Smallint);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure DisableRecordFeatures();
procedure DisablePlayFeatures();

var
  FormamSoundRecorder: TFormamSoundRecorder;
  WorkFile : String;
  OpenedFiles : String;
  ConvertOutputFile : String;
  RecordOutputFile : String;
  ConvertPlayConvert : String;
  PlayVol:integer;
  RecordVol:integer;
  
const
  AppName='Am Sound Recorder';

implementation

uses AudioDev;

{$R *.dfm}

procedure TFormamSoundRecorder.FormCreate(Sender: TObject);
var
  RecordFormat : String;
  ConvertFormat : String;
begin
  PlayVol:=amWavePro1.PlayVolume;
  RecordVol:=amWavePro1.RecordLevel;
  SliderPlayVolume.Position:=(SliderPlayVolume.Max-SliderPlayVolume.Min) div 2;
  SliderRecordVolume.Position:=(SliderRecordVolume.Max-SliderRecordVolume.Min) div 2;
  amWavePro1.PlayVolume :=PlayVol;
  amWavePro1.RecordLevel:=RecordVol;
  amWavePro1.RecordFormat := amUserSelect;
  RecordFormat := GetSetting(AppName, 'Settings', 'Record Format', '');
  if RecordFormat = '' then
      //set a default record format, value can determined reading RecordUserFormat once a format has been selected
      amWavePro1.RecordUserFormat := '01000100401F0000803E0000020010000000'
  else
      amWavePro1.RecordUserFormat := RecordFormat;
  //DisableRecordFeatures;
  LabelRecordAttributes.Caption := amWavePro1.RecordAttributes;
  LabelRecordTag.Caption := amWavePro1.RecordTag;
  ConvertFormat := GetSetting(AppName, 'Settings', 'Convert Format', '');
  if (ConvertFormat = '') then
      //set a default record format, value can determined reading RecordUserFormat once a format has been selected
      amWavePro1.ConvertFormat := '01000100401F0000803E0000020010000000'
  else
      amWavePro1.ConvertFormat := ConvertFormat;
  LabelConvertAttributes.Caption := amWavePro1.ConvertAttributes;
  LabelConvertTag.Caption := amWavePro1.ConvertTag;
  ConvertOutputFile := 'TempC.wav';
  RecordOutputFile := 'TempR.wav';
  ConvertPlayConvert :='TempCPC.wav';
end;

procedure TFormamSoundRecorder.AudioDevices1Click(Sender: TObject);
begin
 FormAudioDevices.Show;
end;

procedure TFormamSoundRecorder.Exit1Click(Sender: TObject);
begin
 Self.Close;
end;

procedure TFormamSoundRecorder.Open1Click(Sender: TObject);
var FileLength,FileHandle:integer;
begin
  CommonDialog1.Execute;
  //Display name of selected file
  OpenedFiles := CommonDialog1.filename;
  if CommonDialog1.FileName <> '' then
   begin
    WorkFile := CommonDialog1.filename;
    CommandPlay.Enabled := True;
    CommandConvert.Enabled := True;
    amWavePro1.PlayFilename := WorkFile;
    LabelPlayAttributes.Caption := amWavePro1.PlayAttributes;
    LabelPlayTag.Caption := amWavePro1.PlayTag;
    Saveas1.Enabled := True;
    Self.Caption := OpenedFiles + ' - Am Sound Recorder';
    FileHandle:=FileOpen(WorkFile,fmOpenRead);  //Open file.
    FileLength := GetFileSize(FileHandle,nil);   //Get length of file.
    LabelLength.Caption := InttoStr(FileLength) + ' Bytes';
    FileClose(FileHandle);   //Close file.
    DisablePlayFeatures;
   end; 

end;

procedure TFormamSoundRecorder.CommandPlayClick(Sender: TObject);
var WaveOutID:integer;
begin
  if (WorkFile = '') then
   exit;
  WaveOutID := GetSetting(AppName, 'Settings', 'Wave Output ID', -1);
  if WaveOutID = -1 then
   begin
    ShowMessage ('Select ''Play to this device''');
    AudioDevices1Click(Self);
    Exit;
   end;
  amWavePro1.PlayBufferLength := 1;
  amWavePro1.Play(WaveOutID);
  CommandStop.Enabled := True;
  CommandPlay.Enabled := False;
  CommandRecord.Enabled := False;
  CommandPause.Enabled := True;
end;

procedure TFormamSoundRecorder.CommandPauseClick(Sender: TObject);
begin
    if amWavePro1.PlayBusy then
        if amWavePro1.PlayPaused then
         begin
            amWavePro1.PlayPaused := False;
            CommandPause.Caption := 'Pause';
         end
        else
         begin
            amWavePro1.PlayPaused := True;
            CommandPause.Caption := 'Resume';
         end;
    if amWavePro1.RecordBusy then
        if amWavePro1.RecordPaused then
         begin
            amWavePro1.RecordPaused := False;
            CommandPause.Caption := 'Pause';
         end
        else
         begin
            amWavePro1.RecordPaused := True;
            CommandPause.Caption := 'Resume';
         end;
end;

procedure TFormamSoundRecorder.CommandStopClick(Sender: TObject);
begin
  amWavePro1.StopPlay;
  amWavePro1.StopRecord;
end;

procedure TFormamSoundRecorder.CommandRecordClick(Sender: TObject);
var WaveInputID : integer;
begin
    if OptionRecordFile.Checked then
        amWavePro1.RecordFilename := RecordOutputFile
    else
        amWavePro1.RecordFilename := '';
    WaveInputID := GetSetting(AppName, 'Settings', 'Wave Input ID', -1);
    if WaveInputID = -1 then
     begin
        ShowMessage ('Select ''Record to this device''');
        AudioDevices1Click(Self);
        Exit;
     end;
    amWavePro1.Record_(WaveInputID);
    CommandStop.Enabled := True;
    CommandRecord.Enabled := False;
    CommandPlay.Enabled := False;
    CommandPause.Enabled := True;
end;

procedure TFormamSoundRecorder.CommandChangeConvertFormatClick(
  Sender: TObject);
begin
  amWavePro1.ShowConvertFormatDialog(Self.Handle);
  LabelConvertAttributes.Caption := amWavePro1.ConvertAttributes;
  LabelConvertTag.Caption := amWavePro1.ConvertTag;
  //Save in the registry the Convert Format
  SaveSetting(AppName, 'Settings', 'Convert Format', amWavePro1.ConvertFormat);
end;

procedure TFormamSoundRecorder.CommandConvertClick(Sender: TObject);
begin
  amWavePro1.Convert(WorkFile, ConvertOutputFile);
end;

procedure TFormamSoundRecorder.CommandChangeFormatClick(Sender: TObject);
begin
  amWavePro1.ShowRecordFormatDialog(Self.Handle);
  LabelRecordAttributes.Caption := amWavePro1.RecordAttributes;
  LabelRecordTag.Caption := amWavePro1.RecordTag;
  //Save in the registry the Record Format
  SaveSetting (AppName, 'Settings', 'Record Format', amWavePro1.RecordUserFormat);
  DisableRecordFeatures;
end;

procedure TFormamSoundRecorder.TrackBar1Change(Sender: TObject);
begin
  amWavePro1.PlayVolume := SliderPlayVolume.Position - 100;
end;

procedure TFormamSoundRecorder.SliderPlayVolumeChange(Sender: TObject);
begin
  amWavePro1.RecordLevel := SliderRecordVolume.Position - 100;
end;

procedure DisableRecordFeatures();
begin
  //Certain features are only available when working with PCM Mono files, these
  //features are specifically designed (althought not exclusively) for telephony use.
  if ((FormamSoundRecorder.amWavePro1.PlayTag = 'PCM') And (Pos('MONO', UpperCase(FormamSoundRecorder.amWavePro1.PlayAttributes)) > 0)) then
   begin
      FormamSoundRecorder.LabelRecordVolume.Enabled := True;
      FormamSoundRecorder.SliderRecordVolume.Enabled := True;
      FormamSoundRecorder.OptionRecordFile.Enabled := True;
      FormamSoundRecorder.OptionRecordNull.Enabled := True;
      FormamSoundRecorder.CheckSilenceDeletion.Enabled := True;
      FormamSoundRecorder.FrameVolumeControls.Enabled := True;
      FormamSoundRecorder.StatusBar.SimpleText := 'SILENCE';
    end
  else
   begin
      FormamSoundRecorder.LabelRecordVolume.Enabled := False;
      FormamSoundRecorder.SliderRecordVolume.Enabled := False;
      FormamSoundRecorder.OptionRecordFile.Enabled := False;
      FormamSoundRecorder.OptionRecordNull.Enabled := False;
      FormamSoundRecorder.CheckSilenceDeletion.Enabled := False;
      if not FormamSoundRecorder.LabelPlayVolume.Enabled then
       FormamSoundRecorder.FrameVolumeControls.Enabled := False;
      FormamSoundRecorder.StatusBar.SimpleText := '';
    end;
end;

procedure DisablePlayFeatures();
begin
  //Certain features are only available when working with PCM Mono files, these
  //features are specifically designed (althought not exclusively) for telephony use.
  if ((FormamSoundRecorder.amWavePro1.PlayTag = 'PCM') And (Pos('MONO', UpperCase(FormamSoundRecorder.amWavePro1.PlayAttributes)) > 0)) then
     begin
      FormamSoundRecorder.LabelPlayVolume.Enabled := True;
      FormamSoundRecorder.SliderPlayVolume.Enabled := True;
      FormamSoundRecorder.FrameVolumeControls.Enabled := True;
   end
  else
   begin
      FormamSoundRecorder.LabelPlayVolume.Enabled := False;
      FormamSoundRecorder.SliderPlayVolume.Enabled := False;
      if Not FormamSoundRecorder.LabelRecordVolume.Enabled Then
       FormamSoundRecorder.FrameVolumeControls.Enabled := False;
   end;
end;


procedure TFormamSoundRecorder.Save1Click(Sender: TObject);
begin
 CopyFile(Pchar(WorkFile), Pchar(OpenedFiles), False);
end;

procedure TFormamSoundRecorder.Saveas1Click(Sender: TObject);
begin
  //Specify default file name
  SaveDialog.filename := OpenedFiles;
  //Display the Open dialog box
  SaveDialog.Execute;
  OpenedFiles := SaveDialog.filename;
  CopyFile(Pchar(WorkFile), Pchar(OpenedFiles), False);
  WorkFile := OpenedFiles;
  Self.Caption := OpenedFiles + ' - Am Sound Recorder';
end;

procedure TFormamSoundRecorder.CheckSilenceDeletionClick(Sender: TObject);
begin
  amWavePro1.SilenceDeletion := CheckSilenceDeletion.Checked;
end;

procedure TFormamSoundRecorder.amWavePro1ConvertDone(Sender: TObject);
var FileLength,FileHandle:integer;
begin
  //test to see if ConvertOutputFile is good, test for error before we copy
  amWavePro1.PlayFilename := ConvertOutputFile;
  CopyFile(Pchar(ConvertOutputFile), Pchar(ConvertPlayConvert),False);
  WorkFile := ConvertPlayConvert;
  amWavePro1.PlayFilename := WorkFile;
  Save1.Enabled := True;
  LabelPlayAttributes.Caption := amWavePro1.PlayAttributes;
  LabelPlayTag.Caption := amWavePro1.PlayTag;
  FileHandle:=FileOpen(WorkFile,fmOpenRead);  //Open file.
  FileLength := GetFileSize(FileHandle,nil);   //Get length of file.
  LabelLength.Caption := InttoStr(FileLength) + ' Bytes';
  FileClose(FileHandle);   //Close file.
  ShowMessage ('Conversion Done' + #13#10 + 'You can now play the converted file');
end;

procedure TFormamSoundRecorder.amWavePro1ConvertError(Sender: TObject;
  ErrNumber: Integer; const ErrDescription: WideString);
begin
  ShowMessage ('Conversion Error No: ' + IntToStr(Errnumber));
end;

procedure TFormamSoundRecorder.amWavePro1DonePlay(Sender: TObject);
begin
  CommandStop.Enabled := False;
  CommandPlay.Enabled := True;
  CommandRecord.Enabled := True;
  CommandPause.Enabled := False;
  CommandPause.Caption := 'Pause';
end;

procedure TFormamSoundRecorder.amWavePro1DoneRecord(Sender: TObject);
var FileLength,FileHandle:integer;
begin
  CommandStop.Enabled := False;
  CommandRecord.Enabled := True;
  CommandPlay.Enabled := True;
  CommandPause.Enabled := False;
  CommandPause.Caption := 'Pause';
  CommandPlay.Enabled := True;
  if OptionRecordFile.Checked then
    begin
      WorkFile := RecordOutputFile;
      amWavePro1.PlayFilename := WorkFile;
      DisablePlayFeatures;
      LabelPlayAttributes.Caption := amWavePro1.PlayAttributes;
      LabelPlayTag.Caption := amWavePro1.PlayTag;
      FileHandle:=FileOpen(WorkFile,fmOpenRead);  //Open file.
      FileLength := GetFileSize(FileHandle,nil);   //Get length of file.
      LabelLength.Caption := InttoStr(FileLength) + ' Bytes';
      FileClose(FileHandle);   //Close file.
      CommandConvert.Enabled := True;
      Save1.Enabled := True;
      Saveas1.Enabled := True;
    end;
end;

procedure TFormamSoundRecorder.amWavePro1Silence(Sender: TObject;
  Silence: WordBool);
begin
  if Silence then
      StatusBar.SimpleText := 'SILENCE'
  else
      StatusBar.SimpleText := 'NOISE';
end;

procedure TFormamSoundRecorder.amWavePro1VUChange(Sender: TObject;
  PlayVolume, RecordLevel: Smallint);
begin
  ProgressBarLevel.Position := PlayVolume + RecordLevel;
end;

end.

⌨️ 快捷键说明

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