Skip to content

Commit

Permalink
build 7
Browse files Browse the repository at this point in the history
  • Loading branch information
lorabit committed Nov 21, 2017
1 parent fabf2b1 commit 77185e1
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 51 deletions.
2 changes: 1 addition & 1 deletion SlugBot/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>4</string>
<string>7</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSContactsUsageDescription</key>
Expand Down
13 changes: 0 additions & 13 deletions SlugBot/Modules/EmotionModule/EmotionModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#define IDLE_INTERVAL 100

@implementation EmotionModule{
NSTimer * timer;
double last_update;
SCChatbotResponse_Emotion current_emotion;
NSDictionary<NSNumber*, NSArray<NSString*>*>* emotion_images;
Expand Down Expand Up @@ -46,12 +45,6 @@ -(instancetype)init{
@(SCChatbotResponse_Emotion_Sad):@[@"sad"],
@(SCChatbotResponse_Emotion_Happy):@[@"happy"],
};
WS(wSelf);
timer = [NSTimer scheduledTimerWithTimeInterval:ROUND_CHECK_INTERVAL repeats:YES block:^(NSTimer * _Nonnull timer) {
if(wSelf){
[wSelf updateEmtion];
}
}];
}
return self;
}
Expand All @@ -67,12 +60,6 @@ -(void)setEmotion:(SCChatbotResponse_Emotion)emotion{
}

-(void)updateEmtion{
if(current_emotion == _emotion){
if(_emotion!=SCChatbotResponse_Emotion_Sleep && [NSDate dateWithTimeIntervalSinceNow:0].timeIntervalSince1970 - last_update>IDLE_INTERVAL){
[self setEmotion:SCChatbotResponse_Emotion_Sleep];
}
return;
}
current_emotion = _emotion;
last_update = [NSDate dateWithTimeIntervalSinceNow:0].timeIntervalSince1970;
if(self.delegate){
Expand Down
2 changes: 2 additions & 0 deletions SlugBot/Modules/MobileService/MobileService.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

//#define HOST @"10.0.0.213:50051"
#define HOST @"ec2-34-216-5-83.us-west-2.compute.amazonaws.com:50051"
//#define HOST @"slugchat.lorabit.com:50051"


#else

Expand Down
28 changes: 14 additions & 14 deletions SlugBot/Modules/SpeechRecognitionModule/SpeechRecognitionModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @implementation SpeechRecognitionModule{
IFlySpeechRecognizer *iFlySpeechRecognizer;
IFlyPcmRecorder *pcmRecorder;
NSString* tmpResult;
BOOL stopCallingDelegate;
}


Expand Down Expand Up @@ -75,11 +76,11 @@ -(void)initRecognizer{
if (iFlySpeechRecognizer != nil) {

//设置最长录音时间
[iFlySpeechRecognizer setParameter:@"3000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
[iFlySpeechRecognizer setParameter:@"-1" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
//设置后端点
[iFlySpeechRecognizer setParameter:@"2000" forKey:[IFlySpeechConstant VAD_EOS]];
//设置前端点
[iFlySpeechRecognizer setParameter:@"5000" forKey:[IFlySpeechConstant VAD_BOS]];
[iFlySpeechRecognizer setParameter:@"30000" forKey:[IFlySpeechConstant VAD_BOS]];
//网络等待时间
[iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];

Expand All @@ -92,7 +93,7 @@ -(void)initRecognizer{
[iFlySpeechRecognizer setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];

//设置是否返回标点符号
[iFlySpeechRecognizer setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
[iFlySpeechRecognizer setParameter:[IFlySpeechConstant ASR_PTT_HAVEDOT] forKey:[IFlySpeechConstant ASR_PTT]];

}

Expand All @@ -111,6 +112,7 @@ -(void)initRecognizer{
}

-(void)start{
stopCallingDelegate = NO;
[self initRecognizer];

[iFlySpeechRecognizer setParameter:@"json" forKey:[IFlySpeechConstant RESULT_TYPE]];
Expand All @@ -128,7 +130,7 @@ -(void)start{
//启动录音器服务
BOOL ret = [pcmRecorder start];
NSLog(@"%s[OUT],Success,Recorder ret=%d",__func__,ret);
if(self.delegate){
if(!stopCallingDelegate && self.delegate){
[self.delegate onStart];
}
}
Expand All @@ -140,9 +142,9 @@ -(void)start{
}

-(void)stop{
stopCallingDelegate = YES;
[iFlySpeechRecognizer stopListening];
[pcmRecorder stop];
[self.delegate onEndWithResult:@""];
}

-(BOOL)isListening{
Expand Down Expand Up @@ -171,7 +173,7 @@ -(void)onResults:(NSArray *)results isLast:(BOOL)isLast{
NSLog(@"%@\n",resultFromJson);
tmpResult = [tmpResult stringByAppendingString:resultFromJson];
}
if(self.delegate != nil && isLast){
if(!stopCallingDelegate && self.delegate != nil && isLast){
[self.delegate onEndWithResult:tmpResult];
}
}
Expand All @@ -186,14 +188,12 @@ -(void)onIFlyRecorderBuffer:(const void *)buffer bufferSize:(int)size{
NSData *audioBuffer = [NSData dataWithBytes:buffer length:size];

int ret = [iFlySpeechRecognizer writeAudio:audioBuffer];
if (!ret)
{
[iFlySpeechRecognizer stopListening];
[pcmRecorder stop];
if(self.delegate){
[self.delegate onEndWithResult:@""];
}
}
// if (!ret)
// {
// [iFlySpeechRecognizer stopListening];
// [pcmRecorder stop];
//
// }
}

-(void)onIFlyRecorderError:(IFlyPcmRecorder *)recoder theError:(int)error{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ -(void)startWithText:(NSString *)text{
}

-(void)stop{
_progress = 0;
_beginPos = 0;
_endPos = 0;
if(iFlySpeechSynthesizer){
[iFlySpeechSynthesizer stopSpeaking];
}
Expand Down
88 changes: 69 additions & 19 deletions SlugBot/ViewController/ChatbotViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#import "EmotionModule.h"

@interface ChatbotViewController ()<
SpeechRecognizerDelegate,
SpeechSynthesizerDelegate,
SpeechRecognizerDelegate,
SpeechSynthesizerDelegate,
EmotionDelegate
>

Expand All @@ -24,13 +24,16 @@ @interface ChatbotViewController ()<
@implementation ChatbotViewController{
UIButton * btn;
UIImageView* emotionView;
BOOL isExiting;
NSDate* lastSpeechTime;
}

- (void)viewDidLoad {
[super viewDidLoad];
isExiting = NO;
self.title = [NSString stringWithFormat:@"%@ - %@",LocalizedStr(@"ChatbotTitle"),[SBUser user].profileName];
self.view.backgroundColor = [UIColor whiteColor];

emotionView = [UIImageView new];
[self.view addSubview:emotionView];

Expand All @@ -42,7 +45,7 @@ - (void)viewDidLoad {
[[SpeechRecognitionModule module] setDelegate:self];
[[SpeechSynthesizerModule module] setDelegate:self];
[[EmotionModule module] setDelegate:self];
[[EmotionModule module] setEmotion:SCChatbotResponse_Emotion_Normal];
[[EmotionModule module] setEmotion:SCChatbotResponse_Emotion_Sleep];


btn = [UIButton buttonWithType:UIButtonTypeCustom];
Expand All @@ -64,31 +67,46 @@ -(void)viewWillAppear:(BOOL)animated{
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[UIApplication sharedApplication].idleTimerDisabled = NO;
[self exit];
}

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self interactWithMobileService:@"你好!"];
[self interactWithMobileService:@"$start"];
}

-(void)exit{
isExiting = YES;
[[SpeechRecognitionModule module] stop];
[[SpeechSynthesizerModule module] stop];
}

-(void)hitBtn{
if([SpeechRecognitionModule module].isListening){
[[SpeechRecognitionModule module] stop];
[self interactWithMobileService:@"$hit"];
return;
}
if([SpeechSynthesizerModule module].endPos>0){
SCLog* log = [SCLog new];
log.profileId = [SBUser user].profileId;
log.logType = SCLog_LogType_InterruptSpeech;
log.content = [NSString stringWithFormat:@"%d,%d,%d",[SpeechSynthesizerModule module].progress,[SpeechSynthesizerModule module].beginPos,[SpeechSynthesizerModule module].endPos];
[[MobileService service] createLogWithRequest:log
handler:^(SCLog * _Nullable response, NSError * _Nullable error) {
if(error){
NSLog(@"%@",error.localizedDescription);
}
}];

if([SpeechSynthesizerModule module].beginPos<20){
return;
}

SCLog* log = [SCLog new];
log.profileId = [SBUser user].profileId;
log.logType = SCLog_LogType_InterruptSpeech;
log.content = [NSString stringWithFormat:@"%d,%d,%d",[SpeechSynthesizerModule module].progress,[SpeechSynthesizerModule module].beginPos,[SpeechSynthesizerModule module].endPos];

[[SpeechSynthesizerModule module] stop];
[[SpeechRecognitionModule module] start];

[[MobileService service] createLogWithRequest:log
handler:^(SCLog * _Nullable response, NSError * _Nullable error) {
if(error){
NSLog(@"%@",error.localizedDescription);
}
}];

}

- (void)didReceiveMemoryWarning {
Expand All @@ -98,28 +116,56 @@ - (void)didReceiveMemoryWarning {


-(void)onStart{
// [btn setTitle:@"Listening..." forState:UIControlStateNormal];
// [btn setTitle:@"Listening..." forState:UIControlStateNormal];

[[EmotionModule module] setEmotion:SCChatbotResponse_Emotion_Listening];
}

-(void)onEndWithResult:(NSString *)text{
// NSLog(@"%@\n",text);
// [btn setTitle:text forState:UIControlStateNormal];
if(isExiting) {
return;
}
// NSLog(@"%@\n",text);
// [btn setTitle:text forState:UIControlStateNormal];
if(text.length == 0){
[self noSpeech];
return;
}
[self interactWithMobileService:text];
}

-(void)noSpeech{
if(lastSpeechTime!=nil && [NSDate date].timeIntervalSince1970 - lastSpeechTime.timeIntervalSince1970 > 30){
[self interactWithMobileService:@"$noSpeech{30}"];
}else{
[self interactWithMobileService:@""];
}
}

-(void)interactWithMobileService:(NSString*) text{
if(isExiting) {
return;
}
if([text length] == 0){
[[SpeechRecognitionModule module] start];
return;
}
lastSpeechTime = [NSDate date];
btn.enabled = NO;
SCUserRequest * userRequest = [SCUserRequest new];
[userRequest setProfileId:[SBUser user].profileId];
[userRequest setText:text];
#ifdef DEBUG
NSDate * startDate = [NSDate date];
#endif
[[MobileService service] getChatbotResponseWithRequest:userRequest
handler:^(SCChatbotResponse * _Nullable response, NSError * _Nullable error) {
#ifdef DEBUG
NSLog(@"Response time: %.4f s\n", [NSDate date].timeIntervalSince1970 - startDate.timeIntervalSince1970);
#endif
if(isExiting) {
return;
}
btn.enabled = YES;
if(error){
NSLog(@"%@\n", [error localizedDescription]);
Expand All @@ -145,10 +191,14 @@ -(void)interactWithMobileService:(NSString*) text{
//}

-(void)onSyncStop:(BOOL)hasError{
if(isExiting) {
return;
}
[[SpeechRecognitionModule module] start];
}

-(void)onUpdateEmotionImage:(UIImage *)image{
[emotionView setImage:image];
}
@end

12 changes: 8 additions & 4 deletions SlugBot/ViewController/ProfileListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,18 @@ -(void)reloadData{
}
profiles = [response profilesArray];
if(profiles.count == 0){
[self hitAddProfileButton];
[self createProfileWithName:LocalizedStr(@"DefaultProfileName")];
}
[collectionView reloadData];
}];
}

-(void)createProfileWithName:(NSString*)name{
AvatarListViewController * avatarListViewController = [AvatarListViewController new];
avatarListViewController.name = name;
[self.navigationController pushViewController:avatarListViewController animated:YES];
}

-(void)hitAddProfileButton{
UIAlertController* alert =
[UIAlertController alertControllerWithTitle:LocalizedStr(@"AddProfileAlertTitle")
Expand All @@ -149,9 +155,7 @@ -(void)hitAddProfileButton{
handler:^(UIAlertAction * _Nonnull action) {
UITextField * nameTextField = [alert textFields][0];
if(nameTextField.text.length>0){
AvatarListViewController * avatarListViewController = [AvatarListViewController new];
avatarListViewController.name = nameTextField.text;
[wSelf.navigationController pushViewController:avatarListViewController animated:YES];
[wSelf createProfileWithName:nameTextField.text];
}
}];
UIAlertAction * cancelAction =
Expand Down
2 changes: 2 additions & 0 deletions SlugBot/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ AddProfileAlertActionOK = "OK";
AddProfileAlertActionCancel = "Cancel";
AddProfileAlertPlaceholder = "Nickname";

DefaultProfileName = "Micro Slug";

AvatarListTitle = "Pick An Avatar";

ChatbotTitle = "SlugChat";
Expand Down
2 changes: 2 additions & 0 deletions SlugBot/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ AddProfileAlertActionOK = "好的";
AddProfileAlertActionCancel = "取消";
AddProfileAlertPlaceholder = "昵称";

DefaultProfileName = "小小虫";

AvatarListTitle = "选择一个头像";

ChatbotTitle = "一只小虫";
Expand Down

0 comments on commit 77185e1

Please sign in to comment.