コード例 #1
0
        public override bool Open(bool prescan)
        {
            Debug.Assert(!_FileOpened);
            if (_FileOpened)
            {
                return(false);
            }

            if (!File.Exists(_Medium))
            {
                Dispose();
                return(false);
            }

            bool ok = true;

            try
            {
                _Source  = AL.GenSource();
                _Buffers = new int[_BufferCount];
                for (int i = 0; i < _BufferCount; i++)
                {
                    _Buffers[i] = AL.GenBuffer();
                    ok          = ok && _Buffers[i] != 0;
                }
            }
            catch (Exception)
            {
                ok = false;
            }
            if (!ok)
            {
                Dispose();
                CLog.Error("Error Init OpenAL Playback");
                return(false);
            }


            _Decoder = new CAudioDecoderFFmpeg();
            if (!_Decoder.Open(_Medium))
            {
                Dispose();
                CLog.Error("Error opening audio file: " + _Medium);
                return(false);
            }
            _Format = _Decoder.GetFormatInfo();
            if (_Format.SamplesPerSecond == 0)
            {
                Dispose();
                CLog.Error("Error Init OpenAL Playback (samples=0)");
                return(false);
            }

            Length = _Decoder.GetLength();

            _ByteCount      = 2 * _Format.ChannelCount;
            _BytesPerSecond = _Format.SamplesPerSecond * _ByteCount;

            _CurrentTime = 0f;
            _TimeCode    = 0f;
            _Timer.Reset();
            _Data       = new CRingBuffer(_Bufsize);
            _NoMoreData = false;
            _SampleBuf  = new byte[(int)CConfig.Config.Sound.AudioBufferSize];
            //From now on closing the driver and the decoder is handled by the thread ONLY!

            _DecoderThread = new Thread(_Execute)
            {
                Priority = ThreadPriority.Normal, Name = Path.GetFileName(_Medium)
            };
            _DecoderThread.Start();

            _FileOpened = true;
            return(true);
        }
コード例 #2
0
ファイル: CPortAudioStream.cs プロジェクト: da-ka/Vocaluxe
        public override bool Open(bool prescan)
        {
            Debug.Assert(!_FileOpened);
            if (_FileOpened)
            {
                return(false);
            }

            if (!File.Exists(_Medium))
            {
                Dispose();
                return(false);
            }

            try
            {
                _PaHandle = new CPortAudioHandle();

                int hostApi = _PaHandle.GetHostApi();
                _ApiInfo          = PortAudioSharp.PortAudio.Pa_GetHostApiInfo(hostApi);
                _OutputDeviceInfo = PortAudioSharp.PortAudio.Pa_GetDeviceInfo(_ApiInfo.defaultOutputDevice);
                if (_OutputDeviceInfo.defaultLowOutputLatency < 0.1)
                {
                    _OutputDeviceInfo.defaultLowOutputLatency = 0.1;
                }

                _PaStreamCallback = _ProcessNewData;
            }
            catch (Exception)
            {
                Dispose();
                CLog.Error("Error Init PortAudio Playback");
                return(false);
            }

            _Decoder = new CAudioDecoderFFmpeg();
            if (!_Decoder.Open(_Medium))
            {
                Dispose();
                CLog.Error("Error opening audio file: " + _Medium);
                return(false);
            }

            SFormatInfo format = _Decoder.GetFormatInfo();

            if (format.SamplesPerSecond == 0)
            {
                Dispose();
                CLog.Error("Error Init PortAudio Playback (samples=0)");
                return(false);
            }

            Length          = _Decoder.GetLength();
            _ByteCount      = 2 * format.ChannelCount;
            _BytesPerSecond = format.SamplesPerSecond * _ByteCount;
            _SyncTimer.Pause();
            _SyncTimer.Time = 0f;

            PortAudioSharp.PortAudio.PaStreamParameters?outputParams = new PortAudioSharp.PortAudio.PaStreamParameters
            {
                channelCount              = format.ChannelCount,
                device                    = _ApiInfo.defaultOutputDevice,
                sampleFormat              = PortAudioSharp.PortAudio.PaSampleFormat.paInt16,
                suggestedLatency          = _OutputDeviceInfo.defaultLowOutputLatency,
                hostApiSpecificStreamInfo = IntPtr.Zero
            };

            if (!_PaHandle.OpenOutputStream(
                    out _Stream,
                    ref outputParams,
                    format.SamplesPerSecond,
                    (uint)CConfig.Config.Sound.AudioBufferSize / 2,
                    PortAudioSharp.PortAudio.PaStreamFlags.paNoFlag,
                    _PaStreamCallback,
                    IntPtr.Zero) || _Stream == IntPtr.Zero)
            {
                Dispose();
                return(false);
            }

            _Latency = CConfig.Config.Sound.AudioLatency / 1000f + (float)PortAudioSharp.PortAudio.Pa_GetStreamInfo(_Stream).outputLatency;

            //From now on closing the driver and the decoder is handled by the thread ONLY!

            _Paused        = true;
            _FileOpened    = true;
            _Data          = new CRingBuffer(_Bufsize);
            _NoMoreData    = false;
            _DecoderThread = new Thread(_Execute)
            {
                Priority = ThreadPriority.Normal, Name = Path.GetFileName(_Medium)
            };
            _DecoderThread.Start();

            return(true);
        }