Beispiel #1
0
        private void Import(string inputFilePath, string outputFilePath, float tapeImportPosition, IProgress <float> progress = null)
        {
            byte[] buffer = new byte[1024 * 128 * 2];

            Utils.WAVFile outputWavFile = Utils.WAVFile.Load(outputFilePath);

            try
            {
                long byteImportPosition = outputWavFile.dataOffsetBytes
                                          +
                                          (long)(Common.WaveFormat.SampleRate * tapeImportPosition)
                                          * (Common.WaveFormat.BitsPerSample / 8)
                                          * Common.WaveFormat.Channels;

                outputWavFile.stream.Seek(byteImportPosition, SeekOrigin.Begin);

                using (var reader = new NAudio.Wave.AudioFileReader(inputFilePath))
                    using (var tempConverter = new NAudio.Wave.Wave32To16Stream(reader))
                        using (var converter = new NAudio.Wave.WaveFormatConversionStream(Common.WaveFormat, tempConverter))
                        {
                            int readCount = 0;

                            int stepCount = 100;
                            int step      = 0;
                            int stepSize  = (int)(reader.Length / stepCount);
                            int nextStep  = 0;

                            do
                            {
                                readCount = converter.Read(buffer, 0, buffer.Length);
                                readCount = (int)Math.Min(readCount, outputWavFile.stream.Length - outputWavFile.stream.Position);
                                outputWavFile.stream.Write(buffer, 0, readCount);

                                if (progress != null && reader.Position >= nextStep)
                                {
                                    nextStep += stepSize;
                                    progress.Report(step / (float)stepCount);
                                    step++;
                                }
                            }while (readCount > 0);
                        }
            }
            finally
            {
                outputWavFile.Close();
            }
        }
        public void StartRending(System.IO.DirectoryInfo baseTempDir, List <VocalUtau.Calculators.BarkerCalculator.BgmPreRender> BList, string RendToWav = "")
        {
            _IsRending   = true;
            _ExitRending = false;
            if (RendingStateChange != null)
            {
                RendingStateChange(this);
            }

            string        ProcessIDStr = Process.GetCurrentProcess().Id.ToString();
            DirectoryInfo tempDir      = baseTempDir.CreateSubdirectory("temp");
            DirectoryInfo cacheDir     = baseTempDir.CreateSubdirectory("cache");

            string TrackFileName = tempDir.FullName + "\\Bgm_" + CacheSignal + ".wav";

            FileStream Fs;

            headSize = InitFile(out Fs, TrackFileName);
            Semaphore semaphore = new Semaphore(1, 1, "VocalUtau.WavTool." + ProcessIDStr + ".Bgm_" + CacheSignal);

            for (int i = 0; i < BList.Count; i++)
            {
                if (BList[i].DelayTime > 0)
                {
                    int ByteTime = (int)(IOHelper.NormalPcmMono16_Format.AverageBytesPerSecond * BList[i].DelayTime);
                    ByteTime -= ByteTime % 2;
                    byte[] byteL = new byte[ByteTime];
                    Array.Clear(byteL, 0, ByteTime);
                    Fs.Write(byteL, 0, ByteTime);
                }
                semaphore.WaitOne();
                try
                {
                    int ByteTime = (int)(IOHelper.NormalPcmMono16_Format.AverageBytesPerSecond * BList[i].PassTime);
                    ByteTime -= ByteTime % 2;
                    using (NAudio.Wave.AudioFileReader reader = new NAudio.Wave.AudioFileReader(BList[i].FilePath))
                    {
                        int JumpLoops = ByteTime / 2;
                        using (NAudio.Wave.Wave32To16Stream w16 = new NAudio.Wave.Wave32To16Stream(reader))
                        {
                            using (NAudio.Wave.WaveStream wfmt = new NAudio.Wave.BlockAlignReductionStream(w16))
                            {
                                using (NAudio.Wave.WaveStream wout = new NAudio.Wave.WaveFormatConversionStream(IOHelper.NormalPcmMono16_Format, wfmt))
                                {
                                    while (wout.Position < wout.Length)
                                    {
                                        if (_ExitRending)
                                        {
                                            break;
                                        }
                                        byte[] by = new byte[2];
                                        int    rd = wout.Read(by, 0, 2);
                                        if (JumpLoops > 0)
                                        {
                                            JumpLoops--;
                                        }
                                        else
                                        {
                                            Fs.Write(by, 0, 2);
                                        }

                                        /*  for (int w = 1; w < w16.WaveFormat.Channels; w++)
                                         * {
                                         *    int rdr = w16.Read(by, 0, 2);
                                         * }*/
                                    }
                                }
                            }
                        }
                    }
                }
                catch {; }
                Fs.Flush();
                semaphore.Release();
                if (_ExitRending)
                {
                    break;
                }
            }
            _IsRending = false;
            long total = Fs.Length;

            byte[] head = IOHelper.GenerateHead((int)(total - headSize));
            Fs.Seek(0, SeekOrigin.Begin);
            Fs.Write(head, 0, head.Length);
            Fs.Flush();
            Fs.Close();
            _ExitRending = false;
            if (RendingStateChange != null)
            {
                RendingStateChange(this);
            }
            if (RendToWav != "")
            {
                File.Copy(TrackFileName, RendToWav, true);
                try
                {
                    File.Delete(TrackFileName);
                }
                catch {; }
            }
        }