MMWPlay.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:16k
- {========================================================================}
- {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
- {========================================================================}
- {= All Rights Reserved =}
- {========================================================================}
- {= D 01099 Dresden = Fax.: +49(0)351-8037944 =}
- {= Loewenstr.7a = info@swiftsoft.de =}
- {========================================================================}
- {= Actual versions on http://www.swiftsoft.de/index.html =}
- {========================================================================}
- {= This code is for reference purposes only and may not be copied or =}
- {= distributed in any format electronic or otherwise except one copy =}
- {= for backup purposes. =}
- {= =}
- {= No Delphi Component Kit or Component individually or in a collection=}
- {= subclassed or otherwise from the code in this unit, or associated =}
- {= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
- {= without express permission from SwiftSoft. =}
- {= =}
- {= For more licence informations please refer to the associated =}
- {= HelpFile. =}
- {========================================================================}
- {= $Date: 09.10.98 - 17:23:11 $ =}
- {========================================================================}
- unit MMWPlay;
- {$I COMPILER.INC}
- interface
- uses
- {$IFDEF WIN32}
- Windows,
- {$ELSE}
- WinTypes,
- WinProcs,
- {$ENDIF}
- SysUtils,
- Classes,
- Controls,
- Dialogs,
- MMSystem,
- MMObj,
- MMDSPobj,
- MMRegs,
- MMUtils,
- MMWave,
- MMACMCvt,
- MMWavOut,
- MMCstDlg,
- MMADCvt;
- type
- {-- TMMWavePlayer ---------------------------------------------------------}
- TMMWavePlayer = class(TMMDSPComponent)
- private
- FStartPos : Longint;
- FStopPos : Longint;
- FSeeking : Boolean;
- FResetPos : Boolean;
- FAutoConvert: Boolean;
- FOnStart : TNotifyEvent;
- FOnStop : TNotifyEvent;
- FOnPause : TNotifyEvent;
- FOnRestart : TNotifyEvent;
- FOnChange : TNotifyEvent;
- FOnData : TMMBufferEvent;
- procedure SetWave(aValue: TMMWave);
- function GetWave: TMMWave;
- procedure SetNumBuffers(aValue: integer);
- function GetNumBuffers: integer;
- procedure SetDeviceID(aValue: TMMDeviceID);
- function GetDeviceID: TMMDeviceID;
- procedure SetProductName(aValue: string);
- function GetProductName: string;
- procedure SetTimeFormat(aValue: TMMTimeFormats);
- function GetTimeFormat: TMMTimeFormats;
- procedure SetCallBackMode(aValue: TMMCBMode);
- function GetCallBackMode: TMMCBMode;
- procedure SetPosition(aValue: Longint);
- function GetPosition: Longint;
- procedure SetLooping(aValue: Boolean);
- function GetLooping: Boolean;
- procedure SetLoopCount(aValue: Word);
- function GetLoopCount: Word;
- procedure SetFileName(aValue: TFileName);
- function GetFileName: TFileName;
- function GetState: TMMWaveOutState;
- procedure SetAutoConvert(aValue: Boolean);
- procedure DoChange(Sender: TObject);
- procedure DoChanged(Sender: TObject);
- procedure DoStart(Sender: TObject);
- procedure DoStop(Sender: TObject);
- procedure DoClose(Sender: TObject);
- procedure DoPause(Sender: TObject);
- procedure DoRestart(Sender: TObject);
- procedure DoData(Sender: TObject; lpwh: PWaveHdr);
- protected
- FWaveFile : TMMWaveFile;
- FADPCMConvert: TMMADPCMConverter;
- FPCMConvert : TMMPCMConverter;
- FWaveOut : TMMWaveOut;
- function GetBufferSize: Longint; override;
- procedure SetBufferSize(aValue: Longint); override;
- public
- constructor Create(aOwner: TComponent); override;
- destructor Destroy; override;
- function SelectFile: Boolean;
- procedure LoadFromFile(const FileName: TFileName);
- procedure Play;
- procedure Stop;
- procedure Pause;
- procedure Restart;
- property FileName: TFileName read GetFileName write SetFileName stored False;
- property PWaveFormat;
- property State: TMMWaveOutState read GetState;
- property Position: Longint read GetPosition write SetPosition;
- published
- property OnStart: TNotifyEvent read FOnStart write FOnStart;
- property OnStop: TNotifyEvent read FOnStop write FOnStop;
- property OnPause: TNotifyEvent read FOnPause write FOnPause;
- property OnRestart: TNotifyEvent read FOnRestart write FOnRestart;
- property OnChange: TNotifyEvent read FOnChange write FOnChange;
- property OnData: TMMBufferEvent read FOnData write FOnData;
- property Output;
- property Wave: TMMWave read GetWave write SetWave;
- property BufferSize: Longint read GetBufferSize write SetBufferSize;
- property NumBuffers: integer read GetNumBuffers write SetNumBuffers;
- property DeviceID: TMMDeviceID read GetDeviceID write SetDeviceID;
- property ProductName: string read GetProductName write SetProductName stored False;
- property TimeFormat: TMMTimeFormats read GetTimeFormat write SetTimeFormat;
- property CallBackMode: TMMCBMode read GetCallBackMode write SetCallBackMode;
- property Looping: Boolean read GetLooping write SetLooping;
- property LoopCount: Word read GetLoopCount write SetLoopCount;
- property ResetPosition: Boolean read FResetPos write FResetPos default True;
- property AutoConvert: Boolean read FAutoConvert write SetAutoConvert default True;
- end;
- implementation
- {== MMWavePlayer ==============================================================}
- constructor TMMWavePlayer.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- FSeeking := False;
- FStartPos := 0;
- FResetPos := True;
- FAutoConvert := True;
- FWaveFile := TMMWaveFile.Create(Self);
- FWaveFile.OnChange := DoChange;
- FWaveFile.OnChanged := DoChanged;
- FADPCMConvert := TMMADPCMConverter.Create(Self);
- FADPCMConvert.Input := FWaveFile;
- FPCMConvert := TMMPCMConverter.Create(Self);
- FPCMConvert.Input := FADPCMConvert;
- FWaveOut := TMMWaveOut.Create(Self);
- FWaveOut.Input := FPCMConvert;
- FWaveOut.OnOpen := DoStart;
- FWaveOut.OnStop := DoStop;
- FWaveOut.OnClose := DoClose;
- FWaveOut.OnPause := DoPause;
- FWaveOut.OnRestart := DoRestart;
- FWaveOut.OnBufferReady := DoData;
- FInputValid := True;
- FWaveOut.Output := Self;
- FInputValid := False;
- ErrorCode := ComponentRegistered(InitCode, Self, ClassName);
- if (ErrorCode <> 0) then RegisterFailed(InitCode, Self , ClassName);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- destructor TMMWavePlayer.Destroy;
- begin
- Stop;
- FWaveFile.Free;
- FADPCMConvert.Free;
- FPCMConvert.Free;
- FWaveOut.Free;
- inherited Destroy;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetWave(aValue: TMMWave);
- begin
- FWaveFile.Wave := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetWave: TMMWave;
- begin
- Result := FWaveFile.Wave;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.SelectFile: Boolean;
- begin
- with TMMWaveOpenDialog.Create(nil) do
- try
- FileName := Self.FileName;
- Result := Execute;
- if Result then
- Self.FileName := FileName;
- finally
- Free;
- end;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.LoadFromFile(const FileName: TFileName);
- begin
- SetFileName(FileName);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetFileName(aValue: TFileName);
- begin
- FWaveFile.Wave.FileName := aValue;
- FADPCMConvert.Enabled := FAutoConvert and FADPCMConvert.CanConvert;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetFileName: TFileName;
- begin
- Result := FWaveFile.Wave.FileName;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetBufferSize(aValue: Longint);
- begin
- FWaveOut.BufferSize := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetBufferSize: Longint;
- begin
- Result := FWaveOut.BufferSize;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetNumBuffers(aValue: integer);
- begin
- FWaveOut.NumBuffers := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetNumBuffers: integer;
- begin
- Result := FWaveOut.NumBuffers;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetDeviceID(aValue: TMMDeviceID);
- begin
- FWaveOut.DeviceID := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetDeviceID: TMMDeviceID;
- begin
- Result := FWaveOut.DeviceID;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetProductName(aValue: string);
- begin
- FWaveOut.ProductName := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetProductName: string;
- begin
- Result := FWaveOut.ProductName;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetTimeFormat(aValue: TMMTimeFormats);
- begin
- FWaveOut.TimeFormat := aValue;
- FWaveFile.Wave.TimeFormat := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetTimeFormat: TMMTimeFormats;
- begin
- Result := FWaveOut.TimeFormat;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetCallBackMode(aValue: TMMCBMode);
- begin
- FWaveOut.CallBackMode := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetCallBackMode: TMMCBMode;
- begin
- Result := FWaveOut.CallBackMode;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetLooping(aValue: Boolean);
- begin
- FWaveOut.Looping := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetLooping: Boolean;
- begin
- Result := FWaveOut.Looping;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetLoopCount(aValue: Word);
- begin
- FWaveOut.LoopCount := aValue;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetLoopCount: Word;
- begin
- Result := FWaveOut.LoopCount;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetState: TMMWaveOutState;
- begin
- Result := FWaveOut.State;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetAutoConvert(aValue: Boolean);
- begin
- if (aValue <> FAutoConvert) then
- begin
- if (wosOpen in FwaveOut.State) then
- raise Exception.Create(LoadResStr(IDS_PROPERTYOPEN));
- FAutoConvert := aValue;
- FPCMConvert.Enabled := aValue;
- FADPCMConvert.Enabled := aValue;
- end;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.SetPosition(aValue: Longint);
- var
- wasPaused: Boolean;
- begin
- if (wosOpen in State) then
- begin
- FSeeking := True;
- wasPaused := (wosPause in State);
- FWaveOut.Pause;
- FWaveFile.Wave.Position := aValue;
- FStartPos := aValue;
- FWaveOut.Reset;
- if not wasPaused then FWaveOut.Restart;
- FSeeking := False;
- end
- else
- begin
- FWaveFile.Wave.Position := aValue;
- end;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- function TMMWavePlayer.GetPosition: Longint;
- begin
- if (wosPlay in State) then
- begin
- Result := FStartPos+FWaveOut.Position;
- end
- else
- begin
- Result := FWaveFile.Wave.Position;
- end;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoChange(Sender: TObject);
- begin
- Stop;
- if assigned(FOnChange) then FOnChange(Self);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoChanged(Sender: TObject);
- begin
- FADPCMConvert.Enabled := FAutoConvert and FADPCMConvert.CanConvert;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoStart(Sender: TObject);
- begin
- if assigned(FOnStart) then FOnStart(Self);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoStop(Sender: TObject);
- begin
- if (FStopPos = -1) then FStopPos := Position;
- FWaveOut.Close;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoClose(Sender: TObject);
- begin
- if not FResetPos then Position := FStopPos;
- if assigned(FOnStop) then FOnStop(Self);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoPause(Sender: TObject);
- begin
- if assigned(FOnPause) and not FSeeking then FOnPause(Self);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoRestart(Sender: TObject);
- begin
- if assigned(FOnRestart) and not FSeeking then FOnRestart(Self);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.DoData(Sender: TObject; lpwh: PWaveHdr);
- begin
- if assigned(FOnData) then FOnData(Self,lpwh);
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.Play;
- begin
- if not (wosPlay in State) and not FWaveFile.Wave.Empty then
- begin
- if (Position >= FWaveFile.Wave.DataSize) then Position := 0;
- FStartPos := Position;
- FStopPos := -1;
- FWaveOut.Start;
- end;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.Stop;
- begin
- FStopPos := Position;
- FWaveOut.Stop;
- FWaveOut.Close;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.Pause;
- begin
- if not (wosPause in State) and not FWaveFile.Wave.Empty then FWaveOut.Pause;
- end;
- {-- TMMWavePlayer -------------------------------------------------------------}
- procedure TMMWavePlayer.Restart;
- begin
- FWaveOut.Restart;
- end;
- end.