CakeFest 2025 Madrid: The Official CakePHP Conference

Voting

: six plus one?
(Example: nine)

The Note You're Voting On

veggie
8 years ago
I got voice recognition working. I'm not sure why the way I called the sink function made it work but I'm more about results right now. This small example had me rolling on the floor laughing.

<?php
/*
* Search this for more info on the voice stuff:
* Automation Interfaces and Objects (SAPI 5.4)
*/

//directions:
//php friend.php
//then fire up windows voice recognition and turn it on and say stuff

$voice = new COM("SAPI.SpVoice");

print
"Hit control+c to end.\n";
print
"Friend: Hello friend!\n";

$voice->Speak("Hello friend!");

class
listen
{

function
Recognition($StreamNumber, $StreamPosition, $RecognitionType, $ISpeechRecoResult)
{

$phrase = $ISpeechRecoResult->PhraseInfo;
$text = $phrase->GetText();

print
"\nYou:$text\n";


global
$voice;
$say = array('oh', 'nice', 'humm', 'interesting', 'you dont say', 'uh huh', 'right', 'what', 'ha ha', 'you have got to be joking', 'right back at you buddy');
$idx = rand(0, count($say)-1);

print
"Friend: " . $say[$idx] . "\n";
$voice->Speak($say[$idx]);

}

}

$recog = new COM("SAPI.SpSharedRecognizer");

$context = $recog->CreateRecoContext();

//SRERecognition = 16 (default)
//SREAllEvents = 393215
//$context->EventInterests = 393215;

//try to listen to events on context
$listen = new listen(); //event handler
if (!com_event_sink($context, $listen, "RecognizerStateChange"))
{
print
"Unable to sink events\n";
exit;
}

$grammar = $context->CreateGrammar();

$i = $grammar->DictationLoad();

$s = $grammar->DictationSetState(1); //1=on, 0=off

while(true)
{

if(!
com_message_pump(1000))
{
print
".";
}

}

?>

<< Back to user notes page

To Top