2011-09-03 04:01:06 +00:00
using System ;
using System.Collections.Generic ;
2013-11-03 23:45:44 +00:00
using System.Diagnostics ;
using System.Windows.Forms ;
using System.IO ;
2011-09-03 04:01:06 +00:00
using System.Linq ;
using System.Text ;
2013-11-03 23:45:44 +00:00
using BizHawk.Emulation.DiscSystem ;
2011-09-03 04:01:06 +00:00
2013-11-03 12:28:33 +00:00
namespace BizHawk.Client.DiscoHawk
2011-09-03 04:01:06 +00:00
{
2013-11-03 12:28:33 +00:00
class AudioExtractor
{
public static string FFmpegPath ;
2011-09-03 04:01:06 +00:00
2013-11-03 12:28:33 +00:00
public static void Extract ( Disc disc , string path , string filebase )
{
2015-06-23 18:57:11 +00:00
var dsr = new DiscSectorReader ( disc ) ;
2011-09-04 06:15:41 +00:00
bool confirmed = false ;
2015-07-08 03:29:11 +00:00
var tracks = disc . Session1 . Tracks ;
2013-11-03 12:28:33 +00:00
foreach ( var track in tracks )
{
2015-07-05 22:36:11 +00:00
if ( ! track . IsAudio )
2013-11-03 12:28:33 +00:00
continue ;
2011-09-03 04:01:06 +00:00
2015-07-08 03:29:11 +00:00
int trackLength = track . NextTrack . LBA - track . LBA ;
var waveData = new byte [ trackLength * 2352 ] ;
2015-07-05 22:36:11 +00:00
int startLba = track . LBA ;
2015-07-08 03:29:11 +00:00
for ( int sector = 0 ; sector < trackLength ; sector + + )
2015-06-23 18:57:11 +00:00
dsr . ReadLBA_2352 ( startLba + sector , waveData , sector * 2352 ) ;
2011-09-03 04:01:06 +00:00
2019-03-20 04:29:38 +00:00
string mp3Path = $"{Path.Combine(path, filebase)} - Track {track.Number:D2}.mp3" ;
2013-11-03 12:28:33 +00:00
if ( File . Exists ( mp3Path ) )
2011-09-04 06:15:41 +00:00
{
if ( ! confirmed )
{
var dr = MessageBox . Show ( "This file already exists. Do you want extraction to proceed overwriting files, or cancel the entire operation immediately?" , "File already exists" , MessageBoxButtons . OKCancel ) ;
if ( dr = = DialogResult . Cancel ) return ;
confirmed = true ;
}
File . Delete ( mp3Path ) ;
}
2011-09-04 05:53:09 +00:00
string tempfile = Path . GetTempFileName ( ) ;
try
{
File . WriteAllBytes ( tempfile , waveData ) ;
2011-09-04 05:54:50 +00:00
var ffmpeg = new FFMpeg ( ) ;
ffmpeg . Run ( "-f" , "s16le" , "-ar" , "44100" , "-ac" , "2" , "-i" , tempfile , "-f" , "mp3" , "-ab" , "192k" , mp3Path ) ;
2011-09-04 05:53:09 +00:00
}
finally
{
File . Delete ( tempfile ) ;
}
2013-11-03 12:28:33 +00:00
}
}
}
2011-09-03 04:01:06 +00:00
}