-
Notifications
You must be signed in to change notification settings - Fork 23
/
BasicWaveDisplay.pas
4465 lines (3725 loc) · 105 KB
/
BasicWaveDisplay.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit BasicWaveDisplay;
interface
// uses
// Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
// Dialogs, ExtCtrls;
uses
soundinterfaces, typex, colorconversion, Geometry, Forms, Classes, StdCtrls, ExtCtrls, Controls, graphics, soundtools, math,
debug, windows, sysutils, betterobject, Generics.Collections.fixed, systemx, numbers, stringx,managedthread, soundsample,
memoryfilestream, Menus, GlassControls, advancedgraphics, framebasevcl, dialogs, fileex, helpers_stream, colorblending,
Vcl.CheckLst, Vcl.ComCtrls, graphicwincontrol, SoundDevice_PortAudio, SoundConversion, soundtools_easywindows, basicwavedisplaydx;
const
AA = 4;
BUTTON_WIDTH = 55;
BUTTON_MARGIN = 1;
BUTTON_TOP = 4;
KNOB_SIZE = 4;
MAX_PARAM_CHANNELS = 255;
SOUND_EDITOR_FILE_BUFFER_SIZE = 1000000000 div 125;
type
TSampleSummary = (ssSignedPeak, ssSignedValley, ssSignedAverage, ssUnsignedPeak, ssUnsignedValley, ssUnsignedAverage, ssBalanceAverage, ssBalancePeak, ssOffBalanceAverage,ssOffBalancePeak);
TLoadFromRawFormat = (rfMono, rfStereoInterlaced);
TDrawMode = (dmAmp, dmWave);
type
iptr = ^integer;
siptr = ^smallint;
TSoundEditorFileStream = TMemoryFileStream;
TSoundMarkerData = packed record
private
function GetCaption: string;
procedure SetCaption(const Value: string);
public
FType: integer;
FStart: int64;
FEnd: int64;
FHardEdge: boolean;
FVelocity: single;
Fcaption: array[0..31] of char;
property Caption: string read GetCaption write SetCaption;
end;
TSoundMarker = class(TBetterObject)
private
Data: TSoundMarkerData;
function GetCaption: string;
procedure SetCaption(const Value: string);
protected
public
property MarkerType: integer read Data.FType write Data.FType;
property StartPoint: int64 read Data.FStart write Data.FStart;
property EndPoint: int64 read Data.FEnd write Data.FEnd;
property Velocity: single read Data.FVelocity write Data.FVelocity;
property HardEdge: boolean read Data.FHardEdge write Data.FHardEdge;
procedure WriteToStream(s: TStream);
procedure ReadFromStream(s: TStream);
property Caption: string read GetCaption write SetCaption;
end;
TInterpolationType = (itNone, itLinear);
TCurveRec = packed record
Sample: int64;
Level: single;
Interpolation: TInterpolationType; // Integer enum size=4
ItParam1: single;
ItParam2: single;
ItParam3: single;
ItParam4: single;
reserved: array [0 .. 63] of byte;
end;
TCurve = class(TBetterObject)
private
procedure SetLevel(const Value: single);
public
Data: TCurveRec;
FLevelAtmouseDown: nativefloat;
constructor create;override;
property Sample: int64 read Data.Sample write Data.Sample;
property Level: single read Data.Level write SetLevel;
property Interpolation
: TInterpolationType read Data.Interpolation write Data.Interpolation;
procedure LoadFromStream(s: TStream);
procedure SaveToStream(s: TStream);
property LevelAtmouseDown: nativefloat read FLevelAtMouseDown;
procedure SaveLevel;
procedure ApplyConstraints;
procedure CopyFrom(cSource: TCurve);
end;
TsoundMarkerList = class(TList<TSoundMarker>)
public
procedure AppendFromFile(sFile: string; iBaseMarkerType: integer);
procedure LoadFromFile(sFile: string);
procedure SaveToFile(sFile: string);
procedure SaveToFile_Direct(sFile: string);
procedure Sort;overload;
procedure Sort(var iTrack: integer);overload;
procedure Swap(i1, i2: integer);
procedure ClearAndFree;
procedure SetVelocityToIndex();
end;
TCurveList = class(TList<TCurve>)
private
procedure SaveToFile_Direct(sFile: string);
function FindLastIndexFromSample(iSample: integer): integer;
public
destructor Destroy;override;
procedure CreateInitialCurve;
procedure LoadFromStream(s: TStream);
procedure LoadFromFile(sFile: string);
procedure FreeAndClearObjects;
procedure SaveToFile(sFile: string);
procedure SaveToStream(s: TStream);
function SortCurve: boolean;
procedure DeleteAndFree(idx: integer);
procedure CopyFrom(clSource: TCurveList);
function GetInterpolatedValue(iSample: int64): nativefloat;
function GetCurveBefore(iSample: int64): integer;
procedure Normalize;
procedure NormalizeTo(r: nativefloat);
procedure DeGlitch();
procedure Posterize(iLevels: integer);
function MaxLevel: nativefloat;
end;
TParamChannel = class(TBetterObject)
strict private
FAxies: TList<TCurveList>;
FMarkers: TSoundMarkerList;
function GetCurve(idx: integer): TCurveList;
function GetCurveT: TCurveList;
function GetCurveX: TCurveList;
function GetCurveY: TCurveList;
function GetCurveZ: TCurveList;
function GetAxisCount: integer;
strict protected
FConstant: integer;
procedure SaveCurves(sBoogerFile: string);
procedure LoadCurves(sBoogerFile: string);
procedure SaveMarkers(sBoogerFile: string);
procedure LoadMarkers(sBoogerFile: string);
public
constructor Create; override;
destructor Destroy; override;
procedure ClearAxies;
procedure ClearAxisData;
procedure ClearMarkers;
property Constant: integer read FConstant write FConstant;
property Axies[idx: integer]: TCurveList read GetCurve; default;
property AxisCount: integer read GetAxisCount;
property CurveX: TCurveList read GetCurveX;
property CurveY: TCurveList read GetCurveY;
property CurveZ: TCurveList read GetCurveZ;
property CurveT: TCurveList read GetCurveT;
property Markers: TSoundMarkerList read FMarkers;
procedure SaveToFile(sBoogerFile: string);
procedure LoadFromFile(sBoogerFile: string);
function GetCurveFileName(sBoogerFile: string; Channel, Axis: integer)
: string;
function GetMarkerFileName(sBoogerFile: string; Channel: integer)
: string;
function HasData: boolean;
end;
TPlarity = (polPos, polNeg, polZero);
TSoundGUISTate = (ssBase, ssZoomIn);
TFrameBasicWaveDisplay = class(TfrmFrameBase)
Draw: TTimer;
popWave: TPopupMenu;
Dele1: TMenuItem;
Cancel1: TMenuItem;
pbWave: TBackBufferedControl;
scrollbar: TScrollBar;
procedure pbWavePaint(Sender: TObject);
procedure ZoomInClick(Sender: TObject);
procedure ZoomOutClick(Sender: TObject);
procedure MarkClick(Sender: TObject);
procedure UnMarkClick(Sender: TObject);
procedure UnSelectClick(Sender: TObject);
procedure TweakInLeftClick(Sender: TObject);
procedure ScrollBarChange(Sender: TObject);
procedure TweakInRightClick(Sender: TObject);
procedure TweakInLoopLeftClick(Sender: TObject);
procedure TweakInLoopRightClick(Sender: TObject);
procedure TweakOutLeftClick(Sender: TObject);
procedure TweakOutRightClick(Sender: TObject);
procedure TweakOutLoopLeftClick(Sender: TObject);
procedure TweakOutLoopRightClick(Sender: TObject);
procedure btZoomOutClick(Sender: TObject);
procedure DrawTimer(Sender: TObject);
procedure btnSaveMarkersClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure pbWaveDblClick(Sender: TObject);
procedure btnSaveCurvesClick(Sender: TObject);
procedure popWavePopup(Sender: TObject);
procedure Dele1Click(Sender: TObject);
procedure FrameResize(Sender: TObject);
procedure btnDeleteClick(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
turb: Tcmd_GenerateTurbulenceData;
FACtiveCurve: TCurve;
FDrawMarks: boolean;
FAA: integer;
FZooming: boolean;
FParamChannels: TList<TParamChannel>;
FEditingChannel: integer;
FEditingAxis: integer;
FLastMouseDownX: integer;
FLastMouseDownY: integer;
FOnDataChanged: TNotifyEvent;
FCurvesNeedSaving: boolean;
FLoadedMetaFrom: String;
FMarkersNeedSaving: boolean;
FCurveMask: integer;
FWaveAlpha: nativefloat;
FAlphaBackGroundColor: TColor;
FTempo: nativefloat;
FMarkMode: boolean;
FFileName: string;
FDeviceIndex: nativeint;
FDrawMode: TDrawMode;
dx: TBasicWaveDisplayDX;
FDirtyWave: boolean;
function GetScreenPositionForSample(x: integer): integer;
procedure HLINE(x1, y1, x2, y2: integer; color: TColor; alpha: nativefloat);
procedure manualline(x1, y1, x2, y2: integer; color: TColor; alpha: nativefloat);
procedure VLINE(x1, y1, x2, y2: integer; color: TColor; alpha: nativefloat);
procedure nativefloatpixel(x, y: nativefloat; color: TColor; alpha: nativefloat);
procedure SetPixelEx(x, y: integer; color: TColor; alpha: nativefloat);
function GetMonoMergedSamples(idx: int64): nativefloat;
function GetMonoMergedIntegerSamples(idx: int64): integer;
function GEtParamChannels(idx: integer): TParamChannel;
procedure MouseDownForCurve(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; x, y: integer);
procedure MouseDownForWave(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; x, y: integer);
function GetCurveKnobPosition(rAmp: nativefloat): integer;
function GetAmplitudeFromScreen(y: nativefloat): nativefloat;
procedure GetKnobWindow(x, y: integer; out xStart, xEnd: int64;
out yStart, yEnd: nativefloat);
function GetPointToPlot(x, y: integer; out ox: int64; out oy: nativefloat)
: boolean;
procedure SetEditingChannel(const Value: integer);
function GetSampleEx(idx: int64; iChannel: integer): nativefloat;
procedure SetCurveMask(const Value: integer);
procedure DrawCurveMarkers;
procedure DrawCurveAxies(iCh, iStart, iThru: integer);
procedure SetEditingAxis(const Value: integer);
procedure SetAlphaBackGroundcolor(const Value: TColor);
procedure SetWaveAlpha(const Value: nativefloat);
procedure ValidateMarkerIndex;
procedure SetTempo(const Value: nativefloat);
procedure SetMarkMode(const Value: boolean);
procedure SetActiveCurve(const Value: TCurve);
procedure UpdateNames;
function GetFSamples(idx: int64): smallint;
procedure SetDeviceIndex(const Value: nativeint);
procedure SetDrawMode(const Value: TDrawMode);
procedure SetDirtyWave(const Value: boolean);
protected
FViewStart: int64;
FViewend: int64;
FSelectEnd: int64;
FSelectStart: int64;
FMouseIsDown: boolean;
FNoPaint: boolean;
FSampleStart: int64;
FSampleend: int64;
FLoopStart: int64;
FLoopend: int64;
FStream: TSoundStream;
FSoundThread: TSoundDevice_portAudio;
FOsc_ForPlayback: TSoundStreamOscillator;
FMarkers: TsoundMarkerList;
FEvents: TsoundMarkerList;
FChannels: integer;
FLoadedFrom: String;
FOnMarkerChanged: TNotifyEvent;
FSelectedMarkerIndex: integer;
procedure SetViewEnd(Value: int64);
procedure SetViewStart(Value: int64);
function GetDataLength: int64;
function GetSampleLength: int64;
procedure SetState(const Value: TSoundGUISTate);
procedure SetSelectEnd(const Value: int64);
procedure DrawEvents;
procedure DrawCurves;
procedure DrawKnob(xScreen, yScreen: integer; c: TColor; bBackground: boolean = false);
procedure DrawKnobLine(x1Screen, y1Screen, x2Screen, y2Screen: integer;
c: TColor);
function GetEventColor(iType: integer): TColor;
procedure SetNoPaint(const Value: boolean);
procedure SetLoopEnd(const Value: int64);
procedure SetLoopStart(const Value: int64);
procedure SetSampleEnd(const Value: int64);
procedure SetSampleStart(const Value: int64);
procedure SetSelectStart(const Value: int64);
function GetStream: TSoundStream;
function GetMarkerCount: integer;
function GetMarkers(idx: integer): TSoundMarker;
function GetSelectedMarker: TSoundMarker;
procedure SetSelectedMarker(const Value: TSoundMarker);
procedure SetSelectedMarkerIndex(const Value: integer);
procedure Play(iFrom, iTo: int64);
function GetIsPlaying: boolean;
function GetEvents(idx: integer): TSoundMarker;
procedure AfterDrawSample(iScreenPosition: integer); virtual;
procedure Line(x1, y1, x2, y2: integer; color: TColor);
procedure ValidateSelection;
procedure ValidateView;
public
// scrollbar: TScrollBar;
FCurveNames: array[0..199] of array[0..11] of string;
Fstate: TSoundGUISTate;
// btnZoomIn: TButton;
// btnZoomOut: TButton;
// btnMark: TButton;
// btnUnMark: TButton;
// btnUnSelect: TButton;
// btnTweakInLeft: TButton;
// btnTweakInRight: TButton;
// btnTweakOutLeft: TButton;
// btnTweakOutRight: TButton;
// btnTweakLoopInLeft: TButton;
// btnTweakLoopInRight: TButton;
// btnTweakLoopOutLeft: TButton;
// btnTweakLoopOutRight: TButton;
// btnPLay: TButton;
lblDebug: TLabel;
RefreshingScrollbar: boolean;
FPlayCommand: Tcmd_PlayStream_Partial;
property FSamples[idx: int64]: smallint read GetFSamples;
procedure SaveCurves;
procedure LoadCurves;
constructor Create(aowner: TComponent); reintroduce; virtual;
procedure CReateSoundThread;
procedure BeforeDestruction;override;
destructor Destroy; override;
procedure InitParamChannels;
procedure ZoomKeepLevel(iPOint: integer);
procedure PanelPaint;
property Stream: TSoundSTream read GetStream;
procedure LoadFromFile(sAdaOrmp3File: ansistring; sMetaFile: string = '');
procedure AfterLoadFromFile; virtual;
procedure SaveToFile(sBoogerFile: ansistring);
procedure SaveMetaData;
procedure LoadMetaData;
procedure SaveLoop(sBoogerFile: ansistring);
property DataLength: int64 read GetDataLength;
property SAmpleLength: int64 read GetSampleLength;
property LoadedFrom: String read FFileName write FFileName;
property LoadedMetaFrom: String read FLoadedMetaFrom write FLoadedMetaFrom;
procedure CleanupPlayCommand;
procedure LoadMarkers();
procedure LoadEvents(sFile: string);
procedure SaveMarkers();
property MarkerList: TSoundMarkerList read FMarkers;
property Markers[idx: integer]: TSoundMarker read GetMarkers;
property Events[idx: integer]: TSoundMarker read GetEvents;
procedure ClearMarkers;
procedure ClearEvents;
property MarkerCount: integer read GetMarkerCount;
procedure AddMarker(sm: TSoundMarker);
procedure DeleteMarker(idx: integer);
function IsInMarker(x: int64): boolean;
property MonoMergedSamples[idx: int64]: nativefloat read GetMonoMergedSamples;
property MonoMergedIntegerSamples[idx: int64]
: integer read GetMonoMergedIntegerSamples;
property ParamChannels[idx: integer]: TParamChannel read GEtParamChannels;
property EditingChannel
: integer read FEditingChannel write SetEditingChannel;
property EditingAxis: integer read FEditingAxis write SetEditingAxis;
procedure SetLastMouseDown(x, y: integer);
property LastMouseDownX: integer read FLastMouseDownX write FLastMouseDownX;
property LastMouseDownY: integer read FLastMouseDownY write FLastMouseDownY;
procedure FreeAndClearParamChannels;
function SampleToBeat(iSample: int64): nativefloat;
property ActiveCurve: TCurve read FActiveCurve write SetActiveCurve;
procedure DrawWave;
procedure ToneGate(iOutputChannel, iPitchChannel, iPitchAxis,
iVelAxis: integer; rLowRange, rhighRange: nativefloat; rOpenGateAt: nativefloat = 0.70; rCloseGateAT: nativefloat = 0.40);
procedure TransientDetection(
iOutputChannel, iPitchChannel,
iPitchAxis, iVelAxis: integer;
rOpenGateAt: nativefloat = 0.03; rCloseGateAT: nativefloat = 0.05);
procedure TransientDetectionFromSpectrum(
iOutputChannel, iInputChannel: integer;
rOpenGateAt: nativefloat = 0.009; rCloseGateAT: nativefloat = 0.02);
property DirtyWave: boolean read FDirtyWave write SetDirtyWave;
published
property DeviceIndex: nativeint read FDeviceIndex write SetDeviceIndex;
property ViewStart: int64 read FViewStart write SetViewStart;
property ViewEnd: int64 read FViewend write SetViewEnd;
property SampleStart: int64 read FSampleStart write SetSampleStart;
property SampleEnd: int64 read FSampleend write SetSampleEnd;
property LoopStart: int64 read FLoopStart write SetLoopStart;
property LoopEnd: int64 read FLoopend write SetLoopEnd;
property DrawMode: TDrawMode read FDrawMode write SetDrawMode;
procedure Box(x1, y1, x2, y2: integer; color: TColor);
procedure HookMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; x, y: integer);
procedure HookMouseMove(Sender: TObject; Shift: TShiftState; x, y: integer);
procedure HookMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; x, y: integer);
function GetSampleOnDisplay(x: nativefloat): integer;
property SelectStart: int64 read FSelectStart write SetSelectStart;
property SelectEnd: int64 read FSelectEnd write SetSelectEnd;
property MouseIsDown: boolean read FMouseIsDown;
property State: TSoundGUISTate read Fstate write SetState;
procedure Debug(sMessage: ansistring);
function VIEW_HEIGHT: integer;
function VIEW_WIDTH: integer;
function BUTTON_TOP: integer;
procedure ZoomTo(iStart, iEnd: int64; rMarginPercent: nativefloat = 0.25);
procedure ZoomMakeShowing(iPoint1, iPOint2: int64);
procedure REfreshScrollbar;
procedure Resize; override;
property NoPaint: boolean read FNoPaint write SetNoPaint;
function SearchForZeroCrossing(iStart: int64; bLeft, bRight: boolean)
: int64;
procedure UnMark;
procedure PlayScrub(iStart: integer; iLength: integer = (44100 shr 1));
procedure PlayLoop(iRepeats: integer = 2);
property OnMarkerChanged
: TNotifyEvent read FOnMarkerChanged write FOnMarkerChanged;
procedure MarkerChanged;
property SelectedMarkerIndex: integer read FSelectedMarkerIndex write
SetSelectedMarkerIndex;
property SelectedMarker: TSoundMarker read GetSelectedMarker write
SetSelectedMarker;
procedure PlaySelectedMarker;
procedure PlaySelection;
procedure StopPlaying;
property IsPlaying: boolean read GetIsPlaying;
property DrawMarks: boolean read FDrawMarks write FDrawMarks;
property AA: integer read FAA write FAA;
function IndexOfMarker(sm: TSoundMarker): integer;
function Playing: boolean;
property Zooming: boolean read FZooming write FZooming;
function HasParamChannel(iConstant: integer): boolean;
function IndexOfParamChannel(iConstant: integer): integer;
procedure NotifyDatachange;
function GetSampleSummary(iStart, iEnd: int64; ss: TSampleSummary; ichannelMask: integer = 3): nativefloat;
procedure DataChanged;
procedure DirtyDisplay;
property CurvesNeedSaving: boolean read FCurvesNeedSaving write FCurvesNeedSaving;
property MarkersNeedSaving: boolean read FMarkersNeedSaving write FMarkersNeedSaving;
property FileName: string read FFileName write FFilename;
published
property OnDataChanged
: TNotifyEvent read FOnDataChanged write FOnDataChanged;
function SortCurve: boolean;
property CurveMask: integer read FCurveMask write SetCurveMask;
property AlphabackGroundColor: TColor read FAlphaBackGroundColor write SetAlphaBackGroundcolor;
property WaveAlpha: nativefloat read FWaveAlpha write SetWaveAlpha;
property Tempo: nativefloat read FTempo write SetTempo;
property MarkMode: boolean read FMarkMode write SetMarkMode;
procedure BuildMetadatafromTurb;
procedure NameAxis(iChannel, iAxis: nativeint; sName: string);
procedure ChangedDeviceID(iDeviceID: integer);
end;
{$R *.dfm}
implementation
uses EasyImage, ProgressForm;
{ TSoundEditor }
procedure TFrameBasicWaveDisplay.AddMarker(sm: TSoundMarker);
var
idx, t: integer;
begin
// SayNatural('Marker added.');
idx := -1;
for t := 0 to FMarkers.count - 1 do
begin
if (FMarkers[t].StartPoint > sm.StartPoint) then
begin
idx := t;
end;
end;
if (idx < 0) then
FMarkers.add(sm)
else
FMarkers.insert(idx, sm);
SaveMarkers;
DirtyWave := true;
pbWave.Dirty := true;
end;
procedure TFrameBasicWaveDisplay.AfterDrawSample;
begin
// no implementation reqd
end;
procedure TFrameBasicWaveDisplay.AfterLoadFromFile;
begin
// no implementation reqd
end;
procedure TFrameBasicWaveDisplay.BeforeDestruction;
begin
inherited;
CleanupPlayCommand;
if assigned(FSoundThread) then begin
TPM.NoNeedThread(FsoundThread);
FSoundThread := nil;
end;
end;
procedure TFrameBasicWaveDisplay.Box(x1, y1, x2, y2: integer; color: TColor);
begin
pbWave.Drawto.brush.color := color;
pbWave.Drawto.pen.color := color;
pbWave.Drawto.FillRect(Rect(x1, y1, x2 + 1, y2 + 1));
end;
procedure TFrameBasicWaveDisplay.Line(x1, y1, x2, y2: integer; color: TColor);
begin
if AA = 0 then
begin
pbWave.Drawto.brush.color := color;
pbWave.Drawto.pen.color := color;
pbWave.Drawto.MoveTo(x1, y1);
pbWave.Drawto.LineTo(x2, y2);
end
else
begin
manualline(x1, y1, x2, y2, color, 1);
end;
end;
procedure TFrameBasicWaveDisplay.manualline(x1, y1, x2, y2: integer; color: TColor;
alpha: nativefloat);
begin
if (x2 - x1) > (y2 - y1) then
begin
HLINE(x1, y1, x2, y2, color, alpha);
end
else
begin
VLINE(x1, y1, x2, y2, color, alpha);
end;
end;
function TFrameBasicWaveDisplay.HasParamChannel(iConstant: integer): boolean;
begin
result := IndexOfParamChannel(iConstant) >= 0;
end;
procedure TFrameBasicWaveDisplay.HLINE(x1, y1, x2, y2: integer; color: TColor;
alpha: nativefloat);
var
t: integer;
ry: nativefloat;
begin
if (x2 < x1) then
begin
Swap(x1, x2);
Swap(y1, y2);
end;
for t := x1 to x2 do
begin
// r := interpolate(
ry := interpolate(t, y1, y2, x1, x2);
nativefloatpixel(t, ry, color, alpha);
end;
end;
procedure TFrameBasicWaveDisplay.nativefloatpixel(x, y: nativefloat; color: TColor; alpha: nativefloat);
var
x1, x2, y1, y2: integer;
a1, a2, a3, a4: nativefloat;
al, ar, at, ab: nativefloat;
begin
a1 := 0;
a2 := 0;
a3 := 0;
a4 := 0;
al := 0;
ar := 0;
at := 0;
ab := 0;
x1 := trunc(x);
x2 := trunc(x) + 1;
y1 := trunc(y);
y2 := trunc(y) + 1;
ar := x - trunc(x);
al := 1 - al;
ab := y - trunc(y);
at := 1 - ab;
a1 := al * at;
a2 := ar * at;
a3 := al * ab;
a4 := ar * ab;
SetPixelEx(x1, y1, color, a1 * alpha);
SetPixelEx(x2, y1, color, a2 * alpha);
SetPixelEx(x1, y2, color, a3 * alpha);
SetPixelEx(x2, y2, color, a4 * alpha);
end;
procedure TFrameBasicWaveDisplay.SetPixelEx(x, y: integer; color: TColor; alpha: nativefloat);
begin
pbWave.Drawto.pixels[x, y] := colorblend(pbWave.Drawto.pixels[x, y], color,
alpha);
end;
procedure TFrameBasicWaveDisplay.VLINE(x1, y1, x2, y2: integer; color: TColor;
alpha: nativefloat);
var
t: integer;
rx: nativefloat;
begin
if (y2 < y1) then
begin
Swap(x1, x2);
Swap(y1, y2);
end;
for t := y1 to y2 do
begin
// r := interpolate(
rx := interpolate(t, x1, x2, y1, y2);
nativefloatpixel(rx, t, color, alpha);
end;
end;
procedure TFrameBasicWaveDisplay.btnDeleteClick(Sender: TObject);
var
t: integer;
cv: TCurve;
sm: TSoundMarker;
begin
inherited;
if MessageDlg('Deleting! There is no undo! Are you sure?', mtConfirmation, [mbYes, mbNo],0) <> mrYes then begin
exit;
end;
if MarkMode then begin
for t:= ParamChannels[EditingChannel].Markers.Count-1 downto 0 do begin
sm := ParamChannels[EditingChannel].Markers[t];
if ((sm.EndPoint >= selectStart) and (sm.EndPoint <= selectEnd))
or ((sm.StartPoint >= selectStart) and (sm.StartPoint <= selectEnd)) then begin
ParamChannels[EditingChannel].Markers.delete(t);
sm.free;
end;
end;
end else begin
for t:= ParamChannels[EditingChannel][EditingAxis].Count-1 downto 0 do begin
cv := ParamChannels[EditingChannel][EditingAxis][t];
if (cv.Sample >= SelectStart)
and (cv.Sample <=SelectEnd) then begin
ParamChannels[EditingChannel][EditingAxis].Delete(t);
cv.free;
end;
end;
end;
dirtyDisplay;
end;
procedure TFrameBasicWaveDisplay.btnSaveCurvesClick(Sender: TObject);
begin
SaveCurves;
end;
procedure TFrameBasicWaveDisplay.btnSaveMarkersClick(Sender: TObject);
begin
if SelectedMarker <> nil then
begin
SelectedMarker.StartPoint := SelectStart;
SelectedMarker.EndPoint := SelectEnd;
MarkerChanged;
SaveMarkers;
end;
end;
procedure TFrameBasicWaveDisplay.btZoomOutClick(Sender: TObject);
begin
ZoomTo(0, SAmpleLength - 1);
end;
procedure TFrameBasicWaveDisplay.BuildMetadatafromTurb;
var
t: integer;
c: TCurve;
sm: TSoundMarker;
b: integer;
ot: nativeint;
nt: nativeint;
begin
ot := 0;
sm := nil;
for b:= 0 to 2 do begin
NameAxis(b, 0, 'Amp_Frame.Peak');
NameAxis(b, 1, 'Amp_Beat.Peak');
NameAxis(b, 2, 'Amp_Beat.AvgPeak');
NameAxis(b, 3, 'NormalizedTurbulence');
NameAxis(b, 4, 'Turb_Modulation');
for t:= 0 to turb.RecordCount-1 do begin
c := TCurve.Create;
c.Data.Sample := turb.Records[t].StartSample;
c.Data.Level := turb.Records[t].bands[b].Amp_Frame.Peak;
self.ParamChannels[b].Axies[0].Add(c);
c := TCurve.Create;
c.Data.Sample := turb.Records[t].StartSample;
c.Data.Level := turb.Records[t].bands[b].Amp_Beat.Peak;
self.ParamChannels[b].Axies[1].Add(c);
c := TCurve.Create;
c.Data.Sample := turb.Records[t].StartSample;
c.Data.Level := turb.Records[t].bands[b].Amp_Beat.AVgPeak;
self.ParamChannels[b].Axies[2].Add(c);
c := TCurve.Create;
c.Data.Sample := turb.Records[t].StartSample;
c.Data.Level := turb.Records[t].bands[b].NormalizedTurbulence;
self.ParamChannels[b].Axies[3].Add(c);
c := TCurve.Create;
c.Data.Sample := turb.Records[t].StartSample;
c.Data.Level := turb.Records[t].bands[b].Turb_Modulation;
self.ParamChannels[b].Axies[4].Add(c);
nt := turb.Records[t].bands[b].Amp_Beat.TriggerNumber;
if nt <> ot then begin
if assigned(sm) then begin
sm.EndPoint := turb.Records[t].StartSample;
self.ParamChannels[b].Markers.Add(sm);
sm := nil;
end;
sm := TsoundMarker.Create;
sm.StartPoint := turb.Records[t].StartSample;
end;
ot := nt;
end;
if assigned(sm) then begin
sm.EndPoint := turb.Records[turb.RecordCount-1].StartSample;
self.ParamChannels[b].Markers.Add(sm);
sm := nil;
end;
end;
end;
procedure TFrameBasicWaveDisplay.Button3Click(Sender: TObject);
var
t: integer;
ab: int64;
sm: TSoundMarker;
i: integer;
lst: TSoundMarkerList;
begin
ab := 999999999999;
i := -1;
if EditingChannel < 0 then
lst := FMarkers
else
lst := ParamChannels[Editingchannel].Markers;
for t := 0 to lst.count - 1 do
begin
sm := lst[t];
if abs(sm.StartPoint - SelectStart) < ab then
begin
i := t;
ab := abs(sm.StartPoint - SelectStart);
end;
end;
SelectedMarkerIndex := i;
if i > -1 then
begin
SelectStart := lst[i].StartPoint;
SelectEnd := lst[i].EndPoint;
end;
DirtyDisplay;
end;
procedure TFrameBasicWaveDisplay.Button4Click(Sender: TObject);
begin
inherited;
SelectEnd := SampleLength-1;
SelectStart := 0;
end;
procedure TFrameBasicWaveDisplay.Button5Click(Sender: TObject);
var
c: Tcmd_GenerateTurbulenceData;
begin
ProgressForm.BeginProgress;
try
if assigned(turb) then begin
turb.cancel;
turb.waitfor;
turb.free;
turb := nil;
end;
c := Tcmd_GenerateTurbulenceData.Create;
c.Rebuild := true;
c.FileName := self.FileName;
c.Start;
ProgressForm.frmProgress.WatchSingleCommand(c);
c.WaitFor;
turb := c;
BuildMetadatafromTurb;
EditingChannel := 0;
finally
ProgressForm.EndProgress;
end;
end;
function TFrameBasicWaveDisplay.BUTTON_TOP: integer;
begin
result := VIEW_HEIGHT + 10;
end;
procedure TFrameBasicWaveDisplay.ChangedDeviceID(iDeviceID: integer);
begin
if iDeviceID = FSoundthread.DeviceID then
exit;
FSoundThread.stop;
FSoundThread.DeviceID := iDeviceID;
FSoundThread.Start;
end;
procedure TFrameBasicWaveDisplay.CleanupPlayCommand;
begin
if FPlayCommand <> nil then
begin
FPlayCommand.Cancel;
FPlayCommand.WaitFor;
FPlayCommand.free;
FPlayCommand := nil;
end;
end;
procedure TFrameBasicWaveDisplay.ClearEvents;
begin
while (FEvents.count > 0) do
begin
FEvents[FEvents.count - 1].free;
FEvents.delete(FEvents.count - 1);
end;
end;
procedure TFrameBasicWaveDisplay.ClearMarkers;
begin
FMarkers.ClearAndFree;
end;
constructor TFrameBasicWaveDisplay.Create(aowner: TComponent);
begin
inherited;
FMarkers := TsoundMarkerList.Create();
FEvents := TsoundMarkerList.Create();
CReateSoundThread;
dx := TBasicWaveDisplayDX.create(self);
dx.parent := self;
dx.align := alTop;
dx.height := 400;
//FullRepaint := false;
// AA := 1;
//doublebuffered := true;
SelectStart := -1;
// scrollbar := TScrollBar.Create(self);
// scrollbar.Parent := self;
// scrollbar.Align := alBottom;
// scrollbar.OnChange := self.ScrollBarChange;
// play loop
// btnPlay := TButton.create(self);
// with btnPlay do begin
// left := btnTweakOutRight.left+btnTweakOutRight.width+BUTTON_MARGIN;
// width := BUTTON_WIDTH;
// top := BUTTON_TOP;
// parent := self;
// caption := '&Play';
// OnClick := self.PlayClick;
// anchors := [akLeft,akBottom];
// end;
lblDebug := TLabel.Create(self);
lblDebug.Parent := self;
lblDebug.top := VIEW_HEIGHT;
lblDebug.anchors := [akLeft, akBottom];
InitParamChannels;
EditingChannel := -1;
FCurveMask := 65535;
FWaveAlpha := 0.5;
end;
procedure TFrameBasicWaveDisplay.CReateSoundThread;
begin
FSoundThread := TPM.NeedThread<TSoundDevice_PortAudio>(nil);// TSoundDevice_PortAudio.Create(self, nil);
FSoundThread.DeviceID := self.DeviceIndex;
FSoundThread.Start;
end;
procedure TFrameBasicWaveDisplay.DataChanged;
begin
DirtyWave := true;
NotifyDatachange;
ValidateMarkerIndex;
CurvesNeedSaving := true;
end;