Complete initial implementation of speech recognizer

This commit is contained in:
Gregory John Casamento 2020-02-04 05:26:52 -05:00
parent 8c35633a56
commit 72697f699c
6 changed files with 82 additions and 18 deletions

View file

@ -26,6 +26,7 @@
#define _NSSpeechRecognizer_h_GNUSTEP_GUI_INCLUDE
#import <Foundation/NSObject.h>
#import <AppKit/AppKitDefines.h>
#if OS_API_VERSION(MAC_OS_X_VERSION_10_0, GS_API_LATEST)
@ -34,7 +35,7 @@ extern "C" {
#endif
@protocol NSSpeechRecognizerDelegate;
@interface NSSpeechRecognizer : NSObject
{
id<NSSpeechRecognizerDelegate> _delegate;
@ -75,6 +76,8 @@ extern "C" {
didRecognizeCommand: (NSString *)command;
@end
APPKIT_EXPORT NSString *GSSpeechRecognizerDidRecognizeWordNotification;
#if defined(__cplusplus)
}
#endif

View file

@ -30,6 +30,8 @@
#import <Foundation/NSThread.h>
#import <Foundation/NSError.h>
#import <Foundation/NSConnection.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import "AppKit/NSWorkspace.h"
id _speechRecognitionServer = nil;
@ -58,12 +60,32 @@ BOOL _serverLaunchTested = NO;
}
}
- (void) processNotification: (NSNotification *)note
{
NSString *word = (NSString *)[note object];
NSEnumerator *en = [_commands objectEnumerator];
id obj = nil;
while((obj = [en nextObject]) != nil)
{
if ([[obj lowercaseString] isEqualToString: [word lowercaseString]])
{
[_delegate speechRecognizer: self
didRecognizeCommand: word];
}
}
}
// Initialize
- (instancetype) init
{
self = [super init];
if (self != nil)
{
[[NSDistributedNotificationCenter defaultCenter]
addObserver: self
selector: @selector(processNotification:)
name: GSSpeechRecognizerDidRecognizeWordNotification
object: nil];
}
return self;
}
@ -152,12 +174,12 @@ BOOL _serverLaunchTested = NO;
// Listening
- (void) startListening
{
// Do nothing...
[_speechRecognitionServer startListening];
}
- (void) stopListening
{
// Do nothing...
[_speechRecognitionServer stopListening];
}
@end

View file

@ -139,8 +139,9 @@ static Class NSSpeechSynthesizerClass;
// Wait for up to five seconds for the server to launch, then give up.
for (i=0 ; i<50 ; i++)
{
server = [[NSConnection rootProxyForConnectionWithRegisteredName: @"GSSpeechServer"
host: nil] retain];
server = [NSConnection rootProxyForConnectionWithRegisteredName: @"GSSpeechServer"
host: nil];
RETAIN(server);
if (nil != server)
{
break;

View file

@ -812,6 +812,10 @@ const NSAppearanceName NSAppearanceNameAccessibilityHighContrastVibrantDark =
@"NSAppearanceNameAccessibilityHighContrastVibrantDark";
const NSAppearanceName NSAppearanceNameLightContent = @"NSAppearanceNameLightContent";
// Speech recognition...
const NSString *GSSpeechRecognizerDidRecognizeWordNotification = @"GSSpeechRecognizerDidRecognizeWordNotification";
extern void __objc_gui_force_linking (void);
void

View file

@ -5,10 +5,10 @@
*/
@implementation GSSpeechRecognitionEngine
+ (GSSpeechRecognitionEngine*)defaultSpeechEngine
+ (GSSpeechRecognitionEngine*) defaultSpeechEngine
{
return [[self new] autorelease];
}
return AUTORELEASE([[self alloc] init]);
}
- (void) startListening
{

View file

@ -1,4 +1,5 @@
#import "GSSpeechRecognitionEngine.h"
#import <Foundation/NSDistributedNotificationCenter.h>
#include <sphinxbase/err.h>
#include <sphinxbase/ad.h>
@ -11,6 +12,32 @@
#define MODELDIR "/share/pocketsphinx/model"
static const arg_t cont_args_def[] = {
POCKETSPHINX_OPTIONS,
/* Argument file. */
{"-argfile",
ARG_STRING,
NULL,
"Argument file giving extra arguments."},
{"-adcdev",
ARG_STRING,
NULL,
"Name of audio device to use for input."},
{"-infile",
ARG_STRING,
NULL,
"Audio file to transcribe."},
{"-inmic",
ARG_BOOLEAN,
"no",
"Transcribe audio from microphone."},
{"-time",
ARG_BOOLEAN,
"no",
"Print word times in file transcription."},
CMDLN_EMPTY_OPTION
};
@interface PocketsphinxSpeechRecognitionEngine : GSSpeechRecognitionEngine
{
ps_decoder_t *ps;
@ -35,12 +62,20 @@
{
if ((self = [super init]) != nil)
{
config = cmd_ln_init(NULL, ps_args(), TRUE,
"-hmm", MODELDIR "/en-us/en-us",
"-lm", MODELDIR "/en-us/en-us.lm.bin",
"-dict", MODELDIR "/en-us/cmudict-en-us.dict",
NULL);
char *arg[3];
arg[0] = "";
arg[1] = "-inmic";
arg[2] = "yes";
config = cmd_ln_parse_r(NULL, cont_args_def, 3, arg, TRUE);
ps_default_search_args(config);
ps = ps_init(config);
if (ps == NULL)
{
cmd_ln_free_r(config);
NSLog(@"Could not start server");
return nil;
}
_listeningThread = nil;
}
return self;
@ -48,7 +83,10 @@
- (void) _recognizedWord: (NSString *)word
{
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName: GSSpeechRecognizerDidRecognizeWordNotification
object: word
userInfo: nil];
}
/*
@ -112,10 +150,6 @@
ad_close(ad);
}
- (void) _startProcessing
{
}
- (void) startListening
{
[NSThread detachNewThreadSelector: @selector(recognize)