forked from KingOfBrian/VocalKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVKPocketSphinxDecoder.m
More file actions
131 lines (103 loc) · 2.96 KB
/
VKPocketSphinxDecoder.m
File metadata and controls
131 lines (103 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// VKDecoder.m
// VocalKit
//
// Created by Brian King on 4/29/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "VKPocketSphinxDecoder.h"
#import "VKController.h"
static const arg_t vk_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." },
CMDLN_EMPTY_OPTION
};
@implementation VKPocketSphinxDecoder
@synthesize configFile;
- (id) initWithConfigFile:(NSString*)config {
self = [super init];
if (self) {
self.configFile = config;
_ps = nil;
_config = nil;
}
return self;
}
- (cmd_ln_t*) config {
if (_config) { return _config; }
_config = cmd_ln_parse_file_r(NULL, vk_args_def, [[self configFile] UTF8String], TRUE);
return _config;
}
- (void) setConfigString:(NSString*)str forKey:(NSString*)key {
cmd_ln_set_str_r([self config], [key UTF8String], [str UTF8String]);
}
- (void) setConfigInt:(int)iValue forKey:(NSString*)key {
cmd_ln_set_int_r([self config], [key UTF8String], iValue);
}
- (void) setConfigFloat:(float)fValue forKey:(NSString*)key {
cmd_ln_set_float_r([self config], [key UTF8String], fValue);
}
- (ps_decoder_t*) ps {
if (_ps) { return _ps; }
_ps = ps_init([self config]);
return _ps;
}
- (void) startDecode {
ps_start_utt([self ps], NULL);
}
- (void) stopDecode {
ps_end_utt([self ps]);
}
- (void) postNotificationOfRecognizedText {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
int32 score;
const char* uttid;
const char* hyp = ps_get_hyp([self ps], &score, &uttid);
[dnc postNotificationName:VKRecognizedPhraseNotification
object:self
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithCString:hyp encoding:[NSString defaultCStringEncoding]],
VKRecognizedPhraseNotificationTextKey,
[NSString stringWithCString:uttid encoding:[NSString defaultCStringEncoding]],
VKRecognizedPhraseNotificationIDKey,
[NSNumber numberWithInt:score],
VKRecognizedPhraseNotificationScoreKey,
nil]
];
}
- (void) recievePackets:(UInt32)packetCount fromBuffer:(AudioQueueBufferRef)buffer {
ps_process_raw([self ps], (int16*)buffer->mAudioData, packetCount, 1, 0);
}
- (void) gramarStates:(NSDictionary*)states transitions:(NSDictionary*)transitions named:(NSString*)name {
// for (NSString *name in [states allKeys]) {
// NSArray *words = [states objectForKey:name];
//
// for (id<NSObject> state in words) {
// if ([state isKindOfClass:[NSArray class]]) {
// // Multiple Pronunciations
// NSString* baseWord = nil;
// } else {
// // Single Pronunciation
// }
// }
// }
}
- (void) printDebug {
int32 score;
const char* uttid;
const char* hyp = ps_get_hyp([self ps], &score, &uttid);
NSLog(@"M=%s", hyp);
}
- (void) dealloc {
// Are we leaking _config?
if (_ps) {
ps_free(_ps);
}
[super dealloc];
}
@end