-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
2913 lines (2263 loc) · 117 KB
/
Form1.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
namespace Guitarsharp
{
using NAudio.Midi;
using NAudio.Wave;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System;
using System.Text.Json;
using System.Windows.Forms;
using static System.Runtime.InteropServices.JavaScript.JSType;
using NAudio.Mixer;
using NAudio.Wave.SampleProviders;
using static System.Windows.Forms.DataFormats;
using Microsoft.VisualBasic;
using System.Linq;
using System.Diagnostics;
using NAudio.Utils;
using System.Security.Principal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using RadioButton = RadioButton;
using MathNet.Numerics.IntegralTransforms;
using System.Numerics;
using System.Diagnostics.Eventing.Reader;
using System.Text;
[Serializable]
public partial class Form1 : Form
{
public KarplusStrong karplusStrong;
private MidiHandler midiHandler;
private WaveOut waveOut;
private WaveOut playWaveOut;
private System.Windows.Forms.Timer midiPlaybackTimer = new System.Windows.Forms.Timer();
private float currentTime = 0;
private int currentEventIndex = 0;
public KarplusStrongSampleProvider sampleProvider;
public int fretboardMidiNoteNumber = 0;
private List<Note> allNotes = new List<Note>();
private Composition composition;
private Composition fingerPatternComposition = new Composition();
private float PixelsPerSecond = 150.0f; // zoom factor: adjust this value as needed
private bool noNoteAddedSinceLastSpacePress;
public Note selectedNote = null;
public Note noteToDelete = null;
public Point initialMousePosition;
public int version = 1;
public float activeNoteDuration = 1f;
bool isDraggingLeft = false;
bool isDraggingRight = false;
private bool isDraggingUp = false;
private bool isDraggingDown = false;
private float baseNoteDuration;
private int timeSignatureNumerator = 5;
private int timeSignatureDenominator = 4;
private int tempo = 100;//default
private int[] midiChannelPerString = { 1, 2, 3, 4, 5, 6 };
private long currentPlaybackPosition = 0;
private List<Note> notes = new List<Note>();
private bool isDotted = false;
private bool isTriplet = false;
private Timer debounceTimer;
private bool isSpacebarPressed = false;
private int initialNoteHeight = 0;
private int initialNoteWidth = 0;
private int numberOfBars = 100;
private IWavePlayer waveOutDevice;
private IWavePlayer PlayWaveOutDevice;
private MixingSampleProvider mixer;
private int currentNoteIndex = 0;
private float currentPlaybackTime = 0;
private FretPattern activeFretPattern;
public List<FretPattern> allFretPatterns = new List<FretPattern>();
public List<FingeringPattern> allFingeringPatterns = new List<FingeringPattern>();
public int activeFingeringPattern = 0;
public bool fingeringPatternMode = false;
public bool fretPatternMode = false;
public bool triplePerBeatMode = false;
public bool quintuplePerBeatMode = false;
public bool sexatupePerBeatMode = false;
public bool septatuplePerBeatMode = false;
private string currentAudioFilePath;
private AudioFileReader audioFileReader;
private int filenumber = 1;
private bool staccatoMode = false;
private int selectedStringIndex;
private Guitar theGuitar;
public float lowPassCutOffValue;
public float lowPassQValue;
private bool applyToAllStrings = false;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
WaveFormat format = GlobalConfig.GlobalWaveFormat;
string midiFilePath = "firstglobal.gur"; // Ensure this path is correct
double defaultTempo = 60.0; // Example default tempo
midiHandler = new MidiHandler(midiFilePath, defaultTempo);
karplusStrong = new KarplusStrong(GlobalConfig.GlobalWaveFormat.SampleRate, 333);
// InitializeAudio();
this.KeyPreview = true;
// Attach the single event handler to all relevant buttons
foreach (Control control in fretboardPanel.Controls)
{
if (control is Button && control.Tag != null)
{
control.Click += FretboardButton_Click;
}
}
karplusStrong = new KarplusStrong(format.SampleRate, 220.0f); // This is just an initial frequency.
karplusStrong.UpdateFrequency(20000f);
sampleProvider = new KarplusStrongSampleProvider(karplusStrong, 20000f);
waveOut = new WaveOut();
waveOut.Init(sampleProvider);
waveOut.Play();
midiHandler.NoteOnReceived += (noteNumber) =>
{
double frequency = midiHandler.MidiNoteToFrequency(noteNumber);
karplusStrong.Pluck((float)frequency);
};
midiPlaybackTimer.Interval = 10; // Check every 10ms
midiPlaybackTimer.Tick += MidiPlaybackTimer_Tick;
InitializeComposition();
noNoteAddedSinceLastSpacePress = true;
baseNoteDuration = CalculateBeatDuration();
guitarRollPanel.Invalidate();
debounceTimer = new Timer { Interval = 500 };
debounceTimer.Tick += (s, e) =>
{
debounceTimer.Stop();
isSpacebarPressed = false;
};
this.KeyPreview = true;
// Set the padding for the guitarRollPanel to accommodate the scrollbar
int scrollbarHeight = SystemInformation.HorizontalScrollBarHeight;
guitarRollPanel.Padding = new Padding(0, 0, 0, scrollbarHeight + 20);
// Set a default length for the guitar roll panel
int defaultLength = ((int)CalculateBarDuration() * numberOfBars) * (int)PixelsPerSecond; // Example length in pixels
guitarRollPanel.AutoScrollMinSize = new Size(defaultLength, guitarRollPanel.Height);
blancheButton.BackColor = Color.LightGreen;
rondeButton.BackColor = Color.LightGreen;
noireButton.BackColor = Color.Coral;
crocheButton.BackColor = Color.LightGreen;
doubleCrocheButton.BackColor = Color.LightGreen;
tripleCrocheButton.BackColor = Color.LightGreen;
triplePerBeatButton.BackColor = Color.LightGreen;
quintuplePerBeatButton.BackColor = Color.LightGreen;
sexatuplePerBeatButton.BackColor = Color.LightGreen;
setptatuplePerBeatButton.BackColor = Color.LightGreen;
CreateFretPatterns();
for (int i = 0; i < 12; i++)
{
FingeringPattern fingeringPattern = new FingeringPattern();
allFingeringPatterns.Add(fingeringPattern);
}
InitFretBoard();
fingerPatternComposition.Title = "fingering pattern";
for (int i = 0; i < 6; i++)
{
RadioButton radioButton = new RadioButton();
radioButton.Height = 50;
radioButton.Width = 200;
radioButton.Text = $"String {i + 1}";
radioButton.Location = new Point((i * 200), 80); // Adjust the location for each button
if (i == 0) radioButton.Checked = true; else radioButton.Checked = false;
radioButton.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
stringSynthroupBox.Controls.Add(radioButton);
}
theGuitar = new Guitar(GlobalConfig.GlobalWaveFormat.SampleRate);
loadfilesfortesting();
Debug.WriteLine("Init completed");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void loadfilesfortesting()
{/*
string guitarbody = "RAMIREZ.wav";
string reverb = "Performance Hall - XY Close.wav";
float[] impulseResponse = LoadWaveFile("RAMIREZ.wav");
for (int i = 0; i < 6; i++)
{
// Now, pass this impulseResponse to the KarplusStrong
theGuitar.strings[i].SetImpulseResponse(impulseResponse);
//might as well set the lowpassfilter:)
theGuitar.strings[i].lowPassCutOffValue = lowPassCutOffValue;//default value
}
LoadFretPatterns("base.gfp");
*/
string FileName = "firstglobal.gur";
string jsonString = File.ReadAllText(FileName);
// Form1Data data = JsonSerializer.Deserialize<Form1Data>(jsonString);
AllDataContainer container = JsonSerializer.Deserialize<AllDataContainer>(jsonString);
this.allNotes = container.Form1Data.AllNotes;
this.composition = container.Form1Data.Composition;
this.fretboardMidiNoteNumber = container.Form1Data.FretboardMidiNoteNumber;
this.PixelsPerSecond = container.Form1Data.PixelsPerSecond;
this.midiChannelPerString = container.Form1Data.MidiChannelPerString;
this.timeSignatureNumerator = container.Form1Data.timeSignatureNumerator;
this.timeSignatureDenominator = container.Form1Data.timeSignatureNumerator;
this.tempo = container.Form1Data.tempo;
//check the version and handle older versions accordingly(e.g., provide default values for new properties or convert data from old formats).
if (this.version != container.Form1Data.Version) MessageBox.Show("wrong version");// we can do better :) but for now that's it...
// Load fret patterns
LoadFretPatternsFromData(container.FretPatternData);
// Load fingering patterns
allFingeringPatterns = container.FingeringPatterns;
guitarRollPanel.Invalidate(); // Refresh the panel to reflect the loaded data
}
private void InitFretBoard()
{
int buttonWidth = 100;
int buttonHeight = 50;
int numberOfStrings = 6;
int numberOfFrets = 24;
// Clear existing buttons if needed
fretboardPanel.Controls.Clear();
for (int stringIndex = 0; stringIndex < numberOfStrings; stringIndex++)
{
for (int fretIndex = 0; fretIndex < numberOfFrets; fretIndex++)
{
int midiNoteNumber = GetMidiNoteNumber(stringIndex, fretIndex);
Button fretButton = new Button
{
Width = buttonWidth,
Height = buttonHeight,
Location = new Point(fretIndex * buttonWidth, stringIndex * buttonHeight),
Name = $"string{stringIndex}fret{fretIndex}Button",
Text = GetNoteName(midiNoteNumber),
Tag = new Tuple<int, int>(midiNoteNumber, stringIndex)
};
// Subscribe to the Click event of the button
fretButton.Click += FretboardButton_Click;
// Add the button to the fretboardPanel
fretboardPanel.Controls.Add(fretButton);
}
}
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
Label[] frequencyLabels = {
EQBand1FrequencyLabel,
EQBand2FrequencyLabel,
EQBand3FrequencyLabel,
EQBand4FrequencyLabel,
EQBand5FrequencyLabel,
EQBand6FrequencyLabel,
EQBand7FrequencyLabel,
EQBand8FrequencyLabel
};
Label[] widthLabels = {
EQBand1WidthLabel,
EQBand2WidthLabel,
EQBand3WidthLabel,
EQBand4WidthLabel ,
EQBand5WidthLabel,
EQBand6WidthLabel,
EQBand7WidthLabel,
EQBand8WidthLabel};
Label[] gainLabels = {
EQBand1GainLabel,
EQBand2GainLabel,
EQBand3GainLabel,
EQBand4GainLabel,
EQBand5GainLabel,
EQBand6GainLabel,
EQBand7GainLabel,
EQBand8GainLabel,};
TrackBar[] frequencies = {
EQBand1FrequencySlider,
EQBand2FrequencySlider,
EQBand3FrequencySlider,
EQBand4FrequencySlider,
EQBand5FrequencySlider,
EQBand6FrequencySlider,
EQBand7FrequencySlider,
EQBand8FrequencySlider};
TrackBar[] gains =
{
EQBand1GainSlider,
EQBand2GainSlider,
EQBand3GainSlider,
EQBand4GainSlider,
EQBand5GainSlider,
EQBand6GainSlider,
EQBand7GainSlider,
EQBand8GainSlider};
TrackBar[] widths =
{
EQBand1WidthSlider,
EQBand2WidthSlider,
EQBand3WidthSlider,
EQBand4WidthSlider,
EQBand5WidthSlider,
EQBand6WidthSlider,
EQBand7WidthSlider,
EQBand8WidthSlider};
if (radioButton != null && radioButton.Checked)
{
selectedStringIndex = int.Parse(radioButton.Text.Split(' ')[1]) - 1;
// Update checkbox based on lowPassActive and eqActive state
lowPassActiveCheckBox.Checked = theGuitar.strings[selectedStringIndex].lowPassActive;
equalizerActiveCheckBox.Checked = theGuitar.strings[selectedStringIndex].eqActive;
//update the string 8 bands eq labels
for (int i = 0; i < 8; i++)
{
frequencies[i].Value = theGuitar.strings[selectedStringIndex].bands[i].Frequency;
frequencyLabels[i].Text = theGuitar.strings[selectedStringIndex].bands[i].Frequency.ToString();
//******************************************************************
// transform value of trackbar between .1 and 3 (i.e. divide by 10)
float scaledValue = Math.Clamp(theGuitar.strings[selectedStringIndex].bands[i].Bandwidth, 0.1f, 3.0f) * 10;
widths[i].Value = (int)scaledValue;
widthLabels[i].Text = (scaledValue / 10f).ToString();
gains[i].Value = (int)theGuitar.strings[selectedStringIndex].bands[i].Gain;
gainLabels[i].Text = theGuitar.strings[selectedStringIndex].bands[i].Gain.ToString();
}
if (theGuitar.strings[selectedStringIndex].lowPassCutOffValue > 50 && theGuitar.strings[selectedStringIndex].lowPassCutOffValue < 10000)
lowPassFrequencyCutoffnumericUpDown.Value = (int)theGuitar.strings[selectedStringIndex].lowPassCutOffValue;
if (theGuitar.strings[selectedStringIndex].lowPassQValue > 1f && theGuitar.strings[selectedStringIndex].lowPassQValue < 10)
{
lowPassQTrackBar.Value = (int)theGuitar.strings[selectedStringIndex].lowPassQValue * 10;
lowPassResonanceLabel.Text = "low pass resonance (Q): " + theGuitar.strings[selectedStringIndex].lowPassQValue;
}
else if (theGuitar.strings[selectedStringIndex].lowPassQValue >= .1f && theGuitar.strings[selectedStringIndex].lowPassQValue <= 1)
{
float newvalue = theGuitar.strings[selectedStringIndex].lowPassQValue * 10;
lowPassQTrackBar.Value = (int)newvalue;
lowPassResonanceLabel.Text = "low pass resonance (Q): " + theGuitar.strings[selectedStringIndex].lowPassQValue;
}
if (theGuitar.strings[selectedStringIndex].attackPhaseSamples >= 1 && theGuitar.strings[selectedStringIndex].attackPhaseSamples <= 48000)
{
attackPhaseNmericUpDown.Value = (int)theGuitar.strings[selectedStringIndex].attackPhaseSamples;
}
float stepValue = 0.000191919f; // calculated step value
float minValue = 1f;
float maxValue = 100f;
float damping = theGuitar.strings[selectedStringIndex].damping;
float difference = .999f - damping;
int trackbarValue = (int)(maxValue - (difference / stepValue));
// Ensure trackbarValue is within the range of minValue and maxValue
trackbarValue = Math.Clamp(trackbarValue, (int)minValue, (int)maxValue);
dampingTrackBar.Value = trackbarValue;
// Update the damping label to display the current damping value
dampingLabel.Text = "Damping: " + theGuitar.strings[selectedStringIndex].damping.ToString();
}
}
private void dampingTrackBar_Scroll(object sender, EventArgs e)
{
float stepValue = 0.000191919f; // calculated step value
// Calculate the damping value based on the trackbar value
float dampingValue = 0.980f + (dampingTrackBar.Value * stepValue);
// Set the damping value for the string from the KarplusStrong object
theGuitar.strings[selectedStringIndex].damping = dampingValue;
// Update the damping label to display the current damping value
dampingLabel.Text = "Damping: " + theGuitar.strings[selectedStringIndex].damping.ToString();
}
private void attackPhaseNmericUpDown_ValueChanged_2(object sender, EventArgs e)
{
theGuitar.strings[selectedStringIndex].attackPhaseSamples = (int)attackPhaseNmericUpDown.Value;
// Debug.WriteLine("attack phase samples: " + theGuitar.strings[selectedStringIndex].attackPhaseSamples + " on string: " + selectedStringIndex);
}
private void lowPassActiveCheckBox_CheckedChanged(object sender, EventArgs e)
{
// Update lowPassActive based on the checkbox checked state
theGuitar.strings[selectedStringIndex].lowPassActive = lowPassActiveCheckBox.Checked;
}
private void lowPassFrequencyCutoffnumericUpDown_ValueChanged_1(object sender, EventArgs e)
{
theGuitar.strings[selectedStringIndex].lowPassCutOffValue = (float)lowPassFrequencyCutoffnumericUpDown.Value;
theGuitar.strings[selectedStringIndex].SetlowPassCutOffValue((float)lowPassFrequencyCutoffnumericUpDown.Value);
}
private void lowPassQTrackBar_ValueChanged_1(object sender, EventArgs e)
{
theGuitar.strings[selectedStringIndex].lowPassQValue = (float)lowPassQTrackBar.Value / 10;//divide the value by 10
lowPassResonanceLabel.Text = "low pass resonance (Q): " + theGuitar.strings[selectedStringIndex].lowPassQValue;
theGuitar.strings[selectedStringIndex].SetlowPassQValue(theGuitar.strings[selectedStringIndex].lowPassQValue);
}
private void InitializeComposition()
{
composition = new Composition
{
Title = "May and June",
Tempo = this.tempo,
TimeSignatureNumerator = this.timeSignatureNumerator,
TimeSignatureDenominator = this.timeSignatureDenominator,
};
// ... You can also initialize Notes here if needed ...
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.S)
{
currentTime += activeNoteDuration;
guitarRollPanel.Invalidate();
}
else if (e.KeyCode == Keys.Space && !isSpacebarPressed)
{
isSpacebarPressed = true;
debounceTimer.Start();
e.SuppressKeyPress = true; // Suppress the key event
// Your code to add a note goes here
}
else if (e.KeyCode == Keys.Delete && noteToDelete != null)
{
var currentComposition = fingeringPatternMode ? fingerPatternComposition : composition;
currentComposition.Notes.Remove(noteToDelete);
noteToDelete = null;
guitarRollPanel.Invalidate();
}
}
private void nextNoteButton_Click(object sender, EventArgs e)
{
currentTime += activeNoteDuration;
guitarRollPanel.Invalidate();
}
private void deleteSelectedNoteButton_Click(object sender, EventArgs e)
{
var currentComposition = fingeringPatternMode ? fingerPatternComposition : composition;
currentComposition.Notes.Remove(noteToDelete);
noteToDelete = null;
guitarRollPanel.Invalidate();
}
private void PlayFromFretboard(int midiNoteNumber)
{
double frequency = MidiUtilities.GetFrequencyFromMidiNote(midiNoteNumber);
// Update the frequency of the existing KarplusStrong object
karplusStrong.UpdateFrequency((float)frequency);
// Pluck the virtual string to generate sound
karplusStrong.Pluck(0.5f); // You can adjust the amplitude value as needed
}
private void buttonPlayMidi_Click(object sender, EventArgs e)
{
currentTime = 0;
currentEventIndex = 0;
midiPlaybackTimer.Start();
Debug.WriteLine("Timer started!"); // Add this line
midiHandler.ReadMidiFile();
}
private void MidiPlaybackTimer_Tick(object sender, EventArgs e)
{
if (midiHandler.SortedMidiEvents.Count == 0)
{
midiPlaybackTimer.Stop(); // Stop the timer if there are no more events.
return;
}
while (midiHandler.SortedMidiEvents.Count > 0 && midiHandler.SortedMidiEvents[0].Timestamp <= currentPlaybackPosition)
{
var midiEventWithTimestamp = midiHandler.SortedMidiEvents[0];
// Handle the MIDI event (e.g., play the note)
if (midiEventWithTimestamp.MidiEvent.CommandCode == MidiCommandCode.NoteOn)
{
var noteOnEvent = (NoteOnEvent)midiEventWithTimestamp.MidiEvent;
if (noteOnEvent.Velocity > 0)
{
double frequency = MidiUtilities.GetFrequencyFromMidiNote(noteOnEvent.NoteNumber);
// Update the frequency of the existing KarplusStrong object
karplusStrong.UpdateFrequency((float)frequency);
// Pluck the virtual string to generate sound
karplusStrong.Pluck(0.5f); // You can adjust the amplitude value as needed
}
}
// Remove the processed event from the list
midiHandler.SortedMidiEvents.RemoveAt(0);
}
currentPlaybackPosition++; // Increment the playback position
}
private Form1Data GetForm1Data()
{
return new Form1Data
{
AllNotes = this.allNotes,
Composition = this.composition,
FretboardMidiNoteNumber = this.fretboardMidiNoteNumber,
PixelsPerSecond = PixelsPerSecond,
Version = this.version,
MidiChannelPerString = this.midiChannelPerString,
timeSignatureNumerator = this.timeSignatureNumerator,
timeSignatureDenominator = this.timeSignatureNumerator,
tempo = this.tempo
};
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.DefaultExt = "gur";
openFileDialog1.Filter = "Guitarsharp Files (*.gur)|*.gur";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string jsonString = File.ReadAllText(openFileDialog1.FileName);
// Form1Data data = JsonSerializer.Deserialize<Form1Data>(jsonString);
AllDataContainer container = JsonSerializer.Deserialize<AllDataContainer>(jsonString);
this.allNotes = container.Form1Data.AllNotes;
this.composition = container.Form1Data.Composition;
this.fretboardMidiNoteNumber = container.Form1Data.FretboardMidiNoteNumber;
this.PixelsPerSecond = container.Form1Data.PixelsPerSecond;
this.midiChannelPerString = container.Form1Data.MidiChannelPerString;
this.timeSignatureNumerator = container.Form1Data.timeSignatureNumerator;
this.timeSignatureDenominator = container.Form1Data.timeSignatureNumerator;
this.tempo = container.Form1Data.tempo;
//check the version and handle older versions accordingly(e.g., provide default values for new properties or convert data from old formats).
if (this.version != container.Form1Data.Version) MessageBox.Show("wrong version");// we can do better :) but for now that's it...
// Load fret patterns
LoadFretPatternsFromData(container.FretPatternData);
// Load fingering patterns
allFingeringPatterns = container.FingeringPatterns;
guitarRollPanel.Invalidate(); // Refresh the panel to reflect the loaded data
}
}
private void LoadFretPatternsFromData(List<FretPatternData> fretPatternData)
{
// Clear the existing UI elements
fretPatternPanel.Controls.Clear();
allFretPatterns.Clear();
// Constants for layout
int patternsPerRow = 7;
int patternSpacing = 100;
int patternWidth = 400; // Adjust as needed
int patternHeight = 1500; // Adjust as needed
// Rebuild the UI elements with the loaded data
for (int i = 0; i < fretPatternData.Count; i++)
{
var patternData = fretPatternData[i];
int row = i / patternsPerRow;
int col = i % patternsPerRow;
// Calculate the location for each pattern
Point location = new Point(col * (patternWidth + patternSpacing) + 20, row * (patternHeight + patternSpacing) + 200);
FretPattern pattern = new FretPattern(patternData.Name, patternData.BaseFret, this);
pattern.Description = patternData.Description;
pattern.IsActive = patternData.IsActive;
// Set up the pattern's UI elements
pattern.CreateFretboard(6, 13, pattern.BaseFret, location, i);
// Update the buttons with the loaded names and back colors
for (int j = 0; j < pattern.Buttons.Count; j++)
{
pattern.Buttons[j].Text = patternData.FretButtonName[j];
pattern.Buttons[j].BackColor = Color.FromArgb(patternData.FretButtonBackColor[j]);
pattern.Buttons[j].Tag = new Tuple<int, int>(patternData.FretTags[j].StringIndex, patternData.FretTags[j].FretIndex);
}
// Add the pattern to the list
allFretPatterns.Add(pattern);
// Add the pattern's UI elements to the panel
foreach (Button button in pattern.Buttons)
{
fretPatternPanel.Controls.Add(button);
}
}
// activeFretPattern = allFretPatterns[0];
// Optionally, refresh the panel or form if necessary
fretPatternPanel.Refresh();
//MessageBox.Show("Fret patterns loaded successfully.", "Load Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.DefaultExt = "gur";
saveFileDialog1.Filter = "Guitarsharp Files (*.gur)|*.gur";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
var container = new AllDataContainer
{
Form1Data = GetForm1Data(),
FretPatternData = GetAllFretPatternData(),
FingeringPatterns = allFingeringPatterns // Assuming this is your list of fingering patterns
};
string jsonString = JsonSerializer.Serialize(container, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(saveFileDialog1.FileName, jsonString);
}
}
private List<FretPatternData> GetAllFretPatternData()
{
List<FretPatternData> allPatternData = new List<FretPatternData>();
foreach (var pattern in allFretPatterns)
{
FretPatternData patternData = pattern.GetSerializableData();
allPatternData.Add(patternData);
}
return allPatternData;
}
public enum NoteType
{
Ronde, // Whole Note
Blanche, // Half Note
Noire, // Quarter Note
Croche, // Eighth Note
DoubleCroche, // Sixteenth Note
TripleCroche // 32nd Note
}
private float GetNoteMultiplier(NoteType noteType)
{
float multiplier = 1.0f;
switch (noteType)
{
case NoteType.Ronde: // Whole Note
if (timeSignatureDenominator == 4) multiplier = 4.0f;
if (timeSignatureDenominator == 8) multiplier = 8.0f;
if (timeSignatureDenominator == 2) multiplier = 2.0f;
break;
case NoteType.Blanche: // Half Note
if (timeSignatureDenominator == 4) multiplier = 2.0f;
if (timeSignatureDenominator == 8) multiplier = 4.0f;
if (timeSignatureDenominator == 2) multiplier = 1.0f;
break;
case NoteType.Noire: // Quarter Note
if (timeSignatureDenominator == 4) multiplier = 1.0f;
if (timeSignatureDenominator == 8) multiplier = 2.0f;
if (timeSignatureDenominator == 2) multiplier = 0.5f;
break;
case NoteType.Croche: // Eighth Note
if (timeSignatureDenominator == 4) multiplier = 0.5f;
if (timeSignatureDenominator == 8) multiplier = 1.0f;
if (timeSignatureDenominator == 2) multiplier = 0.25f;
break;
case NoteType.DoubleCroche: // Sixteenth Note
if (timeSignatureDenominator == 4) multiplier = 0.25f;
if (timeSignatureDenominator == 8) multiplier = 0.5f;
if (timeSignatureDenominator == 2) multiplier = 0.125f;
break;
case NoteType.TripleCroche: // 32nd Note
if (timeSignatureDenominator == 4) multiplier = 0.125f;
if (timeSignatureDenominator == 8) multiplier = 0.25f;
if (timeSignatureDenominator == 2) multiplier = 0.0625f;
break;
default:
throw new Exception("Unsupported note type.");
}
if (isDotted)
{
multiplier *= 1.5f;
}
if (isTriplet)
{
multiplier *= (2.0f / 3.0f);
}
return multiplier;
}
private void tripletButton_Click(object sender, EventArgs e)
{
if (isTriplet == false)
{
tripletButton.BackColor = Color.Coral; // Example active color
if (rondeButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Ronde) * (2.0f / 3.0f);
else if (blancheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Blanche) * (2.0f / 3.0f);
else if (noireButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Noire) * (2.0f / 3.0f);
else if (crocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Croche) * (2.0f / 3.0f);
else if (doubleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.DoubleCroche) * (2.0f / 3.0f);
else if (tripleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.TripleCroche) * (2.0f / 3.0f);
isTriplet = true;
}
else if (isTriplet == true)
{
tripletButton.BackColor = SystemColors.Control; // Default color
isTriplet = false;
if (rondeButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Ronde);
else if (blancheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Blanche);
else if (noireButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Noire);
else if (crocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Croche);
else if (doubleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.DoubleCroche);
else if (tripleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.TripleCroche);
}
}
private void pointeeButton_Click(object sender, EventArgs e)
{
if (isDotted == false)
{
pointeeButton.BackColor = Color.Coral; // Example active color
if (rondeButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Ronde) * 1.5f;
else if (blancheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Blanche) * 1.5f;
else if (noireButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Noire) * 1.5f;
else if (crocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Croche) * 1.5f;
else if (doubleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.DoubleCroche) * 1.5f;
else if (tripleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.TripleCroche) * 1.5f;
isDotted = true;
}
else if (isDotted == true)
{
pointeeButton.BackColor = SystemColors.Control; // Default color
isDotted = false;
if (rondeButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Ronde);
else if (blancheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Blanche);
else if (noireButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Noire);
else if (crocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.Croche);
else if (doubleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.DoubleCroche);
else if (tripleCrocheButton.BackColor == Color.Coral) activeNoteDuration = GetNoteMultiplier(NoteType.TripleCroche);
}
}
private void rondeButton_Click(object sender, EventArgs e)
{
activeNoteDuration = baseNoteDuration * GetNoteMultiplier(NoteType.Ronde);
blancheButton.BackColor = Color.LightGreen;
rondeButton.BackColor = Color.Coral;
noireButton.BackColor = Color.LightGreen;
crocheButton.BackColor = Color.LightGreen;
doubleCrocheButton.BackColor = Color.LightGreen;
tripleCrocheButton.BackColor = Color.LightGreen;
}
private void blancheButton_Click(object sender, EventArgs e)
{
activeNoteDuration = baseNoteDuration * GetNoteMultiplier(NoteType.Blanche);
blancheButton.BackColor = Color.Coral;
rondeButton.BackColor = Color.LightGreen;
noireButton.BackColor = Color.LightGreen;
crocheButton.BackColor = Color.LightGreen;
doubleCrocheButton.BackColor = Color.LightGreen;
tripleCrocheButton.BackColor = Color.LightGreen;
}
private void noireButton_Click(object sender, EventArgs e)
{
activeNoteDuration = baseNoteDuration * GetNoteMultiplier(NoteType.Noire);
blancheButton.BackColor = Color.LightGreen;
rondeButton.BackColor = Color.LightGreen;
noireButton.BackColor = Color.Coral;
crocheButton.BackColor = Color.LightGreen;
doubleCrocheButton.BackColor = Color.LightGreen;
tripleCrocheButton.BackColor = Color.LightGreen;
}
private void crocheButton_Click(object sender, EventArgs e)
{
activeNoteDuration = baseNoteDuration * GetNoteMultiplier(NoteType.Croche);
blancheButton.BackColor = Color.LightGreen;
rondeButton.BackColor = Color.LightGreen;
noireButton.BackColor = Color.LightGreen;
crocheButton.BackColor = Color.Coral;
doubleCrocheButton.BackColor = Color.LightGreen;
tripleCrocheButton.BackColor = Color.LightGreen;
}
private void doubleCrocheButton_Click(object sender, EventArgs e)
{
activeNoteDuration = baseNoteDuration * GetNoteMultiplier(NoteType.DoubleCroche);
blancheButton.BackColor = Color.LightGreen;
rondeButton.BackColor = Color.LightGreen;
noireButton.BackColor = Color.LightGreen;
crocheButton.BackColor = Color.LightGreen;
doubleCrocheButton.BackColor = Color.Coral;
tripleCrocheButton.BackColor = Color.LightGreen;
}
private void tripleCrocheButton_Click(object sender, EventArgs e)
{
activeNoteDuration = baseNoteDuration * GetNoteMultiplier(NoteType.TripleCroche);
blancheButton.BackColor = Color.LightGreen;
rondeButton.BackColor = Color.LightGreen;
noireButton.BackColor = Color.LightGreen;
crocheButton.BackColor = Color.LightGreen;
doubleCrocheButton.BackColor = Color.LightGreen;
tripleCrocheButton.BackColor = Color.Coral;
}
private void triplePerBeatButton_Click(object sender, EventArgs e)
{
triplePerBeatMode = !triplePerBeatMode;
triplePerBeatButton.BackColor = triplePerBeatMode ? Color.Coral : Color.LightGreen;
if (!triplePerBeatMode)
{
activeNoteDuration *= 3;
}
else
{
if (activeNoteDuration > 0)
{
activeNoteDuration /= 3;
}
}
}
private void quintuplePerBeatButton_Click(object sender, EventArgs e)
{
quintuplePerBeatMode = !quintuplePerBeatMode;
quintuplePerBeatButton.BackColor = quintuplePerBeatMode ? Color.Coral : Color.LightGreen;
if (!quintuplePerBeatMode)
{
activeNoteDuration *= 5;
}
else
{
if (activeNoteDuration > 0)
{