Skip to content

Commit

Permalink
startSessionHandler added
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis090909 committed Apr 14, 2021
1 parent 8975e28 commit ea266a2
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ -(NSString *)request {

-(void)inspect:(NSData *)sound withCompletionHandler:(CompletionHandler)completionHandler {
self.voiceData = [NSData dataWithData:sound];
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ typedef void (^CompletionHandler) ( NSError *error, NSObject *result);
/**
* Obtains data with current networking request
*/
-(void)obtainWithCompletionHandler:(CompletionHandler)completionHandler;
-(void)obtainWithCompletionHandler:(CompletionHandler)completionHandler startSessionHandler:(void (^)(void)) startSessionHandler;

/**
* Starts prerequest
*/
-(void)startingRequest;
-(void)startingRequest:(void (^)(void)) startSessionHandler;

/**
* Returns the current request url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ @interface STCNetworkingManager()
@interface STCNetworkingManager(Private)

-(NSDictionary *)authorizationDataAsBody;
-(void)startSession;
-(void)startSession:(void (^)(void)) startSessionHandler;
-(void)closeSession;

@end
Expand All @@ -42,14 +42,14 @@ -(id)init {
return self;
}

-(void)obtainWithCompletionHandler:(CompletionHandler)completionHandler{
-(void)obtainWithCompletionHandler:(CompletionHandler)completionHandler startSessionHandler:(void (^)(void)) startSessionHandler{
self.completionHandler = completionHandler;
[self startingRequest];
[self startingRequest: startSessionHandler];
}

-(void)startingRequest {
-(void)startingRequest:(void (^)(void)) startSessionHandler {
if ( self.sessionId == nil ) {
[self startSession];
[self startSession: startSessionHandler];
return;
}
[self progressRequest];
Expand Down Expand Up @@ -102,7 +102,7 @@ -(void)progressRequest {

@implementation STCNetworkingManager(Private)

-(void)startSession {
-(void)startSession:(void (^)(void)) startSessionHandler {
NSLog(@"start session %@",self.authorizationDataAsBody);
NSURLSessionDataTask *task = [self taskPostRequestForURLString:self.sessionRequest
withBody:self.authorizationDataAsBody
Expand All @@ -116,11 +116,17 @@ -(void)startSession {
self.completionHandler(responseError, nil);
return ;
}



NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
self.sessionId = json[kSessionIdURLParam];

if (startSessionHandler != nil) {
startSessionHandler();
}

[self progressRequest];
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ -(NSString *)request {
-(void)diarize:(NSData *)sound
withCompletionHandler:(CompletionHandler)completionHandler {
self.voiceData = [NSData dataWithData:sound];
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@implementation STCASRPackageListNetworkingManager

-(void)obtainPackagesWithCompletionHandler:(CompletionHandler)completionHandler {
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}

-(NSString *)request {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ -(NSString *)request {
-(void)recognizeSpeech:(NSString *)speech64Base
withCompletionHandler:(CompletionHandler)completionHandler {
self.voice = speech64Base;
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}

-(void)recognizeSpeech:(NSString *)speech64Base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* @param completionHandler The recognition completion handler
*/
-(void)startStreamWithPackage:(NSString *)package
withCompletionHandler:(CompletionHandler)completionHandler;
withCompletionHandler:(CompletionHandler)completionHandler
startSessionHandler:(void (^)(void)) startSessionHandler;

/**
* Closes the stream transaction and provides the finished result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ -(void)recognize {
-(void)startStreamWithCompletionHandler:(CompletionHandler)completionHandler {
self.package = nil;
self.completionHandler = completionHandler;
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}

-(void)startStreamWithPackage:(NSString *)package
withCompletionHandler:(CompletionHandler)completionHandler {
withCompletionHandler:(CompletionHandler)completionHandler
startSessionHandler:(void (^)(void)) startSessionHandler{
self.package = package;
self.completionHandler = completionHandler;
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:startSessionHandler];
}

-(void)closeStreamWithCompletionHandler:(CompletionHandler)completionHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ -(void)startWithCompletionHandler:(RecognizingCompletionHandler)completionHandle
[self.voiceManager record];
}

-(void)startWithPackage:(NSDictionary *)package withCompletionHandler:(RecognizingCompletionHandler)completionHandler {
-(void)startWithPackage:(NSDictionary *)package withCompletionHandler:(RecognizingCompletionHandler)completionHandler startSessionHandler:(void (^)(void)) startSessionHandler {
self.package = package;
[self configureVoicerManagerWithSampleRate:[package[@"sample_rate"] integerValue]];
self.recognizeCompletionHandler = completionHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ -(void)streamWithCompletionHandler:(RecognizeCompletionHandler)completionHandler
}

-(void)streamWithPackage:(NSString *)package
withCompletionHandler:(RecognizeCompletionHandler)completionHandler {
withCompletionHandler:(RecognizeCompletionHandler)completionHandler
startSessionHandler:(void (^)(void)) startSessionHandler{
self.streamCompletionHandler = completionHandler;
self.streamNetworkingManager = [[STCASRStreamNetworkingManager alloc] init];
[self.streamNetworkingManager startStreamWithPackage:package withCompletionHandler:^(NSError *error, NSObject *result) {
[self.streamNetworkingManager startStreamWithPackage:package withCompletionHandler:^(NSError *error, NSObject *result){
NSDictionary *recognizeResult = (NSDictionary *)result;
self.streamCompletionHandler(error, recognizeResult);
}];
} startSessionHandler: startSessionHandler];
}

-(void)closeStreamWithCompletionHandler:(RecognizeCompletionHandler)completionHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ @interface STCStreamRecognizer()
@interface STCStreamRecognizer(Configure)

-(void)configureVoiceManager;
-(void)configureRecognizeKit;
-(void)configureRecognizeKit:(void (^)(void)) startSessionHandler;

@end

Expand Down Expand Up @@ -59,13 +59,13 @@ -(id)init {

-(void)startWithCompletionHandler:(RecognizingCompletionHandler)completionHandler {
self.package = nil;
[self configureRecognizeKit];
[self configureRecognizeKit: nil];
self.recognizeCompletionHandler = completionHandler;
}

-(void)startWithPackage:(NSDictionary *)package withCompletionHandler:(RecognizingCompletionHandler)completionHandler{
-(void)startWithPackage:(NSDictionary *)package withCompletionHandler:(RecognizingCompletionHandler)completionHandler startSessionHandler:(void (^)(void)) startSessionHandler{
self.package = package;
[self configureRecognizeKit];
[self configureRecognizeKit: startSessionHandler];
self.recognizeCompletionHandler = completionHandler;
}

Expand Down Expand Up @@ -166,14 +166,14 @@ -(void)configureVoiceManager {
};
}

-(void)configureRecognizeKit {
-(void)configureRecognizeKit:(void (^)(void)) startSessionHandler {
__weak typeof(self) weakself = self;
self.recognizeKit = [[STCRecognizeKitImplementation alloc] init];
if (self.package!=nil) {
[self.recognizeKit streamWithPackage:self.package[@"package_id"]
withCompletionHandler:^(NSError *error, NSDictionary *result) {
[weakself handleResult:result withError:error];
}];
} startSessionHandler: startSessionHandler];
} else {
[self.recognizeKit streamWithCompletionHandler:^(NSError *error, NSDictionary *result) {
[weakself handleResult:result withError:error];
Expand Down
3 changes: 2 additions & 1 deletion SpeechproSpeechKit/SpeechproSpeechKit/STCRecognizeKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ withCompletionHandler:(RecognizeCompletionHandler)completionHandler;
* @param completionHandler The response block called when the result is received
*/
-(void)streamWithPackage:(NSString *)package
withCompletionHandler:(RecognizeCompletionHandler)completionHandler;
withCompletionHandler:(RecognizeCompletionHandler)completionHandler
startSessionHandler:(void (^)(void)) startSessionHandler;

/**
* Closes the socket
Expand Down
3 changes: 2 additions & 1 deletion SpeechproSpeechKit/SpeechproSpeechKit/STCRecognizing.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ typedef void (^PeakPowerHandler) (Float32 peakPower);
* @param completionHandler The recognition completion handler
*/
-(void)startWithPackage:(NSDictionary *)package
withCompletionHandler:(RecognizingCompletionHandler)completionHandler;
withCompletionHandler:(RecognizingCompletionHandler)completionHandler
startSessionHandler:(void (^)(void)) startSessionHandler;

/**
* Stops recording and recognizing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@implementation STCTTSLanguageNetworkingManager

-(void)obtainLanguagesWithCompletionHandler:(CompletionHandler)completionHandler {
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}

-(NSString *)request {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* @param completionHandler The synthesis completion handler
*/
-(void)streamWithVoice:(NSString *)voice
withCompletionHandler:(CompletionHandler)completionHandler;
withCompletionHandler:(CompletionHandler)completionHandler
startSessionHandler:(void (^)(void)) startSessionHandler;

/**
* Closes the stream transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ -(NSString *)request {
}

-(void)streamWithVoice:(NSString *)voice
withCompletionHandler:(CompletionHandler)completionHandler {
withCompletionHandler:(CompletionHandler)completionHandler
startSessionHandler:(void (^)(void)) startSessionHandler{
self.voice = voice;
self.completionHandler = completionHandler;

[self startingRequest];
[self startingRequest: startSessionHandler];
}

-(void)progressRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ -(void)synthesizeText:(NSString *)text

NSLog(@"%@",[text language]);

[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ @implementation STCTTSVoiceNetworkingManager
-(void)obtainVoicesForLanguage:(NSString *)language
withCompletionHandler:(CompletionHandler)completionHandler {
self.language = language;
[self obtainWithCompletionHandler:completionHandler];
[self obtainWithCompletionHandler:completionHandler startSessionHandler:nil];
}

-(NSString *)request {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ -(void)streamWithVoice:(NSString *)voice

NSString *url = [((NSDictionary *)result) objectForKey:@"url"];
self.streamCompletionHandler(nil, url);
}];
} startSessionHandler:nil];
}

-(void)closeStream:(StreamCompletionHandler)completionHandler {
Expand Down

0 comments on commit ea266a2

Please sign in to comment.