void bwConvertToBns_DoWork(object sender, DoWorkEventArgs e) { try { BackgroundWorker bwConvertToBns = sender as BackgroundWorker; BnsConversionInfo bnsInfo = (BnsConversionInfo)e.Argument; byte[] audioData = new byte[0]; if (bnsInfo.audioFile.ToLower() == "internal sound") { for (int i = 0; i < sourceWad.BannerApp.NumOfNodes; i++) { if (sourceWad.BannerApp.StringTable[i].ToLower() == "sound.bin") { audioData = Headers.IMD5.RemoveHeader(sourceWad.BannerApp.Data[i]); } } internalSound = true; } else { audioData = File.ReadAllBytes(bnsInfo.audioFile); } bool mp3 = bnsInfo.audioFile.EndsWith(".mp3"); if (mp3 && bnsInfo.loopType == BnsConversionInfo.LoopType.FromWave) { bnsInfo.loopType = BnsConversionInfo.LoopType.None; } if (mp3) { bwConvertToBns.ReportProgress(0, "Converting MP3..."); ProcessStartInfo lameI = new ProcessStartInfo(Application.StartupPath + Path.DirectorySeparatorChar + "lame.exe", string.Format("--decode \"{0}\" \"{1}\"", bnsInfo.audioFile, Application.StartupPath + Path.DirectorySeparatorChar + "customizemii_temp.wav")); lameI.CreateNoWindow = true; lameI.UseShellExecute = false; lameI.RedirectStandardError = true; Process lame = Process.Start(lameI); string thisLine = string.Empty; while (lame.HasExited == false) { thisLine = lame.StandardError.ReadLine(); if (!string.IsNullOrEmpty(thisLine)) { if (thisLine.StartsWith("Frame#")) { string thisFrame = thisLine.Remove(thisLine.IndexOf('/')); thisFrame = thisFrame.Remove(0, thisFrame.LastIndexOf(' ') + 1); string Frames = thisLine.Remove(0, thisLine.IndexOf('/') + 1); Frames = Frames.Remove(Frames.IndexOf(' ')); int thisProgress = (int)((Convert.ToDouble(thisFrame) / Convert.ToDouble(Frames)) * 100); bwConvertToBns.ReportProgress(thisProgress); } } } lame.WaitForExit(); lame.Close(); if (File.Exists(Application.StartupPath + Path.DirectorySeparatorChar + "customizemii_temp.wav")) { audioData = File.ReadAllBytes(Application.StartupPath + Path.DirectorySeparatorChar + "customizemii_temp.wav"); File.Delete(Application.StartupPath + Path.DirectorySeparatorChar + "customizemii_temp.wav"); } else { throw new Exception("Error converting MP3..."); } } bwConvertToBns.ReportProgress(0, "Converting to BNS..."); BNS bns = new BNS(audioData, bnsInfo.loopType == BnsConversionInfo.LoopType.FromWave); bns.Progress += new EventHandler <ProgressChangedEventArgs>(bns_ProgressChanged); bns.StereoToMono = bnsInfo.stereoToMono; bns.Convert(); if (bnsInfo.loopType == BnsConversionInfo.LoopType.Manual && bnsInfo.loopStartSample > -1 && bnsInfo.loopStartSample < bns.TotalSampleCount) { bns.SetLoop(bnsInfo.loopStartSample); } newSoundBin = Headers.IMD5.AddHeader(bns.ToByteArray()); replacedSound = bnsInfo.audioFile; } catch (Exception ex) { replacedSound = string.Empty; if (File.Exists(Application.StartupPath + Path.DirectorySeparatorChar + "customizemii_temp.wav")) { File.Delete(Application.StartupPath + Path.DirectorySeparatorChar + "customizemii_temp.wav"); } errorBox("Error during conversion:\n" + ex.Message); } }
private void cmConvertToBns_Click(object sender, EventArgs e) { if (pbProgress.Value == 100) { CustomizeMii_BnsConvert bnsConvert = new CustomizeMii_BnsConvert(File.Exists(Application.StartupPath + Path.DirectorySeparatorChar + "lame.exe")); for (int i = 0; i < sourceWad.BannerApp.NumOfNodes; i++) { if (sourceWad.BannerApp.StringTable[i].ToLower() == "sound.bin") bnsConvert.SourceSound = Headers.IMD5.RemoveHeader(sourceWad.BannerApp.Data[i]); } if (bnsConvert.ShowDialog() == DialogResult.OK) { BnsConversionInfo bnsInfo = new BnsConversionInfo(); bnsInfo.audioFile = bnsConvert.AudioFile; bnsInfo.stereoToMono = false; if (bnsConvert.ChannelCount == 2) { if (MessageBox.Show("Do you want to convert the stereo Wave file to a mono BNS file?\nOnly the left channel will be taken.", "Convert to Mono?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) bnsInfo.stereoToMono = true; } if (bnsConvert.LoopFromAudio) { bnsInfo.loopType = BnsConversionInfo.LoopType.FromWave; } else if (bnsConvert.LoopManually) { bnsInfo.loopStartSample = bnsConvert.LoopStartSample; bnsInfo.loopType = BnsConversionInfo.LoopType.Manual; } else bnsInfo.loopType = BnsConversionInfo.LoopType.None; BackgroundWorker bwConvertToBns = new BackgroundWorker(); bwConvertToBns.WorkerReportsProgress = true; bwConvertToBns.DoWork += new DoWorkEventHandler(bwConvertToBns_DoWork); bwConvertToBns.ProgressChanged += new ProgressChangedEventHandler(bwConvertToBns_ProgressChanged); bwConvertToBns.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwConvertToBns_RunWorkerCompleted); bwConvertToBns.RunWorkerAsync(bnsInfo); } } }