/// <summary>
        /// Converts a BNS audio file to Wave format.
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        /// <returns></returns>
        public static Wave BnsToWave(Stream inputFile)
        {
            BNS b = new BNS();

            byte[] samples = b.Read(inputFile);

            Wave wave = new Wave(b.bnsInfo.ChannelCount, 16, b.bnsInfo.SampleRate, samples);

            if (b.bnsInfo.HasLoop == 1)
            {
                wave.AddLoop((int)b.bnsInfo.LoopStart);
            }
            return(wave);
        }
        public static Wave BnsToWave(byte[] bnsFile)
        {
            BNS b = new BNS();

            byte[] samples;

            using (MemoryStream ms = new MemoryStream(bnsFile))
                samples = b.Read(ms);

            Wave wave = new Wave(b.bnsInfo.ChannelCount, 16, b.bnsInfo.SampleRate, samples);

            if (b.bnsInfo.HasLoop == 1)
            {
                wave.AddLoop((int)b.bnsInfo.LoopStart);
            }
            return(wave);
        }
        public static Wave BnsToWave(string pathToFile)
        {
            BNS b = new BNS();

            byte[] samples;

            using (FileStream fs = new FileStream(pathToFile, FileMode.Open))
                samples = b.Read(fs);

            Wave wave = new Wave(b.bnsInfo.ChannelCount, 16, b.bnsInfo.SampleRate, samples);

            if (b.bnsInfo.HasLoop == 1)
            {
                wave.AddLoop((int)b.bnsInfo.LoopStart);
            }
            return(wave);
        }
Пример #4
0
        public static Wave BnsToWave(byte[] bnsFile)
        {
            BNS b = new BNS();
            byte[] samples;

            using (MemoryStream ms = new MemoryStream(bnsFile))
                samples = b.Read(ms);

            Wave wave = new Wave(b.bnsInfo.ChannelCount, 16, b.bnsInfo.SampleRate, samples);
            if (b.bnsInfo.HasLoop == 1) wave.AddLoop((int)b.bnsInfo.LoopStart);
            return wave;
        }
Пример #5
0
        public static Wave BnsToWave(string pathToFile)
        {
            BNS b = new BNS();
            byte[] samples;

            using (FileStream fs = new FileStream(pathToFile, FileMode.Open))
                samples = b.Read(fs);

            Wave wave = new Wave(b.bnsInfo.ChannelCount, 16, b.bnsInfo.SampleRate, samples);
            if (b.bnsInfo.HasLoop == 1) wave.AddLoop((int)b.bnsInfo.LoopStart);
            return wave;
        }
Пример #6
0
        /// <summary>
        /// Converts a BNS audio file to Wave format.
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        /// <returns></returns>
        public static Wave BnsToWave(Stream inputFile)
        {
            BNS b = new BNS();
            byte[] samples = b.Read(inputFile);

            Wave wave = new Wave(b.bnsInfo.ChannelCount, 16, b.bnsInfo.SampleRate, samples);
            if (b.bnsInfo.HasLoop == 1) wave.AddLoop((int)b.bnsInfo.LoopStart);
            return wave;
        }
        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);
            }
        }