关于hololens调用讯飞语音sdk问题汇总

由于hololens只支持英语的语音识别,而本人的项目需要用到中文语音识别,考虑到讯飞是国内比较著名的语音识别技术供应商,因此尝试将讯飞的windows SDK嫁接到unity3d中进行开发。而结果很遗憾,该程序能在unity 的调试中运行,但是在hololens中无法正常运行,原因可能是讯飞的sdk不能支持win10的缘故吧。只能够等待讯飞那边进行sdk更新。在尝试使用讯飞开发的过程中,由于unity提供的音频格式与讯飞的需要的不同,导致进度一度停滞,经过一番努力终于摸索出了规律,现在分享一下。

实现讯飞语音识别,需要向讯飞服务器上传音频数据,由于讯飞需要该音频数据格式为 pcm 格式,16k采样率,16位数据,单声道,而unity 通过麦克风获取的音频流数据为32位的浮点型双声道音频数组,因此需要将数据转换才能使用,因此流程如下:

1.通过unity获取麦克风音频数据

2.设定采样周期,采样数,处理周期

3.将32位浮点双声道数组转换成16位单声道音频数组

1.unity 获取 麦克风 音频流数据

首先我这个项目实现的功能是这样的,实时进行语音转写。即不能是通过按钮来录取一段音频上传上去识别,而是实时地从麦克风中获取数据上传。因此,需要读取音频流(AudioStream)。在网上提供的方案是通过第三方插件Naudio进行麦克风缓存的获取,但经过测试,Naudio在unity中编译不了,PS:正确来说是Naudio不知UWP编译,因此放弃了该方案,后来查找holoToolkit的Input组件,其提供了MicStream脚本来帮助读取麦克风中的缓存。以下是代码

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.


using System.Runtime.InteropServices;
using UnityEngine;
using System.Text;


namespace HoloToolkit.Unity.InputModule
{
    public class MicStream
    {
        // This class replaces Unity's Microphone object
        // This class is made for HoloLens mic stream selection, but should work well on all windows 10 devices
        // chooses from one of three possible microphone modes on HoloLens
        // There is an example of how to use this script in HoloToolkit\Input\Tests\Scripts\MicStreamDemo.cs


        // Streams: LOW_QUALITY_VOICE is optimized for speech analysis, COMMUNICATIONS is higher quality voice and is probably preferred
        //          ROOM_CAPTURE tries to get the sounds of the room more than the voice of the suer
        // can only be set on initialization
        public enum StreamCategory { LOW_QUALITY_VOICE, HIGH_QUALITY_VOICE, ROOM_CAPTURE }


        public enum ErrorCodes { ALREADY_RUNNING = -10, NO_AUDIO_DEVICE, NO_INPUT_DEVICE, ALREADY_RECORDING, GRAPH_NOT_EXIST, CHANNEL_COUNT_MISMATCH, FILE_CREATION_PERMISSION_ERROR, NOT_ENOUGH_DATA, NEED_ENABLED_MIC_CAPABILITY };


        const int MAX_PATH = 260; // 260 is maximum path length in windows, to be returned when we MicStopRecording


        [UnmanagedFunctionPointer(CallingConvention.StdCall)] // If included in MicStartStream, this callback will be triggered when audio data is ready. This is not the preferred method for Game Engines and can probably be ignored.
        public delegate void LiveMicCallback();


        /// 
        /// Called before calling MicStartStream or MicstartRecording to initialize microphone
        /// 
        /// One of the entries in the StreamCategory enumeratio
        /// 
   
   
    
    error code or 0
   
   
        [DllImport("MicStreamSelector", ExactSpelling = true)]
        public static extern int MicInitializeDefault(int category);


        /// 
        /// Called before calling MicStartStream or MicstartRecording to 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Android Studio中调用语音接口方法,可以通过以下步骤进行: 1. 首先,确保你已经在开放平台注册并获取了AppID和API Key。如果还没有,你可以前往开放平台进行注册和申请。 2. 在Android Studio中创建一个新的Android项目,并在项目的`build.gradle`文件中添加语音SDK的依赖。例如,可以在`dependencies`部分添加以下代码: ```groovy implementation 'com.iflytek:speech-plus:8.6.0' ``` 3. 在你的代码中,首先初始化语音SDK。在你的Application类的`onCreate()`方法中添加以下代码: ```java SpeechUtility.createUtility(getApplicationContext(), SpeechConstant.APPID + "=你的AppID"); ``` 4. 接下来,你可以使用语音SDK提供的各种接口进行语音识别、语音合成等操作。例如,如果你想进行语音识别,可以使用以下代码: ```java // 创建SpeechRecognizer对象 SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(getApplicationContext(), null); // 设置参数 mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); mIat.setParameter(SpeechConstant.ACCENT, "mandarin"); // 开始识别 mIat.startListening(mRecognizerListener); ``` 在上述代码中,`mRecognizerListener`是一个实现了`RecognizerListener`接口的监听器,用于接收识别结果和状态回调。 以上是一个简单的示例,你可以根据自己的需求和语音SDK提供的文档进行更详细的配置和使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值