@@ -15,6 +15,9 @@ namespace flp2midi
1515{
1616 class Program
1717 {
18+ static bool ForceColor { get ; set ; }
19+ static bool DisableEcho { get ; set ; }
20+
1821 //TODO: Actually support feed correctly
1922 static IEnumerable < Note > EchoNotes ( IEnumerable < Note > notes , byte echoamount , uint feed , uint time , int ppq )
2023 {
@@ -44,8 +47,12 @@ static void Main(string[] args)
4447 }
4548
4649 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 ) ;
4754
48- Console . WriteLine ( "flp2midi | Version: 1.0 .0" ) ;
55+ Console . WriteLine ( "flp2midi | Version: 1.1 .0" ) ;
4956 Console . WriteLine ( "Loading FL Studio project file..." ) ;
5057
5158 Project proj = Project . Load ( filePath , false ) ;
@@ -77,7 +84,7 @@ static void Main(string[] args)
7784
7885 return c . Value
7986 . 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 ) )
8188 . ToArray ( ) ;
8289 } ) ;
8390
@@ -91,7 +98,7 @@ static void Main(string[] args)
9198
9299 var trackID = 0 ;
93100
94- MemoryStream [ ] streams = new MemoryStream [ proj . Tracks . Length ] ;
101+ // MemoryStream[] streams = new MemoryStream[proj.Tracks.Length];
95102
96103 var tracks = proj . Tracks . Where ( t => t . Items . Count != 0 ) . OrderBy ( t =>
97104 t . Items . Select ( i =>
@@ -112,7 +119,7 @@ static void Main(string[] args)
112119 4 - Unknown
113120 5 - Tempo Events
114121 */
115-
122+
116123 /*
117124 * Modes:
118125 * 10 - Smooth
@@ -136,9 +143,10 @@ static void Main(string[] args)
136143
137144 ParallelFor ( 0 , tracks . Length , Environment . ProcessorCount , new CancellationToken ( false ) , i =>
138145 {
139- streams [ i ] = new MemoryStream ( ) ;
146+ // streams[i] = new MemoryStream();
140147
141- var trackWriter = new MidiWriter ( streams [ i ] ) ;
148+ var stream = new BufferedStream ( streams . GetStream ( i ) , 1 << 24 ) ;
149+ var trackWriter = new MidiWriter ( stream ) ;
142150
143151 var track = tracks [ i ] ;
144152
@@ -160,7 +168,7 @@ static void Main(string[] args)
160168
161169 if ( channel . Data is GeneratorData data )
162170 {
163- if ( data . EchoFeed > 0 )
171+ if ( data . EchoFeed > 0 && ! DisableEcho )
164172 {
165173 shifted = EchoNotes ( shifted , data . Echo , data . EchoFeed , data . EchoTime , proj . Ppq ) ;
166174 }
@@ -178,13 +186,16 @@ static void Main(string[] args)
178186 } ) . MergeAll ( ) . TrimStart ( ) ;
179187
180188 trackWriter . Write ( notes . ExtractEvents ( ) ) ;
189+ stream . Close ( ) ;
181190
182191 lock ( l )
183192 {
184193 Console . WriteLine ( $ "Generated track { i + 1 } , { ( trackID ++ ) + 1 } /{ tracks . Length } ") ;
185194 }
186195 } ) ;
187196
197+ streams . CloseAllStreams ( ) ;
198+
188199 var writer = new MidiWriter ( Path . Combine ( Path . GetDirectoryName ( filePath ) , Path . GetFileName ( filePath ) + ".mid" ) ) ;
189200 writer . Init ( ( ushort ) proj . Ppq ) ;
190201 writer . InitTrack ( ) ;
@@ -195,20 +206,51 @@ static void Main(string[] args)
195206 {
196207 Console . WriteLine ( $ "Writing track { i + 1 } /{ tracks . Length } ") ;
197208
198- streams [ i ] . Position = 0 ;
209+ var stream = streams . GetStream ( i , true ) ;
210+
211+ stream . Position = 0 ;
199212 unchecked
200213 {
201- writer . WriteTrack ( streams [ i ] ) ;
214+ writer . WriteTrack ( stream ) ;
202215 }
203- streams [ i ] . Close ( ) ;
216+ stream . Close ( ) ;
204217 }
205218
206219 writer . Close ( ) ;
220+ streams . CloseAllStreams ( ) ;
221+ streams . Dispose ( ) ;
222+ File . Delete ( tempFile ) ;
207223
208224 Console . WriteLine ( "Press any key to exit... " ) ;
209225 Console . ReadKey ( ) ;
210226 }
211227
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+
212254 static void ParallelFor ( int from , int to , int threads , CancellationToken cancel , Action < int > func )
213255 {
214256 Dictionary < int , Task > tasks = new Dictionary < int , Task > ( ) ;
@@ -221,9 +263,11 @@ void RunTask(int i)
221263 try
222264 {
223265 func ( i ) ;
266+ }
267+ finally
268+ {
224269 completed . Add ( i ) ;
225270 }
226- catch ( Exception e ) { }
227271 } ) ;
228272 tasks . Add ( i , t ) ;
229273 t . Start ( ) ;
0 commit comments