-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
157 lines (139 loc) · 5.54 KB
/
Program.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
using System;
using MusicCore;
using NAudio.Midi;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using static MusicCore.StaticAndExtensionMethods;
namespace MusicComposer
{
partial class Program
{
static void Play(TwelveToneSet chord, int startFrom)
{
for (int i=startFrom; i< TwelveToneSet.TWELVE + startFrom; i++)
{
if (chord[(i+10*TwelveToneSet.TWELVE) % TwelveToneSet.TWELVE])
{
midiOut.Send(MidiMessage.StartNote(48 + i, 100, 1).RawData);
Thread.Sleep(100);
}
}
}
static void Main2(string[] args)
{
Random rand = new Random();
TwelveToneSet chord = TwelveToneSet.major7;
TwelveToneSet scale = TwelveToneSet.majorScale;
int dist = 0;
Console.WriteLine($"Chord: {chord.ToString()}");
int startFrom = chord.First;
do
{
Play(chord, startFrom);
Thread.Sleep(2000);
TwelveToneSet chordNew = new TwelveToneSet(rand, 4, scale.ShiftRight(rand.Next(TwelveToneSet.TWELVE)));
dist = Distance(chord, chordNew);
for (int i=0; i<10; i++)
{
TwelveToneSet chord2 = new TwelveToneSet(rand, 4, scale.ShiftRight(rand.Next(TwelveToneSet.TWELVE)));
if (chord.Equals(chord2))
continue;
int dist2 = Distance(chord, chord2);
if (dist2 < dist)
{
dist = dist2;
chordNew = chord2;
}
}
Console.Write($"Distance: {dist}={chord.DistanceScales(chordNew)}+{chord.DistanceCommonTones(chordNew)}+{chordNew.InScale()} ");
Console.WriteLine($"Chord: { chordNew.ToString()}");
chord = chordNew;
} while (true);
}
static void Main3(string[] args)
{
RhythmPatternBase pattern = new RhytmPatternComposite("AAAA", RhythmMaker.CreateRhytmPattern());
int tempo = 60;
foreach (var fract in pattern.Notes())
{
midiOut.Send(MidiMessage.StartNote(48, 100, 1).RawData);
Console.Write(fract + " ");
Thread.Sleep(60 * 1000 * fract.p / fract.q / tempo);
}
}
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Playing all songs:");
foreach (MethodInfo mi in typeof(Compositions).GetMethods())
{
if (mi.GetParameters().Length == 0 && mi.ReturnType == typeof(Melody12Tone))
{
Console.Write(mi.Name + " ");
Main(new[] { mi.Name });
Console.WriteLine();
Console.WriteLine("Wait for 1 second...");
Thread.Sleep(1000);
}
}
Console.WriteLine();
return;
}
if (args.Length == 2 && args[0] == "midi")
{
var composition = MidiFileReader.Read(args[1]);
composition.PlayBack(
note =>
{
midiOut.Send(MidiMessage.StartNote(note, 100, 1).RawData);
midiOut.Send(MidiMessage.StartNote(note+4, 100, 1).RawData);
},
note => midiOut.Send(MidiMessage.StopNote(note, 100, 1).RawData),
500
);
return;
}
if (args[0] == "RandomSequencer")
{
RandomSequencer();
return;
}
MethodInfo miStatic = typeof(Compositions).GetMethod(args[0]);
Func<Melody12Tone> dgComposition = Delegate.CreateDelegate(typeof(Func<Melody12Tone>), miStatic) as Func<Melody12Tone>;
Melody12Tone m12tone = dgComposition();
List<int> lastnotes = new List<int>();
foreach (var nwd in m12tone.Notes())
{
foreach (int note in lastnotes)
midiOut.Send(MidiMessage.StopNote(note, 100, 1).RawData);
// Play the note, and if it's pause mute the last note
if (!nwd.IsPause)
{
Note note = nwd;
lastnotes = new List<int>();
do
{
midiOut.Send(MidiMessage.StartNote(note.note, 100, 1).RawData);
lastnotes.Add(note.note);
//note = note.otherNote;
} while (false /*note != null*/);
}
else
foreach (var note in lastnotes)
midiOut.Send(MidiMessage.StartNote(note, 0, 1).RawData);
Fraction fract = nwd.Duration;
Console.Write(fract + " ");
Thread.Sleep(60 * 1000 * fract.p / fract.q / m12tone.tempo);
}
// Mute the last note(s)
foreach (var note in lastnotes)
midiOut.Send(MidiMessage.StartNote(note, 0, 1).RawData);
}
static int Distance(TwelveToneSet chord1, TwelveToneSet chord2)
{
return chord1.TotalDistance(chord2) + chord2.InScale();
}
}
}