예제 #1
0
        public unsafe WaveDuplex(int deviceIndex, double sampleRate, int framesPerBuffer, AudioBufferAvailableDelegate bufferNeededDelegate)
        {
            this._bufferAvailable = bufferNeededDelegate;
            PaStreamParameters paStreamParameters = new PaStreamParameters
            {
                device           = deviceIndex,
                channelCount     = 2,
                suggestedLatency = 0.0,
                sampleFormat     = PaSampleFormat.PaFloat32
            };
            PaError paError = PortAudioAPI.Pa_IsFormatSupported(ref paStreamParameters, ref paStreamParameters, sampleRate);

            if (paError != 0)
            {
                throw new ApplicationException(paError.ToString());
            }
            this._gcHandle = GCHandle.Alloc(this);
            paError        = PortAudioAPI.Pa_OpenStream(out this._streamHandle, ref paStreamParameters, ref paStreamParameters, sampleRate, (uint)framesPerBuffer, PaStreamFlags.PaNoFlag, this._paCallback, (IntPtr)this._gcHandle);
            if (paError != 0)
            {
                this._gcHandle.Free();
                throw new ApplicationException(paError.ToString());
            }
            paError = PortAudioAPI.Pa_StartStream(this._streamHandle);
            if (paError == PaError.paNoError)
            {
                return;
            }
            PortAudioAPI.Pa_CloseStream(this._streamHandle);
            this._gcHandle.Free();
            throw new ApplicationException(paError.ToString());
        }
예제 #2
0
        public static List <AudioDevice> GetDevices(DeviceDirection direction)
        {
            List <AudioDevice> list = new List <AudioDevice>();
            int num  = PortAudioAPI.Pa_GetDefaultInputDevice();
            int num2 = PortAudioAPI.Pa_GetDefaultOutputDevice();
            int num3 = PortAudioAPI.Pa_GetDeviceCount();

            for (int i = 0; i < num3; i++)
            {
                PaDeviceInfo    paDeviceInfo    = PortAudioAPI.Pa_GetDeviceInfo(i);
                DeviceDirection deviceDirection = (paDeviceInfo.maxInputChannels <= 0) ? DeviceDirection.Output : ((paDeviceInfo.maxOutputChannels > 0) ? DeviceDirection.InputOutput : DeviceDirection.Input);
                if (deviceDirection == direction || deviceDirection == DeviceDirection.InputOutput)
                {
                    PaHostApiInfo paHostApiInfo = PortAudioAPI.Pa_GetHostApiInfo(paDeviceInfo.hostApi);
                    AudioDevice   audioDevice   = new AudioDevice();
                    audioDevice.Name      = paDeviceInfo.name;
                    audioDevice.Host      = paHostApiInfo.name;
                    audioDevice.Index     = i;
                    audioDevice.Direction = deviceDirection;
                    audioDevice.IsDefault = (i == num || i == num2);
                    list.Add(audioDevice);
                }
            }
            return(list);
        }
예제 #3
0
        public static List <AudioDevice> GetDevices(DeviceDirection direction)
        {
            var result = new List <AudioDevice>();

            var defaultIn  = PortAudioAPI.Pa_GetDefaultInputDevice();
            var defaultOut = PortAudioAPI.Pa_GetDefaultOutputDevice();
            var count      = PortAudioAPI.Pa_GetDeviceCount();

            for (var i = 0; i < count; i++)
            {
                var di = PortAudioAPI.Pa_GetDeviceInfo(i);
                var deviceDirection = di.maxInputChannels > 0 ? (di.maxOutputChannels > 0 ? DeviceDirection.InputOutput : DeviceDirection.Input) : DeviceDirection.Output;
                if (deviceDirection == direction || deviceDirection == DeviceDirection.InputOutput)
                {
                    var hi = PortAudioAPI.Pa_GetHostApiInfo(di.hostApi);
                    var ad = new AudioDevice();
                    ad.Name      = di.name;
                    ad.Host      = hi.name;
                    ad.Index     = i;
                    ad.Direction = deviceDirection;
                    ad.IsDefault = i == defaultIn || i == defaultOut;
                    result.Add(ad);
                }
            }

            return(result);
        }
예제 #4
0
        public unsafe WavePlayer(int deviceIndex, double sampleRate, int framesPerBuffer, AudioBufferNeededDelegate bufferNeededDelegate)
        {
            Console.WriteLine("WavePlayer started, samplerate=" + sampleRate.ToString() + ", frameCount=" + framesPerBuffer.ToString());
            this._bufferNeeded = bufferNeededDelegate;
            PaStreamParameters paStreamParameters = new PaStreamParameters
            {
                device           = deviceIndex,
                channelCount     = 2,
                suggestedLatency = 0.0,
                sampleFormat     = PaSampleFormat.PaFloat32
            };
            PaError paError = PortAudioAPI.Pa_IsFormatSupported(IntPtr.Zero, ref paStreamParameters, sampleRate);

            if (paError != 0)
            {
                throw new ApplicationException("Init audio output device: " + paError.ToString());
            }
            this._gcHandle = GCHandle.Alloc(this);
            paError        = PortAudioAPI.Pa_OpenStream(out this._streamHandle, IntPtr.Zero, ref paStreamParameters, sampleRate, (uint)framesPerBuffer, PaStreamFlags.PaNoFlag, this._paCallback, (IntPtr)this._gcHandle);
            if (paError != 0)
            {
                this._gcHandle.Free();
                throw new ApplicationException("Open audio output device: " + paError.ToString());
            }
            paError = PortAudioAPI.Pa_StartStream(this._streamHandle);
            if (paError == PaError.paNoError)
            {
                return;
            }
            PortAudioAPI.Pa_CloseStream(this._streamHandle);
            this._gcHandle.Free();
            throw new ApplicationException("Start audio output device: " + paError.ToString());
        }
예제 #5
0
 public void Dispose()
 {
     if (this._streamHandle != IntPtr.Zero)
     {
         PortAudioAPI.Pa_StopStream(this._streamHandle);
         PortAudioAPI.Pa_CloseStream(this._streamHandle);
         this._streamHandle = IntPtr.Zero;
     }
     this._gcHandle.Free();
 }
예제 #6
0
        public WavePlayer(int deviceIndex, double sampleRate, int framesPerBuffer, AudioBufferNeededDelegate bufferNeededDelegate)
        {
            _bufferNeeded = bufferNeededDelegate;

            var ouputParams = new PaStreamParameters();

            ouputParams.device           = deviceIndex;
            ouputParams.channelCount     = 2;
            ouputParams.suggestedLatency = 0;
            ouputParams.sampleFormat     = PaSampleFormat.PaFloat32;

            var pe = PortAudioAPI.Pa_IsFormatSupported(IntPtr.Zero, ref ouputParams, sampleRate);

            if (pe != PaError.paNoError)
            {
                throw new ApplicationException(pe.ToString());
            }

            _gcHandle = GCHandle.Alloc(this);

            pe = PortAudioAPI.Pa_OpenStream(
                out _streamHandle,
                IntPtr.Zero,
                ref ouputParams,
                sampleRate,
                (uint)framesPerBuffer,
                PaStreamFlags.PaNoFlag,
                _paCallback,
                (IntPtr)_gcHandle);

            if (pe != PaError.paNoError)
            {
                _gcHandle.Free();
                throw new ApplicationException(pe.ToString());
            }

            pe = PortAudioAPI.Pa_StartStream(_streamHandle);
            if (pe != PaError.paNoError)
            {
                PortAudioAPI.Pa_CloseStream(_streamHandle);
                _gcHandle.Free();
                throw new ApplicationException(pe.ToString());
            }
        }