Exemple #1
0
 public Audio(int inputChannels, int outputChannels, int frequency, uint framesPerBuffer,
     PortAudio.PaStreamCallbackDelegate paStreamCallback)
 {
     log("Initializing...");
      		this.inputChannels = inputChannels;
      		this.outputChannels = outputChannels;
      		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());
 }
Exemple #2
0
        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.paFloat32;
            inputParams.suggestedLatency = this.inputDeviceInfo.defaultLowInputLatency;
            PortAudio.PaStreamParameters outputParams = new PortAudio.PaStreamParameters();
            outputParams.channelCount     = outputChannels;
            outputParams.device           = outputDevice;
            outputParams.sampleFormat     = PortAudio.PaSampleFormat.paFloat32;
            outputParams.suggestedLatency = this.outputDeviceInfo.defaultLowOutputLatency;
            errorCheck("OpenDefaultStream", PortAudio.Pa_OpenStream(
                           out stream,
                           ref inputParams,
                           ref outputParams,
                           sampleRate,
                           framesPerBuffer,
                           PortAudio.PaStreamFlags.paNoFlag,
                           this.paStreamCallback,
                           data));
            return(stream);
        }
        private PortAudio.PaStreamCallbackResult myPaStreamCallback(
			IntPtr input,
			IntPtr output,
			uint frameCount,
			ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
			PortAudio.PaStreamCallbackFlags statusFlags,
			IntPtr userData)
        {
            float[] mBuffer = new float[frameCount];

            for (int i = 0; i < frameCount; i++)
            {

                for (int j = 0; j < MAX_CHANELS; j++)
                {
                    float f = 0;
                    if (_channels[j].TryDequeue(out  f))
                    {
                        mBuffer[i] = mBuffer[i] + f;
                    }
                }
            }
            Marshal.Copy(mBuffer, 0, output, (int)frameCount);
            return PortAudio.PaStreamCallbackResult.paContinue;
        }
        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;
        }
 internal unsafe PortAudio.PaStreamCallbackResult CallBack(IntPtr inputBuffer, IntPtr outputBuffer,
     uint framesPerBuffer, ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
     PortAudio.PaStreamCallbackFlags statusFlags, IntPtr userData)
 {
     CallBack(framesPerBuffer, (float*)outputBuffer.ToPointer());
     return PortAudio.PaStreamCallbackResult.paContinue;
 }
        public PortAudio.PaStreamCallbackResult myPaStreamCallback(
            IntPtr input,
            IntPtr output,
            uint frameCount,
            ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
            PortAudio.PaStreamCallbackFlags statusFlags,
            IntPtr userData)
        {
            try {
             			// log("Callback called");
             		// log("time: " + timeInfo.currentTime
             		// 		+ " " + timeInfo.inputBufferAdcTime
             		//		+ " " + timeInfo.outputBufferDacTime);
             		// log("statusFlags: "+statusFlags);

             		if (callbackBuffer.Length < frameCount*2)
             			callbackBuffer = new float[frameCount*2];

             		for (int j = 0; j < frameCount*2; j++)
                    callbackBuffer[j] = (float) Math.Sin((double)(callbackPos++)/20.0);

                Marshal.Copy(callbackBuffer, 0, output, (int)frameCount*2);

             		} catch (Exception e) {
             			Console.WriteLine(e.ToString());
             		}

             		return PortAudio.PaStreamCallbackResult.paContinue;
        }
        public HostApiItem(PortAudio.PaHostApiInfo hostApiInfo, IUpdatableControl updatableControl)
        {
            this.hostApiInfo = hostApiInfo;

                int deviceCount = PortAudio.Pa_GetDeviceCount();
                int selectedHostApiDeviceCount = 0;
                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 == hostApiInfo.type) { selectedHostApiDeviceCount++; }
                }

                if (selectedHostApiDeviceCount > 0) {
                    switch(hostApiInfo.type) {
                    case PortAudio.PaHostApiTypeId.paMME:
                        hostApiDeviceControl = new MMEDeviceControl(hostApiInfo, updatableControl);
                        break;
                    case PortAudio.PaHostApiTypeId.paDirectSound:
                        hostApiDeviceControl = new DirectSoundDeviceControl(hostApiInfo, updatableControl);
                        break;
                    case PortAudio.PaHostApiTypeId.paASIO:
                        hostApiDeviceControl = new ASIODeviceControl(hostApiInfo, updatableControl);
                        break;
                    case PortAudio.PaHostApiTypeId.paALSA:
                        hostApiDeviceControl = new ALSADeviceControl(hostApiInfo, updatableControl);
                        break;
                    }
                } else {
                    hostApiDeviceControl = new NoDevicesDeviceControl(hostApiInfo, updatableControl);
                }
        }
Exemple #8
0
        public Audio(int inputChannels, int outputChannels, int frequency, uint framesPerBuffer,
                     PortAudio.PaStreamCallbackDelegate paStreamCallback)
        {
            log("Initializing...");
            this.inputChannels    = inputChannels;
            this.outputChannels   = outputChannels;
            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());
        }
        public MMEDeviceControl(PortAudio.PaHostApiInfo paHostApiInfo, IUpdatableControl updatableControl)
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
            this.paHostApiInfo = paHostApiInfo;
            this.updatableControl = updatableControl;
        }
Exemple #10
0
        /*
         * private void streamWrite(IntPtr stream, float[] buffer) {
         *      errorCheck("WriteStream",PortAudio.Pa_WriteStream(
         *              stream,buffer,(uint)(buffer.Length/2)));
         * }
         */

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // Dispose here any managed resources
                }

                // Dispose here any unmanaged resources
                log("Terminating...");
                errorCheck("Terminate", PortAudio.Pa_Terminate());
            }
            this.disposed = true;
        }
Exemple #11
0
        private int apiSelect()
        {
            int selectedHostApi = PortAudio.Pa_GetDefaultHostApi();
            int apiCount        = PortAudio.Pa_GetHostApiCount();

            for (int i = 0; i < apiCount; i++)
            {
                PortAudio.PaHostApiInfo apiInfo = PortAudio.Pa_GetHostApiInfo(i);
                if ((apiInfo.type == PortAudio.PaHostApiTypeId.paDirectSound) ||
                    (apiInfo.type == PortAudio.PaHostApiTypeId.paALSA))
                {
                    selectedHostApi = i;
                }
            }
            return(selectedHostApi);
        }
Exemple #12
0
        public HostApiItem(PortAudio.PaHostApiInfo hostApiInfo, IUpdatableControl updatableControl)
        {
            this.hostApiInfo = hostApiInfo;

            int deviceCount = PortAudio.Pa_GetDeviceCount();
            int selectedHostApiDeviceCount = 0;

            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 == hostApiInfo.type)
                {
                    selectedHostApiDeviceCount++;
                }
            }

            if (selectedHostApiDeviceCount > 0)
            {
                switch (hostApiInfo.type)
                {
                case PortAudio.PaHostApiTypeId.paMME:
                    hostApiDeviceControl = new MMEDeviceControl(hostApiInfo, updatableControl);
                    break;

                case PortAudio.PaHostApiTypeId.paDirectSound:
                    hostApiDeviceControl = new DirectSoundDeviceControl(hostApiInfo, updatableControl);
                    break;

                case PortAudio.PaHostApiTypeId.paASIO:
                    hostApiDeviceControl = new ASIODeviceControl(hostApiInfo, updatableControl);
                    break;

                case PortAudio.PaHostApiTypeId.paALSA:
                    hostApiDeviceControl = new ALSADeviceControl(hostApiInfo, updatableControl);
                    break;
                }
            }
            else
            {
                hostApiDeviceControl = new NoDevicesDeviceControl(hostApiInfo, updatableControl);
            }
        }
        public PortAudio.PaStreamCallbackResult recordCallback(
	 		IntPtr input,
	 		IntPtr output,
	 		uint frameCount, 
	 		ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
	 		PortAudio.PaStreamCallbackFlags statusFlags,
	 		IntPtr userData)
        {
            try
            {
                float[] callbackBuffer = new float[frameCount];
                Marshal.Copy(input, callbackBuffer, 0, (int)frameCount);
                samplesDelegate(callbackBuffer);
            }
            catch (Exception e)
            {
             			Console.WriteLine(e.ToString());
             		}
             		return PortAudio.PaStreamCallbackResult.paContinue;
        }
Exemple #14
0
 private bool errorCheck(String action, PortAudio.PaError errorCode)
 {
     if (errorCode != PortAudio.PaError.paNoError)
     {
         log(action + " error: " + PortAudio.Pa_GetErrorText(errorCode));
         if (errorCode == PortAudio.PaError.paUnanticipatedHostError)
         {
             PortAudio.PaHostErrorInfo errorInfo = PortAudio.Pa_GetLastHostErrorInfo();
             log("- Host error API type: " + errorInfo.hostApiType);
             log("- Host error code: " + errorInfo.errorCode);
             log("- Host error text: " + errorInfo.errorText);
         }
         return(true);
     }
     else
     {
         log(action + " OK");
         return(false);
     }
 }
Exemple #15
0
        void AudioSettingsControlLoad(object sender, EventArgs e)
        {
            driverTypeComboBox.Items.Clear();
            int hostApiCount = PortAudio.Pa_GetHostApiCount();

            for (int i = 0; i < hostApiCount; i++)
            {
                PortAudio.PaHostApiInfo hostApiInfo = PortAudio.Pa_GetHostApiInfo(i);
                if (hostApiInfo.type != PortAudio.PaHostApiTypeId.paInDevelopment)
                {
                    driverTypeComboBox.Items.Add(new HostApiItem(hostApiInfo, this));
                }
            }
            driverTypeComboBox.SelectedIndex = PortAudio.Pa_GetDefaultHostApi();

            sampleRateComboBox.Items.Clear();
            sampleRateComboBox.Items.Add(192000);
            sampleRateComboBox.Items.Add(176400);
            sampleRateComboBox.Items.Add(96000);
            sampleRateComboBox.Items.Add(88200);
            sampleRateComboBox.Items.Add(48000);
            sampleRateComboBox.Items.Add(44100);
            sampleRateComboBox.Items.Add(38400);
            sampleRateComboBox.Items.Add(37800);
            sampleRateComboBox.Items.Add(32000);
            sampleRateComboBox.Items.Add(24000);
            sampleRateComboBox.Items.Add(22050);
            sampleRateComboBox.Items.Add(19200);
            sampleRateComboBox.Items.Add(18900);
            sampleRateComboBox.Items.Add(16000);
            sampleRateComboBox.Items.Add(12000);
            sampleRateComboBox.Items.Add(11025);
            sampleRateComboBox.Items.Add(9600);
            sampleRateComboBox.Items.Add(8000);
            sampleRateComboBox.SelectedIndex = 5;

            update();
        }
Exemple #16
0
        public PortAudio.PaStreamCallbackResult myPaStreamCallback(
            IntPtr input,
            IntPtr output,
            uint frameCount,
            ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
            PortAudio.PaStreamCallbackFlags statusFlags,
            IntPtr userData)
        {
            try
            {
                frameCount *= 4;
                if (frameCount > 0 && input != IntPtr.Zero)
                {
                    byte[] _recbuffer = new byte[frameCount];
                    byte[] _leftBuffer = new byte[frameCount / 2];
                    byte[] _rightBuffer = new byte[frameCount / 2];

                    // copy from managed to unmanaged memory
                    Marshal.Copy(input, _recbuffer, 0, (int)frameCount);

                    // copy into left/right Buffer
                    for (int i = 0; i < frameCount / 2; i++)
                    {
                        _leftBuffer[i] = _recbuffer[i * 2 - (i % 2)];
                        _rightBuffer[i] = _recbuffer[i * 2 - (i % 2) + 2];
                    }

                    for (int i = 0; i < _recHandle.Length; i++)
                    {
                        if (new IntPtr(i) == userData)
                        {
                            if (_DeviceConfig[i].Inputs[0].PlayerChannel1 > 0)
                                _Buffer[_DeviceConfig[i].Inputs[0].PlayerChannel1 - 1].ProcessNewBuffer(_leftBuffer);

                            if (_DeviceConfig[i].Inputs[0].PlayerChannel2 > 0)
                                _Buffer[_DeviceConfig[i].Inputs[0].PlayerChannel2 - 1].ProcessNewBuffer(_rightBuffer);

                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                CLog.LogError("Error on Stream Callback: " + e.ToString());
            }

            return PortAudio.PaStreamCallbackResult.paContinue;
        }
 public DeviceItem(int deviceIndex, PortAudio.PaDeviceInfo deviceInfo)
 {
     this.deviceIndex = deviceIndex;
     this.deviceInfo = deviceInfo;
 }
Exemple #18
0
 private void streamStop(IntPtr stream)
 {
     errorCheck("StopStream", PortAudio.Pa_StopStream(stream));
 }
Exemple #19
0
        /*
         * private IntPtr streamOpen(int inputChannels,int outputChannels,
         *      int sampleRate, uint framesPerBuffer) {
         *      IntPtr stream = new IntPtr();
         *      IntPtr data = new IntPtr(0);
         *      errorCheck("OpenDefaultStream",PortAudio.Pa_OpenDefaultStream(
         *          out stream,
         *          inputChannels,
         *          outputChannels,
         *          (uint) PortAudio.PaSampleFormat.paFloat32,
         *          sampleRate,
         *          framesPerBuffer,
         *          this.paStreamCallback,
         *          data));
         *      return stream;
         * }
         */

        private void streamClose(IntPtr stream)
        {
            errorCheck("CloseStream", PortAudio.Pa_CloseStream(stream));
        }
 private void OutputError(PortAudio.PaErrorCode err)
 {
     SendErrorMsg(PortAudio.Pa_GetErrorText(err));
 }
        void DeviceSettingsButtonClick(object sender, EventArgs e)
        {
            DeviceItem deviceItem = (DeviceItem)deviceComboBox.Items[deviceComboBox.SelectedIndex];

            PortAudio.PaAsio_ShowControlPanel(deviceItem.DeviceIndex, new IntPtr(this.Handle.ToInt64()));
        }
Exemple #22
0
 private bool errorCheck(String action, PortAudio.PaError errorCode)
 {
     if (errorCode != PortAudio.PaError.paNoError) {
      			log(action + " error: " + PortAudio.Pa_GetErrorText(errorCode));
      			if (errorCode == PortAudio.PaError.paUnanticipatedHostError) {
      				PortAudio.PaHostErrorInfo errorInfo = PortAudio.Pa_GetLastHostErrorInfo();
      				log("- Host error API type: " + errorInfo.hostApiType);
      				log("- Host error code: " + errorInfo.errorCode);
      				log("- Host error text: " + errorInfo.errorText);
      			}
      			return true;
      		} else {
      			log(action + " OK");
      			return false;
      		}
 }
Exemple #23
0
        private bool errorCheck(String action, PortAudio.PaError errorCode)
        {
            if (errorCode != PortAudio.PaError.paNoError)
            {
                if (errorCode == PortAudio.PaError.paStreamIsNotStopped)
                    return false;

                CLog.LogError(action + " error: " + PortAudio.Pa_GetErrorText(errorCode));
                if (errorCode == PortAudio.PaError.paUnanticipatedHostError)
                {
                    PortAudio.PaHostErrorInfo errorInfo = PortAudio.Pa_GetLastHostErrorInfo();
                    CLog.LogError("- Host error API type: " + errorInfo.hostApiType);
                    CLog.LogError("- Host error code: " + errorInfo.errorCode);
                    CLog.LogError("- Host error text: " + errorInfo.errorText);
                }
                return true;
            }

            return false;
        }
Exemple #24
0
        private PortAudio.PaStreamCallbackResult _PaStreamCallback(
            IntPtr input,
            IntPtr output,
            uint frameCount,
            ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
            PortAudio.PaStreamCallbackFlags statusFlags,
            IntPtr userData)
        {
            byte[] buf = new byte[frameCount * _ByteCount];

            if (_Paused)
            {
                try
                {
                    Marshal.Copy(buf, 0, output, (int)frameCount * _ByteCount);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                return PortAudio.PaStreamCallbackResult.paContinue;
            }

            lock (_LockData)
            {
                if (_NoMoreData || _data.BytesNotRead >= buf.Length)
                {
                    _data.Read(ref buf);

                    byte[] b = new byte[2];
                    for (int i = 0; i < buf.Length; i += _ByteCount)
                    {
                        b[0] = buf[i];
                        b[1] = buf[i + 1];

                        b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax));
                        buf[i] = b[0];
                        buf[i + 1] = b[1];

                        if (_ByteCount == 4)
                        {
                            b[0] = buf[i + 2];
                            b[1] = buf[i + 3];

                            b = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(b, 0) * _Volume * _VolumeMax));
                            buf[i + 2] = b[0];
                            buf[i + 3] = b[1];
                        }
                    }
                }

                if (_data.BytesNotRead < BUFSIZE - 10000L)
                {
                    EventDecode.Set();
                    _waiting = false;
                }
                else
                    _waiting = true;

                float latency = buf.Length / _BytesPerSecond + CConfig.AudioLatency/1000f;
                float time = _TimeCode - _data.BytesNotRead / _BytesPerSecond - latency;

                if (!_NoMoreData)
                    _CurrentTime = _SyncTimer.Update(time);
            }
  
            try
            {
                Marshal.Copy(buf, 0, output, (int)frameCount * _ByteCount);
            }
            catch (Exception e)
            {
                CLog.LogError("Error PortAudio.StreamCallback: " + e.Message);
            }

            return PortAudio.PaStreamCallbackResult.paContinue;
        }