chivox_aiengine 0.0.3 chivox_aiengine: ^0.0.3 copied to clipboard
Chivox Speech Assessment SDK
import 'dart:io';
import 'dart:convert';
import 'package:flutter/rendering.dart';
import 'package:flutter_sound/public/flutter_sound_player.dart';
import 'package:flutter_sound/public/flutter_sound_recorder.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:chivox_aiengine/chivox_aiengine.dart';
import 'package:permission_handler/permission_handler.dart';
const appKey = "your appKey";
const secretKey = "your secretKey";
const provisionB64 ="your provision";
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
ChivoxAiengine? _engine ;
String textValue = '';
String displayText = '';
var refText;
String coreType = '';
FlutterSoundPlayer _player = FlutterSoundPlayer();
FlutterSoundRecorder _recorder = FlutterSoundRecorder();
@override
void initState()
{
super.initState();
_player.openAudioSession().then((value) {
print('Player initialization successful');
});
_recorder.openAudioSession().then((value) {
print('Recorder initialized successfully');
});
initPlatformState();
displayText = "apple";
refText = "apple";
coreType = "en.word.score";
aiengineNew();
}
@override
void dispose() {
_player.closeAudioSession();
_recorder.closeAudioSession();
super.dispose();
}
Future<void> _startRecording() async {
// await _recorder.startRecorder(toFile: 'recording.aac');
PermissionStatus status = await Permission.microphone.request();
print("=======start recording=====");
if (status.isGranted) {
aiengineStart();
//Recording permission has been granted
} else if (status.isDenied) {
//Recording permission denied
print("audio not granted ");
} else if (status.isPermanentlyDenied) {
//The recording permission is permanently denied, and the user is directed to the system settings page for manual authorization.
print("audio not granted forever");
}
}
Future<void> _stopRecording() async {
//await _recorder.stopRecorder();
aiengineStop();
}
Future<void> _SetCoreType(String coreTypeTemp) async {
print('Overall: $coreTypeTemp');
coreType = coreTypeTemp;
if("en.word.score" == coreType)
{
displayText = "apple";
refText = "apple";
}
else if("en.sent.score" == coreType)
{
displayText = "Thanks for coming to see me";
refText = "Thanks for coming to see me";
}
else if("en.pred.score" == coreType)
{
displayText = "Happiness is about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.";
refText = "Happiness is about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.";
}
else if("en.choc.score" == coreType)
{
displayText = "Tell me an animal that can fly.\n A. Tiger \n B. Panda\n C. Bird \n";
var lm = {
"lm": [
{"answer": 0,"text": "Tiger"},
{"answer": 0,"text": "Panda"},
{"answer": 1,"text": "Bird"},
]
};
refText = lm;
}
print('after set refText');
textValue = ' ';
setState(() {});
print("updata UI");
}
Future<void> _cancelRecording() async {
//await _player.startPlayer(fromURI: 'recording.aac');
aiengineCancel();
}
Future<void> _stopPlaying() async {
await _player.stopPlayer();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await ChivoxAiengine.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Chivox Online Assesment'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
[
//Text(
// "After clicking the \"Start Recording\" button, read the following content. After reading, click the \"Stop Recording\" button, and the scores will be output.\n",//显示要朗读的内容
// style: TextStyle(fontSize: 20, height:1 ),
//),
Text(
displayText + '\n\n', //Show content to be read aloud
style: TextStyle(fontSize: 25, height:1),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton (
child: Text(' Word '),
onPressed:()
{
_SetCoreType("en.word.score");
},
//onPressed:
//onPressed: _SetCoreType,
),
SizedBox(width: 16.0), // 两个按钮之间的间距
ElevatedButton (
child: Text(' Sentence '),
onPressed:()
{
_SetCoreType("en.sent.score");
},
)
],
),
// 第二行按钮
SizedBox(height: 16.0), // 两行按钮之间的间距
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton (
child: Text('Paragraph'),
onPressed:()
{
_SetCoreType("en.pred.score");
},
),
SizedBox(width: 16.0),
ElevatedButton (
child: Text(' Choice '),
onPressed:()
{
_SetCoreType("en.choc.score");
},
)
],
),
Text(
"\nAssessment result:\n",
style: TextStyle(fontSize: 20, height:1),
),
Text(
textValue, //
style: TextStyle(fontSize: 25),
),
SizedBox(height: 16.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton (
child: Text('Start Recording'),
onPressed: _startRecording,
),
SizedBox(width: 16.0),
ElevatedButton (
child: Text('Stop Recording'),
onPressed: _stopRecording,
),
],
),
],
),
),
),
);
}
Future<void> aiengineNew() async {
Map cfg = {
"appKey": appKey,
"secretKey": secretKey,
"provision": provisionB64,
"cloud": {"server": "wss://cloud.chivox.com:443"}
};
_engine = await ChivoxAiengine.create(json.encode(cfg));
print("after new");
print(_engine);
setState(() {
//aiengineStart();
});
}
Future<void> aiengineDelete() async {
/*
try {
await _chivoxAienginePlugin.aiengineDelete(engineId);
} on PlatformException catch (e) {
print(e.code);
print(e.message);
}
*/
setState(() {});
}
Future<void> aiengineStart() async {
textValue = '';
setState(() {});
var audioSrc = {
"srcType": "innerRecorder", // innerRecorder - 内置录音机
"innerRecorderParam": {
//"duration": 20000,
"channel": 1,
"sampleBytes": 2,
"sampleRate": 16000,
},
};
var param = {
"soundIntensityEnable": 1,
"coreProvideType": "cloud",
"app": {
"userId": "flutter-test-user"
},
"vad": {
"vadEnable": 1,
"refDuration": 2,
"speechLowSeek": 20
},
"audio": {
"audioType": "wav",
"sampleRate": 16000,
"sampleBytes": 2,
"channel": 1
},
"request": {
"rank": 100,
"refText": refText,
"coreType":coreType,
"attachAudioUrl": 1
}
};
print(param);
print(_engine);
await _engine!.start(
audioSrc,
json.encode(param),
ChivoxAiengineResultListener(
onEvalResult: (ChivoxAiengineResult result) {
print("==========onEvalResult================");
var jsonString = result.text;
Map<String, dynamic> jsonData = jsonDecode(jsonString.toString());
int overall = 0;
int fluency = 0;
int integrity = 0;
int accuracy = 0;
String OverallScore = ' ';
String FluencyScore = ' ';
String IntegrityScore = ' ';
String AccuracyScore = ' ';
print('callback coreType: $coreType');
if("en.word.score" == coreType)
{
overall = jsonData['result']['overall'];
OverallScore = 'Overall Score : ${overall}';
print('en.word.score Overall: $overall');
textValue = 'Overall Score : ${overall}' + '\n';
}
else if(("en.sent.score" == coreType))
{
overall = jsonData['result']['overall'];
fluency = jsonData['result']['fluency']['overall'];
integrity = jsonData['result']['integrity'];
accuracy = jsonData['result']['accuracy'];
OverallScore = 'Overall Score : ${overall}';
FluencyScore = 'Fluency Score : ${fluency}';
IntegrityScore = 'Integrity Score : ${integrity}';
AccuracyScore = 'Accuracy Score : ${accuracy}';
textValue = OverallScore + '\n' + FluencyScore + '\n' + IntegrityScore + '\n' + AccuracyScore;
}
else if("en.pred.score" == coreType)
{
overall = jsonData['result']['overall'];
fluency = jsonData['result']['fluency']['overall'];
integrity = jsonData['result']['integrity'];
accuracy = jsonData['result']['accuracy'];
OverallScore = 'Overall Score : ${overall}';
FluencyScore = 'Fluency Score : ${fluency}';
IntegrityScore = 'Integrity Score : ${integrity}';
AccuracyScore = 'Accuracy Score : ${accuracy}';
textValue = OverallScore + '\n' + FluencyScore + '\n' + IntegrityScore + '\n' + AccuracyScore;
}
else if("en.choc.score" == coreType)
{
overall = jsonData['result']['overall'];
OverallScore = 'Overall Score : ${overall}';
textValue = OverallScore + '\n';
}
//jsonData['result']['overall'];
print('Overall: $overall');
print("==========before set text value================");
setState(() {});
print("==========after set text value=================");
//plog.log(result.text!);
},
onBinaryResult: (result) {
print("==========onBinaryResult================");
},
onError: (result) {
print("==========onError================");
print(result.text);
},
onVad: (result) {
print("==========onVad================");
textValue = result.text.toString();
setState(() {});
},
onSoundIntensity: (result) {
print("==========onSoundIntensity================");
textValue = result.text.toString();
setState(() {});
},
onOther: (result) {
print("==========onOther================");
}));
setState(() {
Timer.periodic(const Duration(seconds: 5), (timer) {
timer.cancel();
//aiengineStop();
});
});
}
Future<void> aiengineStop() async {
print("======stop 1=====");
await _engine!.stop();
print("======stop 2=====");
setState(() {});
}
Future<void> aiengineCancel() async {
print("======cancel 1=====");
await _engine!.cancel();
print("======cancel 2=====");
setState(() {});
}
}
class MyButton extends StatelessWidget {
final String text;
MyButton(this.text);
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
// 按钮点击事件
},
child: Text(text),
);
}
}