MMFirDlg.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:17k
- {========================================================================}
- {= (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/mmtools.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: 06.09.98 - 14:25:24 $ =}
- {========================================================================}
- unit MMFirDlg;
- {$I COMPILER.INC}
- interface
- uses
- Windows,
- Messages,
- SysUtils,
- Classes,
- Graphics,
- Controls,
- Forms,
- Dialogs,
- Mask,
- StdCtrls,
- ExtCtrls,
- MMObj,
- MMUtils,
- MMMath,
- MMSpin,
- MMFIR,
- MMDIBCv,
- MMSpectr,
- MMDSPObj,
- MMPanel,
- MMEnvelp,
- MMEdit;
- type
- {-- TMMFIRDlgForm -----------------------------------------------------------}
- TMMFIRDlgForm = class(TForm)
- Bevel1: TBevel;
- Label1: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- Spectrum: TMMSpectrum;
- btnSave: TButton;
- btnLoad: TButton;
- btnReset: TButton;
- btnCancel: TButton;
- btnOK: TButton;
- btnClear: TButton;
- FilterComboBox: TComboBox;
- spinf1: TMMSpinButton;
- spinGain: TMMSpinButton;
- OpenDialog: TOpenDialog;
- SaveDialog: TSaveDialog;
- TmpFIR: TMMFIRFilter;
- MMPanel1: TMMPanel;
- Envelope: TMMEnvelope;
- Label2: TLabel;
- spinOrder: TMMSpinButton;
- edOrder: TMMNumberEdit;
- mef1: TMMFloatNumberEdit;
- meGain: TMMFloatNumberEdit;
- procedure btnResetClick(Sender: TObject);
- procedure btnLoadClick(Sender: TObject);
- procedure btnSaveClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure btnClearClick(Sender: TObject);
- procedure FilterComboBoxChange(Sender: TObject);
- procedure spinChange(Sender: TObject);
- procedure EnvelopeChange(Sender: TObject);
- procedure EnvelopeTrackBegin(Sender: TObject);
- procedure EnvelopeTrack(Sender: TObject);
- procedure mefExit(Sender: TObject);
- procedure mefKeyPress(Sender: TObject; var Key: Char);
- procedure spinOrderChange(Sender: TObject);
- procedure edOrderKeyPress(Sender: TObject; var Key: Char);
- procedure edOrderExit(Sender: TObject);
- procedure EnvelopeTrackEnd(Sender: TObject);
- procedure FormShow(Sender: TObject);
- private
- FTitle: String;
- FFIR: TMMFIRFilter;
- UpdateFilters: Boolean;
- UpdateSpins: Boolean;
- procedure SetTitle(aValue: String);
- procedure SetFIR(aValue: TMMFIRFilter);
- procedure UpdateControls;
- procedure FillComboBox;
- procedure UpdateMaskEdits;
- procedure GetLimits(idx: integer; var MinX, MaxX: integer);
- public
- property Title: String read FTitle write SetTitle;
- property FIRFilter: TMMFIRFilter read FFIR write SetFIR;
- end;
- {-- TMMFIRFilterDialog ------------------------------------------------------}
- TMMFIRFilterDialog = class(TMMNonVisualComponent)
- private
- FSource: TMMFIRFilter;
- FTitle : string;
- procedure SetSource(aSource: TMMFIRFilter);
- protected
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- public
- constructor Create(AOwner: TComponent); override;
- function Execute: Boolean;
- published
- property Source: TMMFIRFilter read FSource write SetSource;
- property Title: string read FTitle write FTitle;
- end;
- var
- MMFIRDlgForm: TMMFIRDlgForm;
- implementation
- {$R *.DFM}
- {------------------------------------------------------------------------------}
- function VolumeToDB(Volume: Longint): Float;
- begin
- if Volume <= 0 then
- Result := -99
- else
- Result := Log10(Volume/VOLUMEBASE)*20;
- end;
- {------------------------------------------------------------------------------}
- function DBToVolume(DB: Float): integer;
- begin
- Result := Trunc(pow(10.0,DB/20.0)*VOLUMEBASE);
- end;
- {== TMMFIRDlgForm =============================================================}
- procedure TMMFIRDlgForm.FormCreate(Sender: TObject);
- begin
- FTitle := Caption;
- FFIR := nil;
- FilterComboBox.ItemIndex := -1;
- UpdateFilters := True;
- UpdateSpins := True;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.FormShow(Sender: TObject);
- begin
- {$IFDEF BUILD_ACTIVEX}
- TmpFIR.Left := -50;
- {$ENDIF}
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- if (ModalResult = mrOK) then
- begin
- if (FFIR <> nil) then FFIR.Assign(TmpFIR);
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.SetTitle(aValue: String);
- begin
- if (aValue <> FTitle) then
- begin
- FTitle := aValue;
- Caption := FTitle + ' - ' + TmpFIR.Description;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.FillComboBox;
- var
- i,old: integer;
- begin
- with FilterComboBox,TmpFIR do
- begin
- old := Max(ItemIndex,0);
- Items.Clear;
- for i := 0 to Filters.Count-1 do
- with Filters[i] do
- begin
- Items.Add(IntToStr(i+1));
- end;
- if (old >= 0) and (old < Items.Count) then ItemIndex := old;
- UpdateMaskEdits;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.FilterComboBoxChange(Sender: TObject);
- begin
- if (FilterComboBox.ItemIndex >= 0) then
- begin
- Envelope.SelectAll(False);
- Envelope.Select[FilterComboBox.ItemIndex] := True;
- //Envelope.Locator := Envelope.Points[FilterComboBox.ItemIndex].X_Value;
- end;
- UpdateMaskEdits;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.GetLimits(idx: integer; var MinX, MaxX: integer);
- begin
- MinX := 0;
- MaxX := TmpFIR.SampleRate div 2;
- if (idx > 0) then
- begin
- MinX := Envelope.Points[idx-1].X_Value+1;
- if (idx < Envelope.Count-1) then
- MaxX := Envelope.Points[idx+1].X_Value-1;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.UpdateMaskEdits;
- var
- XMin,XMax: integer;
- begin
- with FilterComboBox,TmpFIR do
- begin
- UpdateSpins := False;
- if (ItemIndex >= 0) then
- with TmpFIR.Filters[ItemIndex] do
- begin
- mef1.Value := f1;
- meGain.Value := Gain;
- spinf1.Value := Trunc(f1*100);
- spinGain.Value := Trunc(Gain*100);
- meGain.Enabled := True;
- spinGain.Enabled:= True;
- if (FilterComboBox.ItemIndex = 0) or
- (FilterComboBox.ItemIndex = Envelope.Count-1) then
- begin
- mef1.Enabled := False;
- spinf1.Enabled := False;
- end
- else
- begin
- mef1.Enabled := True;
- spinf1.Enabled := True;
- GetLimits(FilterComboBox.ItemIndex,XMin,XMax);
- spinf1.MinValue := XMin;
- spinf1.MaxValue := XMax;
- end;
- end
- else
- begin
- mef1.Value := 0;
- meGain.Value := 0;
- spinf1.Value := 0;
- spinGain.Value := 0;
- mef1.Enabled := False;
- meGain.Enabled := False;
- spinf1.Enabled := False;
- spinGain.Enabled:= False;
- end;
- UpdateSpins := True;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.spinChange(Sender: TObject);
- begin
- if UpdateSpins and UpdateFilters and (FilterComboBox.ItemIndex >= 0) then
- with TmpFIR.Filters[FilterComboBox.ItemIndex] do
- begin
- if Sender = spinf1 then
- begin
- f1 := spinf1.Value/100;
- mef1.Value := f1;
- mef1.Update;
- end
- else
- begin
- Gain := spinGain.Value/100;
- meGain.Value := Gain;
- meGain.Update;
- end;
- with Envelope.Points[FilterComboBox.ItemIndex] do
- begin
- X_Value := Trunc(f1*100);
- Y_Value := DBToVolume(Gain);
- end;
- Envelope.Refresh;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.mefExit(Sender: TObject);
- begin
- spinf1.Value := Trunc(mef1.Value*100);
- spinGain.Value := Trunc(meGain.Value*100);
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.mefKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key = #13) then TMaskEdit(Sender).OnExit(Sender);
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.spinOrderChange(Sender: TObject);
- begin
- with TmpFIR do
- begin
- Order := spinOrder.Value;
- edOrder.Value := Order;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.edOrderExit(Sender: TObject);
- var
- val: integer;
- begin
- val := Max(edOrder.Value,1);
- if (val mod 2 = 0) then inc(Val);
- spinOrder.Value := val;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.edOrderKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key = #13) then TEdit(Sender).OnExit(Sender);
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.EnvelopeTrackBegin(Sender: TObject);
- var
- Pos: TPoint;
- i: integer;
- begin
- GetCursorPos(Pos);
- Pos := Envelope.ScreenToClient(Pos);
- i := Envelope.FindPointAtPos(Pos.X, Pos.Y);
- if (i >= 0) and (i < FilterComboBox.Items.Count) then
- begin
- FilterComboBox.ItemIndex := i;
- UpdateMaskEdits;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.EnvelopeTrack(Sender: TObject);
- var
- f1,Gain: Float;
- begin
- with FilterComboBox,Envelope do
- begin
- if (ItemIndex >= 0) then
- with Envelope.Points[ItemIndex] do
- begin
- UpdateFilters := False;
- f1 := X_Value/100;
- Gain := VolumeToDB(Y_Value);
- mef1.Value := f1;
- meGain.Value := Gain;
- spinf1.Value := Trunc(f1*100);
- spinGain.Value := Trunc(Gain*100);
- UpdateFilters := True;
- end
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.EnvelopeTrackEnd(Sender: TObject);
- begin
- Envelope.Locator := -1;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.EnvelopeChange(Sender: TObject);
- var
- i: integer;
- begin
- if UpdateFilters then
- with TmpFIR do
- begin
- Filters.BeginUpdate;
- try
- Filters.FreeAll;
- for i := 0 to Envelope.Count-1 do
- with Envelope.Points[i] do
- begin
- Filters.AddObject(TMMFIRFilterItem.CreateEx(X_Value/100,
- VolumeToDB(Y_Value)));
- end;
- finally
- Filters.EndUpdate;
- end;
- FillComboBox;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.UpdateControls;
- var
- i: integer;
- begin
- with TmpFIR do
- begin
- Caption := Title + ' - '+ Description;
- FillComboBox;
- spinOrder.MaxValue := MAXTAPS;
- edOrder.MaxValue := MAXTAPS;
- spinOrder.Value := TmpFIR.Order;
- edOrder.Value := TmpFIR.Order;
- with Envelope do
- begin
- UpdateFilters := False;
- try
- RangeMinX := 0;
- RangeMaxX := (SampleRate div 2)*100;
- DispMinX := 0;
- DispMaxX := (SampleRate div 2)*100;
- RangeMinY := 0;
- RangeMaxY := 2*VOLUMEBASE;
- DispMinY := 0;
- DispMaxY := 2*VOLUMEBASE;
- BaseY := VOLUMEBASE;
- GridWidthX:= RangeMaxX / 16;
- GridWidthY:= RangeMaxY / 10;
- Clear;
- if (Filters.Count > 0) then
- Envelope.Points[0].Y_Value := DBToVolume(Filters[0].Gain);
- for i := 1 to Filters.Count-2 do
- with Filters[i] do
- begin
- AddPoint(TMMEnvelopePoint.CreateEx(Trunc(f1*100),
- DBToVolume(Gain),
- False),False);
- end;
- if (Filters.Count > 1) then
- Envelope.Points[Envelope.Count-1].Y_Value := DBToVolume(Filters[Filters.Count-1].Gain);
- finally
- UpdateFilters := True;
- end;
- end;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.SetFIR(aValue: TMMFIRFilter);
- begin
- if (aValue <> Nil) then
- begin
- FFIR := aValue;
- TmpFIR.assign(aValue);
- UpdateControls;
- EnvelopeChange(Envelope);
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.btnResetClick(Sender: TObject);
- begin
- SetFIR(FFIR);
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.btnClearClick(Sender: TObject);
- begin
- Envelope.Clear;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.btnLoadClick(Sender: TObject);
- begin
- if OpenDialog.Execute then
- begin
- TmpFIR.Filters.LoadFromFile(OpenDialog.FileName);
- FilterComboBox.ItemIndex := -1;
- UpdateControls;
- end;
- end;
- {-- TMMFIRDlgForm -------------------------------------------------------------}
- procedure TMMFIRDlgForm.btnSaveClick(Sender: TObject);
- begin
- if SaveDialog.Execute then
- begin
- TmpFIR.Filters.SaveToFile(SaveDialog.FileName);
- end;
- end;
- {== TMMFIRFilterDialog ========================================================}
- constructor TMMFIRFilterDialog.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FTitle := 'FIR-Filter Editor';
- FSource := nil;
- ErrorCode := ComponentRegistered(InitCode, Self, ClassName);
- if (ErrorCode <> 0) then RegisterFailed(InitCode, Self , ClassName);
- end;
- {-- TMMFIRFilterDialog --------------------------------------------------------}
- procedure TMMFIRFilterDialog.SetSource(aSource: TMMFIRFilter);
- begin
- if Longint(aSource) = Longint(Self) then exit;
- if (aSource is TMMFIRFilter) or (aSource = Nil) then
- begin
- if (FSource <> aSource) then
- begin
- FSource := aSource;
- end;
- end;
- end;
- {-- TMMFIRFilterDialog --------------------------------------------------------}
- procedure TMMFIRFilterDialog.Notification(AComponent: TComponent; Operation: TOperation);
- begin
- inherited Notification(AComponent, Operation);
- if (Operation = opRemove) and (AComponent = FSource) then
- FSource := Nil;
- end;
- {-- TMMFIRFilterDialog --------------------------------------------------------}
- function TMMFIRFilterDialog.Execute: Boolean;
- begin
- Result := False;
- if assigned(FSource) then
- begin
- with TMMFIRDlgForm.Create(Application) do
- try
- FIRFilter := (Self.FSource as TMMFIRFilter);
- Title := Self.FTitle;
- Result := (ShowModal = mrOK);
- finally
- Free;
- end;
- end;
- end;
- end.