-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patterns.cs
202 lines (183 loc) · 7.1 KB
/
Patterns.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
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.IO;
namespace PlayNotes
{
public partial class Patterns : Form
{
private MyPiano myParent;
private struct onePhrase
{ public string genCodeID; public List<MyPiano.NoteStruct> noteList; }
private List<onePhrase> phraseList = new List<onePhrase>();
private string MRUfile;
public Patterns(MyPiano parent)
{
InitializeComponent();
myParent = parent;
if (myParent.mySettings.Rows.Count > 1) { rbA1B1A2B2.Enabled = true; }
MRUfile = myParent.setDir + "\\PatternsMRU.txt";
LoadMRUlist();
}
private void LoadMRUlist()
{
ComboBox.ObjectCollection MRUitems = cbMRU.Items;
try
{
var lines = File.ReadLines(MRUfile);
foreach (var oneLine in lines)
{ MRUitems.Add(oneLine.ToString()); }
}
catch { cbMRU.Enabled = false; return; }
}
private void btnPlayPattern_Click(object sender, EventArgs e)
{
tbCustomPattern.Text = tbCustomPattern.Text.ToUpper();
string playPattern = tbCustomPattern.Text;
if (rbYourOwn.Checked)
{
try { cbMRU.Items.Remove(playPattern); } catch { }
cbMRU.Items.Insert(0, playPattern);
PlayPattern(tbCustomPattern.Text);
return;
}
if (rbA1A1.Checked) { playPattern = makePatternA1A1(); }
if (rbA1A1A2.Checked) { playPattern = makePatternA1A1A2(); }
if (rbA1B1A2B2.Checked) { playPattern = makePatternA1B1A2B2(); }
PlayPattern(playPattern);
}
private string makePatternA1B1A2B2()
{
string playPattern = "";
for (int i = 0; i < myParent.numOfPhrases; i++)
{
playPattern += "A" + (i + 1).ToString();
playPattern += "B" + (i + 1).ToString();
}
return playPattern;
}
private string makePatternA1A1A2()
{
string playPattern = "";
for (int i = 0; i < myParent.numOfPhrases; i++)
{
int j = i * 2;
if (j + 2 > 9) { break; }
playPattern += "A" + (j + 1).ToString();
playPattern += "A" + (j + 1).ToString();
playPattern += "A" + (j + 2).ToString();
}
return playPattern;
}
private string makePatternA1A1()
{
string playPattern = "";
for (int i = 0; i < myParent.numOfPhrases; i++)
{
playPattern += "A" + (i + 1).ToString();
playPattern += "A" + (i + 1).ToString();
}
return playPattern;
}
private void PlayPattern(string patternText)
{
string genCode = "";
for (int i = 0; i < patternText.Length; i++)
{
string playChar = patternText.Substring(i, 1);
Console.WriteLine(playChar);
if (playChar == " ") { continue; }
if (Char.IsNumber(playChar,0))
{
genCode += playChar;
List<MyPiano.NoteStruct> phraseBack = FindExistingNotes(genCode);
if (phraseBack == null)
{ CreateAndPlay(genCode); continue; }
// play the existing phrase
PlayThePhrase(phraseBack, genCode);
continue;
}
genCode = playChar;
}
}
private void CreateAndPlay(string genCode)
{
Console.WriteLine("Will create music for new code: " + genCode);
myParent.tbKey.Text = genCode.Substring(0, 1);
Application.DoEvents();
myParent.onePhrase.Clear();
myParent.BuildNotePhrase(myParent.onePhrase, myParent.random);
PlayThePhrase(myParent.onePhrase, genCode);
onePhrase myPhrase = new onePhrase();
myPhrase.genCodeID = genCode;
myPhrase.noteList = new List<MyPiano.NoteStruct>();
foreach (MyPiano.NoteStruct noteSpec in myParent.onePhrase)
{ myPhrase.noteList.Add(noteSpec); }
phraseList.Add(myPhrase);
}
private void PlayThePhrase(List<MyPiano.NoteStruct> phraseBack, string genCode)
{
string Generator = genCode.ToUpper().Substring(0, 1);
myParent.tbKey.Text = Generator;
// locate the settings for this key and implement them
DataRow[] theSettings = myParent.mySettings.Select("Generator = '" + Generator + "'");
myParent.SettingsFromDatarow(theSettings[0]);
myParent.SetTheSliders();
Application.DoEvents();
using (var waveOut = new WaveOutEvent())
{
// Create a new WaveProvider32 with the SignalGenerator
var waveProvider = myParent.signalGenerator.ToWaveProvider();
waveOut.Init(waveProvider);
bool nearEnd = false;
for (int j = 0; j < phraseBack.Count; j++)
{
if (j == phraseBack.Count - 1) { nearEnd = true; }
myParent.unpackAndPlay(phraseBack[j], myParent.signalGenerator, waveOut, nearEnd);
}
}
System.Threading.Thread.SpinWait(2000);
System.Threading.Thread.Sleep(1000);
}
private List<MyPiano.NoteStruct> FindExistingNotes(string genCode)
{
Console.WriteLine("Searching For " + genCode);
foreach (onePhrase myPhrase in phraseList)
{
string chkGenCode = myPhrase.genCodeID;
if (chkGenCode == genCode)
{
Console.Write("Found: ");
foreach (MyPiano.NoteStruct noteSpec in myPhrase.noteList)
{ Console.Write(noteSpec.Notefrequency.ToString() + " "); }
Console.WriteLine();
return myPhrase.noteList;
}
}
return null;
}
private void tbCustomPattern_TextChanged(object sender, EventArgs e)
{
rbYourOwn.Checked = true;
}
private void Patterns_FormClosing(object sender, FormClosingEventArgs e)
{
StreamWriter mruWriter = File.CreateText(MRUfile);
foreach (var oneItem in cbMRU.Items)
{ mruWriter.WriteLine(oneItem.ToString()); }
mruWriter.Flush();
mruWriter.Close();
}
private void cbMRU_SelectedIndexChanged(object sender, EventArgs e)
{
int itemSel = cbMRU.SelectedIndex;
tbCustomPattern.Text = cbMRU.Items[itemSel].ToString();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
phraseList.Clear();
}
}
}