Exemplo n.º 1
0
        public void SetOptions(string deviceName, AudioQuality quality)
        {
            if (IsInited)
            {
                Stop();
                capture.Dispose();
            }

            Initialize(deviceName, quality);
        }
 public void Dispose()
 {
     if (mic != null)
     {
         mic.Pause();
         mic.Dispose();
         mic = null;
     }
     if (cam != null)
     {
         cam.Pause();
         cam.Dispose();
         cam = null;
     }
     if (EncoderRunning)
     {
         Stop();
     }
     if (coreloops != null)
     {
         foreach (var loop in coreloops)
         {
             if (loop == null)
             {
                 continue;
             }
             loop.Join();
         }
         coreloops.Clear();
     }
 }
Exemplo n.º 3
0
 public void StopRecording()
 {
     source.Cancel();
     chunk = null;
     audioCapture.Flush();
     audioCapture.Unprepare();
     audioCapture.Dispose();
     isRecording = false;
 }
Exemplo n.º 4
0
        private void Initialize(string deviceName, AudioQuality quality)
        {
            try
            {
                this.quality     = quality;
                this.samplesSize = DefaultBufferSize;

                ALFormat format;

                if (quality.Channels == 1)
                {
                    format = quality.Bits == 8 ? ALFormat.Mono8 : ALFormat.Mono16;
                }
                else
                {
                    format = quality.Bits == 8 ? ALFormat.Stereo8 : ALFormat.Stereo16;
                }

                lock (syncObj)
                {
                    buffer = new byte[quality.Channels * (quality.Bits / 8) * samplesSize * 2];

                    if (string.IsNullOrEmpty(deviceName))
                    {
                        deviceName = AudioCapture.DefaultDevice;
                    }

                    if (!AudioCapture.AvailableDevices.Contains(deviceName))
                    {
                        deviceName = AudioCapture.DefaultDevice;
                    }

                    capture = new AudioCapture(deviceName, quality.Frequency, format, samplesSize);
                }
            }
            catch (Exception e)
            {
                if (capture != null)
                {
                    capture.Dispose();
                }

                capture = null;

                ClientModel.Logger.Write(e);
                throw new ModelException(ErrorCode.AudioNotEnabled, "Audio recorder do not initialized.", e, deviceName);
            }
        }
Exemplo n.º 5
0
        public void StopEcho()
        {
            if (Capture == null)
            {
                return;
            }
            AL.SourceStop(PlaybackSrc);
            int bufc;

            AL.GetSource(PlaybackSrc, ALGetSourcei.BuffersQueued, out bufc);
            if (bufc > 0)
            {
                int[] bufs = AL.SourceUnqueueBuffers(PlaybackSrc, bufc);
                AL.DeleteBuffers(bufs);
            }
            AL.DeleteSource(PlaybackSrc);
            PlaybackSrc = 0;
            Capture.Stop();
            Capture.Dispose();
            Capture = null;
        }
Exemplo n.º 6
0
 /// <summary>Deinitializes audio.</summary>
 internal static void Deinitialize()
 {
     StopAllSounds();
     UnloadAllBuffers();
     UnloadAllMicBuffers();
     if (OpenAlContext != ContextHandle.Zero)
     {
         Alc.MakeContextCurrent(ContextHandle.Zero);
         Alc.DestroyContext(OpenAlContext);
         OpenAlContext = ContextHandle.Zero;
     }
     if (OpenAlDevice != IntPtr.Zero)
     {
         Alc.CloseDevice(OpenAlDevice);
         OpenAlDevice = IntPtr.Zero;
     }
     if (OpenAlMic != null)
     {
         OpenAlMic.Dispose();
         OpenAlMic = null;
     }
 }
        void StopRecording()
        {
            timer_GetSamples.Stop();

            if (audio_capture != null)
            {
                audio_capture.Stop();
                audio_capture.Dispose();
                audio_capture = null;
            }

            if (audio_context != null)
            {
                int r;
                AL.GetSource(src, ALGetSourcei.BuffersQueued, out r);
                ClearBuffers(r);

                AL.DeleteSource(src);

                audio_context.Dispose();
                audio_context = null;
            }
        }
Exemplo n.º 8
0
 private void ChEnable_CheckedChanged(object sender, EventArgs e)
 {
     if (ChEnable.Checked)
     {
         try
         {
             AudioCapturer = new AudioCapture(AudioCapture.AvailableDevices[LsAudioDevices.SelectedIndex], 8000, ALFormat.Mono8, 256);
             AudioCapturer.Start();
         }
         catch
         {
         }
     }
     else
     {
         if (AudioCapturer != null)
         {
             AudioCapturer.Stop();
             AudioCapturer.Dispose();
             AudioCapturer = null;
             System.Threading.Thread.Sleep(100);
         }
     }
 }
Exemplo n.º 9
0
 public override void Exit()
 {
     capture.Dispose();
 }
Exemplo n.º 10
0
 /// <summary>
 /// 释放资源
 /// </summary>
 public void Dispose()
 {
     _audioPlayer.Dispose();
     _audioCapture.Dispose();
 }
Exemplo n.º 11
0
        private void Initialize(string deviceName, AudioQuality quality)
        {
            try
              {
            this.quality = quality;
            this.samplesSize = DefaultBufferSize;

            ALFormat format;

            if (quality.Channels == 1)
              format = quality.Bits == 8 ? ALFormat.Mono8 : ALFormat.Mono16;
            else
              format = quality.Bits == 8 ? ALFormat.Stereo8 : ALFormat.Stereo16;

            lock (syncObj)
            {
              buffer = new byte[quality.Channels * (quality.Bits / 8) * samplesSize * 2];

              if (string.IsNullOrEmpty(deviceName))
            deviceName = AudioCapture.DefaultDevice;

              if (!AudioCapture.AvailableDevices.Contains(deviceName))
            deviceName = AudioCapture.DefaultDevice;

              capture = new AudioCapture(deviceName, quality.Frequency, format, samplesSize);
            }
              }
              catch (Exception e)
              {
            if (capture != null)
              capture.Dispose();

            capture = null;

            ClientModel.Logger.Write(e);
            throw new ModelException(ErrorCode.AudioNotEnabled, "Audio recorder do not initialized.", e, deviceName);
              }
        }
Exemplo n.º 12
0
        public RecorderDiagnostic()
        {
            Trace.WriteLine("--- AudioCapture related errors ---");
            IsDeviceAvailable = false;

            try
            {
                r = new AudioCapture(AudioCapture.DefaultDevice, 16000, ALFormat.Mono16, 4096);
            }
            catch (AudioDeviceException ade)
            {
                Trace.WriteLine("AudioCapture Exception caught: " + ade.Message);
                return;
            }
            IsDeviceAvailable = true;
            DeviceName = r.CurrentDevice;
            CheckRecorderError("Alc.CaptureOpenDevice");

            r.Start();
            CheckRecorderError("Alc.CaptureStart");
            Thread.Sleep(100);
            r.Stop();
            CheckRecorderError("Alc.CaptureStop");

            byte[] Buffer = new byte[8192];

            Thread.Sleep(10);  // Wait for a few samples to become available.
            int SamplesBefore = r.AvailableSamples;

            CheckRecorderError("Alc.GetInteger(...CaptureSamples...)");
            r.ReadSamples(Buffer, (SamplesBefore > 4096 ? 4096 : SamplesBefore));
            CheckRecorderError("Alc.CaptureSamples");

            int SamplesCaptured = SamplesBefore - r.AvailableSamples;

            uint ZeroCounter = 0;
            for (int i = 0; i < SamplesCaptured * 2; i++)
            {
                if (Buffer[i] == 0)
                    ZeroCounter++;
            }

            for (int i = 0; i < SamplesCaptured; i++)
            {
                short sample = BitConverter.ToInt16(Buffer, i * 2);
                if (sample > MaxSample)
                    MaxSample = sample;
                if (sample < MinSample)
                    MinSample = sample;
            }

            if (ZeroCounter < SamplesCaptured * 2 && SamplesCaptured > 0)
                BufferContentsAllZero = false;
            else
                BufferContentsAllZero = true;

            r.Dispose();
            CheckRecorderError("Alc.CaptureCloseDevice");

            // no playback test needed due to Parrot test app.
            /*
            uint buf;
            AL.GenBuffer(out buf);
            AL.BufferData(buf, ALFormat.Mono16, BufferPtr, SamplesCaptured * 2, 16000);
            uint src;
            AL.GenSource(out src);
            AL.BindBufferToSource(src, buf);
            AL.Listener(ALListenerf.Gain, 16.0f);
            AL.SourcePlay(src);
            while (AL.GetSourceState(src) == ALSourceState.Playing)
            {
                Thread.Sleep(0);
            }
            AL.SourceStop(src);

            AL.DeleteSource(ref src);
            AL.DeleteBuffer(ref buf);
            */
        }
Exemplo n.º 13
0
        // --- initialization and deinitialization ---

        /// <summary>Initializes audio. A call to Deinitialize must be made when terminating the program.</summary>
        /// <returns>Whether initializing audio was successful.</returns>
        public void Initialize(HostInterface host, SoundRange range)
        {
            if (host.Platform == HostPlatform.MicrosoftWindows)
            {
                /*
                 *  If shipping an AnyCPU build and OpenALSoft / SDL, these are architecture specific PInvokes
                 *  Add the appropriate search path so this will work (common convention)
                 */
                string path = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
                if (path != null)
                {
                    path = Path.Combine(path, IntPtr.Size == 4 ? "x86" : "x64");
                    bool ok = SetDllDirectory(path);
                    if (!ok)
                    {
                        throw new System.ComponentModel.Win32Exception();
                    }
                }
            }
            Deinitialize();

            CurrentHost = host;

            switch (range)
            {
            case SoundRange.Low:
                OuterRadiusFactorMinimum      = 2.0;
                OuterRadiusFactorMaximum      = 8.0;
                OuterRadiusFactorMaximumSpeed = 1.0;
                break;

            case SoundRange.Medium:
                OuterRadiusFactorMinimum      = 4.0;
                OuterRadiusFactorMaximum      = 16.0;
                OuterRadiusFactorMaximumSpeed = 2.0;
                break;

            case SoundRange.High:
                OuterRadiusFactorMinimum      = 6.0;
                OuterRadiusFactorMaximum      = 24.0;
                OuterRadiusFactorMaximumSpeed = 3.0;
                break;
            }
            OuterRadiusFactor      = Math.Sqrt(OuterRadiusFactorMinimum * OuterRadiusFactorMaximum);
            OuterRadiusFactorSpeed = 0.0;
            OpenAlDevice           = Alc.OpenDevice(null);
            string deviceName = Alc.GetString(OpenAlDevice, AlcGetString.DefaultDeviceSpecifier);

            if ((Environment.OSVersion.Platform == PlatformID.Win32S | Environment.OSVersion.Platform == PlatformID.Win32Windows | Environment.OSVersion.Platform == PlatformID.Win32NT) && deviceName == "Generic Software")
            {
                /*
                 * Creative OpenAL implementation on Windows seems to be limited to max 16 simulataneous sounds
                 * Now shipping OpenAL Soft, but detect this and don't glitch
                 * Further note that the current version of OpenAL Soft (1.20.0 at the time of writing) does not like OpenTK
                 * The version in use is 1.17.0 found here: https://openal-soft.org/openal-binaries/
                 */
                systemMaxSounds = 16;
            }
            try
            {
                OpenAlMic = new AudioCapture(AudioCapture.DefaultDevice, SamplingRate, ALFormat.Mono16, BufferSize);
            }
            catch
            {
                OpenAlMic = null;
            }

            if (OpenAlDevice != IntPtr.Zero)
            {
                OpenAlContext = Alc.CreateContext(OpenAlDevice, (int[])null);
                if (OpenAlContext != ContextHandle.Zero)
                {
                    Alc.MakeContextCurrent(OpenAlContext);
                    try
                    {
                        AL.SpeedOfSound(343.0f);
                    }
                    catch
                    {
                        MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_version"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    AL.DistanceModel(ALDistanceModel.None);
                    return;
                }
                Alc.CloseDevice(OpenAlDevice);
                OpenAlDevice = IntPtr.Zero;
                if (OpenAlMic != null)
                {
                    OpenAlMic.Dispose();
                    OpenAlMic = null;
                }
                MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_context"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            OpenAlContext = ContextHandle.Zero;
            MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_device"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
Exemplo n.º 14
0
 /// <summary>
 /// 释放资源
 /// </summary>
 public void Dispose()
 {
     Stop();
     _audioCapture.Dispose();
 }
Exemplo n.º 15
0
 public void Dispose()
 {
     stop();
     myDevice.Dispose();
 }
        public RecorderDiagnostic()
        {
            Trace.WriteLine("--- AudioCapture related errors ---");
            IsDeviceAvailable = false;

            try
            {
                r = new AudioCapture(AudioCapture.DefaultDevice, 16000, ALFormat.Mono16, 4096);
            }
            catch (AudioDeviceException ade)
            {
                Trace.WriteLine("AudioCapture Exception caught: " + ade.Message);
                return;
            }
            IsDeviceAvailable = true;
            DeviceName        = r.CurrentDevice;
            CheckRecorderError("Alc.CaptureOpenDevice");

            r.Start();
            CheckRecorderError("Alc.CaptureStart");
            Thread.Sleep(100);
            r.Stop();
            CheckRecorderError("Alc.CaptureStop");

            byte[] Buffer = new byte[8192];

            Thread.Sleep(10);  // Wait for a few samples to become available.
            int SamplesBefore = r.AvailableSamples;

            CheckRecorderError("Alc.GetInteger(...CaptureSamples...)");
            r.ReadSamples(Buffer, (SamplesBefore > 4096 ? 4096 : SamplesBefore));
            CheckRecorderError("Alc.CaptureSamples");

            int SamplesCaptured = SamplesBefore - r.AvailableSamples;

            uint ZeroCounter = 0;

            for (int i = 0; i < SamplesCaptured * 2; i++)
            {
                if (Buffer[i] == 0)
                {
                    ZeroCounter++;
                }
            }

            for (int i = 0; i < SamplesCaptured; i++)
            {
                short sample = BitConverter.ToInt16(Buffer, i * 2);
                if (sample > MaxSample)
                {
                    MaxSample = sample;
                }
                if (sample < MinSample)
                {
                    MinSample = sample;
                }
            }

            if (ZeroCounter < SamplesCaptured * 2 && SamplesCaptured > 0)
            {
                BufferContentsAllZero = false;
            }
            else
            {
                BufferContentsAllZero = true;
            }

            r.Dispose();
            CheckRecorderError("Alc.CaptureCloseDevice");

            // no playback test needed due to Parrot test app.

            /*
             * uint buf;
             * AL.GenBuffer(out buf);
             * AL.BufferData(buf, ALFormat.Mono16, BufferPtr, SamplesCaptured * 2, 16000);
             * uint src;
             * AL.GenSource(out src);
             * AL.BindBufferToSource(src, buf);
             * AL.Listener(ALListenerf.Gain, 16.0f);
             * AL.SourcePlay(src);
             * while (AL.GetSourceState(src) == ALSourceState.Playing)
             * {
             *  Thread.Sleep(0);
             * }
             * AL.SourceStop(src);
             *
             * AL.DeleteSource(ref src);
             * AL.DeleteBuffer(ref buf);
             */
        }
Exemplo n.º 17
0
        // --- initialization and deinitialization ---

        /// <summary>Initializes audio. A call to Deinitialize must be made when terminating the program.</summary>
        /// <returns>Whether initializing audio was successful.</returns>
        internal static void Initialize()
        {
            Deinitialize();
            switch (Interface.CurrentOptions.SoundRange)
            {
            case Interface.SoundRange.Low:
                OuterRadiusFactorMinimum      = 2.0;
                OuterRadiusFactorMaximum      = 8.0;
                OuterRadiusFactorMaximumSpeed = 1.0;
                break;

            case Interface.SoundRange.Medium:
                OuterRadiusFactorMinimum      = 4.0;
                OuterRadiusFactorMaximum      = 16.0;
                OuterRadiusFactorMaximumSpeed = 2.0;
                break;

            case Interface.SoundRange.High:
                OuterRadiusFactorMinimum      = 6.0;
                OuterRadiusFactorMaximum      = 24.0;
                OuterRadiusFactorMaximumSpeed = 3.0;
                break;
            }
            OuterRadiusFactor      = Math.Sqrt(OuterRadiusFactorMinimum * OuterRadiusFactorMaximum);
            OuterRadiusFactorSpeed = 0.0;
            OpenAlDevice           = Alc.OpenDevice(null);
            try
            {
                OpenAlMic = new AudioCapture(AudioCapture.DefaultDevice, SamplingRate, ALFormat.Mono16, BufferSize);
            }
            catch
            {
            }

            if (OpenAlDevice != IntPtr.Zero)
            {
                OpenAlContext = Alc.CreateContext(OpenAlDevice, (int[])null);
                if (OpenAlContext != ContextHandle.Zero)
                {
                    Alc.MakeContextCurrent(OpenAlContext);
                    try
                    {
                        AL.SpeedOfSound(343.0f);
                    }
                    catch
                    {
                        MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_version"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    AL.DistanceModel(ALDistanceModel.None);
                    return;
                }
                Alc.CloseDevice(OpenAlDevice);
                OpenAlDevice = IntPtr.Zero;
                OpenAlMic.Dispose();
                OpenAlMic = null;
                MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_context"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            OpenAlContext = ContextHandle.Zero;
            MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_device"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
Exemplo n.º 18
0
        // --- initialization and deinitialization ---

        /// <summary>Initializes audio. A call to Deinitialize must be made when terminating the program.</summary>
        /// <returns>Whether initializing audio was successful.</returns>
        public void Initialize(HostInterface host, SoundRange range)
        {
            Deinitialize();

            CurrentHost = host;

            switch (range)
            {
            case SoundRange.Low:
                OuterRadiusFactorMinimum      = 2.0;
                OuterRadiusFactorMaximum      = 8.0;
                OuterRadiusFactorMaximumSpeed = 1.0;
                break;

            case SoundRange.Medium:
                OuterRadiusFactorMinimum      = 4.0;
                OuterRadiusFactorMaximum      = 16.0;
                OuterRadiusFactorMaximumSpeed = 2.0;
                break;

            case SoundRange.High:
                OuterRadiusFactorMinimum      = 6.0;
                OuterRadiusFactorMaximum      = 24.0;
                OuterRadiusFactorMaximumSpeed = 3.0;
                break;
            }
            OuterRadiusFactor      = Math.Sqrt(OuterRadiusFactorMinimum * OuterRadiusFactorMaximum);
            OuterRadiusFactorSpeed = 0.0;
            OpenAlDevice           = Alc.OpenDevice(null);
            string deviceName = Alc.GetString(OpenAlDevice, AlcGetString.DefaultDeviceSpecifier);

            if ((Environment.OSVersion.Platform == PlatformID.Win32S | Environment.OSVersion.Platform == PlatformID.Win32Windows | Environment.OSVersion.Platform == PlatformID.Win32NT) && deviceName == "Generic Software")
            {
                /*
                 * Creative OpenAL implementation on Windows seems to be limited to max 16 simulataneous sounds
                 * Now shipping OpenAL Soft, but detect this and don't glitch
                 * Further note that the current version of OpenAL Soft (1.20.0 at the time of writing) does not like OpenTK
                 * The version in use is 1.17.0 found here: https://openal-soft.org/openal-binaries/
                 */
                systemMaxSounds = 16;
            }
            try
            {
                OpenAlMic = new AudioCapture(AudioCapture.DefaultDevice, SamplingRate, ALFormat.Mono16, BufferSize);
            }
            catch
            {
            }

            if (OpenAlDevice != IntPtr.Zero)
            {
                OpenAlContext = Alc.CreateContext(OpenAlDevice, (int[])null);
                if (OpenAlContext != ContextHandle.Zero)
                {
                    Alc.MakeContextCurrent(OpenAlContext);
                    try
                    {
                        AL.SpeedOfSound(343.0f);
                    }
                    catch
                    {
                        MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_version"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    AL.DistanceModel(ALDistanceModel.None);
                    return;
                }
                Alc.CloseDevice(OpenAlDevice);
                OpenAlDevice = IntPtr.Zero;
                OpenAlMic.Dispose();
                OpenAlMic = null;
                MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_context"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            OpenAlContext = ContextHandle.Zero;
            MessageBox.Show(Translations.GetInterfaceString("errors_sound_openal_device"), Translations.GetInterfaceString("program_title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }