基于DSP_CPLD_aP8942A_LM1791的语音控制
阅读原文时间:2023年07月12日阅读:1

语音驱动程序  drv_voice.c

语音服务程序  srv_voice.c

1、先运行初始化函数,主要是设置初始音量,并建立一个软件定时器来,以10ms的周期来调用语音播放函数。

1 void srvVoiceInit()
2 {
3 T_SOFT_TIMER_HANDLE hSoftTimer;
4 UINT16 usVoiceVolumeTunerAdjust;
5
6 g_hVoiceDevHandle = osalDevOpen(GEN20_PROTO_VOICE_DEV_NAME);
7 if (g_hVoiceDevHandle == INVALID_HANDLE)
8 {
9 LOG_ERROR_LOG(("srvVoiceInit failed at voice driver open.\r\n"));
10 }
11 else
12 {
13 /* Initialize voice play info structure, must before soft timer started. */
14 /* TODO: read default voice from EEPROM */
15 srvVoiSetDefaultVoiceType(VDVT_TYPE_A);
16 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_IDLE;
17 gVoiPlayState = VPSS_VOICE_IDLE;
18 gVoiInnerEvent = E_NULL;
19 usVoiceVolumeTunerAdjust = 160;
20 //gVoiceToSet = 54;
21 srvSetVoiceVolume(63);
22 osalDevIoctl(g_hVoiceDevHandle, VOICE_CTRL_RST_PLAYBACK_CLR , NULL);
23 osalDevIoctl(g_hVoiceDevHandle, VOICE_CTRL_SET_VOICE_VOLUME, &usVoiceVolumeTunerAdjust);
24
25 /* Create the soft timer for push button service, the handle is not needed. */
26 if (OSAL_IS_RESULT_OK(osalTimCreateSoftTimer(&hSoftTimer, 10, STT_REPEAT, &srvVoicePlayTimerRoutine, NULL)))
27 {
28 if (OSAL_IS_RESULT_OK(osalTimStartSoftTimer(hSoftTimer)))
29 {
30 g_hVoiceMode = 0;
31 }
32 else
33 {
34 LOG_ERROR_LOG(("srvVoiceInit failed at SoftTimer start.\r\n"));
35 }
36 }
37 else
38 {
39 LOG_ERROR_LOG(("srvVoiceInit failed at SoftTimer create.\r\n"));
40 }
41 }
42 }

2 语音播放函数,先根据状态来设置语音模式,再根据模式来播放语音

1 static void srvVoicePlayTimerRoutine(void *pvParam)
2 {
3 /* Wait event and return immediately. */
4 if(0 != g_hVoiceMode)
5 {
6 /* Some thing happened, adjust current voice play accordingly. */
7 srvVoiUpdateVoicePlayInfo(g_hVoiceMode);
8
9 /* Control voice play according to the information. */
10 srvVoiPlayVoice();
11 g_hVoiceMode = 0;
12 }
13 else
14 {
15 srvVoiPlayVoice();
16 }
17
18 }

3 更新语音播放信息,根据语音模式来更新

根据接收的语音事件来播放对应的语音片段,

语音包括以下

SRV_VOICE_PLAY_SYSTEM_START;系统启动声音

SRV_VOICE_PLAY_OPERATION_DENIED;操作禁止声音

SRV_VOICE_PLAY_TEST;测试声音

SRV_VOICE_PLAY_MIN_OUTPUT;小档位声音

SRV_VOICE_PLAY_MAX_OUTPUT;大档位声音

SRV_VOICE_PLAY_OVER_CURRENT;过流警报声

SRV_VOICE_PLAY_RECOVERABLE_FAULT;可恢复性错误声音

SRV_VOICE_PLAY_NON_RECOVERABLE_FAULT;不可恢复性错误

SRV_VOICE_PLAY_FAULT_REPLAY;错误重放

SRV_VOICE_PLAY_SHOW_DEFAULT_VOICE;默认语音

1 static void srvVoiUpdateVoicePlayInfo(UINT uVoiceEvent)
2 {
3 if ((uVoiceEvent & SRV_VOICE_PLAY_SYSTEM_START) == SRV_VOICE_PLAY_SYSTEM_START)
4 {
5 /* Play welcome voice 1 time in fixed volume 60. */
6 gVoiInnerEvent = E_PLAY_VOICE;
7 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
8 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_SECTION_FLASH_START;
9 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
10 g_tVoicePlayInfo.usVoicePlayRepeat = 1;
11 g_tVoicePlayInfo.usVoiceVolume = srvGetVoiceVolume();
12 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
13 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
14 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
15 }
16
17 if ((uVoiceEvent & SRV_VOICE_PLAY_OPERATION_DENIED) == SRV_VOICE_PLAY_OPERATION_DENIED)
18 {
19 /* Play welcome voice 1 time in fixed volume 60. */
20 gVoiInnerEvent = E_PLAY_VOICE;
21 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
22 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_SECTION_OPERATION_DENIED;
23 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
24 g_tVoicePlayInfo.usVoicePlayRepeat = 1;
25 g_tVoicePlayInfo.usVoiceVolume = 60;
26 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
27 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
28 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
29 }
30 if ((uVoiceEvent & SRV_VOICE_PLAY_TEST) == SRV_VOICE_PLAY_TEST)
31 {
32 /* TODO: */
33 gVoiInnerEvent = E_PLAY_VOICE;
34 g_tVoicePlayInfo.voiMode = VOI_MODE_TIME_LIMITED;
35 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_SECTION_TEST_IN_PROGRESS;
36 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
37 g_tVoicePlayInfo.usVoicePlayRepeat = VOICE_PLAY_REPEAT_INFINITE;
38 g_tVoicePlayInfo.usVoiceVolume = srvGetVoiceVolume();
39 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
40 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
41 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
42 }
43 if ((uVoiceEvent & SRV_VOICE_PLAY_MIN_OUTPUT) == SRV_VOICE_PLAY_MIN_OUTPUT)
44 {
45 gVoiInnerEvent = E_PLAY_VOICE;
46 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
47 g_tVoicePlayInfo.wCurrVoiceSection = g_tVoicePlayInfo.wVoiceDefaultMinOutput;
48 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
49 g_tVoicePlayInfo.usVoicePlayRepeat = VOICE_PLAY_REPEAT_INFINITE;
50 g_tVoicePlayInfo.usVoiceVolume = srvGetVoiceVolume();// VOICE_PLAY_CURRENT_VOICE_VOLUME;
51 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
52 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 30; // last 26
53 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
54 }
55 if ((uVoiceEvent & SRV_VOICE_PLAY_MAX_OUTPUT) == SRV_VOICE_PLAY_MAX_OUTPUT)
56 {
57 gVoiInnerEvent = E_PLAY_VOICE;
58 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
59 g_tVoicePlayInfo.wCurrVoiceSection = g_tVoicePlayInfo.wVoiceDefaultMaxOutput;
60 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
61 g_tVoicePlayInfo.usVoicePlayRepeat = VOICE_PLAY_REPEAT_INFINITE;
62 g_tVoicePlayInfo.usVoiceVolume = srvGetVoiceVolume(); //VOICE_PLAY_CURRENT_VOICE_VOLUME;
63 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
64 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 15; //last 10
65 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
66 }
67 if ((uVoiceEvent & SRV_VOICE_PLAY_OVER_CURRENT) == SRV_VOICE_PLAY_OVER_CURRENT)
68 {
69 /* TODO: */
70 /* Play fatal error voice 1 time in fixed volume 60. */
71 gVoiInnerEvent = E_PLAY_VOICE;
72 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
73 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_SECTION_FATAL_ERROR;
74 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
75 g_tVoicePlayInfo.usVoicePlayRepeat = 1;
76 g_tVoicePlayInfo.usVoiceVolume = 60;
77 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
78 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
79 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
80 }
81 if ((uVoiceEvent & SRV_VOICE_PLAY_RECOVERABLE_FAULT) == SRV_VOICE_PLAY_RECOVERABLE_FAULT)
82 {
83 /* Play fatal error voice 1 time in fixed volume 60. */
84 gVoiInnerEvent = E_PLAY_VOICE;
85 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
86 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_SECTION_FATAL_ERROR;
87 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
88 g_tVoicePlayInfo.usVoicePlayRepeat = 1;//VOICE_PLAY_REPEAT_INFINITE;
89 g_tVoicePlayInfo.usVoiceVolume = 60;
90 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
91 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
92 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
93 }
94 if ((uVoiceEvent & SRV_VOICE_PLAY_NON_RECOVERABLE_FAULT) == SRV_VOICE_PLAY_NON_RECOVERABLE_FAULT)
95 {
96 /* Play fatal error voice 3 timeS in fixed volume 60. */
97 gVoiInnerEvent = E_PLAY_VOICE;
98 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
99 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_SECTION_FATAL_ERROR;
100 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
101 g_tVoicePlayInfo.usVoicePlayRepeat = 1;//VOICE_PLAY_REPEAT_INFINITE;
102 g_tVoicePlayInfo.usVoiceVolume = 60;
103 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
104 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
105 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
106 }
107 if ((uVoiceEvent & SRV_VOICE_PLAY_FAULT_REPLAY) == SRV_VOICE_PLAY_FAULT_REPLAY)
108 {
109 /* Play fatal error voice 1 time in fixed volume 60. */
110 gVoiInnerEvent = E_PLAY_VOICE;
111 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
112 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_SECTION_FATAL_ERROR;
113 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = VOICE_PLAY_DURATION_TIME_INFINITE;
114 g_tVoicePlayInfo.usVoicePlayRepeat = 1;
115 g_tVoicePlayInfo.usVoiceVolume = 60;
116 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
117 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
118 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
119 }
120 if ((uVoiceEvent & SRV_VOICE_PLAY_SHOW_DEFAULT_VOICE) == SRV_VOICE_PLAY_SHOW_DEFAULT_VOICE)
121 {
122 gVoiInnerEvent = E_PLAY_VOICE;
123 g_tVoicePlayInfo.voiMode = VOI_MODE_REPEAT;
124 g_tVoicePlayInfo.wCurrVoiceSection = VOICE_PLAY_DEFAULT_VOICE_SECTION;
125 g_tVoicePlayInfo.usVoicePlayTimeIn10mS = 100;
126 g_tVoicePlayInfo.usVoicePlayRepeat = VOICE_PLAY_REPEAT_INFINITE;
127 g_tVoicePlayInfo.usVoiceVolume = srvGetVoiceVolume();
128 g_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS = 10;
129 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
130 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_START_PLAY;
131 }
132 if ((uVoiceEvent & SRV_VOICE_PLAY_STOP) == SRV_VOICE_PLAY_STOP)
133 {
134 /* Stop current play, don't change wCurrVoiceSection, left it done in srvVoiPlayVoice(). */
135 gVoiInnerEvent = E_STOP_VOICE ;
136 //g_tVoicePlayInfo.usVoiceStopTimeoutIn10mS = 3;
137 g_tVoicePlayInfo.usVoicePlayRepeat = 1;
138 g_tVoicePlayInfo.usVoiceVolume = 60; //
139 //g_tVoicePlayInfo.eSwitchStatus = VPSS_VOICE_EXPLICIT_STOP_PLAY;
140 }
141 }

4 语音信息结构体

1 typedef struct{
2 VOI_MODE_TYPE voiMode;
3 UINT16 wCurrVoiceSection; /* 当前语音段. */
4 UINT16 usVoicePlayTimeIn10mS; /* 语音总时长. */
5 UINT16 usVoiceStopIntervalTimeIn10mS; /* 重复播放的间隔时间 */
6 UINT16 usVoiceStopIntervalRemainTimeIn10mS; /* 间隔时间的剩余量. */
7 //UINT16 usVoiceStopTimeoutIn10mS; /* Timeout when checking whether voice stopped. */
8 UINT16 usVoicePlayRepeat; /* 语音重复次数 */
9 UINT16 usVoiceVolume; /* 音量 */
10 UINT16 wVoiceDefaultMaxOutput;
11 UINT16 wVoiceDefaultMinOutput;
12 } T_VOICE_PLAY_INFO;

5 播放语音

static void srvVoiPlayVoice(void)
{
UINT16 wVoiceSection; //语音段
//UINT16 usVoiceVolumeTunerAdjust;
UINT16 usVoiceVolumeToSet; //设置的音量
UINT16 wVoiceStatus; //语音状态
T_OSAL_RESULT eRslt = OSAL_OK;
static UINT16 wLastVoiceStatus = 0; //上一次语音的状态
static UINT32 ticForwait;//等待的时钟数
static E_VOICE_PLAY_SWITCH_STATUS afterResetState = VPSS_VOICE_IDLE;

if(gVoiPlayState != VPSS\_VOICE\_IDLE)  
{   //语音状态非IDLE状态  
    if(gVoiInnerEvent == E\_PLAY\_VOICE)  
    {  
        gVoiPlayState = VPSS\_VOICE\_RESET;  
        afterResetState = VPSS\_VOICE\_LOAD\_VOICE;  
    }else if(gVoiInnerEvent == E\_STOP\_VOICE){  
        gVoiPlayState = VPSS\_VOICE\_RESET;  
        afterResetState = VPSS\_VOICE\_IDLE;  
    }  
}

switch (gVoiPlayState)  
{  
    case VPSS\_VOICE\_IDLE:  
        if(gVoiInnerEvent == E\_PLAY\_VOICE)  
        {  
            gVoiPlayState = VPSS\_VOICE\_LOAD\_VOICE;  
        }  
    break;

    case VPSS\_VOICE\_LOAD\_VOICE:  
        usVoiceVolumeToSet = g\_tVoicePlayInfo.usVoiceVolume;  
        eRslt = osalDevIoctl(g\_hVoiceDevHandle, VOICE\_CTRL\_SET\_VOICE\_VOLUME, &usVoiceVolumeToSet);//设置音量

        if (OSAL\_IS\_RESULT\_OK(eRslt))  
        { //等音量设置完  
            wVoiceSection = g\_tVoicePlayInfo.wCurrVoiceSection;  
            eRslt = osalDevWrite(g\_hVoiceDevHandle, &wVoiceSection, 1); //加载待播放的语音

            if (OSAL\_IS\_RESULT\_OK(eRslt))  
            { //等待语音加载完成  
                if(g\_tVoicePlayInfo.voiMode == VOI\_MODE\_REPEAT)  
                { //播放模式为重复播放模式  
                    ticForwait = osalTimGetOSTime();  
                    gVoiPlayState = VPSS\_VOICE\_TRIGGER\_WAIT;  
                }  
                else if(g\_tVoicePlayInfo.voiMode == VOI\_MODE\_TIME\_LIMITED)  
                {  //限时播放模式  
                    gVoiPlayState = VPSS\_VOICE\_PLAY\_TIME\_LIMITED;  
                }  
            }else{  
                LOG\_ERROR\_LOG(("Voice driver write failed. Error: %08x.\\r\\n", eRslt));  
            }  
        }else{  
            LOG\_ERROR\_LOG(("Voice driver ioctl failed. Error: %08x.\\r\\n", eRslt));  
        }  
    break;

    case VPSS\_VOICE\_TRIGGER\_WAIT:  
        //wait 100ms  
        if((osalTimGetOSTime() -ticForwait) > 1000)  
        {  
            wVoiceSection = VOICE\_SECTION\_NULL;  
            eRslt = osalDevWrite(g\_hVoiceDevHandle, &wVoiceSection, 1);  
            gVoiPlayState = VPSS\_VOICE\_PLAY\_REPEAT;  
        }  
    break;

    case VPSS\_VOICE\_PLAY\_REPEAT:  
        eRslt = osalDevIoctl(g\_hVoiceDevHandle, VOICE\_CTRL\_READ\_STATUS, &wVoiceStatus);  
        if (OSAL\_IS\_RESULT\_OK(eRslt))  
        {  
            /\* Check if current playing is finished \*/  
            if((wVoiceStatus == 0x0000) )//&(wLastVoiceStatus == 0x0001)  
            {  
                wLastVoiceStatus = 0;  
                g\_tVoicePlayInfo.usVoiceStopIntervalRemainTimeIn10mS = g\_tVoicePlayInfo.usVoiceStopIntervalTimeIn10mS;  
                gVoiPlayState = VPSS\_VOICE\_RELOAD\_VOICE;  
            }else{  
                wLastVoiceStatus = wVoiceStatus;  
            }  
        }else{  
            LOG\_ERROR\_LOG(("Voice driver ioctl failed. Error: %08x.\\r\\n", eRslt));  
        }  
    break;

    case VPSS\_VOICE\_RELOAD\_VOICE:

        if(g\_tVoicePlayInfo.usVoiceStopIntervalRemainTimeIn10mS != 0)  
        {  
            g\_tVoicePlayInfo.usVoiceStopIntervalRemainTimeIn10mS --;  
        }else{  
            if(g\_tVoicePlayInfo.usVoicePlayRepeat != 0){  
                   if(g\_tVoicePlayInfo.usVoicePlayRepeat != VOICE\_PLAY\_REPEAT\_INFINITE)  
                   {  
                       g\_tVoicePlayInfo.usVoicePlayRepeat--;  
                   }  
                   if(g\_tVoicePlayInfo.usVoicePlayRepeat != 0)  
                   {  
                       wVoiceSection = g\_tVoicePlayInfo.wCurrVoiceSection;  
                       eRslt = osalDevWrite(g\_hVoiceDevHandle, &wVoiceSection, 1);  
                       ticForwait = osalTimGetOSTime();  
                       gVoiPlayState = VPSS\_VOICE\_TRIGGER\_WAIT;  
                   }  
                   else{  
                       gVoiPlayState = VPSS\_VOICE\_IDLE;  
                   }  
            }else{  
                gVoiPlayState = VPSS\_VOICE\_IDLE;  
            }  
        }  
    break;

    case VPSS\_VOICE\_RESET:  
        wVoiceSection = VOICE\_SECTION\_NULL;  
        eRslt = osalDevWrite(g\_hVoiceDevHandle, &wVoiceSection, 1);  
        eRslt = osalDevIoctl(g\_hVoiceDevHandle, VOICE\_CTRL\_RST\_PLAYBACK\_SET, NULL);  
        ticForwait = osalTimGetOSTime();  
        gVoiPlayState = VPSS\_VOICE\_RESET\_WAIT;  
    break;

    case VPSS\_VOICE\_RESET\_WAIT:  
        if((osalTimGetOSTime() - ticForwait) > 500)  
        {  
            eRslt = osalDevIoctl(g\_hVoiceDevHandle, VOICE\_CTRL\_RST\_PLAYBACK\_CLR , NULL);  
            gVoiPlayState = afterResetState;  
        }  
    break;

    case VPSS\_VOICE\_PLAY\_TIME\_LIMITED:  
        if(g\_tVoicePlayInfo.usVoicePlayTimeIn10mS != VOICE\_PLAY\_DURATION\_TIME\_INFINITE )  
        {  
            if(g\_tVoicePlayInfo.usVoicePlayTimeIn10mS != 0)  
            {  
                g\_tVoicePlayInfo.usVoicePlayTimeIn10mS--;  
            }  
            else  
            {  
                gVoiPlayState = VPSS\_VOICE\_RESET;  
                afterResetState = VPSS\_VOICE\_IDLE;  
            }  
        }  
    break;  
}  
gVoiInnerEvent = E\_NULL;

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章