Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
laves committed Aug 8, 2023
1 parent 1970fa8 commit 3466610
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 62 deletions.
2 changes: 1 addition & 1 deletion demo/flutter/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
platform :ios, '9.0'
platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
18 changes: 14 additions & 4 deletions demo/flutter/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ PODS:
- Cheetah-iOS (~> 1.1.0)
- Flutter
- Flutter (1.0.0)
- flutter_voice_processor (1.0.6):
- flutter_voice_processor (1.1.0):
- Flutter
- ios-voice-processor (~> 1.1.0)
- integration_test (0.0.1):
- Flutter
- ios-voice-processor (1.1.0)
- path_provider_ios (0.0.1):
- Flutter

DEPENDENCIES:
- cheetah_flutter (from `.symlinks/plugins/cheetah_flutter/ios`)
- Flutter (from `Flutter`)
- flutter_voice_processor (from `.symlinks/plugins/flutter_voice_processor/ios`)
- integration_test (from `.symlinks/plugins/integration_test/ios`)
- path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`)

SPEC REPOS:
trunk:
- Cheetah-iOS
- ios-voice-processor

EXTERNAL SOURCES:
cheetah_flutter:
Expand All @@ -26,16 +32,20 @@ EXTERNAL SOURCES:
:path: Flutter
flutter_voice_processor:
:path: ".symlinks/plugins/flutter_voice_processor/ios"
integration_test:
:path: ".symlinks/plugins/integration_test/ios"
path_provider_ios:
:path: ".symlinks/plugins/path_provider_ios/ios"

SPEC CHECKSUMS:
Cheetah-iOS: 6fb7be693878f5b1dec0ea5b6534fbba30954afc
cheetah_flutter: 5ecf97be04a1bea435da8331f180d94439f5a773
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
flutter_voice_processor: afb59b604d99397a1ccf15c935ac8f2c2327f09f
flutter_voice_processor: 53afbf59ad3feb82f4a379fea9ed8dc98495210f
integration_test: a1e7d09bd98eca2fc37aefd79d4f41ad37bdbbe5
ios-voice-processor: 8e32d7f980a06d392d128ef1cd19cf6ddcaca3c1
path_provider_ios: 7d7ce634493af4477d156294792024ec3485acd5

PODFILE CHECKSUM: a75497545d4391e2d394c3668e20cfb1c2bbd4aa
PODFILE CHECKSUM: 7368163408c647b7eb699d0d788ba6718e18fb8d

COCOAPODS: 1.11.2
COCOAPODS: 1.11.3
73 changes: 33 additions & 40 deletions demo/flutter/lib/cheetah_manager.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022 Picovoice Inc.
// Copyright 2022-2023 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
Expand All @@ -20,13 +20,10 @@ typedef TranscriptCallback = Function(String transcript);
typedef ProcessErrorCallback = Function(CheetahException error);

class CheetahManager {
final VoiceProcessor? _voiceProcessor;
VoiceProcessor? _voiceProcessor;
Cheetah? _cheetah;

final TranscriptCallback _transcriptCallback;
final ProcessErrorCallback _processErrorCallback;
RemoveListener? _removeVoiceProcessorListener;
RemoveListener? _removeErrorListener;

static Future<CheetahManager> create(
String accessKey,
Expand All @@ -38,36 +35,25 @@ class CheetahManager {
return CheetahManager._(cheetah, transcriptCallback, processErrorCallback);
}

CheetahManager._(
this._cheetah, this._transcriptCallback, this._processErrorCallback)
: _voiceProcessor = VoiceProcessor.getVoiceProcessor(
_cheetah!.frameLength, _cheetah.sampleRate) {
if (_voiceProcessor == null) {
throw CheetahRuntimeException("flutter_voice_processor not available.");
}
_removeVoiceProcessorListener =
_voiceProcessor!.addListener((buffer) async {
List<int> cheetahFrame;
try {
cheetahFrame = (buffer as List<dynamic>).cast<int>();
} on Error {
CheetahException castError = CheetahException(
"flutter_voice_processor sent an unexpected data type.");
_processErrorCallback(castError);
CheetahManager._(this._cheetah, this._transcriptCallback,
ProcessErrorCallback processErrorCallback)
: _voiceProcessor = VoiceProcessor.instance {
_voiceProcessor?.addFrameListener((List<int> frame) async {
if (!(await _voiceProcessor?.isRecording() ?? false)) {
return;
}

if (_cheetah == null) {
throw CheetahInvalidStateException(
"Cannot process with Cheetah - resources have already been released");
processErrorCallback(CheetahInvalidStateException(
"Cannot process with Cheetah - resources have already been released"));
return;
}

try {
CheetahTranscript partialResult = await _cheetah!.process(cheetahFrame);
CheetahTranscript partialResult = await _cheetah!.process(frame);

if (partialResult.isEndpoint) {
CheetahTranscript remainingResult = await _cheetah!.flush();
var finalTranscript =
String finalTranscript =
partialResult.transcript + remainingResult.transcript;
if (remainingResult.transcript.isNotEmpty) {
finalTranscript += " ";
Expand All @@ -77,28 +63,32 @@ class CheetahManager {
_transcriptCallback(partialResult.transcript);
}
} on CheetahException catch (error) {
_processErrorCallback(error);
processErrorCallback(error);
}
});

_removeErrorListener = _voiceProcessor!.addErrorListener((errorMsg) {
CheetahException nativeError = CheetahException(errorMsg as String);
_processErrorCallback(nativeError);
_voiceProcessor?.addErrorListener((VoiceProcessorException error) {
processErrorCallback(CheetahException(error.message));
});
}

Future<void> startProcess() async {
if (await _voiceProcessor?.isRecording() ?? false) {
return;
}

if (_cheetah == null || _voiceProcessor == null) {
throw CheetahInvalidStateException(
"Cannot start Cheetah - resources have already been released");
}

if (await _voiceProcessor?.hasRecordAudioPermission() ?? false) {
try {
await _voiceProcessor!.start();
} on PlatformException {
await _voiceProcessor?.start(
_cheetah!.frameLength, _cheetah!.sampleRate);
} on PlatformException catch (e) {
throw CheetahRuntimeException(
"Audio engine failed to start. Hardware may not be supported.");
"Failed to start audio recording: ${e.message}");
}
} else {
throw CheetahRuntimeException(
Expand All @@ -112,20 +102,23 @@ class CheetahManager {
"Cannot start Cheetah - resources have already been released");
}

if (_voiceProcessor?.isRecording ?? false) {
await _voiceProcessor!.stop();
if (await _voiceProcessor?.isRecording() ?? false) {
try {
await _voiceProcessor?.stop();
} on PlatformException catch (e) {
throw CheetahRuntimeException(
"Failed to stop audio recording: ${e.message}");
}

CheetahTranscript cheetahTranscript = await _cheetah!.flush();
_transcriptCallback((cheetahTranscript.transcript) + " ");
}
}

Future<void> delete() async {
if (_voiceProcessor?.isRecording ?? false) {
await _voiceProcessor!.stop();
}
_removeVoiceProcessorListener?.call();
_removeErrorListener?.call();
await stopProcess();
_voiceProcessor = null;

_cheetah?.delete();
_cheetah = null;
}
Expand Down
6 changes: 3 additions & 3 deletions demo/flutter/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022 Picovoice Inc.
// Copyright 2022-2023 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
Expand Down Expand Up @@ -113,7 +113,7 @@ class _MyAppState extends State<MyApp> {
isProcessing = true;
});
} on CheetahException catch (ex) {
print("Failed to start audio capture: ${ex.message}");
errorCallback(ex);
}
}

Expand All @@ -128,7 +128,7 @@ class _MyAppState extends State<MyApp> {
isProcessing = false;
});
} on CheetahException catch (ex) {
print("Failed to start audio capture: ${ex.message}");
errorCallback(ex);
}
}

Expand Down
29 changes: 18 additions & 11 deletions demo/flutter/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.6"
version: "3.1.11"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -56,7 +56,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
crypto:
dependency: transitive
description:
Expand All @@ -70,7 +70,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
ffi:
dependency: transitive
description:
Expand Down Expand Up @@ -106,7 +106,7 @@ packages:
name: flutter_voice_processor
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.7"
version: "1.1.0"
fuchsia_remote_debug_protocol:
dependency: transitive
description: flutter
Expand All @@ -131,6 +131,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
meta:
dependency: transitive
description:
Expand All @@ -144,7 +151,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
path_provider:
dependency: transitive
description:
Expand Down Expand Up @@ -200,7 +207,7 @@ packages:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
version: "3.1.0"
plugin_platform_interface:
dependency: transitive
description:
Expand All @@ -226,7 +233,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -268,7 +275,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.3"
version: "0.4.9"
typed_data:
dependency: transitive
description:
Expand All @@ -282,14 +289,14 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
version: "2.1.2"
vm_service:
dependency: transitive
description:
name: vm_service
url: "https://pub.dartlang.org"
source: hosted
version: "7.3.0"
version: "8.2.2"
webdriver:
dependency: transitive
description:
Expand All @@ -312,5 +319,5 @@ packages:
source: hosted
version: "0.2.0+1"
sdks:
dart: ">=2.15.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
flutter: ">=2.8.1"
6 changes: 3 additions & 3 deletions demo/flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
flutter:
sdk: flutter

flutter_voice_processor: ^1.0.7
flutter_voice_processor: ^1.1.0

cheetah_flutter: ^1.1.0

Expand All @@ -27,8 +27,8 @@ dev_dependencies:
flutter:
uses-material-design: true
assets:
- assets/
- assets/models/
- assets/models/ios/
- assets/models/android/
- assets/test_resources/
- assets/test_resources/audio_samples/
- assets/test_resources/model_files/

0 comments on commit 3466610

Please sign in to comment.