Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SOURCE_TYPE_FILENAME #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 141 additions & 78 deletions example/app/lib/ti.speech.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var recognitionTask;
var speechRecognizer;
var SOURCE_TYPE_URL = 'url';
var SOURCE_TYPE_MICROPHONE = 'microphone';
var SOURCE_TYPE_FILENAME = 'filename';

/**
* @function initialize
Expand Down Expand Up @@ -218,119 +219,180 @@ exports.startRecognition = function(args) {
request = null;
}

if (type == SOURCE_TYPE_URL) {
var url = args.url.split('.');
var ext = url.pop();
var soundPath = NSBundle.mainBundle.pathForResourceOfType(url.join('.'), ext);
var soundURL = NSURL.fileURLWithPath(soundPath);
switch (type) {
case SOURCE_TYPE_URL:
var url = args.url.split('.');
var ext = url.pop();
var soundPath = NSBundle.mainBundle.pathForResourceOfType(url.join('.'), ext);
var soundURL = NSURL.fileURLWithPath(soundPath);

request = SFSpeechURLRecognitionRequest.alloc().initWithURL(soundURL);
if (!request) {
console.error('Unable to created a SFSpeechURLRecognitionRequest object');
return false;
}

request = SFSpeechURLRecognitionRequest.alloc().initWithURL(soundURL);
if (!request) {
console.error('Unable to created a SFSpeechURLRecognitionRequest object');
return false;
}
request.shouldReportPartialResults = true;

request.shouldReportPartialResults = true;
if (!speechRecognizer) {
exports.initialize();
}

if (!speechRecognizer) {
exports.initialize();
}
recognitionTask = speechRecognizer.recognitionTaskWithRequestResultHandler(request, function(result, error) {

recognitionTask = speechRecognizer.recognitionTaskWithRequestResultHandler(request, function(result, error) {
if (!recognitionTask) {
// The recognitionTask has already been cancelled.
return;
}

if (!recognitionTask) {
// The recognitionTask has already been cancelled.
return;
}
if (recognitionTask.state === Speech.SFSpeechRecognitionTaskStateCanceling) {
// The recognitionTask is being cancelled so no progress should be reported after this.
console.info('The speech recognition task has been cancelled.');
progressCallback &&
progressCallback({
error: error,
value: result && result.bestTranscription.formattedString,
state: recognitionTask.state,
finished: true,
});

progressCallback = null;
request = null;
recognitionTask = null;
return;
}

if (recognitionTask.state === Speech.SFSpeechRecognitionTaskStateCanceling) {
// The recognitionTask is being cancelled so no progress should be reported after this.
console.info('The speech recognition task has been cancelled.');
progressCallback &&
progressCallback({
error: error,
value: result && result.bestTranscription.formattedString,
state: recognitionTask.state,
finished: true,
finished: result && result.isFinal(),
});

progressCallback = null;
request = null;
recognitionTask = null;
return;
if (error || (result && result.isFinal())) {
recognitionTask = null;
request = null;
return;
}
});

return true;
break;
case SOURCE_TYPE_MICROPHONE:
if (!audioEngine) {
audioEngine = new AVAudioEngine();
}

progressCallback &&
if (!audioEngine.inputNode) {
console.error('Audio engine has no input node');
return false;
}

request = new SFSpeechAudioBufferRecognitionRequest();
request.shouldReportPartialResults = true;

// Create recognition task that will listen to live speech and send progress to callback
recognitionTask = speechRecognizer.recognitionTaskWithRequestResultHandler(request, function(result, error) {

progressCallback({
error: error,
value: result && result.bestTranscription.formattedString,
state: recognitionTask.state,
finished: result && result.isFinal(),
});

if (error || (result && result.isFinal())) {
recognitionTask = null;
request = null;
return;
}
});
if (error || (result && result.isFinal())) {
if (audioEngine.isRunning()) {
audioEngine.stop();
}
if (request) {
request.endAudio();
}
audioEngine.inputNode.removeTapOnBus(0);
recognitionTask = null;
request = null;

return;
}
});

return true;
} else if (type == SOURCE_TYPE_MICROPHONE) {
audioEngine.inputNode.installTapOnBusBufferSizeFormatBlock(0, 1024, audioEngine.inputNode.outputFormatForBus(0), function(buffer, when) {
request && request.appendAudioPCMBuffer(buffer);
});

if (!audioEngine) {
audioEngine = new AVAudioEngine();
}
audioEngine.prepare();
var audioEngineStartError = new NSError();
var audioEngineStartSuccess = audioEngine.startAndReturnError(audioEngineStartError);
if (!audioEngineStartSuccess) {
//TODO: Do something with audioEngineStartError
return false;
}

if (!audioEngine.inputNode) {
console.error('Audio engine has no input node');
return false;
}
return true;
break;
case SOURCE_TYPE_FILENAME:
var url = args.url;
var soundURL = NSURL.URLWithString(url);

request = SFSpeechURLRecognitionRequest.alloc().initWithURL(soundURL);
if (!request) {
console.error('Unable to created a SFSpeechURLRecognitionRequest object');
return false;
}

request = new SFSpeechAudioBufferRecognitionRequest();
request.shouldReportPartialResults = true;
request.shouldReportPartialResults = true;

// Create recognition task that will listen to live speech and send progress to callback
recognitionTask = speechRecognizer.recognitionTaskWithRequestResultHandler(request, function(result, error) {
if (!speechRecognizer) {
exports.initialize();
}

progressCallback({
error: error,
value: result && result.bestTranscription.formattedString,
state: recognitionTask.state,
finished: result && result.isFinal(),
});
recognitionTask = speechRecognizer.recognitionTaskWithRequestResultHandler(request, function(result, error) {

if (error || (result && result.isFinal())) {
if (audioEngine.isRunning()) {
audioEngine.stop();
if (!recognitionTask) {
// The recognitionTask has already been cancelled.
return;
}
if (request) {
request.endAudio();

if (recognitionTask.state === Speech.SFSpeechRecognitionTaskStateCanceling) {
// The recognitionTask is being cancelled so no progress should be reported after this.
console.info('The speech recognition task has been cancelled.');
progressCallback &&
progressCallback({
error: error,
value: result && result.bestTranscription.formattedString,
state: recognitionTask.state,
finished: true,
});

progressCallback = null;
request = null;
recognitionTask = null;
return;
}
audioEngine.inputNode.removeTapOnBus(0);
recognitionTask = null;
request = null;

return;
}
});
progressCallback &&
progressCallback({
error: error,
value: result && result.bestTranscription.formattedString,
state: recognitionTask.state,
finished: result && result.isFinal(),
});

audioEngine.inputNode.installTapOnBusBufferSizeFormatBlock(0, 1024, audioEngine.inputNode.outputFormatForBus(0), function(buffer, when) {
request && request.appendAudioPCMBuffer(buffer);
});
if (error || (result && result.isFinal())) {
recognitionTask = null;
request = null;
return;
}
});

audioEngine.prepare();
var audioEngineStartError = new NSError();
var audioEngineStartSuccess = audioEngine.startAndReturnError(audioEngineStartError);
if (!audioEngineStartSuccess) {
//TODO: Do something with audioEngineStartError
return false;
}
return true;

return true;
} else {
console.error('Unhandled type supplied:' + type);
return false;
break;
default:
console.error('Unhandled type supplied:' + type);
return false;
break;
}
};

Expand All @@ -351,6 +413,7 @@ exports.stopRecognition = function() {
}
};

exports.SOURCE_TYPE_FILENAME = SOURCE_TYPE_FILENAME;
exports.SOURCE_TYPE_URL = SOURCE_TYPE_URL;
exports.SOURCE_TYPE_MICROPHONE = SOURCE_TYPE_MICROPHONE;
exports.RECOGNITION_STATE_STARTING = Speech.SFSpeechRecognitionTaskStateStarting;
Expand Down