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(); }
private IntPtr streamOpen(int inputDevice, int inputChannels, int outputDevice, int outputChannels, int sampleRate, uint framesPerBuffer) { IntPtr stream = new IntPtr(); IntPtr data = new IntPtr(0); PortAudio.PaStreamParameters inputParams = new PortAudio.PaStreamParameters(); inputParams.channelCount = inputChannels; inputParams.device = inputDevice; inputParams.sampleFormat = PortAudio.PaSampleFormat.paInt16; inputParams.suggestedLatency = 0.0; PortAudio.PaStreamParameters outputParams = new PortAudio.PaStreamParameters(); outputParams.channelCount = outputChannels; outputParams.device = outputDevice; outputParams.sampleFormat = PortAudio.PaSampleFormat.paInt16; outputParams.suggestedLatency = 0.0; errorCheck("OpenDefaultStream", PortAudio.Pa_OpenStream( out stream, ref inputParams, ref outputParams, sampleRate, framesPerBuffer, PortAudio.PaStreamFlags.paNoFlag, this.paStreamCallback, data)); return(stream); }
private void button1_Click(object sender, EventArgs e) { if (button1.Text == "Start") { this.Errror.Text = string.Empty; PortAudio.PaStreamParameters outputParameters = new PortAudio.PaStreamParameters(); outputParameters.channelCount = 2; outputParameters.device = deviceNumber; outputParameters.hostApiSpecificStreamInfo = IntPtr.Zero; outputParameters.sampleFormat = PortAudio.PaSampleFormat.paFloat32 | PortAudio.PaSampleFormat.paNonInterleaved; outputParameters.suggestedLatency = 0; int result = PortAudio.NativeMethods.Pa_OpenStream(out this.stream, null, outputParameters, sampleRate, 2048, 0, callback, GCHandle.ToIntPtr(this.thisPtr)); if (result != 0) { this.Errror.Text = PortAudio.NativeMethods.Pa_GetErrorText(result); return; } result = PortAudio.NativeMethods.Pa_StartStream(stream); if (result != 0) { this.Errror.Text = PortAudio.NativeMethods.Pa_GetErrorText(result); PortAudio.NativeMethods.Pa_CloseStream(stream); stream = IntPtr.Zero; return; } button1.Text = "Stop"; this.stopFlag = false; } else { button1.Text = "Start"; this.stopFlag = true; PortAudio.NativeMethods.Pa_StopStream(stream); PortAudio.NativeMethods.Pa_CloseStream(stream); stream = IntPtr.Zero; } }
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); }
private void button1_Click(object sender, EventArgs e) { if (button1.Text == "Start") { PortAudio.PaStreamParameters outputParameters = new PortAudio.PaStreamParameters(); outputParameters.channelCount = 2; outputParameters.device = deviceNumber; outputParameters.hostApiSpecificStreamInfo = IntPtr.Zero; outputParameters.sampleFormat = PortAudio.PaSampleFormat.paFloat32 | PortAudio.PaSampleFormat.paNonInterleaved; outputParameters.suggestedLatency = 0; int result = PortAudio.NativeMethods.Pa_OpenStream(out this.stream, null, outputParameters, sampleRate, 2048, 0, callback, IntPtr.Zero); if (result != 0) { this.Errror.Text = PortAudio.NativeMethods.Pa_GetErrorText(result); return; } result = PortAudio.NativeMethods.Pa_StartStream(stream); if (result != 0) { this.Errror.Text = PortAudio.NativeMethods.Pa_GetErrorText(result); PortAudio.NativeMethods.Pa_CloseStream(stream); stream = IntPtr.Zero; return; } button1.Text = "Stop"; this.stopFlag = false; } else { button1.Text = "Start"; this.stopFlag = true; PortAudio.NativeMethods.Pa_StopStream(stream); PortAudio.NativeMethods.Pa_CloseStream(stream); stream = IntPtr.Zero; } }
/// <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); }