Esempio n. 1
0
        private void GetChannelNames(RealtimeHostConfig config)
        {
            if (config == null)
            {
                InputNames  = new string[0];
                OutputNames = new string[0];
                return;
            }

            var inputDeviceInfo  = PortAudio.Pa_GetDeviceInfo(config.InputDeviceID);
            var outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(config.OutputDeviceID);

            InputNames = Enumerable.Range(0, inputDeviceInfo.maxInputChannels)
                         .Select(ch =>
            {
                string chName = null;
                PortAudio.PaAsio_GetInputChannelName((PortAudio.PaDeviceIndex)config.InputDeviceID, ch, ref chName);
                return((ch + 1) + ": " + chName);
            })
                         .ToArray();

            OutputNames = Enumerable.Range(0, outputDeviceInfo.maxOutputChannels)
                          .Select(ch =>
            {
                string chName = null;
                PortAudio.PaAsio_GetOutputChannelName((PortAudio.PaDeviceIndex)config.OutputDeviceID, ch, ref chName);
                return((ch + 1) + ": " + chName);
            })
                          .ToArray();
        }
Esempio n. 2
0
        void ASIODeviceControlLoad(object sender, EventArgs e)
        {
            int deviceCount = PortAudio.Pa_GetDeviceCount();

            Console.WriteLine("Device count: " + paHostApiInfo.deviceCount + " default input device: " + paHostApiInfo.defaultInputDevice);

            deviceComboBox.Items.Clear();
            for (int i = 0; i < deviceCount; i++)
            {
                PortAudio.PaDeviceInfo  paDeviceInfo = PortAudio.Pa_GetDeviceInfo(i);
                PortAudio.PaHostApiInfo paHostApi    = PortAudio.Pa_GetHostApiInfo(paDeviceInfo.hostApi);
                if (paHostApi.type == PortAudio.PaHostApiTypeId.paASIO)
                {
                    Console.WriteLine("\n#" + i + "\n" + paDeviceInfo);
                    if (paDeviceInfo.maxOutputChannels > 0)
                    {
                        deviceComboBox.Items.Add(new DeviceItem(i, paDeviceInfo));
                        if (i == paHostApiInfo.defaultOutputDevice)
                        {
                            deviceComboBox.SelectedIndex = deviceComboBox.Items.Count - 1;
                        }
                    }
                }
            }

            bufferSizeComboBox.Items.Clear();
            int bufferSize = 256;

            while (bufferSize < 44100 / 2)
            {
                bufferSizeComboBox.Items.Add(bufferSize);
                bufferSize *= 2;
            }
            bufferSizeComboBox.SelectedIndex = 2;
        }
Esempio n. 3
0
        public void Record()
        {
            IntPtr userdata = IntPtr.Zero; //intptr.zero is essentially just a null pointer

            callbackDelegate = new PortAudio.PaStreamCallbackDelegate(myPaStreamCallback);
            PortAudio.Pa_Initialize();

            PortAudio.PaStreamParameters outputparams = new PortAudio.PaStreamParameters();

            outputparams.channelCount              = 1;
            outputparams.sampleFormat              = PortAudio.PaSampleFormat.paInt16;
            outputparams.device                    = PortAudio.Pa_GetDefaultInputDevice();
            outputparams.suggestedLatency          = PortAudio.Pa_GetDeviceInfo(outputparams.device).defaultLowInputLatency;
            outputparams.hostApiSpecificStreamInfo = IntPtr.Zero;


            PortAudio.PaStreamParameters a = new PortAudio.PaStreamParameters(); //uninteresting output params cause i cant give it null

            a.channelCount              = 1;
            a.sampleFormat              = PortAudio.PaSampleFormat.paInt16;
            a.device                    = PortAudio.Pa_GetDefaultOutputDevice();
            a.suggestedLatency          = PortAudio.Pa_GetDeviceInfo(a.device).defaultLowOutputLatency;
            a.hostApiSpecificStreamInfo = IntPtr.Zero;


            PortAudio.PaError error = PortAudio.Pa_OpenStream(out stream, ref outputparams, ref a, this.sampleRate,
                                                              (uint)NUM_SAMPLES, PortAudio.PaStreamFlags.paClipOff, callbackDelegate, IntPtr.Zero);

            this.isRecording = true;
            PortAudio.Pa_StartStream(stream);

            Thread myThread = new Thread(new ThreadStart(record_loop));

            myThread.Start();
        }
        public PortAudioPlayer(int channels, int frequency, uint framesPerBuffer,
                               PortAudio.PaStreamCallbackDelegate paStreamCallback)
        {
            log("Initializing...");
            this.channels         = channels;
            this.frequency        = frequency;
            this.framesPerBuffer  = framesPerBuffer;
            this.paStreamCallback = paStreamCallback;
            if (errorCheck("Initialize", PortAudio.Pa_Initialize()))
            {
                this.disposed = true;
                // if Pa_Initialize() returns an error code,
                // Pa_Terminate() should NOT be called.
                throw new Exception("Can't initialize audio");
            }
            int apiCount = PortAudio.Pa_GetHostApiCount();

            for (int i = 0; i < apiCount; i++)
            {
                PortAudio.PaHostApiInfo availableApiInfo = PortAudio.Pa_GetHostApiInfo(i);
                log("available API index: " + i + "\n" + availableApiInfo.ToString());
            }
            this.hostApi = apiSelect();
            log("selected Host API: " + this.hostApi);
            this.apiInfo          = PortAudio.Pa_GetHostApiInfo(this.hostApi);
            this.inputDeviceInfo  = PortAudio.Pa_GetDeviceInfo(apiInfo.defaultInputDevice);
            this.outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(apiInfo.defaultOutputDevice);
            log("input device:\n" + inputDeviceInfo.ToString());
            log("output device:\n" + outputDeviceInfo.ToString());
        }
Esempio n. 5
0
        /// <summary>
        /// Init PortAudio and list record devices
        /// </summary>
        /// <returns>true if success</returns>
        public bool Init()
        {
            _Devices = new List <SRecordDevice>();

            try
            {
                if (_initialized)
                {
                    CloseAll();
                }

                if (errorCheck("Initialize", PortAudio.Pa_Initialize()))
                {
                    return(false);
                }

                _initialized = true;
                int hostAPI = apiSelect();

                int numDevices = PortAudio.Pa_GetDeviceCount();
                for (int i = 0; i < numDevices; i++)
                {
                    PortAudio.PaDeviceInfo info = PortAudio.Pa_GetDeviceInfo(i);
                    if (info.hostApi == hostAPI && info.maxInputChannels > 0)
                    {
                        SRecordDevice dev = new SRecordDevice();

                        dev.ID     = i;
                        dev.Name   = info.name;
                        dev.Driver = info.name + i.ToString();
                        dev.Inputs = new List <SInput>();

                        SInput inp = new SInput();
                        inp.Name = "Default";

                        inp.Channels = info.maxInputChannels;
                        if (inp.Channels > 2)
                        {
                            inp.Channels = 2; //more are not supported in vocaluxe
                        }
                        dev.Inputs.Add(inp);
                        _Devices.Add(dev);
                    }
                }

                _recHandle = new IntPtr[_Devices.Count];
                _myRecProc = new PortAudio.PaStreamCallbackDelegate(myPaStreamCallback);

                _DeviceConfig = _Devices.ToArray();
            }
            catch (Exception e)
            {
                _initialized = false;
                CLog.LogError("Error initializing PortAudio: " + e.Message);
                return(false);
            }

            return(true);
        }
Esempio n. 6
0
        public int Open(string FileName)
        {
            if (_FileOpened)
            {
                return(-1);
            }

            if (!System.IO.File.Exists(FileName))
            {
                return(-1);
            }

            if (_FileOpened)
            {
                return(-1);
            }

            _Decoder = new CAudioDecoderFFmpeg();
            _Decoder.Init();

            try
            {
                if (errorCheck("Initialize", PortAudio.Pa_Initialize()))
                {
                    return(-1);
                }

                _Initialized = true;
                int hostApi = apiSelect();
                _apiInfo          = PortAudio.Pa_GetHostApiInfo(hostApi);
                _outputDeviceInfo = PortAudio.Pa_GetDeviceInfo(_apiInfo.defaultOutputDevice);
                _paStreamCallback = new PortAudio.PaStreamCallbackDelegate(_PaStreamCallback);

                if (_outputDeviceInfo.defaultLowOutputLatency < 0.1)
                {
                    _outputDeviceInfo.defaultLowOutputLatency = 0.1;
                }
            }

            catch (Exception)
            {
                _Initialized = false;
                CLog.LogError("Error Init PortAudio Playback");
                return(-1);
            }

            _FileName = FileName;
            _Decoder.Open(FileName);
            _Duration = _Decoder.GetLength();

            FormatInfo format = _Decoder.GetFormatInfo();

            _ByteCount      = 2 * format.ChannelCount;
            _BytesPerSecond = format.SamplesPerSecond * _ByteCount;
            _CurrentTime    = 0f;
            _SyncTimer.Time = _CurrentTime;

            AudioStreams stream = new AudioStreams(0);

            IntPtr data = new IntPtr(0);

            PortAudio.PaStreamParameters outputParams = new PortAudio.PaStreamParameters();
            outputParams.channelCount     = format.ChannelCount;
            outputParams.device           = _apiInfo.defaultOutputDevice;
            outputParams.sampleFormat     = PortAudio.PaSampleFormat.paInt16;
            outputParams.suggestedLatency = _outputDeviceInfo.defaultLowOutputLatency;

            errorCheck("OpenDefaultStream", PortAudio.Pa_OpenStream(
                           out _Ptr,
                           IntPtr.Zero,
                           ref outputParams,
                           format.SamplesPerSecond,
                           (uint)CConfig.AudioBufferSize,
                           PortAudio.PaStreamFlags.paNoFlag,
                           _paStreamCallback,
                           data));

            stream.handle = _Ptr.ToInt32();

            if (stream.handle != 0)
            {
                _Paused                 = true;
                _waiting                = true;
                _FileOpened             = true;
                _data                   = new RingBuffer(BUFSIZE);
                _NoMoreData             = false;
                _DecoderThread.Priority = ThreadPriority.Normal;
                _DecoderThread.Name     = Path.GetFileName(FileName);
                _DecoderThread.Start();

                return(stream.handle);
            }
            return(-1);
        }
Esempio n. 7
0
        /// <summary>
        /// Start Voice Capturing
        /// </summary>
        /// <param name="DeviceConfig"></param>
        /// <returns></returns>
        public bool Start(SRecordDevice[] DeviceConfig)
        {
            if (!_initialized)
            {
                return(false);
            }

            if (DeviceConfig == null)
            {
                return(false);
            }

            if (_recHandle == null)
            {
                return(false);
            }

            if (_recHandle.Length == 0)
            {
                return(false);
            }

            for (int i = 0; i < _Buffer.Length; i++)
            {
                _Buffer[i].Reset();
            }

            for (int i = 0; i < _recHandle.Length; i++)
            {
                _recHandle[i] = IntPtr.Zero;
            }

            _DeviceConfig = DeviceConfig;
            bool[] active = new bool[DeviceConfig.Length];
            for (int dev = 0; dev < DeviceConfig.Length; dev++)
            {
                active[dev] = false;
                for (int inp = 0; inp < DeviceConfig[dev].Inputs.Count; inp++)
                {
                    if (DeviceConfig[dev].Inputs[inp].PlayerChannel1 > 0 ||
                        DeviceConfig[dev].Inputs[inp].PlayerChannel2 > 0)
                    {
                        active[dev] = true;
                    }
                }
            }

            bool result = true;

            for (int i = 0; i < _recHandle.Length; i++)
            {
                if (active[i])
                {
                    PortAudio.PaStreamParameters inputParams = new PortAudio.PaStreamParameters();
                    inputParams.channelCount     = _DeviceConfig[i].Inputs[0].Channels;
                    inputParams.device           = _DeviceConfig[i].ID;
                    inputParams.sampleFormat     = PortAudio.PaSampleFormat.paInt16;
                    inputParams.suggestedLatency = PortAudio.Pa_GetDeviceInfo(_DeviceConfig[i].ID).defaultLowInputLatency;

                    if (errorCheck("OpenStream", PortAudio.Pa_OpenStream(
                                       out _recHandle[i],
                                       ref inputParams,
                                       IntPtr.Zero,
                                       44100,
                                       882,
                                       PortAudio.PaStreamFlags.paNoFlag,
                                       _myRecProc,
                                       new IntPtr(i))))
                    {
                        return(false);
                    }

                    if (errorCheck("Start Stream", PortAudio.Pa_StartStream(_recHandle[i])))
                    {
                        return(false);
                    }
                }
            }
            return(result);
        }