-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfAudVid.cs
1064 lines (1019 loc) · 48.3 KB
/
fAudVid.cs
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
using TimeSpan = System.TimeSpan;
using Math = System.Math;
using Wew.Control;
using Wew.Media;
using Wew;
namespace Demo;
class fAudVid : cDockControl
{ const int SAMPLE_RATE = 44100;
const float TIME_FRACTION = 1.0f / SAMPLE_RATE;
const int AUD_SAMPLES = SAMPLE_RATE / 20;
const int TOT_AUD_SAMPLES = AUD_SAMPLES * 2;
class cUserPlayer : cPlayer
{ readonly cColorKeyEffect m_ckeKey; readonly c3DTransformEffect m_3teMirror; Matrix4x4 m_m44Mirror;
cSingleInputEffect? m_sieCurrEffect;
public cUserPlayer()
{ m_ckeKey = new cColorKeyEffect { Background = mRes.BmpDisplMap, Color = eColor.White, Tolerance = 0.3f};
m_m44Mirror = new Matrix4x4(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
m_3teMirror = new c3DTransformEffect { Matrix = m_m44Mirror};
cLabel lbl = new () { LocationMargin = new Point(150, 0), Text = "Effect"};
ToolBar.AddControl(lbl);
float[] a_f = new float[] { 0, 0.15f, 0.65f, 0.75f, 0.9f};
cComboBox cbo = new () { LocationMargin = new Point(200, 0), Width = 300};
cbo.Load(new cSingleInputEffect?[]
{ null
, m_ckeKey
, new cBrightnessEffect { WhitePoint = new Point(0.5f, 1)}
, new cColorMatrixEffect { Matrix = new Matrix5x4(0.213f, 0.213f, 0.213f, 0, 0.715f, 0.715f, 0.715f, 0, 0.072f, 0.072f, 0.072f, 0, 0, 0, 0, 1, 0, 0, 0, 0)}
, m_3teMirror
, new cColorMatrixEffect { Matrix = new Matrix5x4(-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 1, 1, 1, 0)}
, new cColorMatrixEffect { Matrix = new Matrix5x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)}
, new cDiscreteTransferEffect { Red = a_f, Green = a_f, Blue = a_f}
, new cGaussianBlurEffect()
, new cDirectionalBlurEffect { Deviation = 20, Angle = 30}
, new cConvolveMatrixEffect { Matrix = new float[] { -2.2f, -1.4f, -0.4f, -1, 9, -1, -1, -1, -1}}
, new cThicknessEffect { KernelWidth = 8}
, new cTileEffect { Rectangle = new Rectangle(200, 100, 200, 200), ModeX = eExtendMode.Mirror}
, new cDisplacementMapEffect { Map = mRes.BmpDisplMap, Scale = 30}
});
cbo.SelectedIndex = 0;
cbo.Items[0].Text = "None";
cbo.Items[1].Text = "Color key";
cbo.Items[2].Text = "Brightness";
cbo.Items[3].Text = "Black and white";
cbo.Items[4].Text = "Mirror";
cbo.Items[5].Text = "Negative";
cbo.Items[6].Text = "Yellow channel";
cbo.Items[7].Text = "Painting";
cbo.Items[8].Text = "Gaussian blur";
cbo.Items[9].Text = "Directional blur";
cbo.Items[10].Text = "Emboss";
cbo.Items[11].Text = "Thin";
cbo.Items[12].Text = "Tile";
cbo.Items[13].Text = "Displacement map";
cbo.SelectionChanged += cboEffect_SelectionChanged;
ToolBar.AddControl(cbo);
cToolButton tbt = new () { Bounds = new Rectangle(510, 0, 21, 21)
, Bitmap = Wew.eResource.BmpOpen, ToolTip = "Open key bitmap"};
tbt.Click += tbtOpenKeyBmp_Click;
ToolBar.AddControl(tbt);
cColorButton cbt = new () { Bounds = new Rectangle(540, 0, 21, 21), ToolTip = "Color key"};
cbt.ColorChanged += tbtColorKey_Click;
ToolBar.AddControl(cbt);
cSlider sli = new () { LocationMargin = new Point(570, 0), Width = 80, Maximum = 0.1f, Value = 0.05f};
sli.ValueChanged += sliKeyTolerance_ValueChanged;
ToolBar.AddControl(sli);
OpenDialogGuid = mMod.DLG_VID_GUID;
//SubtitleProperties.BackColor = eColor.White; SubtitleProperties.FillColor = eColor.Transparent;
// SubtitleProperties.InnerBorderColor = eColor.Blue; SubtitleProperties.OuterBorderColor = eColor.Red;
}
protected override void OnPaintFrame(cGraphics g, cGpuBitmap frame, Rectangle destination)
{ if (m_sieCurrEffect == m_3teMirror && frame.Size.X != m_m44Mirror.M30)
{ m_m44Mirror.M30 = frame.Size.X; m_3teMirror.Matrix = m_m44Mirror;
}
g.DrawBitmap(frame, destination, eAlignment.None, eResize.Stretch, 1, eInterpolation.Linear, m_sieCurrEffect);
}
private void cboEffect_SelectionChanged(object sender)
{ m_sieCurrEffect = (cSingleInputEffect?)((cComboBox)sender).SelectedValue; View.Invalidate();
}
private void tbtOpenKeyBmp_Click(object sender)
{ string? s;
s = mDialog.ShowOpenFile(mMod.DLG_IMG_EXTS, mMod.DLG_IMG_GUID);
if (s is not null) { m_ckeKey.Background = new cBitmap(s); View.Invalidate();}
}
private void tbtColorKey_Click(object sender) { m_ckeKey.Color = ((cColorButton)sender).Color; View.Invalidate();}
private void sliKeyTolerance_ValueChanged(object sender, cTrackBar.eAction action)
{ m_ckeKey.Tolerance = ((cSlider)sender).Value; View.Invalidate();
}
}
class cPlayIn3D : cContainer
{ readonly c3DModel m_mdlTv, m_mdlScreen; cTexture? m_texScreen; readonly cMan m_manMan;
cMedia? m_medSource; cVideoFrame? m_vfmFrame;
readonly cRenderControl renView;
readonly cTimer m_tmrTimer;
public cPlayIn3D()
{ cButton btn;
renView = new cRenderControl { Margins = new Rect(10, 10, 10, 35), BackColor = eBrush.LightSteelBlue
, FocusMode = eFocusMode.FocusControl};
renView.Render += renView_Render;
AddControl(renView);
btn = new cButton { Margins = new Rect(10, float.NaN, float.NaN, 10), Text = "Open"};
btn.Click += btnOpen_Click;
AddControl(btn);
btn = new cButton { Margins = new Rect(120, float.NaN, float.NaN, 10), Text = "Close"};
btn.Click += btnClose_Click;
AddControl(btn);
m_mdlTv = new c3DModel(wMain.Device, typeof(mRes), "Res.Tv.mdl");
m_mdlScreen = m_mdlTv.Parts["Frame"].Parts["Screen"];
m_manMan = new cMan(wMain.Device) { Z = 15, Yaw = 180};
m_manMan.SitOnFloor();
renView.Camera.Location = new Vector(-8, 8, 30); renView.Camera.Yaw = 170; renView.Camera.Pitch = 10;
m_tmrTimer = new cTimer { Interval = 30};
m_tmrTimer.Tick += m_tmrTimer_Tick;
}
public override void Close()
{ btnClose_Click(null); m_mdlTv.Dispose();
base.Close();
}
private void renView_Render(object sender, c3DGraphics g3d)
{ g3d.SetDefaults(renView.Camera);
g3d.Render(m_mdlTv); g3d.Render(m_manMan);
}
private void btnOpen_Click(object sender)
{ string? s; cMediaSource.cVideoTrack? vtkV;
s = mDialog.ShowOpenFile(mMod.DLG_VID_EXTS, mMod.DLG_VID_GUID); if (s is null) return;
btnClose_Click(null);
m_medSource = new cMedia(wMain.Device, s); m_medSource.GetFirstTracks(out vtkV, out _);
if (vtkV is null) { btnClose_Click(null); return;}
vtkV.Selected = true; vtkV.SetOutputFormat(vtkV.Format, true);
m_tmrTimer.Start();
}
private void btnClose_Click(object? sender)
{ m_tmrTimer.Stop();
m_medSource?.Dispose(); m_medSource = null;
m_vfmFrame?.Dispose();
}
private void m_tmrTimer_Tick(object sender)
{ // ** Read frame
cAudioFrame? afm = null; cSubtitleFrame? sfm = null;
if (m_medSource?.Read(out _, out _, ref m_vfmFrame, ref afm, ref sfm) == eMediaReadResult.Eof)
{ btnClose_Click(null);
return;
}
// ** Show frame
if (m_vfmFrame?.IsEmpty == false)
{ m_vfmFrame.GetTexture(ref m_texScreen); m_mdlScreen.Material.Texture = m_texScreen; renView.Invalidate();
}
}
}
class cCapture : cContainer
{ cMedia? m_medSource; cVideoFrame? m_vfmVideo; cAudioFrame? m_afm; cGpuBitmap? m_gbmImg;
readonly cComboBox cboDevVid, cboDevAud;
readonly cPaintControl pntVideo; readonly cButton btnPause;
readonly cTimer m_tmrTimer;
public cCapture()
{ cLabel lbl; cButton btn;
lbl = new cLabel { LocationMargin = new Point(10, 10), Text = "Video device"};
AddControl(lbl);
cboDevVid = new cComboBox { LocationMargin = new Point(100, 10), Width = 300};
cboDevVid.Load(cMediaSource.GetCaptureDevices(true));
AddControl(cboDevVid);
lbl = new cLabel { LocationMargin = new Point(410, 10), Text = "Audio device"};
AddControl(lbl);
cboDevAud = new cComboBox { LocationMargin = new Point(500, 10), Width = 300};
cboDevAud.Load(cMediaSource.GetCaptureDevices(false));
AddControl(cboDevAud);
pntVideo = new cPaintControl { Bounds = new Rectangle(10, 40, 600, 320), BackColor = eBrush.Black};
pntVideo.Paint += pntVideo_Paint;
AddControl(pntVideo);
btn = new cButton { LocationMargin = new Point(10, 370), Text = "Start"};
btn.Click += btnStart_Click;
AddControl(btn);
btnPause = new cButton { LocationMargin = new Point(120, 370), Text = "Pause", Type = eButtonType.Check};
btnPause.Click += btnPause_Click;
AddControl(btnPause);
btn = new cButton { LocationMargin = new Point(230, 370), Text = "Stop"};
btn.Click += btnStop_Click;
AddControl(btn);
m_tmrTimer = new cTimer { Interval = 15};
m_tmrTimer.Tick += m_tmrTimer_Tick;
cboDevVid.SelectIndex(0);
cboDevAud.SelectIndex(0);
}
public new void Close() { btnStop_Click(null);}
private void pntVideo_Paint(object sender, PaintArgs e)
{ if (m_gbmImg is not null)
e.Graphics.DrawBitmap(m_gbmImg, pntVideo.ClientRectangle, eAlignment.Center | eAlignment.Middle, eResize.Scale);
}
private void btnStart_Click(object sender)
{ cMediaSource.cVideoTrack? vtkV; cMediaSource.cAudioTrack? atkA;
if (cboDevVid.SelectedItem is null && cboDevAud.SelectedItem is null) return;
btnStop_Click(null);
m_medSource = new cMedia((cboDevVid.SelectedValue is not null ? ((CaptureDevice)cboDevVid.SelectedValue).Id : null) // ** Open
, (cboDevAud.SelectedValue is not null ? ((CaptureDevice)cboDevAud.SelectedValue).Id : null)
, wMain.Device);
m_medSource.GetFirstTracks(out vtkV, out atkA);
if (vtkV is not null) { vtkV.Selected = true; vtkV.SetOutputFormat(vtkV.Format, true);}
if (atkA is not null) atkA.Selected = true;
m_tmrTimer.Start();
}
private void btnPause_Click(object sender) { if (btnPause.Checked) m_tmrTimer.Stop(); else m_tmrTimer.Start();}
private void btnStop_Click(object? sender)
{ m_medSource?.Dispose(); m_medSource = null;
m_vfmVideo?.Dispose(); m_vfmVideo = null;
m_gbmImg?.Dispose(); m_gbmImg = null;
btnPause.Checked = false; m_tmrTimer.Stop(); pntVideo.Invalidate();
}
private void m_tmrTimer_Tick(object sender)
{ cSubtitleFrame? sfm = null;
// ** Read frame
if (m_medSource?.Read(out _, out _, ref m_vfmVideo, ref m_afm, ref sfm) == eMediaReadResult.Eof)
{ btnStop_Click(null);
return;
}
// ** Show frame
if (m_vfmVideo?.IsEmpty == false)
{ m_vfmVideo.GetBitmap(ref m_gbmImg); pntVideo.Invalidate();
}
}
}
class cTranscode : cContainer
{ const int COL_ID = 0;
const int COL_TP = 1;
const int COL_W = 2;
const int COL_H = 3;
const int COL_FPS = 4;
const int COL_BPS = 5;
const int COL_CHN = 6;
cMedia? m_medSource; bool m_bProcessing;
string[]? ma_sPath; bool m_bIsSequence;
readonly cEditControl edtSource; readonly cTextBox txtDest;
readonly cGrid grdPistas;
readonly cTimeControl timStart, timEnd;
readonly cPlayer plyPlayer;
readonly cButton btnPause; readonly cSlider sliPriority;
public cTranscode()
{ cLabel lbl; cButton btn;
lbl = new cLabel { LocationMargin = new Point(10, 10), Text = "Sources"};
AddControl(lbl);
edtSource = new cEditControl { Bounds = new Rectangle(80, 10, 300, 50), ReadOnly = true};
AddControl(edtSource);
btn = new cButton { LocationMargin = new Point(390, 10), Width = 90, Text = "Open source"};
btn.Click += btnOpen_Click;
AddControl(btn);
btn = new cButton { LocationMargin = new Point(390, 40), Width = 90, Text = "Open sequence"};
btn.Click += btnOpenSeq_Click;
AddControl(btn);
lbl = new cLabel { LocationMargin = new Point(10, 70), Text = "Tracks"};
AddControl(lbl);
grdPistas = new cGrid { Bounds = new Rectangle(80, 70, 400, 170), ColumnCount = 7};
grdPistas.ConfigureColumn(COL_ID, "Id", 30, eTextFormat.CenterMiddle);
grdPistas.ConfigureColumn(COL_TP, "Type", 50, eTextFormat.CenterMiddle);
grdPistas.ConfigureColumn(COL_W, "Width", 50, eTextFormat.RightMiddle, null, false, true, typeof(int));
grdPistas.ConfigureColumn(COL_H, "Height", 50, eTextFormat.RightMiddle, null, false, true, typeof(int));
grdPistas.ConfigureColumn(COL_FPS, "Fps", 50, eTextFormat.RightMiddle, null, false, true, typeof(int));
grdPistas.ConfigureColumn(COL_BPS, "Bit rate", 60, eTextFormat.RightMiddle, null, false, true, typeof(int));
grdPistas.ConfigureColumn(COL_CHN, "Channels", 60, eTextFormat.RightMiddle, null, false, true, typeof(int));
AddControl(grdPistas);
btn = new cButton { LocationMargin = new Point(80, 250), Width = 120, Text = "Remove"};
btn.Click += btnRemove_Click;
AddControl(btn);
lbl = new cLabel { LocationMargin = new Point(10, 280), Text = "Start"};
AddControl(lbl);
timStart = new cTimeControl { LocationMargin = new Point(80, 280)};
AddControl(timStart);
lbl = new cLabel { LocationMargin = new Point(190, 280), Text = "End"};
AddControl(lbl);
timEnd = new cTimeControl { LocationMargin = new Point(260, 280)};
AddControl(timEnd);
lbl = new cLabel { LocationMargin = new Point(10, 310), Text = "Target"};
AddControl(lbl);
txtDest = new cTextBox { LocationMargin = new Point(80, 310), Width = 360};
AddControl(txtDest);
btn = new cButton { LocationMargin = new Point(450, 310), Width = 30, Text = "..."};
btn.Click += btnSetDest_Click;
AddControl(btn);
btn = new cButton { LocationMargin = new Point(500, 10), Width = 120, Text = "Play"};
btn.Click += btnPlay_Click;
AddControl(btn);
plyPlayer = new cPlayer { Bounds = new Rectangle(500, 40, 470, 290)};
AddControl(plyPlayer);
btn = new cButton { LocationMargin = new Point(80, 370), Text = "Transcode"};
btn.Click += btnTranscode_Click;
AddControl(btn);
btnPause = new cButton { LocationMargin = new Point(190, 370), Text = "Pause", Type = eButtonType.Check};
AddControl(btnPause);
btn = new cButton { LocationMargin = new Point(300, 370), Text = "Cancel"};
btn.Click += btnCancel_Click;
AddControl(btn);
lbl = new cLabel { LocationMargin = new Point(430, 370), Text = "Priority"};
AddControl(lbl);
sliPriority = new cSlider { LocationMargin = new Point(480, 370), Width = 70, Maximum = 30, Value = 20};
AddControl(sliPriority);
}
public new bool Close() { btnCancel_Click(this); return !m_bProcessing;}
private void btnOpen_Click(object sender)
{ string[]? a_s;
if (m_bProcessing) return;
a_s = mDialog.ShowOpenFiles(mMod.DLG_VID_EXTS, mMod.DLG_VID_GUID); if (a_s is null) return;
m_Open(a_s, false);
}
private void btnOpenSeq_Click(object sender)
{ string[]? a_s;
if (m_bProcessing) return;
a_s = mDialog.ShowOpenFiles(mMod.DLG_VID_EXTS, mMod.DLG_VID_GUID); if (a_s is null) return;
a_s.Sort();
m_Open(a_s, true);
}
private void btnPlay_Click(object sender)
{ if (ma_sPath is null) return;
if (m_bIsSequence) plyPlayer.OpenMedia(new cFileListStream(ma_sPath), true); else plyPlayer.OpenMedia(ma_sPath);
plyPlayer.Play();
}
private void btnRemove_Click(object sender) { grdPistas.RemoveSelectedRows();}
private void btnSetDest_Click(object sender)
{ string? s;
s = mDialog.ShowSaveFile("Mp4 videos|*.mp4;*.mpg", mMod.DLG_VID_GUID); if (s is not null) txtDest.Text = s;
}
private void btnTranscode_Click(object sender)
{ cMediaSink? snkSink = null; cFrame? fmeFrame; cVideoFrame? vfm = null; cAudioFrame? afm = null; cSubtitleFrame? sfm = null;
TimeSpan tsStartTs, tsEndTs; System.DateTime dt;
if (m_medSource is null || m_bProcessing || grdPistas.RowCount == 1 || txtDest.Text == "" // Invalid settings: exit
|| timStart.Value >= timEnd.Value || timEnd.Value > m_medSource.Duration)
{ return;
}
btnPause.Checked = false; m_bProcessing = true;
wMain.Device.ProgressMinimum = (int)timStart.Value.TotalSeconds;
wMain.Device.ProgressMaximum = (int)timEnd.Value.TotalSeconds; wMain.Device.ProgressValue = 0;
wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Normal;
try
{ // ** Create target
snkSink = new cMediaSink(txtDest.Text);
// ** Add tracks
for (int i = 1; i < grdPistas.RowCount; i++)
{ cMediaSource.cTrack stkTrack = m_medSource.Tracks[(int)grdPistas[i, COL_ID]! - 1];
if (stkTrack is cMediaSource.cVideoTrack vtkVid)
{ VideoFormat vfmt = vtkVid.Format; int iFps = (int)grdPistas[i, COL_FPS]!;
vfmt.Size = new PointI((int)grdPistas[i, COL_W]!, (int)grdPistas[i, COL_H]!);
vfmt.BitRate = (int)grdPistas[i, COL_BPS]!;
if (iFps != mMath.RoundToInt(vfmt.Fps)) { vfmt.FpsNum = 1; vfmt.FpsDen = iFps;}
if (vtkVid.Type == snkSink.DefaultVideoType && vfmt.Size == vtkVid.Format.Size) // ** Copy
{ vtkVid.DestinationTrack = snkSink.AddVideoTrack(vtkVid);
} else // ** Transcode
{ vtkVid.SetOutputFormat(vfmt, false); vtkVid.DestinationTrack = snkSink.AddVideoTrack(vfmt);
}
vtkVid.Selected = true;
} else if (stkTrack is cMediaSource.cAudioTrack atkAud)
{ AudioFormat afmt = atkAud.Format;
if ((int)grdPistas[i, COL_CHN]! != afmt.Channels) afmt.ChannelLayout = 0;
afmt.Channels = (int)grdPistas[i, COL_CHN]!;
afmt.SampleRate = (int)grdPistas[i, COL_FPS]!; afmt.BitRate = (int)grdPistas[i, COL_BPS]!;
if (atkAud.Type == snkSink.DefaultAudioType // ** Copy
&& afmt.Channels == atkAud.Format.Channels && afmt.SampleRate == atkAud.Format.SampleRate)
{ atkAud.DestinationTrack = snkSink.AddAudioTrack(atkAud);
} else // ** Transcode
{ atkAud.SetOutputFormat(afmt); atkAud.DestinationTrack = snkSink.AddAudioTrack(afmt);
}
atkAud.Selected = true;
} else if (stkTrack is cMediaSource.cSubtitleTrack stkSubtit)
{ stkSubtit.DestinationTrack = snkSink.AddSubtitleTrack(stkSubtit); // ** Copy
stkSubtit.Selected = true;
}
}
// ** Set range
tsStartTs = timStart.Value; m_medSource.Seek(tsStartTs);
tsEndTs = (timEnd.Value < m_medSource.Duration ? timEnd.Value : TimeSpan.MaxValue); // Try to read the whole file (duration might be wrong)
// ** Process frames
eMediaReadResult rr; cMediaSource.cTrack? stkSrcTrack; cMediaSink.cTrack kstDestTrack;
TimeSpan tsAbsoluteTs, tsRelativeTs, tsMax;
System.IO.FileInfo fi = new (txtDest.Text);
snkSink.BeginWriting();
dt = System.DateTime.Now; tsMax = TimeSpan.Zero;
// ** Read frame
while ((rr = m_medSource.Read(out stkSrcTrack, out fmeFrame, ref vfm, ref afm, ref sfm)) != eMediaReadResult.Eof)
{ if (rr != eMediaReadResult.Ok || fmeFrame is null) continue;
kstDestTrack = stkSrcTrack!.DestinationTrack!; tsAbsoluteTs = fmeFrame.TimeStamp;
// ** Write frame
tsRelativeTs = tsAbsoluteTs - tsStartTs; // ** Make ts relative to user start
if (tsAbsoluteTs >= tsStartTs)
{ if (tsAbsoluteTs <= tsEndTs)
{ fmeFrame.TimeStamp = tsRelativeTs;
snkSink.WriteFrame(kstDestTrack, fmeFrame);
} else // ** End of track: flush
stkSrcTrack.Flush();
}
fmeFrame.Clear();
// ** Show progress: allow to cancel
if ((System.DateTime.Now - dt).TotalSeconds >= 1)
{ if (tsRelativeTs > tsMax) tsMax = tsRelativeTs;
s_ShowStatistics(tsMax, fi);
mApplication.DoEvents(); // ** Get user input
while (btnPause.Checked && m_medSource is not null) // ** Paused: wait
{ wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Paused;
mApplication.DoEvents(); System.Threading.Thread.Sleep(1);
}
wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Normal;
if (m_medSource is null) { snkSink.EndWriting(); return;} // ** Canceled: exit
dt = System.DateTime.Now;
}
int iSleep = (int)(sliPriority.Maximum - sliPriority.Value); // ** Avoid cpu overload
if (iSleep != 0) System.Threading.Thread.Sleep(iSleep);
}
// ** Close file
snkSink.EndWriting(); s_ShowStatistics(tsMax, fi);
mDialog.MsgBoxInformation("Transcoding finished", "Transcode");
btnCancel_Click(null);
} catch (System.Exception ex)
{ wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Error; mDialog.MsgBoxError(ex.Message);
} finally
{ snkSink?.Dispose();
m_bProcessing = false; vfm?.Dispose(); afm?.Dispose(); sfm?.Dispose();
wMain.Stat2 = null;
}
wMain.Device.ProgressState = cWindow.eTaskButtonProgress.None;
}
private void btnCancel_Click(object? sender)
{ if (sender is not null && m_bProcessing && !mDialog.MsgBoxQuestion("Cancel process?", "Transcode")) return;
grdPistas.RowCount = 1;
timStart.Value = timEnd.Value = TimeSpan.Zero; edtSource.Text = null; txtDest.Text = null;
m_medSource?.Dispose(); m_medSource = null; ma_sPath = null;
}
private void m_Open(string[] a_sPath, bool bIsSequence)
{ int i;
if (m_bProcessing) return;
btnCancel_Click(null);
try
{ m_medSource = (bIsSequence ? new cMedia(new cFileListStream(a_sPath), wMain.Device, true) : new cMedia(wMain.Device, a_sPath));
timEnd.Value = m_medSource.Duration;
grdPistas.RowCount = 1 + m_medSource.Tracks.Count; i = 1;
foreach (cMediaSource.cTrack stm in m_medSource.Tracks)
{ grdPistas[i, COL_ID] = i;
if (stm is cMediaSource.cVideoTrack vtk)
{ VideoFormat vtp = vtk.Format;
grdPistas[i, COL_TP] = "Video";
grdPistas[i, COL_FPS] = mMath.RoundToInt(vtp.Fps); grdPistas[i, COL_BPS] = (vtp.BitRate != 0 ? vtp.BitRate : 500000);
grdPistas[i, COL_W] = vtp.Size.X; grdPistas[i, COL_H] = vtp.Size.Y;
} else if (stm is cMediaSource.cAudioTrack atk)
{ AudioFormat atp = atk.Format;
grdPistas[i, COL_TP] = "Audio";
grdPistas[i, COL_FPS] = atp.SampleRate; grdPistas[i, COL_BPS] = (atp.BitRate != 0 ? atp.BitRate : 128000);
grdPistas[i, COL_CHN] = atp.Channels;
} else if (stm is cMediaSource.cSubtitleTrack stk)
{ SubtitleFormat stp = stk.Format;
grdPistas[i, COL_TP] = "Subtitle";
}
i++;
}
ma_sPath = a_sPath; m_bIsSequence = bIsSequence; edtSource.Text = string.Join("\r\n", ma_sPath);
} catch (System.Exception ex)
{ mDialog.MsgBoxError(ex.Message);
btnCancel_Click(null);
}
}
}
class cConcatenate : cContainer
{ const int COL_SRC = 0;
const int COL_PATH = 1;
const int COL_START = 2;
const int COL_END = 3;
const int COL_ID = 4;
const int COL_STM_TP = 5;
const int COL_TP = 6;
const int COL_W = 7;
const int COL_H = 8;
const int COL_FPS = 9;
const int COL_BPS = 10;
const int COL_CHN = 11;
cMedia? m_medSource; bool m_bProcessing;
readonly cGrid grdSources;
readonly cTextBox txtDest;
readonly cButton btnPause;
public cConcatenate()
{ cLabel lbl; cButton btn;
lbl = new cLabel { LocationMargin = new Point(10, 10), Text = "Sources"};
AddControl(lbl);
grdSources = new cGrid { Bounds = new Rectangle(80, 10, 870, 260), ColumnCount = 12};
grdSources.Columns(COL_SRC).Visible = false;
grdSources.ConfigureColumn(COL_PATH, "Path", 300);
grdSources.ConfigureColumn(COL_START, "Start", 60, eTextFormat.CenterMiddle, null, false, true, typeof(TimeSpan));
grdSources.ConfigureColumn(COL_END, "End", 60, eTextFormat.CenterMiddle, null, false, true, typeof(TimeSpan));
grdSources.ConfigureColumn(COL_ID, "Id", 30, eTextFormat.CenterMiddle);
grdSources.ConfigureColumn(COL_STM_TP, "Track type", 70, eTextFormat.CenterMiddle);
grdSources.ConfigureColumn(COL_TP, "Type", 50, eTextFormat.CenterMiddle);
grdSources.ConfigureColumn(COL_W, "Width", 50, eTextFormat.RightMiddle);
grdSources.ConfigureColumn(COL_H, "Height", 50, eTextFormat.RightMiddle);
grdSources.ConfigureColumn(COL_FPS, "Fps", 50, eTextFormat.RightMiddle);
grdSources.ConfigureColumn(COL_BPS, "Bit rate", 60, eTextFormat.RightMiddle);
grdSources.ConfigureColumn(COL_CHN, "Channels", 60, eTextFormat.RightMiddle);
AddControl(grdSources);
btn = new cButton { LocationMargin = new Point(80, 280), Width = 120, Text = "Add source"};
btn.Click += btnAddSource_Click;
AddControl(btn);
btn = new cButton { LocationMargin = new Point(210, 280), Width = 120, Text = "Remove"};
btn.Click += btnRemove_Click;
AddControl(btn);
lbl = new cLabel { LocationMargin = new Point(10, 310), Text = "Target"};
AddControl(lbl);
txtDest = new cTextBox { LocationMargin = new Point(80, 310), Width = 360};
AddControl(txtDest);
btn = new cButton { LocationMargin = new Point(450, 310), Width = 30, Text = "..."};
btn.Click += btnSetDest_Click;
AddControl(btn);
btn = new cButton { LocationMargin = new Point(80, 370), Text = "Concatenate"};
btn.Click += btnConcat_Click;
AddControl(btn);
btnPause = new cButton { LocationMargin = new Point(190, 370), Text = "Pause", Type = eButtonType.Check};
AddControl(btnPause);
btn = new cButton { LocationMargin = new Point(300, 370), Text = "Cancel"};
btn.Click += btnCancel_Click;
AddControl(btn);
}
private void btnAddSource_Click(object sender)
{ string? s; int iRow, iStm;
if (m_bProcessing) return;
s = mDialog.ShowOpenFile(mMod.DLG_VID_EXTS, mMod.DLG_VID_GUID); if (s is null) return;
try
{ m_medSource = new cMedia(wMain.Device, s);
iRow = grdSources.RowCount; grdSources.RowCount += 1 + m_medSource.Tracks.Count;
grdSources[iRow, COL_SRC] = m_medSource; grdSources[iRow, COL_PATH] = s;
grdSources[iRow, COL_START] = TimeSpan.Zero; grdSources[iRow, COL_END] = m_medSource.Duration;
iRow++; iStm = 1;
foreach (cMediaSource.cTrack stm in m_medSource.Tracks)
{ grdSources[iRow, COL_ID] = iStm;
if (stm is cMediaSource.cVideoTrack vtk)
{ VideoFormat vtp = vtk.Format;
grdSources[iRow, COL_STM_TP] = "Video"; grdSources[iRow, COL_TP] = vtk.Type.ToString();
grdSources[iRow, COL_FPS] = mMath.RoundToInt(vtp.Fps);
grdSources[iRow, COL_W] = vtp.Size.X; grdSources[iRow, COL_H] = vtp.Size.Y;
} else if (stm is cMediaSource.cAudioTrack atk)
{ AudioFormat atp = atk.Format;
grdSources[iRow, COL_STM_TP] = "Audio"; grdSources[iRow, COL_TP] = atk.Type.ToString();
grdSources[iRow, COL_FPS] = atp.SampleRate; grdSources[iRow, COL_BPS] = (atp.BitRate != 0 ? atp.BitRate : 128000);
grdSources[iRow, COL_CHN] = atp.Channels;
} else if (stm is cMediaSource.cSubtitleTrack stk)
{ SubtitleFormat stp = stk.Format;
grdSources[iRow, COL_STM_TP] = "Subtitle"; grdSources[iRow, COL_TP] = stk.Type.ToString();
}
iRow++; iStm++;
}
} catch (System.Exception ex)
{ mDialog.MsgBoxError(ex.Message);
}
}
private void btnRemove_Click(object sender)
{ int iRow;
iRow = grdSources.CurrentRow; if (iRow == -1) return;
if (grdSources[iRow, COL_SRC] is not null) // ** Remove source
{ do grdSources.RemoveRows(iRow, 1); while (iRow < grdSources.RowCount && grdSources[iRow, COL_SRC] is null);
} else // ** Remove track
{ grdSources.RemoveRows(iRow, 1);
}
}
private void btnSetDest_Click(object sender)
{ string? s;
s = mDialog.ShowSaveFile("Mp4 videos|*.mp4;*.mpg", mMod.DLG_VID_GUID); if (s is not null) txtDest.Text = s;
}
private void btnConcat_Click(object sender)
{ int iRow, iRow0, iRow0Max; cMedia medSrc0, medSrc; cMediaSource.cTrack stmStm;
cMediaSink? snkSink = null; cFrame? fmeFrame; cVideoFrame? vfm = null; cAudioFrame? afm = null; cSubtitleFrame? sfm = null;
TimeSpan tsMax; System.DateTime dt;
System.IO.FileInfo fi;
if (m_bProcessing || grdSources.RowCount == 1 || txtDest.Text == "") // Invalid settings: exit
{ return;
}
btnPause.Checked = false; m_bProcessing = true;
wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Indeterminate;
try
{ // ** Create target
snkSink = new cMediaSink(txtDest.Text);
// ** Add tracks (only from first source)
iRow = 1; medSrc0 = (cMedia)grdSources[iRow, COL_SRC]!;
for (iRow++; iRow < grdSources.RowCount && grdSources[iRow, COL_SRC] is null; iRow++)
{ stmStm = medSrc0.Tracks[(int)grdSources[iRow, COL_ID]! - 1];
if (stmStm is cMediaSource.cVideoTrack vtkVid)
{ if (vtkVid.Type == snkSink.DefaultVideoType) // Same type
{ vtkVid.DestinationTrack = snkSink.AddVideoTrack(vtkVid);
} else // Error
{ throw new System.FormatException();
}
vtkVid.Selected = true;
} else if (stmStm is cMediaSource.cAudioTrack atkAud)
{ if (atkAud.Type == snkSink.DefaultAudioType) // Same type
{ atkAud.DestinationTrack = snkSink.AddAudioTrack(atkAud);
} else // Error
{ throw new System.FormatException();
}
atkAud.Selected = true;
} else if (stmStm is cMediaSource.cSubtitleTrack stkSubtit)
{ stkSubtit.DestinationTrack = snkSink.AddSubtitleTrack(stkSubtit);
stkSubtit.Selected = true;
}
}
// ** Validate tracks of other sources
iRow0Max = iRow;
for (; iRow < grdSources.RowCount; )
{ medSrc = (cMedia)grdSources[iRow, COL_SRC]!; iRow0 = 2;
for (iRow++; iRow < grdSources.RowCount && grdSources[iRow, COL_SRC] is null; iRow++, iRow0++)
{ if (iRow0 == iRow0Max) throw new System.FormatException("Track count mismatch");
stmStm = medSrc.Tracks[(int)grdSources[iRow, COL_ID]! - 1];
if (stmStm is cMediaSource.cVideoTrack vtkVid)
{ cMediaSource.cVideoTrack vst0 = (cMediaSource.cVideoTrack)medSrc0.Tracks[(int)grdSources[iRow0, COL_ID]! - 1];
if (vtkVid.Type == vst0.Type // Same type and fmt
/*&& vstVid.Format.Width == vst0.Format.Width && vstVid.Format.Height == vst0.Format.Height*/)
{ vtkVid.DestinationTrack = vst0.DestinationTrack;
} else // Error
{ throw new System.FormatException();
}
vtkVid.Selected = true;
} else if (stmStm is cMediaSource.cAudioTrack atkAud)
{ cMediaSource.cAudioTrack ast0 = (cMediaSource.cAudioTrack)medSrc0.Tracks[(int)grdSources[iRow0, COL_ID]! - 1];
if (atkAud.Type == ast0.Type // Same type and fmt
&& atkAud.Format.Channels == ast0.Format.Channels && atkAud.Format.SampleRate == ast0.Format.SampleRate)
{ atkAud.DestinationTrack = ast0.DestinationTrack;
} else // Error
{ throw new System.FormatException();
}
atkAud.Selected = true;
} else if (stmStm is cMediaSource.cSubtitleTrack stkSubtit)
{ cMediaSource.cSubtitleTrack sst0 = (cMediaSource.cSubtitleTrack)medSrc0.Tracks[(int)grdSources[iRow0, COL_ID]! - 1];
stkSubtit.DestinationTrack = sst0.DestinationTrack;
stkSubtit.Selected = true;
}
}
if (iRow0 != iRow0Max) throw new System.FormatException("Track count mismatch");
}
// ** Copy frames
eMediaReadResult rr; cMediaSource.cTrack? stkSrcTrack; cMediaSink.cTrack ktkDestTrack;
TimeSpan tsSinkTs, tsStartTs, tsEndTs, tsSrcTs, tsRelativeTs;
snkSink.BeginWriting();
dt = System.DateTime.Now; tsSinkTs = tsMax = TimeSpan.Zero;
fi = new System.IO.FileInfo(txtDest.Text);
for (iRow = 1; iRow < grdSources.RowCount; iRow++)
{ medSrc = (cMedia)grdSources[iRow, COL_SRC]!; if (medSrc is null) continue; // Stm: continue
// ** Set range
tsStartTs = (TimeSpan)grdSources[iRow, COL_START]!; medSrc.Seek(tsStartTs);
tsEndTs = (TimeSpan)grdSources[iRow, COL_END]!;
if (tsEndTs >= medSrc.Duration) tsEndTs = TimeSpan.MaxValue; // Try to read the whole file (duration might be wrong)
// ** Read frame
while ((rr = medSrc.Read(out stkSrcTrack, out fmeFrame, ref vfm, ref afm, ref sfm)) != eMediaReadResult.Eof)
{ if (rr != eMediaReadResult.Ok || fmeFrame is null) continue;
ktkDestTrack = stkSrcTrack!.DestinationTrack!; tsSrcTs = fmeFrame.TimeStamp;
// ** Write frame
tsRelativeTs = tsSinkTs + tsSrcTs - tsStartTs; // ** Make ts relative to user start
if (tsRelativeTs > tsMax) tsMax = tsRelativeTs;
if (tsSrcTs >= tsStartTs)
{ if (tsSrcTs <= tsEndTs)
{ fmeFrame.TimeStamp = tsRelativeTs;
snkSink.WriteFrame(ktkDestTrack, fmeFrame);
} else // ** End of track: flush
stkSrcTrack.Flush();
}
// ** Show progress: allow to cancel
if ((System.DateTime.Now - dt).TotalSeconds >= 1)
{ s_ShowStatistics(tsMax, fi);
mApplication.DoEvents(); // ** Get user input
while (btnPause.Checked && grdSources.RowCount != 1) // ** Paused: wait
{ wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Paused;
mApplication.DoEvents(); System.Threading.Thread.Sleep(1);
}
if (grdSources.RowCount == 1) { snkSink.EndWriting(); return;} // ** Canceled: exit
dt = System.DateTime.Now;
}
}
tsSinkTs = tsMax;
}
// ** Close file
snkSink.EndWriting(); s_ShowStatistics(tsMax, fi);
mDialog.MsgBoxInformation("Transcoding finished", "Transcode");
btnCancel_Click(null);
} catch (System.Exception ex)
{ wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Error; mDialog.MsgBoxError(ex.Message);
} finally
{ snkSink?.Dispose();
m_bProcessing = false; vfm?.Dispose(); afm?.Dispose(); sfm?.Dispose();
wMain.Stat2 = null;
}
wMain.Device.ProgressState = cWindow.eTaskButtonProgress.None;
}
private void btnCancel_Click(object? sender)
{ if (sender is not null && m_bProcessing && !mDialog.MsgBoxQuestion("Cancel process?", "Concatenate")) return;
for (int iRow = 1; iRow < grdSources.RowCount; iRow++)
{ ((cMediaSource?)grdSources[iRow, COL_SRC])?.Dispose(); grdSources[iRow, COL_SRC] = null;
}
grdSources.RowCount = 1; txtDest.Text = null;
}
}
class cGenerate : cContainer
{ bool m_bGenerating;
public cGenerate()
{ cButton btn;
btn = new cButton { LocationMargin = new Point(10, 10), Text = "Generate..."};
btn.Click += btnGenerate_Click;
AddControl(btn);
}
public new bool Close() { return !m_bGenerating;}
private void btnGenerate_Click(object sender)
{ /*const*/ TimeSpan DURATION = new (0, 0, 3); const int SLEEP = 10;
string? sFile; cMediaSink snkSink; cMediaSink.cVideoTrack vtkVideoTrack; cMediaSink.cAudioTrack atkAudioTrack;
VideoFormat vfmtVideo; System.IO.FileInfo fi; TimeSpan tsMax;
if (m_bGenerating) return;
// ** Create output file
sFile = mDialog.ShowSaveFile("Mp4 videos|*.mp4|All files|*.*", mMod.DLG_VID_GUID); if (sFile is null) return;
snkSink = new cMediaSink(sFile); m_bGenerating = true;
wMain.Device.ProgressMinimum = 0; wMain.Device.ProgressMaximum = (int)DURATION.TotalSeconds; wMain.Device.ProgressValue = 0;
wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Normal;
try
{ // ** Add video track
vfmtVideo = new VideoFormat(630, 420);
vtkVideoTrack = snkSink.AddVideoTrack(vfmtVideo);
// ** Add audio track
atkAudioTrack = snkSink.AddAudioTrack(new AudioFormat(2, SAMPLE_RATE));
// ** Write
snkSink.BeginWriting();
fi = new System.IO.FileInfo(sFile); tsMax = TimeSpan.Zero;
using cVideoFrame vfmVideo = new (); using cAudioFrame afmAudio = new ();
using cPlane plnModel = new (wMain.Device) { Yaw = 90, RudderYaw = -1};
TimeSpan tsVFrameDurat; float fVFrameDuratSecs;
cCamera camCam;
float[] a_fFrame; bool bAudioLeftChn; double dTimeL, dTimeR, fVol;
System.DateTime dt;
tsVFrameDurat = new TimeSpan((long)(10000000 / vfmtVideo.Fps)); fVFrameDuratSecs = (float)tsVFrameDurat.TotalSeconds;
camCam = new cCamera(vfmtVideo.Size.X, vfmtVideo.Size.Y) { Pitch = -10, Location = new Vector(0, 1, -10)};
a_fFrame = new float[TOT_AUD_SAMPLES]; bAudioLeftChn = true; dTimeL = dTimeR = 0; fVol = mMath.PI / 2;
dt = System.DateTime.Now;
for (TimeSpan tsVideoTs = TimeSpan.Zero, tsAudioTs = TimeSpan.Zero; tsVideoTs < DURATION; )
{ // ** Compose video
vfmVideo.Reset(wMain.Device, vfmtVideo.Size.X, vfmtVideo.Size.Y);
//wMain.Device.Render3DTo(vfmVideo, (object o, c3DGraphics g3d) =>
//{ g3d.ClearTarget(wMain.Device.BackBuffer, eColor.SkyBlue); g3d.SetDefaults(camCam);
// g3d.Render(plnModel); _ = plnModel.Act(fVFrameDuratSecs);
//});
//wMain.Device.Render2DTo(vfmVideo, (cGraphics g) =>
//{ if (tsVideoTs.TotalSeconds is >= 1 and <= 4)
// { g.DrawText("Text", new Rectangle(50, 290, 200, 20), eBrush.Black, eFont.SystemBoldText, eTextFormat.Default);
// }
//});
// ** Write video
vfmVideo.TimeStamp = tsVideoTs;
snkSink.WriteFrame(vtkVideoTrack, vfmVideo);
tsVideoTs += tsVFrameDurat; vfmVideo.Clear();
// ** Compose audio
while (tsAudioTs < tsVideoTs)
{ if (bAudioLeftChn)
m_GenerateAudio(afmAudio, a_fFrame, ref dTimeL, ref dTimeR, 150, mMath.Sin(fVol) / 2 + 0.5f, 0, 0);
else
{ m_GenerateAudio(afmAudio, a_fFrame, ref dTimeL, ref dTimeR, 0, 0, 150, -mMath.Sin(fVol) / 2 + 0.5f);
}
bAudioLeftChn = !bAudioLeftChn; fVol = (fVol + 0.08f) % mMath.PIx2;
// ** Write audio
afmAudio.TimeStamp = tsAudioTs;
snkSink.WriteFrame(atkAudioTrack, afmAudio);
tsAudioTs += afmAudio.Duration; afmAudio.Clear();
}
// ** Show progress
if ((System.DateTime.Now - dt).TotalSeconds >= 1)
{ if (tsVideoTs > tsMax) tsMax = tsVideoTs;
s_ShowStatistics(tsMax, fi);
mApplication.DoEvents();
if (IsClosed) { snkSink.Dispose(); return;} // ** Form closed: exit
dt = System.DateTime.Now;
}
System.Threading.Thread.Sleep(SLEEP);
}
// ** Close file
snkSink.EndWriting(); s_ShowStatistics(tsMax, fi);
mDialog.MsgBoxInformation("Generation finished", "Generate");
} catch (System.Exception ex)
{ wMain.Device.ProgressState = cWindow.eTaskButtonProgress.Error; mDialog.MsgBoxError(ex.Message);
} finally
{ m_bGenerating = false;
snkSink.Dispose();
wMain.Stat2 = null;
}
wMain.Device.ProgressState = cWindow.eTaskButtonProgress.None;
}
private void m_GenerateAudio(cAudioFrame afmAudio, float[] a_fFrame, ref double dTimeL, ref double dTimeR
, float fFrecL, float fVolL, float fFrecR, float fVolR)
{ double dTL = dTimeL, dTR = dTimeR;
fFrecL *= mMath.PIx2 / SAMPLE_RATE; fFrecR *= mMath.PIx2 / SAMPLE_RATE;
for (int i = 0; i < TOT_AUD_SAMPLES; )
{ a_fFrame[i++] = mMath.Sin(dTL) * fVolL; a_fFrame[i++] = mMath.Sin(dTR) * fVolR;
dTL += fFrecL; dTR += fFrecR;
}
dTimeL = dTL % mMath.PIx2; dTimeR = dTR % mMath.PIx2;
afmAudio.Reset(a_fFrame, AUD_SAMPLES, 2);
afmAudio.Duration = TimeSpan.FromSeconds((double)AUD_SAMPLES / SAMPLE_RATE);
}
}
class cSynthesizer : cContainer
{ private class cBeatCtl : cStackPanel
{ float m_fTime; bool m_bBeat;
readonly cSlider sliNote, sliAttack, sliDecay, sliSustain, sliSustainAmp, sliRelease, sliDelay, sliRepetition;
readonly cComboBox cboPreset;
public event dEvent? Changed, Restart;
public cBeatCtl()
{ cLabel lbl;
lbl = new cLabel { Text = "Note"};
AddControl(lbl);
sliNote = new cSlider { Width = 60, Minimum = 1, Maximum = 16, Value = 1};
sliNote.ValueChanged += sliSlider_ValueChanged;
AddControl(sliNote);
lbl = new cLabel { Text = "Attack"};
AddControl(lbl);
sliAttack = new cSlider { Width = 60, Minimum = 0, Maximum = 1, Value = 0.01f};
sliAttack.ValueChanged += sliSlider_ValueChanged;
AddControl(sliAttack);
lbl = new cLabel { Text = "Decay"};
AddControl(lbl);
sliDecay = new cSlider { Width = 60, Minimum = 0, Maximum = 2, Value = 0.5f};
sliDecay.ValueChanged += sliSlider_ValueChanged;
AddControl(sliDecay);
lbl = new cLabel { Text = "Sustain amp"};
AddControl(lbl);
sliSustainAmp = new cSlider { Width = 60, Minimum = 0, Maximum = 1, Value = 0.01f};
sliSustainAmp.ValueChanged += sliSlider_ValueChanged;
AddControl(sliSustainAmp);
lbl = new cLabel { Text = "Sustain"};
AddControl(lbl);
sliSustain = new cSlider { Width = 60, Minimum = 0, Maximum = 1, Value = 0.01f};
sliSustain.ValueChanged += sliSlider_ValueChanged;
AddControl(sliSustain);
lbl = new cLabel { Text = "Release"};
AddControl(lbl);
sliRelease = new cSlider { Width = 60, Minimum = 0, Maximum = 1, Value = 0.01f};
sliRelease.ValueChanged += sliSlider_ValueChanged;
AddControl(sliRelease);
lbl = new cLabel { Text = "Delay"};
AddControl(lbl);
sliDelay = new cSlider { Width = 60, Minimum = 0, Maximum = 10, Value = 0};
sliDelay.ValueChanged += sliDelay_ValueChanged;
AddControl(sliDelay);
lbl = new cLabel { Text = "Repetition"};
AddControl(lbl);
sliRepetition = new cSlider { Width = 60, Minimum = 0, Maximum = 4, Value = 1.5f};
sliRepetition.ValueChanged += sliSlider_ValueChanged;
AddControl(sliRepetition);
lbl = new cLabel { Text = "Preset"};
AddControl(lbl);
cboPreset = new cComboBox { Width = 60, Tag = this};
cboPreset.Load(new string[] { "A", "B"});
cboPreset.SelectionChanged += cboPreset_SelectionChanged;
AddControl(cboPreset);
AutoSize = eAutoSize.Both; BorderStyle = eBorderStyle.Default;
Reset();
}
public void WriteSound(float[] a_fFrame)
{ float fNote = sliNote.Value; // Get properties
float fFreq = m_fNoteToFreq(fNote);
float fAttackTime = sliAttack.Value;
float fDecayTime = sliDecay.Value;
float fSustainTime = sliSustain.Value;
float fSustainAmplitude = sliSustainAmp.Value;
float fReleaseTime = sliRelease.Value;
float fRepetition = sliRepetition.Value;
for (int iSamp = 0; iSamp < TOT_AUD_SAMPLES; iSamp += 2)
{ if (m_bBeat)
{ float fVal, fAmp, fBeatTime;
// ** Calculate value
fVal = m_fOscSin(fFreq) // Generate carrier
//fVal = m_fOscSquare(fFreq) * 0.5f // Generate carrier
//fVal = (float)(Math.Asin(Math.Sin(fFreq)) * (2.0 / Math.PI)); // Generate carrier
//fVal = (float)((2.0 / Math.PI) * (fFreq * Math.PI * (m_fTime % (1.0 / fFreq)) - (Math.PI / 2.0))); // Generate carrier
+ m_fOscSin(fFreq, 1, 0.125f) * 0.3f // Add noise with out-of-phase angles but same freq
+ m_fOscSin(fFreq, 1, 0.25f) * 0.1f
+ m_fOscSin(m_fNoteToFreq(fNote - 12)) * 0.25f // Modify freq
+ m_fOscSin(m_fNoteToFreq(fNote - 24)) * 0.1f
;
// ** Modulate amplitude
fBeatTime = m_fTime;
if (fBeatTime <= fAttackTime)
fAmp = (fAttackTime != 0 ? fBeatTime / fAttackTime : 1);
else if ((fBeatTime -= fAttackTime) < fDecayTime)
fAmp = 1 - (fBeatTime / fDecayTime) * (1 - fSustainAmplitude);
else if ((fBeatTime -= fDecayTime) <= fSustainTime)
fAmp = fSustainAmplitude;
else if ((fBeatTime -= fSustainTime) < fReleaseTime)
fAmp = fSustainAmplitude - (fBeatTime / fReleaseTime) * fSustainAmplitude;
else
{ fAmp = 0; m_bBeat = false;
}
fVal *= fAmp;
// ** Add value to channels
a_fFrame[iSamp] += fVal; a_fFrame[iSamp + 1] += fVal;
m_fTime += TIME_FRACTION;
} else if (m_fTime >= fRepetition || m_fTime == 0)
{ m_fTime = 0; m_bBeat = true;
} else
{ m_fTime += TIME_FRACTION;
}
}
}
public void Reset() { m_fTime = -sliDelay.Value; m_bBeat = false;}
private void sliSlider_ValueChanged(object sender, cTrackBar.eAction action) { Changed?.Invoke(this);}
private void sliDelay_ValueChanged(object sender, cTrackBar.eAction action) { Restart?.Invoke(this);}
private void cboPreset_SelectionChanged(object sender)
{ cBeatCtl bct = (cBeatCtl)cboPreset.Tag!;
switch (cboPreset.SelectedIndex)
{ case 0:
bct.sliAttack.Value = 0.01f;
bct.sliDecay.Value = 1;
bct.sliSustain.Value = 0;
bct.sliSustainAmp.Value = 0;
bct.sliRelease.Value = 1;
break;
case 1:
bct.sliAttack.Value = 0.01f;
bct.sliDecay.Value = 0.15f;
bct.sliSustain.Value = 0;
bct.sliSustainAmp.Value = 0;
bct.sliRelease.Value = 0;
break;
}
}
private float m_fNoteToFreq(float fNote) { return (float)(256 * Math.Pow(1.0594630943592952645618252949463, fNote));}
private float m_fCalcAngle(float fFreq, float fSpeed = 1, float fOffset = 0)
{ return mMath.PIx2 * fFreq * m_fTime * fSpeed + fOffset * mMath.PIx2;
}
private float m_fOscSin(float fFreq, float fFactor = 1, float fOffset = 0)
{ return mMath.Sin(m_fCalcAngle(fFreq, fFactor, fOffset));
}
private float m_fOscSquare(float fFreq, float fFactor = 1, float fOffset = 0)
{ return (mMath.Sin(m_fCalcAngle(fFreq, fFactor, fOffset)) >= 0 ? 1 : -1);
}
}
readonly cAudioRenderer m_arnAudRen; readonly float[] ma_fFrame;
readonly cStackPanel spnBeats;
readonly cButton btnPlay;
readonly cSlider sliVol;
readonly cTimer m_tmrTimer;
public cSynthesizer()
{ cLabel lbl; cScrollableControl sct; cButton btn;
lbl = new cLabel { LocationMargin = new Point(10, 10), Text = "Beats"};
AddControl(lbl);
sct = new cScrollableControl { LocationMargin = new Point(50, 10), RightMargin = 10, Height = 350};
sct.VerticalBar.Visible = true; sct.VerticalBar.AutoHide = false; sct.HorizontalBar.AutoHide = false;
AddControl(sct);
spnBeats = new cStackPanel { AutoSize = eAutoSize.Both, Direction = eDirection.Bottom};
sct.ClientArea.AddControl(spnBeats);
btn = new cButton { Margins = new Rect(50, float.NaN, float.NaN, 0), Text = "Add"};
btn.Click += btnAdd_Click;
AddControl(btn);
btnPlay = new cButton { Margins = new Rect(160, float.NaN, float.NaN, 0), Text = "Play / Stop", Type = eButtonType.Check};
btnPlay.Click += btnPlay_Click;
AddControl(btnPlay);
sliVol = new cSlider { Margins = new Rect(float.NaN, float.NaN, 5, 0), Width = 70, Value = 50};
sliVol.ValueChanged += sliVol_ValueChanged;
AddControl(sliVol);
m_tmrTimer = new cTimer { Interval = 40};
m_tmrTimer.Tick += m_tmrTimer_Tick;
m_arnAudRen = new cAudioRenderer();
m_arnAudRen.SetFormat(new AudioFormat(2, SAMPLE_RATE));
ma_fFrame = new float[TOT_AUD_SAMPLES];
btnAdd_Click(null);
}
public override void Close() { m_tmrTimer.Stop(); m_arnAudRen.Dispose(); base.Close();}
private void btnAdd_Click(object? sender)
{ cBeatCtl bea;