c#获取麦克风数据并播放

本程序使用了第三方(傲瑞科技)开发库:

  • ESBasic.dll
  • ESBasic.xml
  • Oraycn.MCapture.dll
  • Oraycn.MCapture.xml
  • Oraycn.MPlayer.dll
  • Oraycn.MPlayer.xml
    访问官方网址
    将以上文件拷贝到项目Debug目录下,并在项目中添加引用
    添加开发库的引用

捕获音频数据类

using Oraycn.MCapture;
using RemoteDiagnosis.net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RemoteDiagnosis.audio
{
    public class MicrophoneHelper
    {
        private static MicrophoneHelper micphoneHelper = new MicrophoneHelper();
        private IMicrophoneCapturer microphoneCapturer;
        public bool started = false;

        private MicrophoneHelper() { }

        public static MicrophoneHelper getIns()
        {
            return micphoneHelper;
        }
        public bool start()
        {
            try
            {
                this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
                this.microphoneCapturer.AudioCaptured += new ESBasic.CbGeneric<byte[]>(microphoneCapturer_AudioCaptured);
                //this.audioPlayer = PlayerFactory.CreateAudioPlayer(0, 16000, 1, 16, 3);
                this.microphoneCapturer.Start();
                started = true;
            }
            catch (Exception )
            {
                started = false;
            }
            return started;
        }

        //捕获麦克风的音频数据
        void microphoneCapturer_AudioCaptured(byte[] audioData)
        {
            MyAudioClient.getIns().send(audioData);
            //SpeakerHelper.getIns().speak(audioData);
        }

        public void stop()
        {
            if (this.microphoneCapturer != null)
            {
                this.microphoneCapturer.Stop();
                this.microphoneCapturer.Dispose();
                this.microphoneCapturer = null;
            }
        }
    }
}

播放捕获的音频数据

using Oraycn.MPlayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RemoteDiagnosis.audio
{
    public class SpeakerHelper
    {
        private static SpeakerHelper speaker = new SpeakerHelper();
        private IAudioPlayer audioPlayer;

        private SpeakerHelper()
        {
            this.audioPlayer = PlayerFactory.CreateAudioPlayer(0, 16000, 1, 16, 3);
            //this.audioPlayer = PlayerFactory.CreateAudioPlayer(0, 16000, 2, 16, 3);
        }

        public static SpeakerHelper getIns()
        {
            return speaker;
        }

        /// <summary>
        /// 播放数据
        /// </summary>
        /// <param name="buffer"></param>
        public void speak(byte[] buffer)
        {
            if (this.audioPlayer != null)
            {
                //Debug.WriteLine("audioData :" + audioData.Length);
                this.audioPlayer.Play(buffer);
            }
        }

        public void stop()
        {
            if (this.audioPlayer != null)
            {
                this.audioPlayer.Clear();
                this.audioPlayer.Dispose();
                this.audioPlayer = null;
            }
        }
    }
}

如果音频播放没有声音,请查看麦克风与扬声器的音量是否为零或禁用了,查看方式为进入控制面板-声音-扬声器(如果没有接设备可能没有)-属性-级别

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
获取麦克风声音频率需要使用音频处理的知识,可以通过以下步骤实现: 1. 使用C#中的NAudio库来获取麦克风的音频流。 2. 将音频流转换成音频数据,可以使用NAudio库提供的WaveBuffer类。 3. 对音频数据进行快速傅里叶变换(FFT),可以使用NAudio库提供的FFT类。 4. 获取音频数据的频谱信息,可以通过FFT计算得到。 5. 根据频谱信息计算出音频的频率。 以下是获取麦克风声音频率的代码示例: ```csharp using NAudio.Wave; using System.Numerics; //创建一个录音机对象 var recorder = new WaveInEvent(); //设置录音机的参数 recorder.WaveFormat = new WaveFormat(44100, 1); //开启录音机 recorder.StartRecording(); //定义一个缓冲区,用于存储音频数据 byte[] buffer = new byte[recorder.WaveFormat.BlockAlign]; while (true) { //读取录音机中的音频数据 recorder.Read(buffer, 0, buffer.Length); //将音频数据转换成WaveBuffer对象 var waveBuffer = new WaveBuffer(buffer); //对音频数据进行FFT变换 var fft = new FFT(); Complex[] result = fft.Transform(waveBuffer.FloatBuffer); //获取音频数据的频谱信息 float[] spectrum = new float[result.Length]; for (int i = 0; i < result.Length; i++) { spectrum[i] = (float)(result[i].Magnitude); } //计算音频的频率 float maxFrequency = 0; float maxAmplitude = 0; for (int i = 0; i < spectrum.Length; i++) { float amplitude = spectrum[i]; if (amplitude > maxAmplitude) { maxFrequency = (float)i / (float)spectrum.Length * recorder.WaveFormat.SampleRate; maxAmplitude = amplitude; } } //输出音频的频率 Console.WriteLine("Frequency: " + maxFrequency); } ``` 需要注意的是,以上代码只是一个简单的示例,实际应用中还需要进行优化,例如对音频数据进行预处理、滤波等,以提高频率计算的准确性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值