unsafe public override bool RecordStart(Sound sound)
        {
            criticalSection.Enter();

            DirectCaptureSound captureSound = sound as DirectCaptureSound;

            if (captureSound == null)
            {
                criticalSection.Leave();
                DirectSoundWorld.Warning("Recording failed. Is sound a not for recording.");
                return(false);
            }

            captureSound.readPosition = 0;

            int hr = IDirectSoundCaptureBuffer.Start(captureSound.captureBuffer,
                                                     DSound.DSCBSTART_LOOPING);

            if (Wrapper.FAILED(hr))
            {
                criticalSection.Leave();
                DirectSoundWorld.Warning("IDirectSoundCaptureBuffer.Start", hr);
                return(false);
            }

            recordingSound = captureSound;

            criticalSection.Leave();

            return(true);
        }
        unsafe public override void RecordStop()
        {
            criticalSection.Enter();

            if (recordingSound != null)
            {
                int hr = IDirectSoundCaptureBuffer.Stop(recordingSound.captureBuffer);
                if (Wrapper.FAILED(hr))
                {
                    DirectSoundWorld.Warning("IDirectSoundCaptureBuffer.Stop", hr);
                }
                recordingSound = null;
            }

            criticalSection.Leave();
        }
Exemple #3
0
        unsafe protected override void OnDispose()
        {
            DirectSoundWorld.criticalSection.Enter();

            if (captureBuffer != null)
            {
                IDirectSoundCaptureBuffer.Release(captureBuffer);
                captureBuffer = null;
            }

            if (soundCapture != null)
            {
                IDirectSoundCapture8.Release(soundCapture);
                soundCapture = null;
            }

            DirectSoundWorld.criticalSection.Leave();

            base.OnDispose();
        }
Exemple #4
0
        unsafe public override int RecordRead(byte[] buffer, int length)
        {
            DirectSoundWorld.criticalSection.Enter();

            int hr;

            uint dwBufferPosition;

            hr = IDirectSoundCaptureBuffer.GetCurrentPosition(captureBuffer,
                                                              (uint *)null, &dwBufferPosition);
            if (Wrapper.FAILED(hr))
            {
                DirectSoundWorld.criticalSection.Leave();
                return(0);
            }
            int bufferPosition = (int)dwBufferPosition;

            int bytesAvailable;

            if (bufferPosition >= readPosition)
            {
                bytesAvailable = bufferPosition - readPosition;
            }
            else
            {
                bytesAvailable = (bufferSize - readPosition) + bufferPosition;
            }

            int needLength = Math.Min(length, bytesAvailable);

            if (needLength == 0)
            {
                DirectSoundWorld.criticalSection.Leave();
                return(0);
            }

            void *lockedBuffer      = null;
            uint  lockedBufferSize  = 0;
            void *lockedBuffer2     = null;
            uint  lockedBufferSize2 = 0;

            int startPosition = readPosition - needLength;

            if (startPosition < 0)
            {
                startPosition += bufferSize;
            }

            hr = IDirectSoundCaptureBuffer.Lock(captureBuffer, (uint)startPosition, (uint)needLength,
                                                &lockedBuffer, &lockedBufferSize, &lockedBuffer2, &lockedBufferSize2, 0);
            if (Wrapper.FAILED(hr))
            {
                DirectSoundWorld.criticalSection.Leave();
                DirectSoundWorld.Warning("IDirectSoundCaptureBuffer.Lock", hr);
                return(0);
            }

            if (lockedBuffer != null && lockedBufferSize != 0)
            {
                Marshal.Copy((IntPtr)lockedBuffer, buffer, 0, (int)lockedBufferSize);
            }
            if (lockedBuffer2 != null && lockedBufferSize2 != 0)
            {
                Marshal.Copy((IntPtr)lockedBuffer2, buffer,
                             (int)lockedBufferSize, (int)lockedBufferSize2);
            }

            IDirectSoundCaptureBuffer.Unlock(captureBuffer, lockedBuffer, lockedBufferSize,
                                             lockedBuffer2, lockedBufferSize2);

            readPosition += needLength;
            if (readPosition >= bufferSize)
            {
                readPosition -= bufferSize;
            }

            DirectSoundWorld.criticalSection.Leave();

            return((int)lockedBufferSize + (int)lockedBufferSize2);
        }
Exemple #5
0
        unsafe public DirectCaptureSound(SoundMode mode, int channels, int frequency, int bufferSize)
        {
            SoundMode newMode = mode | SoundMode.Loop | SoundMode.Software;

            int hr;

            if (DirectSoundWorld.Instance.recordDriverIndex == -1)
            {
                DirectSoundWorld.Warning("Recording failed. No active device.");
                return;
            }
            GUID deviceGuid = DirectSoundWorld.Instance.recordDriverGuids[
                DirectSoundWorld.Instance.recordDriverIndex];

            //soundCapture
            void */*IDirectSoundCapture8*/ tempSoundCapture;

            hr = DSound.DirectSoundCaptureCreate(&deviceGuid, out tempSoundCapture, null);
            if (Wrapper.FAILED(hr))
            {
                DirectSoundWorld.Warning("DirectSoundCaptureCreate", hr);
                return;
            }
            soundCapture = (IDirectSoundCapture8 *)tempSoundCapture;

            //waveFormat
            waveFormat = (WAVEFORMATEX *)NativeUtils.Alloc(NativeMemoryAllocationType.SoundAndVideo,
                                                           sizeof(WAVEFORMATEX));
            NativeUtils.ZeroMemory((IntPtr)waveFormat, sizeof(WAVEFORMATEX));
            waveFormat->wFormatTag      = DSound.WAVE_FORMAT_PCM;
            waveFormat->nChannels       = (ushort)channels;
            waveFormat->nSamplesPerSec  = (uint)frequency;
            waveFormat->wBitsPerSample  = 16;
            waveFormat->nBlockAlign     = (ushort)((waveFormat->nChannels * waveFormat->wBitsPerSample) / 8);
            waveFormat->nAvgBytesPerSec = waveFormat->nSamplesPerSec * waveFormat->nBlockAlign;

            //captureBuffer

            DSCBUFFERDESC bufferDesc = new DSCBUFFERDESC();

            //ZeroMemory( &bufferDesc, sizeof( DSCBUFFERDESC ) );
            bufferDesc.dwSize        = (uint)sizeof(DSCBUFFERDESC);
            bufferDesc.dwBufferBytes = (uint)bufferSize;
            bufferDesc.lpwfxFormat   = waveFormat;

            void */*IDirectSoundCaptureBuffer*/ tempCaptureBuffer;

            hr = IDirectSoundCapture8.CreateCaptureBuffer(soundCapture,
                                                          ref bufferDesc, out tempCaptureBuffer, null);
            if (Wrapper.FAILED(hr))
            {
                DirectSoundWorld.Warning("CreateCaptureBuffer", hr);
                IDirectSoundCapture8.Release(soundCapture);
                soundCapture = null;
                return;
            }
            captureBuffer = (IDirectSoundCaptureBuffer *)tempCaptureBuffer;

            //get bufferSize
            DSCBCAPS bufferCaps = new DSCBCAPS();

            //ZeroMemory( &bufferCaps, sizeof( DSCBCAPS ) );
            bufferCaps.dwSize = (uint)sizeof(DSCBCAPS);
            IDirectSoundCaptureBuffer.GetCaps(captureBuffer, ref bufferCaps);
            this.bufferSize = (int)bufferCaps.dwBufferBytes;

            Init(null, newMode, 100000.0f, channels, frequency);
        }