From a7473af40f8d118741204ad86d2a786400da6a1d Mon Sep 17 00:00:00 2001 From: Krille Date: Sun, 4 Aug 2024 19:22:08 +0200 Subject: [PATCH] feat: Record voice message with opus/ogg if supported --- lib/pages/chat/events/audio_player.dart | 26 +++------------ lib/pages/chat/recording_dialog.dart | 9 ++++-- licenses.yaml | 1 + macos/Flutter/GeneratedPluginRegistrant.swift | 2 -- pubspec.lock | 32 +++++-------------- pubspec.yaml | 2 +- 6 files changed, 21 insertions(+), 51 deletions(-) diff --git a/lib/pages/chat/events/audio_player.dart b/lib/pages/chat/events/audio_player.dart index a3de4e02b..430bac2d7 100644 --- a/lib/pages/chat/events/audio_player.dart +++ b/lib/pages/chat/events/audio_player.dart @@ -6,7 +6,7 @@ import 'package:flutter/material.dart'; import 'package:just_audio/just_audio.dart'; import 'package:matrix/matrix.dart'; -import 'package:nyx_converter/nyx_converter.dart'; +import 'package:opus_caf_converter_dart/opus_caf_converter_dart.dart'; import 'package:path_provider/path_provider.dart'; import 'package:fluffychat/utils/error_reporter.dart'; @@ -77,27 +77,11 @@ class AudioPlayerState extends State { if (Platform.isIOS && matrixFile.mimeType.toLowerCase() == 'audio/ogg') { Logs().v('Convert ogg audio file for iOS...'); - final convertedFile = File('${file.path}.aac'); - if (await convertedFile.exists()) { - file = convertedFile; - } else { - final completer = Completer(); - NyxConverter.convertTo( - file.path, - tempDir.path, - fileName: '${fileName}_${matrixFile.name}', - container: NyxContainer.aac, - execution: ( - String? path, - NyxStatus status, { - String? errorMessage, - }) { - if (path != null) completer.complete(File(path)); - if (errorMessage != null) completer.completeError(errorMessage); - }, - ); - file = await completer.future; + final convertedFile = File('${file.path}.caf'); + if (await convertedFile.exists() == false) { + OpusCaf().convertOpusToCaf(file.path, convertedFile.path); } + file = convertedFile; } } diff --git a/lib/pages/chat/recording_dialog.dart b/lib/pages/chat/recording_dialog.dart index bc92f3ff5..be22ff308 100644 --- a/lib/pages/chat/recording_dialog.dart +++ b/lib/pages/chat/recording_dialog.dart @@ -13,7 +13,6 @@ import 'package:fluffychat/utils/platform_infos.dart'; import 'events/audio_player.dart'; class RecordingDialog extends StatefulWidget { - static const String recordingFileType = 'm4a'; const RecordingDialog({ super.key, }); @@ -36,9 +35,11 @@ class RecordingDialogState extends State { Future startRecording() async { try { + final useOpus = + await _audioRecorder.isEncoderSupported(AudioEncoder.opus); final tempDir = await getTemporaryDirectory(); final path = _recordedPath = - '${tempDir.path}/recording${DateTime.now().microsecondsSinceEpoch}.${RecordingDialog.recordingFileType}'; + '${tempDir.path}/recording${DateTime.now().microsecondsSinceEpoch}.${useOpus ? 'ogg' : 'm4a'}'; final result = await _audioRecorder.hasPermission(); if (result != true) { @@ -46,14 +47,16 @@ class RecordingDialogState extends State { return; } await WakelockPlus.enable(); + await _audioRecorder.start( - const RecordConfig( + RecordConfig( bitRate: bitRate, sampleRate: samplingRate, numChannels: 1, autoGain: true, echoCancel: true, noiseSuppress: true, + encoder: useOpus ? AudioEncoder.opus : AudioEncoder.aacLc, ), path: path, ); diff --git a/licenses.yaml b/licenses.yaml index 7b2f79bbf..ebab43440 100644 --- a/licenses.yaml +++ b/licenses.yaml @@ -12,6 +12,7 @@ permittedLicenses: - BSD-2-Clause - BSD-3-Clause - EUPL-1.2 + - LGPL-3.0 - MIT - MPL-2.0 - Zlib diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 552a22ed9..50894732e 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -11,7 +11,6 @@ import desktop_drop import device_info_plus import dynamic_color import emoji_picker_flutter -import ffmpeg_kit_flutter_full_gpl import file_selector_macos import flutter_app_badger import flutter_local_notifications @@ -43,7 +42,6 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin")) DynamicColorPlugin.register(with: registry.registrar(forPlugin: "DynamicColorPlugin")) EmojiPickerFlutterPlugin.register(with: registry.registrar(forPlugin: "EmojiPickerFlutterPlugin")) - FFmpegKitFlutterPlugin.register(with: registry.registrar(forPlugin: "FFmpegKitFlutterPlugin")) FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin")) FlutterAppBadgerPlugin.register(with: registry.registrar(forPlugin: "FlutterAppBadgerPlugin")) FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 98e3900cf..0979b4a90 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -385,22 +385,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" - ffmpeg_kit_flutter_full_gpl: - dependency: transitive - description: - name: ffmpeg_kit_flutter_full_gpl - sha256: "4f269bcb636bfcb544e5b4d65c706a3d311839970cb42638e72406410c1b5b7b" - url: "https://pub.dev" - source: hosted - version: "6.0.3" - ffmpeg_kit_flutter_platform_interface: - dependency: transitive - description: - name: ffmpeg_kit_flutter_platform_interface - sha256: addf046ae44e190ad0101b2fde2ad909a3cd08a2a109f6106d2f7048b7abedee - url: "https://pub.dev" - source: hosted - version: "0.2.1" file: dependency: transitive description: @@ -1286,14 +1270,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.2" - nyx_converter: - dependency: "direct main" - description: - name: nyx_converter - sha256: "29684f29a650119f0417f7faa736c5b8ac65e5b32dbae40412bd6c3d7a692fc7" - url: "https://pub.dev" - source: hosted - version: "0.1.1" olm: dependency: transitive description: @@ -1302,6 +1278,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.3" + opus_caf_converter_dart: + dependency: "direct main" + description: + name: opus_caf_converter_dart + sha256: e08156066916f790a54df305e103d6dec4d853ec23147e6a02eda3c06f67ba1a + url: "https://pub.dev" + source: hosted + version: "1.0.1" package_config: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index ac3341975..9d00c0d9d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -66,7 +66,7 @@ dependencies: linkify: ^5.0.0 matrix: ^0.31.0 native_imaging: ^0.1.1 - nyx_converter: ^0.1.1 + opus_caf_converter_dart: ^1.0.1 package_info_plus: ^6.0.0 pasteboard: ^0.2.0 path: ^1.9.0