/// <summary>
        /// Helper function to retrieve a WaveFormat structure from a pointer
        /// </summary>
        /// <param name="pointer">WaveFormat structure</param>
        /// <returns></returns>
        public static WaveFormatProvider MarshalFromPtr(IntPtr pointer)
        {
            WaveFormatProvider waveFormat = (WaveFormatProvider)Marshal.PtrToStructure(pointer, typeof(WaveFormatProvider));

            switch (waveFormat.Encoding)
            {
            case WaveFormatEncoding.Pcm:
                // can't rely on extra size even being there for PCM so blank it to avoid reading
                // corrupt data
                waveFormat.extraSize = 0;
                break;

            case WaveFormatEncoding.Extensible:
                waveFormat = (WaveFormatExtensible)Marshal.PtrToStructure(pointer, typeof(WaveFormatExtensible));
                break;

            case WaveFormatEncoding.Adpcm:
                waveFormat = (AdpcmWaveFormat)Marshal.PtrToStructure(pointer, typeof(AdpcmWaveFormat));
                break;

            case WaveFormatEncoding.Gsm610:
                waveFormat = (Gsm610WaveFormat)Marshal.PtrToStructure(pointer, typeof(Gsm610WaveFormat));
                break;

            default:
                if (waveFormat.ExtraSize > 0)
                {
                    waveFormat = (WaveFormatExtraData)Marshal.PtrToStructure(pointer, typeof(WaveFormatExtraData));
                }
                break;
            }
            return(waveFormat);
        }
        /// <summary>
        /// Creates a WaveFormat with custom members
        /// </summary>
        /// <param name="tag">The encoding</param>
        /// <param name="sampleRate">Sample Rate</param>
        /// <param name="channels">Number of channels</param>
        /// <param name="averageBytesPerSecond">Average Bytes Per Second</param>
        /// <param name="blockAlign">Block Align</param>
        /// <param name="bitsPerSample">Bits Per Sample</param>
        /// <returns></returns>
        public static WaveFormatProvider CreateCustomFormat(WaveFormatEncoding tag, int sampleRate, int channels, int averageBytesPerSecond, int blockAlign, int bitsPerSample)
        {
            WaveFormatProvider waveFormat = new WaveFormatProvider();

            waveFormat.waveFormatTag         = tag;
            waveFormat.channels              = (short)channels;
            waveFormat.sampleRate            = sampleRate;
            waveFormat.averageBytesPerSecond = averageBytesPerSecond;
            waveFormat.blockAlign            = (short)blockAlign;
            waveFormat.bitsPerSample         = (short)bitsPerSample;
            waveFormat.extraSize             = 0;
            return(waveFormat);
        }
        /// <summary>
        /// Creates a new 32 bit IEEE floating point wave format
        /// </summary>
        /// <param name="sampleRate">sample rate</param>
        /// <param name="channels">number of channels</param>
        public static WaveFormatProvider CreateIeeeFloatWaveFormat(int sampleRate, int channels)
        {
            WaveFormatProvider wf = new WaveFormatProvider();

            wf.waveFormatTag         = WaveFormatEncoding.IeeeFloat;
            wf.channels              = (short)channels;
            wf.bitsPerSample         = 32;
            wf.sampleRate            = sampleRate;
            wf.blockAlign            = (short)(4 * channels);
            wf.averageBytesPerSecond = sampleRate * wf.blockAlign;
            wf.extraSize             = 0;
            return(wf);
        }
Beispiel #4
0
        /// <summary>
        /// Open the recorder.
        /// </summary>
        /// <param name="outStream">The stream to write the audio to.</param>
        /// <param name="format">The audio wave format.</param>
        /// <param name="audioRecordingFormat">The audio recording format.</param>
        public void Open(System.IO.Stream outStream, WaveFormatProvider format = null, AudioRecordingFormat audioRecordingFormat = AudioRecordingFormat.WaveIn)
        {
            // If not created.
            if (!_waveInCreated)
            {
                _audioStream    = outStream;
                _internalStream = false;
                _isBufferStream = false;

                // Initialise.
                Init(format, audioRecordingFormat);
                _waveInCreated = true;
            }
        }
Beispiel #5
0
        internal MmResult WaveOutOpen(out IntPtr waveOutHandle, int deviceNumber, WaveFormatProvider waveFormat, WaveInterop.WaveCallback callback)
        {
            MmResult result;

            if (Strategy == WaveCallbackStrategy.FunctionCallback)
            {
                result = WaveInterop.waveOutOpen(out waveOutHandle, (IntPtr)deviceNumber, waveFormat, callback, IntPtr.Zero, WaveInOutOpenFlags.CallbackFunction);
            }
            else
            {
                result = WaveInterop.waveOutOpenWindow(out waveOutHandle, (IntPtr)deviceNumber, waveFormat, this.Handle, IntPtr.Zero, WaveInOutOpenFlags.CallbackWindow);
            }
            return(result);
        }
        /// <summary>
        /// Compares with another WaveFormat object
        /// </summary>
        /// <param name="obj">Object to compare to</param>
        /// <returns>True if the objects are the same</returns>
        public override bool Equals(object obj)
        {
            WaveFormatProvider other = obj as WaveFormatProvider;

            if (other != null)
            {
                return(waveFormatTag == other.waveFormatTag &&
                       channels == other.channels &&
                       sampleRate == other.sampleRate &&
                       averageBytesPerSecond == other.averageBytesPerSecond &&
                       blockAlign == other.blockAlign &&
                       bitsPerSample == other.bitsPerSample);
            }
            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// Open the recorder.
        /// </summary>
        /// <param name="filename">The path and filename of the file to write the audio to.</param>
        /// <param name="format">The audio wave format.</param>
        /// <param name="audioRecordingFormat">The audio recording format.</param>
        public void Open(string filename, WaveFormatProvider format = null, AudioRecordingFormat audioRecordingFormat = AudioRecordingFormat.WaveIn)
        {
            // If not created.
            if (!_waveInCreated)
            {
                _filename       = filename;
                _audioStream    = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read);
                _internalStream = true;
                _isBufferStream = false;

                // Initialise.
                Init(format, audioRecordingFormat);
                _waveInCreated = true;
            }
        }
Beispiel #8
0
 public static extern MmResult waveInOpenWindow(out IntPtr hWaveIn, IntPtr uDeviceID, WaveFormatProvider lpFormat, IntPtr callbackWindowHandle, IntPtr dwInstance, WaveInOutOpenFlags dwFlags);
Beispiel #9
0
 public static extern MmResult waveInOpen(out IntPtr hWaveIn, IntPtr uDeviceID, WaveFormatProvider lpFormat, WaveCallback dwCallback, IntPtr dwInstance, WaveInOutOpenFlags dwFlags);
Beispiel #10
0
        /// <summary>
        /// Initialise the waveIn stream.
        /// </summary>
        /// <param name="format">The audio wave format.</param>
        /// <param name="audioRecordingFormat">The audio recording format.</param>
        private void Init(WaveFormatProvider format, AudioRecordingFormat audioRecordingFormat)
        {
            _audioFormat = audioRecordingFormat;

            // Get all the catpure devices.
            MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
            MMDeviceCollection devices = DevEnum.EnumerateAudioEndPoints(EDataFlow.Capture, EDeviceState.Active);

            // Select the device index.
            _mmDevice = devices[_device.Index];

            // If null the setup defaults.
            if (format == null)
            {
                // Select the provider.
                switch (audioRecordingFormat)
                {
                case AudioRecordingFormat.WasapiLoopback:
                    _waveIn = new WasapiLoopbackCapture(_mmDevice);
                    break;

                case AudioRecordingFormat.Wasapi:
                    _waveIn            = new WasapiCapture(_mmDevice);
                    _waveIn.WaveFormat = new WaveFormatProvider(8000, 16, 1);
                    break;

                case AudioRecordingFormat.WaveInEvent:
                    _waveIn = new WaveInEvent()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = new WaveFormatProvider(8000, 16, 1);
                    break;

                case AudioRecordingFormat.WaveIn:
                default:
                    _waveIn = new WaveIn()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = new WaveFormatProvider(8000, 16, 1);
                    break;
                }
            }
            else
            {
                // Select the provider.
                switch (audioRecordingFormat)
                {
                case AudioRecordingFormat.WasapiLoopback:
                    _waveIn = new WasapiLoopbackCapture(_mmDevice);
                    break;

                case AudioRecordingFormat.Wasapi:
                    _waveIn            = new WasapiCapture(_mmDevice);
                    _waveIn.WaveFormat = format;
                    break;

                case AudioRecordingFormat.WaveInEvent:
                    _waveIn = new WaveInEvent()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = format;
                    break;

                case AudioRecordingFormat.WaveIn:
                default:
                    _waveIn = new WaveIn()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = format;
                    break;
                }
            }

            // Set the capture.
            _waveIn.DataAvailable    += _waveIn_DataAvailable;
            _waveIn.RecordingStopped += _waveIn_RecordingStopped;
        }