Introduction
In this article, we are going to see how voice recognition can translate spoken words into text using closed captions to enable a person with hearing loss to understand what others are saying. It's most useful for students, employees, and client communications purposes rather than using voice commands instead of typing.
First, we have created a basic Angular application using Angular CLI.
Here I have used ngx Speech Recognition for custom plugins or we can create a separate library inside the application, and be able to use the newly created library.
Please refer to this link for angular custom library creations.
I have used Ngx Speech Recognition service library from the applications created as ngx-speech-recognition-test.

Then created main and sub-components. The main components are just using some project-related purpose or whatever content we need to add this main component. If you don’t need it, please ignore it. In the sub-components, we used the voice recognition part. Please check the following code.
Here I have added all sub-components codes.
Sub.component.ts
import {
Component
} from '@angular/core';
import {
ColorGrammar
} from './sub.component.grammar';
import {
SpeechRecognitionLang,
SpeechRecognitionMaxAlternatives,
SpeechRecognitionGrammars,
SpeechRecognitionService,
} from '../../../../../projects/ngx-speech-recognition-test/src/public_api';
@Component({
selector: 'test-sub',
templateUrl: './sub.component.html',
styleUrls: ['./sub.component.css'],
providers: [
// Dependency Inject to SpeechRecognitionService
// like this.
{
provide: SpeechRecognitionLang,
useValue: 'en-US',
}, {
provide: SpeechRecognitionMaxAlternatives,
useValue: 1,
}, {
provide: SpeechRecognitionGrammars,
useValue: ColorGrammar,
},
SpeechRecognitionService,
],
})
export class SubComponent {
public started = false;
public message = '';
constructor(private service: SpeechRecognitionService) {
// Dependency The injected services are displayed on the console.
// Dependence was resolved from SubComponent's provider.
this.service.onstart = (e) => {
console.log('voice start');
};
this.service.onresult = (e) => {
this.message = e.results[0].item(0).transcript;
console.log('voice message result', this.message, e);
};
}
start() {
this.started = true;
this.service.start();
}
stop() {
this.started = false;
this.service.stop();
}
}
SpeechRecognitionLang, SpeechRecognitionMaxAlternatives, SpeechRecognitionGrammars and SpeechRecognitionService all imported from ngx-speech-recognition-test.
We need to inject SpeechRecognitionServices. SpeechRecognitionLang provides mapping, we can map language code mentioned 'en-US'
SpeechRecognitionMaxAlternatives is Recognition voice count. I have marked 1.
SpeechRecognitionGrammars purpose checked to translate text for grammar validations.
ColorGrammar(Sub.component.grammar.ts). All the setup configurations are in the above codes.
Sub.component.grammar.ts
Sub.component.grammar.ts
/**
* @see https://developer.mozilla.org/ja/docs/Web/API/SpeechGrammarList#Examples
*/
export const ColorGrammar = new SpeechGrammarList();
ColorGrammar.addFromString(`#JSGF V1.0;
grammar colors;
public <color> = aqua | azure | beige | bisque |
black | blue | brown | chocolate | coral | crimson | cyan | fuchsia |
ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki |
lavender | lime | linen | magenta | maroon | moccasin | navy | olive |
orange | orchid | peru | pink | plum | purple | red | salmon | sienna |
silver | snow | tan | teal | thistle | tomato | turquoise |
violet | white | yellow ;
`, 1);




chirag joshiPosted Sep 28, 2022, 5:43 AM
Hi Satheesh, Sub.component.grammar.ts showing error in Mozilla. for other browser it's work fine.