Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Ecer committed Jan 13, 2017
0 parents commit 0c94db0
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2017 Daniel Ecer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Web Speech API for React Native
===============================

This module attempts to provide the Web Speech API for React Native.

The main purpose of the module is allow common web and pre-existing code to use the Web Speech API.

This is currently in a very early stage:

* Only Android supported at present
* Only [SpeechRecognition API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition) is mostly implemented

Installation
============
`npm install react-native-web-speech-api`

Dependencies
============
[react-native-android-speech-recognizer](https://www.npmjs.com/package/react-native-android-speech-recognizer)
is used to provide the Android React Native module.

Please follow its installation instructions.

Usage
=====

```javascript
import {
isSpeechRecognitionSupported,
SpeechRecognition
} from 'react-native-web-speech-api';

if (isSpeechRecognitionSupported()) {
const recognition = new SpeechRecognition();
recognition.onerror = event =>
console.log("Error:", event.error);
recognition.onresult = event =>
console.log("Result:", event.results[0][0].transcript);
}
```
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// use ES5/commonjs for web version

var index = require('./src/index');

module.exports = index;
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "react-native-web-speech-api",
"repository": "de-code/react-native-web-speech-api",
"version": "0.0.1",
"description": "Web Speech API for React Native",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Daniel Ecer",
"license": "MIT",
"dependencies": {
"react-native-android-speech-recognizer": "0.0.1"
},
"peerDependencies": {
"react-native": "^0.29.0"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10"
}
}
60 changes: 60 additions & 0 deletions src/index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
SpeechRecognizer,
RecognizerIntent,
RecognitionListener
} from 'react-native-android-speech-recognizer';

export const isSpeechRecognitionSupported = () =>
!!SpeechRecognizer;

export class SpeechRecognition {
abort() {
if (this._recognizer) {
this._recognizer.destroy();
}
}

start() {
if (this._recognizer) {
this._recognizer.destroy();
this._recognizer = null;
}
SpeechRecognizer.createSpeechRecognizer().then(recognizer => {
this._recognizer = recognizer;
const listeners = {};
if (this.onspeechstart) {
listeners.onBeginningOfSpeech = () => this.onspeechstart();
}
if (this.onspeechend) {
listeners.onEndOfSpeech = () => this.onspeechend();
}
if (this.onerror) {
listeners.onError = event => this.onerror({
error: "Failed with error code: " + event.error
});
}
listeners.onResults = event => {
const recognition = event.results[SpeechRecognizer.RESULTS_RECOGNITION];
const bestRecognition = recognition[0];
const speechRecognitionEvent = {
resultIndex: 0,
results: [[{
transcript: bestRecognition
}]],
}
speechRecognitionEvent.results[0].isFinal = true;
if (this.onresult) {
this.onresult(speechRecognitionEvent);
}
}
recognizer.setRecognitionListener(listeners);
recognizer.startListening(RecognizerIntent.ACTION_RECOGNIZE_SPEECH, {});
});
}

stop() {
if (this._recognizer) {
this._recognizer.cancel();
}
}
}
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var isSpeechRecognitionSupported = function() {
window && 'webkitSpeechRecognition' in window;
}

var SpeechRecognition = window && window.webkitSpeechRecognition;

module.exports = {
isSpeechRecognitionSupported: isSpeechRecognitionSupported,
SpeechRecognition: SpeechRecognition
}

0 comments on commit 0c94db0

Please sign in to comment.