예제 #1
0
파일: AvsReader.cs 프로젝트: pphh77/MeGui
 public long ReadAudioSamples(long nStart, int nAmount, IntPtr buf)
 {
     clip.ReadAudio(buf, nStart, nAmount);
     return(nAmount);
 }
예제 #2
0
        private void encode()
        {
            try
            {
                using (AviSynthScriptEnvironment env = new AviSynthScriptEnvironment())
                {
                    using (AviSynthClip a = env.ParseScript(_avisynthAudioScript))
                    {
                        if (0 == a.ChannelsCount)
                        {
                            throw new ApplicationException("Can't find audio stream");
                        }

                        _logBuilder.AppendFormat("Input: Channels={0}, BitsPerSample={1}, SampleRate={2}Hz{3}", a.ChannelsCount, a.BitsPerSample, a.AudioSampleRate, Environment.NewLine);
                        _start = DateTime.Now;

                        const int MAX_SAMPLES_PER_ONCE = 4096;
                        int       frameSample          = 0;
                        int       lastUpdateSample     = 0;
                        int       frameBufferTotalSize = MAX_SAMPLES_PER_ONCE * a.ChannelsCount * a.BytesPerSample;
                        byte[]    frameBuffer          = new byte[frameBufferTotalSize];
                        createEncoderProcess(a);
                        try
                        {
                            using (Stream target = _encoderProcess.StandardInput.BaseStream)
                            {
                                // let's write WAV Header
                                if (_mustSendWavHeaderToEncoderStdIn)
                                {
                                    writeHeader(target, a);
                                }

                                _sampleRate = a.AudioSampleRate;

                                raiseEvent("Preprocessing...");
                                bool hasStartedEncoding = false;

                                GCHandle h       = GCHandle.Alloc(frameBuffer, GCHandleType.Pinned);
                                IntPtr   address = h.AddrOfPinnedObject();
                                try
                                {
                                    su.ClipLength = TimeSpan.FromSeconds((double)a.SamplesCount / (double)_sampleRate);
                                    while (frameSample < a.SamplesCount)
                                    {
                                        _mre.WaitOne();

                                        if (_encoderProcess != null)
                                        {
                                            if (_encoderProcess.HasExited)
                                            {
                                                throw new ApplicationException("Abnormal encoder termination " + _encoderProcess.ExitCode.ToString());
                                            }
                                        }
                                        int nHowMany = Math.Min((int)(a.SamplesCount - frameSample), MAX_SAMPLES_PER_ONCE);
                                        a.ReadAudio(address, frameSample, nHowMany);

                                        _mre.WaitOne();
                                        if (!hasStartedEncoding)
                                        {
                                            raiseEvent("Encoding audio...");
                                            hasStartedEncoding = true;
                                        }


                                        target.Write(frameBuffer, 0, nHowMany * a.ChannelsCount * a.BytesPerSample);
                                        target.Flush();
                                        frameSample += nHowMany;
                                        if (frameSample - lastUpdateSample > SAMPLES_PER_UPDATE)
                                        {
                                            setProgress((decimal)frameSample / (decimal)a.SamplesCount);
                                            lastUpdateSample = frameSample;
                                        }
                                        Thread.Sleep(0);
                                    }
                                }
                                finally
                                {
                                    h.Free();
                                }
                                setProgress(1M);

                                if (_mustSendWavHeaderToEncoderStdIn && a.BytesPerSample % 2 == 1)
                                {
                                    target.WriteByte(0);
                                }
                            }
                            raiseEvent("Finalizing encoder");
                            _encoderProcess.WaitForExit();
                            _readFromStdErrThread.Join();
                            _readFromStdOutThread.Join();
                            if (0 != _encoderProcess.ExitCode)
                            {
                                throw new ApplicationException("Abnormal encoder termination " + _encoderProcess.ExitCode.ToString());
                            }
                        }
                        finally
                        {
                            if (!_encoderProcess.HasExited)
                            {
                                _encoderProcess.Kill();
                                _encoderProcess.WaitForExit();
                                _readFromStdErrThread.Join();
                                _readFromStdOutThread.Join();
                            }
                            _readFromStdErrThread = null;
                            _readFromStdOutThread = null;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                deleteOutputFile();
                if (e is ThreadAbortException)
                {
                    _logBuilder.Append("ABORTING!\n");
                    su.WasAborted = true;
                    raiseEvent();
                }
                else
                {
                    // Better Errors Exception for Audio Encoders
                    int    encoder_path  = _encoderExecutablePath.LastIndexOf(@"\");
                    string audio_encoder = _encoderExecutablePath.Substring(encoder_path + 1).ToLower();

                    _logBuilder.Append("\n");
                    _logBuilder.Append("\nError:\n");

                    if (audioJob.Settings is WinAmpAACSettings)
                    {
                        if (File.Exists(encoder_path + "enc_aacplus.dll") == false)
                        {
                            _logBuilder.Append("enc_aacplus.dll not found in the path...\n");
                        }
                        if (File.Exists(Environment.SystemDirectory + @"\nscrt.dll") == false)
                        {
                            _logBuilder.Append("nscrt.dll must be in your Windows System directory...\n");
                        }
                    }
                    su.HasError = true;
                    raiseEvent();
                }
                return;
            }
            finally
            {
                deleteTempFiles();
            }
            su.IsComplete = true;
            raiseEvent();
        }
예제 #3
0
            public long ReadAudioSamples(long nStart, int nAmount, IntPtr buf)
            {
                clip.ReadAudio(buf, nStart, nAmount);
#warning Need to check whether nAmount samples are guaranteed to be returned
                return(nAmount);
            }
예제 #4
0
        private void encode()
        {
            try
            {
                using (AviSynthScriptEnvironment env = new AviSynthScriptEnvironment())
                {
                    using (AviSynthClip a = env.ParseScript(_avisynthAudioScript))
                    {
                        if (0 == a.ChannelsCount)
                        {
                            throw new ApplicationException("Can't find audio stream");
                        }

                        _logBuilder.AppendFormat("Channels={0}, BitsPerSample={1}, SampleRate={2}Hz{3}", a.ChannelsCount, a.BitsPerSample, a.AudioSampleRate, Environment.NewLine);

                        const int MAX_SAMPLES_PER_ONCE = 4096;
                        int       frameSample          = 0;
                        int       frameBufferTotalSize = MAX_SAMPLES_PER_ONCE * a.ChannelsCount * a.BytesPerSample;
                        byte[]    frameBuffer          = new byte[frameBufferTotalSize];
                        createEncoderProcess(a);
                        try
                        {
                            using (Stream target = _encoderProcess.StandardInput.BaseStream)
                            {
                                // let's write WAV Header
                                if (_mustSendWavHeaderToEncoderStdIn)
                                {
                                    writeHeader(target, a);
                                }

                                _sampleRate = a.AudioSampleRate;

                                raiseEvent("Preprocessing...");

                                GCHandle h       = GCHandle.Alloc(frameBuffer, GCHandleType.Pinned);
                                IntPtr   address = h.AddrOfPinnedObject();
                                try
                                {
                                    while (frameSample < a.SamplesCount)
                                    {
                                        _mre.WaitOne();

                                        if (_encoderProcess != null)
                                        {
                                            if (_encoderProcess.HasExited)
                                            {
                                                throw new ApplicationException("Abnormal encoder termination " + _encoderProcess.ExitCode.ToString());
                                            }
                                        }
                                        int nHowMany = Math.Min((int)(a.SamplesCount - frameSample), MAX_SAMPLES_PER_ONCE);
                                        a.ReadAudio(address, frameSample, nHowMany);

                                        _mre.WaitOne();

                                        setProgress(((100 * (double)frameSample) / a.SamplesCount), frameSample);
                                        target.Write(frameBuffer, 0, nHowMany * a.ChannelsCount * a.BytesPerSample);
                                        target.Flush();
                                        frameSample += nHowMany;
                                        Thread.Sleep(0);
                                    }
                                }
                                finally
                                {
                                    h.Free();
                                }
                                setProgress(100, frameSample);

                                if (_mustSendWavHeaderToEncoderStdIn && a.BytesPerSample % 2 == 1)
                                {
                                    target.WriteByte(0);
                                }
                            }
                            raiseEvent("Finalizing encoder");
                            _encoderProcess.WaitForExit();
                            _readFromStdErrThread.Join();
                            _readFromStdOutThread.Join();
                            if (0 != _encoderProcess.ExitCode)
                            {
                                throw new ApplicationException("Abnormal encoder termination " + _encoderProcess.ExitCode.ToString());
                            }
                        }
                        finally
                        {
                            if (!_encoderProcess.HasExited)
                            {
                                _encoderProcess.Kill();
                                _encoderProcess.WaitForExit();
                                _readFromStdErrThread.Join();
                                _readFromStdOutThread.Join();
                            }
                            _readFromStdErrThread = null;
                            _readFromStdOutThread = null;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                deleteOutputFile();
                if (e is ThreadAbortException)
                {
                    _logBuilder.Append("ABORTING!\n");
                    StatusUpdate u = new StatusUpdate();
                    u.WasAborted = true;
                    raiseEvent(u);
                }
                else
                {
                    _logBuilder.Append("Error:\n" + e.ToString());
                    StatusUpdate u = new StatusUpdate();
                    u.HasError = true;
                    u.Error    = e.ToString();
                    raiseEvent(u);
                }
                return;
            }
            finally
            {
                deleteTempFiles();
            }
            StatusUpdate u2 = new StatusUpdate();

            u2.IsComplete = true;
            raiseEvent(u2);
        }