@@ -210,6 +210,38 @@ public static void Draw(Sfx sfx)
210210 player . Render ( ) ;
211211 }
212212
213+ public async void PromptResample ( )
214+ {
215+ var rate = await Program . ShowIntPrompt ( "New sample rate" , "Resample Audio" , ( int ) thisSample . sampleData . sampleRate ) ;
216+ if ( rate != null )
217+ {
218+ var inData = new List < byte > ( ) ;
219+ for ( int i = 0 ; i < sampleData . Length ; i ++ )
220+ {
221+ var bytes = BitConverter . GetBytes ( sampleData [ i ] ) ;
222+ if ( BitConverter . IsLittleEndian )
223+ {
224+ bytes = bytes . Reverse ( ) . ToArray ( ) ;
225+ }
226+ inData . AddRange ( bytes ) ;
227+ }
228+ var inStream = new MemoryStream ( inData . ToArray ( ) ) ;
229+ var outStream = new MemoryStream ( ) ;
230+ FFMpegArguments . FromPipeInput ( new StreamPipeSource ( inStream ) , options => options
231+ . WithAudioCodec ( "pcm_s16be" ) . ForceFormat ( "s16be" ) . WithAudioSamplingRate ( ( int ) thisSample . sampleData . sampleRate ) )
232+ . OutputToPipe ( new StreamPipeSink ( outStream ) , options => options
233+ . WithAudioCodec ( "pcm_s16be" ) . ForceFormat ( "s16be" ) . WithAudioSamplingRate ( rate . Value ) . WithCustomArgument ( "-ac 1" ) ) . WithLogLevel ( FFMpegLogLevel . Info ) . ProcessSynchronously ( ) ;
234+
235+
236+ var data = outStream . ToArray ( ) ;
237+ thisSample . sampleData . samples = [ ..data ] ;
238+ thisSample . sampleData . sampleRate = ( uint ) rate ;
239+ thisSample . sampleData . sampleCount = ( uint ) data . Length / 2 ;
240+ thisSample . sampleData . encoding = SynthSample . SampleData . Encoding . kBigEndPCM ;
241+ LoadData ( ) ;
242+ }
243+ }
244+
213245 public void Render ( )
214246 {
215247 ImGui . Button ( FontAwesome5 . EllipsisH , new Vector2 ( SamplePlayerHeight , SamplePlayerHeight ) ) ;
@@ -247,17 +279,33 @@ public void Render()
247279 }
248280 }
249281
250- if ( ImGui . MenuItem ( FontAwesome5 . FileExport + " Export Audio File" ) )
282+ if ( dataState == State . Loaded && ImGui . MenuItem ( FontAwesome5 . FileExport + " Export Audio File" ) )
251283 {
252284 var ( cancelled , path ) =
253285 TinyDialogs . SaveFileDialog ( "Select file to save" , "" , new FileFilter ( "WAV file" , [ "*.wav" ] ) ) ;
254286 if ( ! cancelled )
255287 {
256- var memoryStream = new MemoryStream ( thisSample . sampleData . samples . ToArray ( ) ) ;
288+ var data = new List < byte > ( ) ;
289+ for ( int i = 0 ; i < sampleData . Length ; i ++ )
290+ {
291+ var bytes = BitConverter . GetBytes ( sampleData [ i ] ) ;
292+ if ( BitConverter . IsLittleEndian )
293+ {
294+ bytes = bytes . Reverse ( ) . ToArray ( ) ;
295+ }
296+ data . AddRange ( bytes ) ;
297+ }
298+ var memoryStream = new MemoryStream ( data . ToArray ( ) ) ;
299+
257300 FFMpegArguments . FromPipeInput ( new StreamPipeSource ( memoryStream ) , options => options
258301 . WithAudioCodec ( "pcm_s16be" ) . ForceFormat ( "s16be" ) . WithAudioSamplingRate ( ( int ) thisSample . sampleData . sampleRate ) ) . OutputToFile ( path ) . ProcessSynchronously ( ) ;
259302 }
260303 }
304+
305+ if ( dataState == State . Loaded && ImGui . MenuItem ( FontAwesome5 . FileAudio + " Resample Audio" ) )
306+ {
307+ PromptResample ( ) ;
308+ }
261309 }
262310 else
263311 {
0 commit comments