-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProgram.cs
340 lines (281 loc) · 9.22 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Monad.FLParser;
using MIDIModificationFramework;
using MIDIModificationFramework.Generator;
using MIDIModificationFramework.MIDIEvents;
using Note = MIDIModificationFramework.Note;
using System.Collections.Concurrent;
namespace flp2midi
{
class Program
{
static bool ForceColor { get; set; }
static bool DisableEcho { get; set; }
//TODO: Actually support feed correctly
static IEnumerable<Note> EchoNotes(IEnumerable<Note> notes, byte echoamount, uint feed, uint time, int ppq)
{
if(feed == 0) return notes;
List<IEnumerable<Note>> echos = new List<IEnumerable<Note>>();
echos.Add(notes);
for(var i = 1; i <= echoamount; i++)
{
var shifted = notes.OffsetTime((time * i * ppq) / 96.0 / 2);
echos.Add(shifted);
}
return echos.MergeAll();
}
static void Main(string[] args)
{
if(args.Length < 1 || !File.Exists(args[0]))
{
PrintHelp();
}
GetArgs(args);
var filePath = args[0];
var tempFile = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileName(filePath) + ".mid.tmp");
File.Delete(tempFile); //Delete old temp file if it exist
var streams = new ParallelStream(File.Open(tempFile, FileMode.Create));
Console.WriteLine("flp2midi | Version: 1.3.0");
Console.WriteLine("Loading FL Studio project file...");
Project proj = Project.Load(filePath, false);
string title = proj.ProjectTitle;
string version = proj.VersionString;
Console.WriteLine("Title: " + title + " | Version: " + version);
object l = new object();
var patternDict = new Dictionary<int, Dictionary<Channel, Note[]>>();
Parallel.ForEach(proj.Patterns, pat =>
{
int id = pat.Id;
string name = pat.Name;
var notes = pat.Notes.ToDictionary(c => c.Key, c =>
{
byte channel = 0;
var colorchan = false;
if(c.Key.Data is GeneratorData data && data.GeneratorName.ToLower() == "midi out")
{
if(data.PluginSettings[29] == 0x01) colorchan = true;
channel = data.PluginSettings[4];
}
var noteList = new List<Note>(c.Value.Count);
var lastNoteZeroTick = -1.0;
foreach(var n in c.Value.OrderBy(n => n.Position))
{
var newNote = new Note((colorchan || ForceColor) ? n.Color : channel, Math.Min((byte)127, n.Key), Math.Min((byte)127, n.Velocity), (double)n.Position, (double)n.Position + (double)n.Length);
noteList.Add(newNote);
if(lastNoteZeroTick != -1.0 && lastNoteZeroTick != newNote.Start)
{
lastNoteZeroTick = -1.0;
noteList[^2].End = newNote.Start;
}
if(newNote.Length == 0)
{
lastNoteZeroTick = newNote.Start;
newNote.End = double.PositiveInfinity;
}
}
return noteList.ToArray();
/*
return c.Value
.OrderBy(n => n.Position)
.Select(n => new Note((colorchan || ForceColor) ? n.Color : channel,
Math.Min((byte)127, n.Key),
Math.Min((byte)127, n.Velocity),
(double)n.Position,
(double)n.Position + (double)n.Length))
.ToArray();
*/
});
lock(l)
{
patternDict.Add(id, notes);
}
Console.WriteLine($"Pattern found: ({id}) {name}");
});
var trackID = 0;
//MemoryStream[] streams = new MemoryStream[proj.Tracks.Length];
//var tracks = proj.Tracks.Where(t => t.Items.Count != 0).OrderBy(t =>
// t.Items.Select(i =>
// {
// var pi = i as PatternPlaylistItem;
// if(pi == null) return 0;
// return -patternDict[pi.Pattern.Id].Select(p => p.Value.Length).Sum();
// }).Sum()
//).ToArray();
var tracks = proj.Tracks.Where(t => t.Items.Count != 0).ToArray();
//var automations = proj.Channels.Where(c => c.Data is AutomationData).ToArray();
/*
Parameters:
1 - Unknown
2 - Unknown
3 - Unknown
4 - Unknown
5 - Tempo Events
*/
/*
* Modes:
* 10 - Smooth
* 2 - Hold
* 0 - Single Curve
* 7 - Single Curve 2
* 11 - Single Curve 3
* 1 - Double Curve
* 8 - Double Curve 2
* 12 - Double Curve 3
* 9 - Half Sine
* 3 - Stairs
* 4 - Smooth Stairs
* 5 - Pulse
* 6 - Wave
*/
//foreach (var auto in automations)
//{
//}
ParallelFor(0, tracks.Length, Environment.ProcessorCount, new CancellationToken(false), i =>
{
//streams[i] = new MemoryStream();
var stream = new BufferedStream(streams.GetStream(i), 1 << 24);
var trackWriter = new MidiWriter(stream);
var track = tracks[i];
var notes = track.Items.Select(item =>
{
if(item is PatternPlaylistItem && item.Muted == false)
{
var pi = item as PatternPlaylistItem;
var pattern = patternDict[pi.Pattern.Id];
var merged = pattern.Select(c =>
{
var shifted = c.Value
.TrimStart(Math.Max(0, item.StartOffset))
.TrimEnd(Math.Max(0, item.EndOffset == -1 ? item.Length : item.EndOffset))
//.Where(n => n.Length > 0)
.OffsetTime(item.Position - item.StartOffset);
var channel = c.Key;
if(channel.Data is GeneratorData data)
{
if(data.EchoFeed > 0 && !DisableEcho)
{
shifted = EchoNotes(shifted, data.Echo, data.EchoFeed, data.EchoTime, proj.Ppq);
}
}
return shifted;
}).MergeAll();
return merged;
}
else
{
return new Note[0];
}
}).MergeAll().TrimStart();
trackWriter.Write(notes.ExtractEvents());
stream.Close();
lock(l)
{
Console.WriteLine($"Generated track {i + 1}, {(trackID++) + 1}/{tracks.Length}");
}
});
streams.CloseAllStreams();
var writer = new MidiWriter(Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileName(filePath) + ".mid"));
writer.Init((ushort)proj.Ppq);
writer.InitTrack();
writer.Write(new TempoEvent(0, (int)(60000000.0 / proj.Tempo)));
writer.EndTrack();
for(int i = 0; i < tracks.Length; i++)
{
Console.WriteLine($"Writing track {i + 1}/{tracks.Length}");
var stream = streams.GetStream(i, true);
stream.Position = 0;
unchecked
{
writer.WriteTrack(stream);
}
stream.Close();
}
writer.Close();
streams.CloseAllStreams();
streams.Dispose();
File.Delete(tempFile);
Console.WriteLine("Press any key to exit... ");
Console.ReadKey();
}
//TODO: Abstract console vars
static void GetArgs(string[] args)
{
for(var i = 0; i < args.Length; i++)
{
switch(args[i])
{
case "-fc":
case "--forcecolor":
{
ForceColor = true;
break;
}
case "-de":
case "--disable-echo":
{
DisableEcho = true;
break;
}
case "-h":
case "--help":
{
PrintHelp();
break;
}
default:
{
break;
}
}
}
}
static void PrintHelp()
{
Console.WriteLine("Usage: flp2midi.exe <path to flp file>");
Console.WriteLine("List of current console vars:" +
"\n-fc | --force-color > Makes all notes map to the color they are mapped to in the pattern than what the MIDI Out channel is set to" +
"\n-de | --disable-echo > Disables the generation of echo notes if echo is enabled on the channel");
Console.WriteLine("Press any key to exit... ");
Console.ReadKey();
System.Environment.Exit(1);
}
static void ParallelFor(int from, int to, int threads, CancellationToken cancel, Action<int> func)
{
Dictionary<int, Task> tasks = new Dictionary<int, Task>();
BlockingCollection<int> completed = new BlockingCollection<int>();
void RunTask(int i)
{
var t = new Task(() =>
{
try
{
func(i);
}
finally
{
completed.Add(i);
}
});
tasks.Add(i, t);
t.Start();
}
void TryTake()
{
var t = completed.Take(cancel);
tasks[t].Wait();
tasks.Remove(t);
}
for(int i = from; i < to; i++)
{
RunTask(i);
if(tasks.Count > threads) TryTake();
}
while(completed.Count > 0 || tasks.Count > 0) TryTake();
}
}
}