-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChordToMidiConvertor.cs
146 lines (126 loc) · 4.74 KB
/
ChordToMidiConvertor.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
using Sanford.Multimedia.Midi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace MidiChord
{
public class ChordToMidiConvertor : ChordPlayer
{
private Sanford.Multimedia.Midi.Sequence _sequence;
public ChordToMidiConvertor(ChordList chordNotes, DrumList drumNotes)
: base(chordNotes, drumNotes)
{
SendNoteOffForDrums = true;
}
private int get_division()
{
// Minimum PPQN value is 24, aka resolution of midifile
return 24;
}
private void create_track_from_song()
{
int division = get_division();
//int beat_delay = GetBeatTimeInMs(BeatsPerMinute);
_sequence = new Sanford.Multimedia.Midi.Sequence(division);
Track track_chords = new Track();
Track track_metronome = new Track();
Track track_drum = new Track();
bool track_finised = false;
while (!track_finised)
{
int beat_time = CurrentBeatIndex * division;
track_chords .Insert(0, _chordMetaText );
track_metronome .Insert(0, _metronomeMetaText);
track_drum .Insert(0, _drumMetaText );
track_chords.Insert(1, GetDefaultInstrument());
//track_drum .Insert(1, _drumInstument);
var midi = GetMidiCommandsAtBeat(CurrentBeatIndex);
saveMidiCommands(track_chords, beat_time, midi.MetaData );
saveMidiCommands(track_chords, beat_time, midi.Chords );
saveMidiCommands(track_metronome, beat_time, midi.Metronome );
saveMidiDrumCommands(track_drum, beat_time, midi.Drums );
NextBeatIndex();
if (midi.EndOfSong)
{
track_finised = true;
}
}
_sequence.Add(track_chords);
_sequence.Add(track_drum);
_sequence.Add(track_metronome);
}
private void saveMidiDrumCommands(Track track, int beattime, List<IMidiMessage> midi)
{
foreach (var cmd in midi)
{
int note_off_delay = 0;
var message = cmd as ChannelMessage;
if ( message != null )
{
if ( message.Command == ChannelCommand.NoteOff )
{
note_off_delay = 3;
}
}
track.Insert(beattime+ note_off_delay, cmd);
}
}
private void saveMidiCommands(Track track, int beattime, List<IMidiMessage> midi)
{
foreach (var cmd in midi)
{
track.Insert(beattime, cmd);
}
}
private IMidiMessage GetDefaultInstrument()
{
var factory = new MidiCommandFactory();
return factory.Instrument(SongInstrument);
}
//private void createDrumTrack(Track drum_track, int beattime)
//{
// for (int drum = 0; drum < 3; drum++)
// {
// if (_drumPatterns[drum] != null)
// {
// int beat_in_measure = (CurrentBeatIndex % 4);
// if (beat_in_measure < _drumPatterns[drum].NotesOn.Count)
// {
// var midiEvent = _drumPatterns[drum].NotesOn[beat_in_measure];
// if (midiEvent != null)
// {
// drum_track.Insert(beattime, midiEvent);
// Debug.WriteLine("Insert drum event: " + midiEvent.Command.ToString());
// }
// }
// if (beat_in_measure < _drumPatterns[drum].NotesOff.Count)
// {
// var midiEvent = _drumPatterns[drum].NotesOff[beat_in_measure];
// if (midiEvent != null)
// {
// drum_track.Insert(beattime+3, midiEvent);
// Debug.WriteLine("Insert drum event: " + midiEvent.Command.ToString());
// }
// }
// }
// }
//}
public void Save(string filename)
{
create_track_from_song();
try
{
_sequence.Save(filename);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}