15490a39dSopenharmony_ci# Intelligent Voice Framework 25490a39dSopenharmony_ci 35490a39dSopenharmony_ci## Overview 45490a39dSopenharmony_ci 55490a39dSopenharmony_ci### Introduction 65490a39dSopenharmony_ci 75490a39dSopenharmony_ciThe intelligent voice framework consists of the intelligent voice service framework and intelligent voice driver. It implements voice enrollment and voice wakeup. 85490a39dSopenharmony_ci 95490a39dSopenharmony_ci**Figure 1** Architecture of the intelligent voice framework 105490a39dSopenharmony_ci 115490a39dSopenharmony_ci 125490a39dSopenharmony_ci 135490a39dSopenharmony_ciThe intelligent voice service framework provides the following features: 145490a39dSopenharmony_ci 155490a39dSopenharmony_ci- System event monitoring: monitoring system events such as unlocking upon power-on and screen-on/off 165490a39dSopenharmony_ci- Concurrency policy: intelligent voice service concurrency management 175490a39dSopenharmony_ci 185490a39dSopenharmony_ci- Intelligent voice service: voice enrollment, voice wakeup, and more 195490a39dSopenharmony_ci- Sound trigger: Digital Signal Processor (DSP) model loading, DSP algorithm enabling/disabling, and DSP event processing 205490a39dSopenharmony_ci 215490a39dSopenharmony_ciThe intelligent voice driver provides the following features: 225490a39dSopenharmony_ci 235490a39dSopenharmony_ci- Engine algorithm: intelligent voice algorithm engine and event reporting 245490a39dSopenharmony_ci 255490a39dSopenharmony_ci- Device driver: DSP model loading/unloading, DSP algorithm enabling/disabling, event reporting, and hardware-related channel configuration 265490a39dSopenharmony_ci 275490a39dSopenharmony_ci### Basic Concepts 285490a39dSopenharmony_ci- Voice enrollment: process of converting a wakeup word spoken by a user into an acoustic model and a voiceprint feature, which will be used for comparison during voice wakeup 295490a39dSopenharmony_ci- Voice wakeup: process of checking whether the current speaker is a registered user and if yes, waking up the system 305490a39dSopenharmony_ci- DSP chip: chip that implements digital signal processing 315490a39dSopenharmony_ci 325490a39dSopenharmony_ci### Directory Structure 335490a39dSopenharmony_ci 345490a39dSopenharmony_ciThe structure of the repository directory is as follows: 355490a39dSopenharmony_ci 365490a39dSopenharmony_ci```shell 375490a39dSopenharmony_ci/foundation/ai/intelligent_voice_framework # Service code of the intelligent audio framework 385490a39dSopenharmony_ci├── frameworks # Framework code 395490a39dSopenharmony_ci│ ├── native # Internal API implementation 405490a39dSopenharmony_ci│ └── js # External API implementation 415490a39dSopenharmony_ci├── interfaces # API code 425490a39dSopenharmony_ci│ ├── inner_api # Internal APIs 435490a39dSopenharmony_ci│ └── kits # External APIs 445490a39dSopenharmony_ci├── sa_profile # Service configuration profile 455490a39dSopenharmony_ci├├── services # Service code 465490a39dSopenharmony_ci├── LICENSE # License file 475490a39dSopenharmony_ci├── tests # Developer test 485490a39dSopenharmony_ci└── utils # Public functions 495490a39dSopenharmony_ci``` 505490a39dSopenharmony_ci 515490a39dSopenharmony_ci### Constraints 525490a39dSopenharmony_ci 535490a39dSopenharmony_ciCurrently, the intelligent voice framework supports the enrollment and wakeup of only one wakeup word. 545490a39dSopenharmony_ci 555490a39dSopenharmony_ci 565490a39dSopenharmony_ci## Available APIs 575490a39dSopenharmony_ci### APIs Used for Voice Enrollment 585490a39dSopenharmony_ci 595490a39dSopenharmony_ci| API | Description | 605490a39dSopenharmony_ci| ------------------------------------------------------------ | ------------------ | 615490a39dSopenharmony_ci| createEnrollIntelligentVoiceEngine(descriptor: EnrollIntelligentVoiceEngineDescriptor): EnrollIntelligentVoiceEngine | Creates an enrollment engine. | 625490a39dSopenharmony_ci| init(config: EnrollEngineConfig): EnrollIntelligentVoiceEngineCallbackInfo | Initializes this enrollment engine. | 635490a39dSopenharmony_ci| start(isLast: boolean): EnrollIntelligentVoiceEngineCallbackInfo | Starts enrollment. | 645490a39dSopenharmony_ci| stop(): void | Stops enrollment. | 655490a39dSopenharmony_ci| commit(): EnrollIntelligentVoiceEngineCallbackInfo | Commits the enrollment data. | 665490a39dSopenharmony_ci| setWakeupHapInfo(info: WakeupHapInfo): void | Sets the wakeup application information.| 675490a39dSopenharmony_ci| setSensibility(sensibility: SensibilityType): void | Sets the sensitivity. | 685490a39dSopenharmony_ci| release(): void | Releases this enrollment engine. | 695490a39dSopenharmony_ci 705490a39dSopenharmony_ci### APIs Used for Voice Wakeup 715490a39dSopenharmony_ci 725490a39dSopenharmony_ci| API | Description | 735490a39dSopenharmony_ci| ------------------------------------------------------------ | ------------------ | 745490a39dSopenharmony_ci| createWakeupIntelligentVoiceEngine(descriptor: WakeupIntelligentVoiceEngineDescriptor): WakeupIntelligentVoiceEngine | Creates a wakeup engine. | 755490a39dSopenharmony_ci| setWakeupHapInfo(info: WakeupHapInfo): void | Sets the wakeup application information.| 765490a39dSopenharmony_ci| setSensibility(sensibility: SensibilityType): void | Sets the sensitivity. | 775490a39dSopenharmony_ci| on(type: 'wakeupIntelligentVoiceEvent', callback: Callback<WakeupIntelligentVoiceEngineCallbackInfo>): void | Subscribes to wakeup events. | 785490a39dSopenharmony_ci| release(): void | Releases this wakeup engine. | 795490a39dSopenharmony_ci 805490a39dSopenharmony_ci 815490a39dSopenharmony_ci## How to Develop 825490a39dSopenharmony_ci 835490a39dSopenharmony_ci### Voice Enrollment 845490a39dSopenharmony_ci 855490a39dSopenharmony_ciThe voice enrollment process is an interaction process initiated by a user through the enrollment page of an application. The main process is as follows: 865490a39dSopenharmony_ci1. A user starts enrollment (creating and initializing the enrollment engine), and the enrollment page is displayed. 875490a39dSopenharmony_ci2. The enrollment page asks the user to speak a wakeup word, and the user speaks the wakeup word (starting enrollment). The enrollment page asks the user to speak the wakeup word again several times. 885490a39dSopenharmony_ci3. After the enrollment data is committed, the enrollment process is complete. 895490a39dSopenharmony_ciThe code snippet is as follows: 905490a39dSopenharmony_ci 915490a39dSopenharmony_ci```js 925490a39dSopenharmony_ci// Import the intelligentVoice module. 935490a39dSopenharmony_ciimport intelligentVoice from '@ohos.ai.intelligentVoice'; 945490a39dSopenharmony_ci 955490a39dSopenharmony_ci// Obtain the intelligent audio management service. 965490a39dSopenharmony_civar manager = intellVoice.getIntelligentVoiceManager(); 975490a39dSopenharmony_ciif (manager == null) { 985490a39dSopenharmony_ci console.error("Get IntelligentVoiceManager failed."); 995490a39dSopenharmony_ci} else { 1005490a39dSopenharmony_ci console.info("Get IntelligentVoiceManager success."); 1015490a39dSopenharmony_ci return; 1025490a39dSopenharmony_ci} 1035490a39dSopenharmony_ci 1045490a39dSopenharmony_ci// Create an enrollment engine. 1055490a39dSopenharmony_civar engine = null; 1065490a39dSopenharmony_cilet engineDescriptor = { 1075490a39dSopenharmony_ci wakeupPhrase: '', // Set a wakeup word. 1085490a39dSopenharmony_ci} 1095490a39dSopenharmony_ciawait intellVoice.createEnrollIntelligentVoiceEngine(engineDescriptor).then((data) => { 1105490a39dSopenharmony_ci engine = data; 1115490a39dSopenharmony_ci console.info('Create EnrollIntelligentVoice Engine finish'); 1125490a39dSopenharmony_ci}).catch((err) => { 1135490a39dSopenharmony_ci console.error('Create EnrollIntelligentVoice Engine failed, err: ' + err.message); 1145490a39dSopenharmony_ci}); 1155490a39dSopenharmony_ciif (engine == null) { 1165490a39dSopenharmony_ci console.error('Create EnrollIntelligentVoice Engine failed'); 1175490a39dSopenharmony_ci return; 1185490a39dSopenharmony_ci} 1195490a39dSopenharmony_ci 1205490a39dSopenharmony_ci// Initialize the enrollment engine. 1215490a39dSopenharmony_cilet config = { 1225490a39dSopenharmony_ci language: "zh", // Chinese 1235490a39dSopenharmony_ci area: "CN", // China 1245490a39dSopenharmony_ci} 1255490a39dSopenharmony_ciengine.init(config).then((data) => { 1265490a39dSopenharmony_ci console.info('Init EnrollIntelligentVoice Engine finish'); 1275490a39dSopenharmony_ci}).catch((err) => { 1285490a39dSopenharmony_ci console.info('Init EnrollIntelligentVoice Engine failed, err: '+ err.message); 1295490a39dSopenharmony_ci}); 1305490a39dSopenharmony_ci 1315490a39dSopenharmony_ci// Start enrollment. 1325490a39dSopenharmony_cilet isLast = true; // The value true means that this is the last time to start enrollment, and false means the opposite. The value true is used here. 1335490a39dSopenharmony_ciengine.start(isLast).then((data) => { 1345490a39dSopenharmony_ci console.info('Start enrollment finish'); 1355490a39dSopenharmony_ci}).catch((err) => { 1365490a39dSopenharmony_ci console.info('Start enrollment failed, err: '+ err.message); 1375490a39dSopenharmony_ci}); 1385490a39dSopenharmony_ci 1395490a39dSopenharmony_ci// Commit the enrollment data. 1405490a39dSopenharmony_ciengine.commit().then((data) => { 1415490a39dSopenharmony_ci console.info('Commit enroll result finish'); 1425490a39dSopenharmony_ci}).catch((err) => { 1435490a39dSopenharmony_ci console.info('Commit enroll result failed, err: '+ err.message); 1445490a39dSopenharmony_ci}); 1455490a39dSopenharmony_ci 1465490a39dSopenharmony_ci// Deliver the voice wakeup application information. 1475490a39dSopenharmony_cilet info = { 1485490a39dSopenharmony_ci bundleName: "demo", // Bundle name of the application. demo here is for reference only. Set this parameter based on your application. 1495490a39dSopenharmony_ci abilityName: "demo", // Ability name of the application. demo here is for reference only. Set this parameter based on your application. 1505490a39dSopenharmony_ci} 1515490a39dSopenharmony_ciengine.setWakeupHapInfo(info).then((data) => { 1525490a39dSopenharmony_ci console.info('Set wakeup hap info finish'); 1535490a39dSopenharmony_ci}).catch((err) => { 1545490a39dSopenharmony_ci console.info('Set wakeup hap info failed, err: '+ err.message); 1555490a39dSopenharmony_ci}); 1565490a39dSopenharmony_ci 1575490a39dSopenharmony_ci// Release the enrollment engine. 1585490a39dSopenharmony_ciengine.release().then((data) => { 1595490a39dSopenharmony_ci console.info('Release EnrollIntelligentVoice engine success.'); 1605490a39dSopenharmony_ci}).catch((err) => { 1615490a39dSopenharmony_ci console.info('Release EnrollIntelligentVoice engine failed, err: '+ err.message); 1625490a39dSopenharmony_ci}); 1635490a39dSopenharmony_ci``` 1645490a39dSopenharmony_ci 1655490a39dSopenharmony_ci### Voice Wakeup 1665490a39dSopenharmony_ci 1675490a39dSopenharmony_ciVoice wakeup is controlled by the intelligent voice framework. Upper-layer applications only need to create a wakeup engine by calling **createWakeupIntelligentVoiceEngine** and then subscribe to wakeup events. 1685490a39dSopenharmony_ci 1695490a39dSopenharmony_ci```js 1705490a39dSopenharmony_ci// Obtain the wakeup engine. 1715490a39dSopenharmony_civar engine = null; 1725490a39dSopenharmony_cilet engineDescriptor = { 1735490a39dSopenharmony_ci needApAlgEngine: true, // Specify whether the framework needs to provide the AP algorithm engine. 1745490a39dSopenharmony_ci wakeupPhrase: '', // Set a wakeup word. 1755490a39dSopenharmony_ci} 1765490a39dSopenharmony_ciawait intellVoice.createWakeupIntelligentVoiceEngine(engineDescriptor).then((data) => { 1775490a39dSopenharmony_ci engine = data; 1785490a39dSopenharmony_ci console.info('Create WakeupIntelligentVoice Engine finish'); 1795490a39dSopenharmony_ci}).catch((err) => { 1805490a39dSopenharmony_ci console.error('Create WakeupIntelligentVoice Engine failed, err: ' + err.message); 1815490a39dSopenharmony_ci}); 1825490a39dSopenharmony_ciif (engine == null) { 1835490a39dSopenharmony_ci console.error('Create WakeupIntelligentVoice Engine failed'); 1845490a39dSopenharmony_ci return; 1855490a39dSopenharmony_ci} 1865490a39dSopenharmony_ci 1875490a39dSopenharmony_ci// Subscribe to wakeup events. 1885490a39dSopenharmony_ciengine.on('wakeupIntelligentVoiceEvent',(callback) => { 1895490a39dSopenharmony_ci console.info('wakeupIntelligentVoiceEvent CallBackInfo:') 1905490a39dSopenharmony_ci for (let prop in callback) { 1915490a39dSopenharmony_ci console.info('wakeupIntelligentVoiceEvent prop: ' + prop); 1925490a39dSopenharmony_ci console.info('wakeupIntelligentVoiceEvent value: ' + callback[prop]); 1935490a39dSopenharmony_ci } 1945490a39dSopenharmony_ci}); 1955490a39dSopenharmony_ci``` 1965490a39dSopenharmony_ci 1975490a39dSopenharmony_ci## Repositories Involved 1985490a39dSopenharmony_ci 1995490a39dSopenharmony_ciintelligent_voice_framework 200