游雁
2024-02-19 94de39dde2e616a01683c518023d0fab72b4e103
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
132
133
134
135
136
137
138
139
140
141
142
143
/**
 * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
 * Reserved. MIT License  (https://opensource.org/licenses/MIT)
 */
 
//
//  ViewController.m
//  paraformer_online
//
//  Created by 邱威 on 2023/6/6.
//
 
#import "ViewController.h"
 
#include "AudioCapture.h"
 
@interface ViewController ()<UITextViewDelegate>
 
@property (nonatomic, strong) AudioCapture *audioCapture;
@property (nonatomic, copy) NSString *pre_partial_text;
 
@property (weak, nonatomic) IBOutlet UITextView *resultView;
@property (weak, nonatomic) IBOutlet UIButton *startButton;
 
@property (strong, nonatomic) UIView *coverView;
@property (strong, nonatomic) UIButton *launchOnnxButton;
@property (strong, nonatomic) UIButton *launchButton;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
 
 
@end
 
@implementation ViewController
{
    bool is_mic_;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.startButton.layer.cornerRadius = 60;
    self.startButton.layer.masksToBounds = YES;
    self.startButton.selected = NO;
    self.resultView.editable = NO;
    
    is_mic_ = true;
    
    [self.view addSubview:self.coverView];
    [self initEngine:YES];
}
 
- (UIView *)coverView {
    if (!_coverView) {
        NSInteger screenWidth = self.view.bounds.size.width;
        NSInteger screenHeight = self.view.bounds.size.height;
        _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
        _coverView.backgroundColor = [UIColor whiteColor];
        
        NSInteger x = 30;
        NSInteger heigh = 60;
        NSInteger y = screenHeight/2-120;
        NSInteger width = (screenWidth-30*2);
        
        UILabel *tip = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, heigh)];
        tip.backgroundColor = [UIColor clearColor];
        tip.textColor = [UIColor grayColor];
        tip.font = [UIFont systemFontOfSize:20];
        tip.textAlignment = NSTextAlignmentCenter;
        tip.text = @"正在初始化模型...";
        [_coverView addSubview:tip];
        _coverView.alpha = 0.8;
    }
    
    return _coverView;
}
 
- (void)initEngine:(BOOL)onnxModel {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSInteger screenWidth = self.view.bounds.size.width;
        NSInteger screenHeight = self.view.bounds.size.height;
        NSInteger activityIndicatorWith = screenWidth/2;
        NSInteger activityIndicatorHeight = activityIndicatorWith;
    //    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((screenWidth-activityIndicatorWith)/2, (screenHeight-activityIndicatorHeight)/2, activityIndicatorWith, activityIndicatorHeight)];
        self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
        self.activityIndicator.frame = CGRectMake((screenWidth-activityIndicatorWith)/2, (screenHeight-activityIndicatorHeight)/2, activityIndicatorWith, activityIndicatorHeight);
        [self.view addSubview:self.activityIndicator];
        [self.activityIndicator startAnimating];
        
        
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self initASR:onnxModel];
    });
}
 
- (void)initASR:(BOOL)onnxModel {
    if (is_mic_) {
        self.pre_partial_text = @"";
        self.audioCapture = [[AudioCapture alloc] initWithOnnxModel:onnxModel];
        __weak __typeof(self) weakSelf = self;
 
        self.audioCapture.resultBlock = ^(NSString *result) {
            if ([result length] == 0) {
                return;
            }
            NSLog(@"%@", result);
            NSString *text = nil;
            text = [NSString stringWithFormat:@"%@%@", weakSelf.pre_partial_text, result];
            weakSelf.pre_partial_text = text;
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.resultView.text = text;
                
            });
        };
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.activityIndicator stopAnimating];
        [self.activityIndicator removeFromSuperview];
        [self.coverView removeFromSuperview];
    });
}
 
- (IBAction)startButtonClicked:(id)sender {
    self.startButton.enabled = NO;
    if (!self.startButton.selected) {
        if (is_mic_) [self.audioCapture startRecorder];
    } else {
        if (is_mic_) [self.audioCapture stopRecorder];
    }
 
    self.startButton.selected = !self.startButton.selected;
    self.startButton.enabled = YES;
}
 
- (void)textViewDidChange:(UITextView *)textView {
    [textView scrollRangeToVisible:NSMakeRange([textView.text length] - 1, 1)];
}
 
 
@end