MMVolume.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:8k
- {========================================================================}
- {= (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: 17.09.98 - 12:19:42 $ =}
- {========================================================================}
- unit MMVolume;
- {$I COMPILER.INC}
- interface
- uses
- {$IFDEF WIN32}
- Windows,
- {$ELSE}
- WinTypes,
- WinProcs,
- {$ENDIF}
- SysUtils,
- Classes,
- Controls,
- MMSystem,
- MMRegs,
- MMObj,
- MMDSPObj,
- MMUtils,
- MMWaveIO,
- MMPCMSup;
- type
- EMMVolumeError = class(Exception);
- {-- TMMVolume ---------------------------------------------------------}
- TMMVolume = class(TMMDSPComponent)
- private
- FEnabled : Boolean;
- FOpen : Boolean;
- FOverflow : Boolean; { overflow detected }
- FVolume : Longint; { 0 = silence, 16384 = 0 dB, 32768 = +6dB }
- FPanning : Longint; { L <- -16384..0..16384 -> R }
- FLeftVolume : Longint;
- FRightVolume : Longint;
- procedure SetEnabled(aValue: Boolean);
- procedure SetVolumeValues(index: integer; aValue: Longint);
- protected
- procedure SetPWaveFormat(aValue: PWaveFormatEx); override;
- procedure Opened; override;
- procedure Closed; override;
- procedure Started; override;
- procedure Stopped; override;
- procedure Paused; override;
- procedure Restarted; override;
- procedure Reseting; override;
- procedure BufferReady(lpwh: PWaveHdr); override;
- procedure BufferLoad(lpwh: PWaveHdr; var MoreBuffers: Boolean); override;
- public
- constructor Create(aOwner: TComponent); override;
- destructor Destroy; override;
- procedure Open;
- procedure Close;
- procedure Process(Buffer: PChar; Length: integer);
- property Overflow: Boolean read FOverflow write FOverflow;
- published
- property Input;
- property Output;
- property Enabled: Boolean read FEnabled write SetEnabled default True;
- property Volume: Longint index 0 read FVolume write SetVolumeValues default VOLUMEBASE;
- property Panning: Longint index 1 read FPanning write SetVolumeValues default 0;
- end;
- implementation
- {== TMMVolume ============================================================}
- constructor TMMVolume.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- FEnabled := True;
- FOpen := False;
- FLeftVolume := VOLUMEBASE;
- FRightVolume := VOLUMEBASE;
- FVolume := VOLUMEBASE;
- FPanning := 0;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- destructor TMMVolume.Destroy;
- begin
- Close;
- inherited Destroy;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.SetEnabled(aValue: Boolean);
- begin
- if (aValue <> FEnabled) then
- begin
- FEnabled := aValue;
- end;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.SetVolumeValues(index: integer; aValue: Longint);
- begin
- { calc the volumes for the left and right channel }
- case index of
- 0: if (FVolume = aValue) then exit else FVolume := MinMax(aValue,0,2*VOLUMEBASE);
- 1: if (FPanning = aValue) then exit else FPanning:= MinMax(aValue,-VOLUMEBASE,VOLUMEBASE);
- end;
- CalcVolume(VOLUMEBASE, FVolume,FPanning, FLeftVolume, FRightVolume);
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.SetPWaveFormat(aValue: PWaveFormatEx);
- begin
- { we can only handle PCM format }
- if (aValue <> nil) then
- begin
- if not (csDesigning in ComponentState) then
- if not pcmIsValidFormat(aValue) then
- raise EMMVolumeError.Create(LoadResStr(IDS_INVALIDFORMAT));
- end;
- inherited SetPWaveFormat(aValue);
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Open;
- begin
- if not FOpen then
- begin
- if pcmIsValidFormat(PWaveFormat) then
- begin
- // determine the maximal possible datasize we might receive in the events
- // FMaxDatasize := Max(BufferSize,Max(QUEUE_READ_SIZE,QUEUE_WRITE_SIZE));
- // DataBuffer := GlobalAllocMem(FMaxDataSize);
- FOpen := True;
- end;
- end;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Close;
- begin
- if FOpen then
- begin
- FOpen := False;
- end;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Process(Buffer: PChar; Length: integer);
- begin
- { process the buffer }
- if pcmVolume(PWaveFormat, Buffer, Length, FLeftVolume, FRightVolume) then
- FOverflow := True;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Opened;
- begin
- { pipe opened }
- Open;
- inherited Opened;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Closed;
- begin
- { pipe closed }
- Close;
- inherited Closed;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Started;
- begin
- { pipe started }
- inherited Started;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Stopped;
- begin
- { pipe stopped }
- inherited Stopped;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Paused;
- begin
- { pipe paused }
- inherited Paused;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Restarted;
- begin
- { pipe restarted }
- inherited Restarted;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.Reseting;
- begin
- { pipe reseting }
- inherited Reseting;
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.BufferReady(lpwh: PWaveHdr);
- begin
- { we are the output, process the data and put it to the right side }
- if Enabled and FOpen then
- begin
- Process(lpwh^.lpData, lpwh^.dwBytesRecorded);
- end;
- inherited BufferReady(lpwh);
- end;
- {-- TMMVolume ------------------------------------------------------------}
- procedure TMMVolume.BufferLoad(lpwh: PWaveHdr; var MoreBuffers: Boolean);
- begin
- { we are the input, load new data from the left side }
- inherited BufferLoad(lpwh, MoreBuffers);
- if Enabled and FOpen then
- begin
- { and process the data }
- Process(lpwh^.lpData, lpwh^.dwBytesRecorded);
- end;
- end;
- end.