@@ -15,6 +15,9 @@ namespace flp2midi
15
15
{
16
16
class Program
17
17
{
18
+ static bool ForceColor { get ; set ; }
19
+ static bool DisableEcho { get ; set ; }
20
+
18
21
//TODO: Actually support feed correctly
19
22
static IEnumerable < Note > EchoNotes ( IEnumerable < Note > notes , byte echoamount , uint feed , uint time , int ppq )
20
23
{
@@ -44,8 +47,12 @@ static void Main(string[] args)
44
47
}
45
48
46
49
var filePath = args [ 0 ] ;
50
+ var tempFile = Path . Combine ( Path . GetDirectoryName ( filePath ) , Path . GetFileName ( filePath ) + ".mid.tmp" ) ;
51
+ var streams = new ParallelStream ( File . Open ( tempFile , FileMode . Create ) ) ;
52
+
53
+ GetArgs ( args ) ;
47
54
48
- Console . WriteLine ( "flp2midi | Version: 1.0 .0" ) ;
55
+ Console . WriteLine ( "flp2midi | Version: 1.1 .0" ) ;
49
56
Console . WriteLine ( "Loading FL Studio project file..." ) ;
50
57
51
58
Project proj = Project . Load ( filePath , false ) ;
@@ -77,7 +84,7 @@ static void Main(string[] args)
77
84
78
85
return c . Value
79
86
. OrderBy ( n => n . Position )
80
- . Select ( n => new Note ( colorchan ? n . Color : channel , n . Key , Math . Min ( ( byte ) 127 , n . Velocity ) , ( double ) n . Position , ( double ) n . Position + ( double ) n . Length ) )
87
+ . 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 ) )
81
88
. ToArray ( ) ;
82
89
} ) ;
83
90
@@ -91,7 +98,7 @@ static void Main(string[] args)
91
98
92
99
var trackID = 0 ;
93
100
94
- MemoryStream [ ] streams = new MemoryStream [ proj . Tracks . Length ] ;
101
+ // MemoryStream[] streams = new MemoryStream[proj.Tracks.Length];
95
102
96
103
var tracks = proj . Tracks . Where ( t => t . Items . Count != 0 ) . OrderBy ( t =>
97
104
t . Items . Select ( i =>
@@ -112,7 +119,7 @@ static void Main(string[] args)
112
119
4 - Unknown
113
120
5 - Tempo Events
114
121
*/
115
-
122
+
116
123
/*
117
124
* Modes:
118
125
* 10 - Smooth
@@ -136,9 +143,10 @@ static void Main(string[] args)
136
143
137
144
ParallelFor ( 0 , tracks . Length , Environment . ProcessorCount , new CancellationToken ( false ) , i =>
138
145
{
139
- streams [ i ] = new MemoryStream ( ) ;
146
+ // streams[i] = new MemoryStream();
140
147
141
- var trackWriter = new MidiWriter ( streams [ i ] ) ;
148
+ var stream = new BufferedStream ( streams . GetStream ( i ) , 1 << 24 ) ;
149
+ var trackWriter = new MidiWriter ( stream ) ;
142
150
143
151
var track = tracks [ i ] ;
144
152
@@ -160,7 +168,7 @@ static void Main(string[] args)
160
168
161
169
if ( channel . Data is GeneratorData data )
162
170
{
163
- if ( data . EchoFeed > 0 )
171
+ if ( data . EchoFeed > 0 && ! DisableEcho )
164
172
{
165
173
shifted = EchoNotes ( shifted , data . Echo , data . EchoFeed , data . EchoTime , proj . Ppq ) ;
166
174
}
@@ -178,13 +186,16 @@ static void Main(string[] args)
178
186
} ) . MergeAll ( ) . TrimStart ( ) ;
179
187
180
188
trackWriter . Write ( notes . ExtractEvents ( ) ) ;
189
+ stream . Close ( ) ;
181
190
182
191
lock ( l )
183
192
{
184
193
Console . WriteLine ( $ "Generated track { i + 1 } , { ( trackID ++ ) + 1 } /{ tracks . Length } ") ;
185
194
}
186
195
} ) ;
187
196
197
+ streams . CloseAllStreams ( ) ;
198
+
188
199
var writer = new MidiWriter ( Path . Combine ( Path . GetDirectoryName ( filePath ) , Path . GetFileName ( filePath ) + ".mid" ) ) ;
189
200
writer . Init ( ( ushort ) proj . Ppq ) ;
190
201
writer . InitTrack ( ) ;
@@ -195,20 +206,51 @@ static void Main(string[] args)
195
206
{
196
207
Console . WriteLine ( $ "Writing track { i + 1 } /{ tracks . Length } ") ;
197
208
198
- streams [ i ] . Position = 0 ;
209
+ var stream = streams . GetStream ( i , true ) ;
210
+
211
+ stream . Position = 0 ;
199
212
unchecked
200
213
{
201
- writer . WriteTrack ( streams [ i ] ) ;
214
+ writer . WriteTrack ( stream ) ;
202
215
}
203
- streams [ i ] . Close ( ) ;
216
+ stream . Close ( ) ;
204
217
}
205
218
206
219
writer . Close ( ) ;
220
+ streams . CloseAllStreams ( ) ;
221
+ streams . Dispose ( ) ;
222
+ File . Delete ( tempFile ) ;
207
223
208
224
Console . WriteLine ( "Press any key to exit... " ) ;
209
225
Console . ReadKey ( ) ;
210
226
}
211
227
228
+ static void GetArgs ( string [ ] args )
229
+ {
230
+ for ( var i = 0 ; i < args . Length ; i ++ )
231
+ {
232
+ switch ( args [ i ] )
233
+ {
234
+ case "-fc" :
235
+ case "--forcecolor" :
236
+ {
237
+ ForceColor = true ;
238
+ break ;
239
+ }
240
+ case "-de" :
241
+ case "--disable-echo" :
242
+ {
243
+ DisableEcho = true ;
244
+ break ;
245
+ }
246
+ default :
247
+ {
248
+ break ;
249
+ }
250
+ }
251
+ }
252
+ }
253
+
212
254
static void ParallelFor ( int from , int to , int threads , CancellationToken cancel , Action < int > func )
213
255
{
214
256
Dictionary < int , Task > tasks = new Dictionary < int , Task > ( ) ;
@@ -221,9 +263,11 @@ void RunTask(int i)
221
263
try
222
264
{
223
265
func ( i ) ;
266
+ }
267
+ finally
268
+ {
224
269
completed . Add ( i ) ;
225
270
}
226
- catch ( Exception e ) { }
227
271
} ) ;
228
272
tasks . Add ( i , t ) ;
229
273
t . Start ( ) ;
0 commit comments