Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/cubit/receive_tab_cubit/receive_tab_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ class ReceiveTabCubit extends Cubit<AReceiveTabState> {
late AudioDecoder _audioDecoder;
bool _canceledByUserBool = false;

ReceiveTabCubit() : super(const ReceiveTabEmptyState());
ReceiveTabCubit() : super(const ReceiveTabEmptyState()) {
_audioDecoder = AudioDecoder(
onMetadataFrameReceived: _handleMetadataFrameReceived,
onDecodingCompleted: _handleDecodingCompleted,
onDecodingFailed: _handleDecodingFailed,
);
}

void resetScreen() {
emit(const ReceiveTabEmptyState());
Expand All @@ -37,14 +43,8 @@ class ReceiveTabCubit extends Cubit<AReceiveTabState> {

void startRecording() {
try {
_audioDecoder = AudioDecoder(
audioSettingsModel: _audioSettingsModel,
onMetadataFrameReceived: _handleMetadataFrameReceived,
onDecodingCompleted: _handleDecodingCompleted,
onDecodingFailed: _handleDecodingFailed,
);
emit(const ReceiveTabRecordingState());
_audioDecoder.startRecording();
_audioDecoder.startRecording(_audioSettingsModel);
} catch (e) {
AppLogger().log(message: 'Cannot start recording: $e');
emit(const ReceiveTabEmptyState());
Expand Down
53 changes: 24 additions & 29 deletions lib/cubit/send_tab_cubit/send_tab_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ import 'package:share_plus/share_plus.dart';
import 'package:whisp/cubit/send_tab_cubit/a_send_tab_state.dart';
import 'package:whisp/cubit/send_tab_cubit/states/send_tab_emitting_state.dart';
import 'package:whisp/cubit/send_tab_cubit/states/send_tab_empty_state.dart';
import 'package:whisp/cubit/send_tab_cubit/states/send_tab_preparing_state.dart';
import 'package:whisp/shared/audio_settings_mode.dart';
import 'package:whisp/shared/utils/file_utils.dart';

class SendTabCubit extends Cubit<ASendTabState> {
late final AudioGenerator _audioGenerator;
late AudioSettingsModel audioSettingsModel = AudioSettingsModel(
frequencyGenerator: MusicalFrequencyGenerator(
frequencies: MusicalFrequencies.fdm9FullScaleAMaj,
),
);
AudioGenerator? _audioGenerator;

SendTabCubit() : super(SendTabEmptyState());
SendTabCubit() : super(SendTabEmptyState()) {
_audioGenerator = AudioGenerator(onGenerationCompleted: () {
emit(SendTabEmptyState());
});
}

void switchAudioType(AudioSettingsMode audioSettingsMode) {
if (audioSettingsMode == AudioSettingsMode.rocket) {
Expand All @@ -38,23 +43,18 @@ class SendTabCubit extends Cubit<ASendTabState> {
}

Future<void> playSound(String text) async {
emit(SendTabPreparingState());
Uint8List textBytes = utf8.encode(text);
AudioStreamSink audioStreamSink = AudioStreamSink();
emit(SendTabEmittingState());
_audioGenerator = AudioGenerator(
audioSink: audioStreamSink,
await _audioGenerator.startGenerating(AudioGeneratorParams(
audioSettingsModel: audioSettingsModel,
);
await _audioGenerator!.generate(textBytes);

await audioStreamSink.future;

emit(SendTabEmptyState());
bytes: textBytes,
audioSinkArgs: StreamAudioSinkArgs(),
));
emit(SendTabEmittingState());
}

void stopSound() {
_audioGenerator?.stop();
emit(SendTabEmptyState());
_audioGenerator.cancelGenerating();
}

Future<void> saveFile(String text) async {
Expand All @@ -67,7 +67,7 @@ class SendTabCubit extends Cubit<ASendTabState> {
// For desktop platforms (Linux, macOS & Windows), this function does not actually
// save a file. It only opens the dialog to let the user choose a location and
// file name. This function only returns the **path** to this (non-existing) file.
// Since AudioFileSink handles saving the files in both cases,
// Since FileAudioSink handles saving the files in both cases,
// creating a temporary empty file is needed for Android.
bytes: Platform.isWindows ? null : Uint8List(0),
);
Expand All @@ -93,15 +93,13 @@ class SendTabCubit extends Cubit<ASendTabState> {
Uint8List textBytes = utf8.encode(text);
String filePath = '${tempDir.path}/generated_audio_message.wav';
File wavFile = File(filePath);
AudioFileSink audioFileSink = AudioFileSink(wavFile);
_audioGenerator = AudioGenerator(
audioSink: audioFileSink,
await _audioGenerator.startGenerating(AudioGeneratorParams(
audioSettingsModel: audioSettingsModel,
);
unawaited(_audioGenerator?.generate(textBytes));

await audioFileSink.future;
bytes: textBytes,
audioSinkArgs: FileAudioSinkArgs(file: wavFile),
));

await _audioGenerator.future;
XFile xWavFile = XFile(filePath);
await Share.shareXFiles(<XFile>[xWavFile], text: 'Share');

Expand All @@ -125,14 +123,11 @@ class SendTabCubit extends Cubit<ASendTabState> {
Future<void> _writeFile(String text, String outputPath) async {
Uint8List bytes = Uint8List.fromList(utf8.encode(text));
File sinkFile = File(outputPath);
AudioFileSink audioSink = AudioFileSink(sinkFile);

_audioGenerator = AudioGenerator(
audioSink: audioSink,
await _audioGenerator.startGenerating(AudioGeneratorParams(
audioSettingsModel: audioSettingsModel,
);
unawaited(_audioGenerator?.generate(bytes));

await audioSink.future;
bytes: bytes,
audioSinkArgs: FileAudioSinkArgs(file: sinkFile),
));
}
}
3 changes: 3 additions & 0 deletions lib/cubit/send_tab_cubit/states/send_tab_preparing_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'package:whisp/cubit/send_tab_cubit/states/send_tab_emitting_state.dart';

class SendTabPreparingState extends SendTabEmittingState {}
4 changes: 3 additions & 1 deletion lib/page/send_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:whisp/cubit/send_tab_cubit/a_send_tab_state.dart';
import 'package:whisp/cubit/send_tab_cubit/send_tab_cubit.dart';
import 'package:whisp/cubit/send_tab_cubit/states/send_tab_emitting_state.dart';
import 'package:whisp/cubit/send_tab_cubit/states/send_tab_preparing_state.dart';
import 'package:whisp/cubit/theme_cubit/theme_assets.dart';
import 'package:whisp/widgets/buttons_panel.dart';
import 'package:whisp/widgets/custom_app_bar.dart';
Expand Down Expand Up @@ -70,8 +71,9 @@ class _SendTabState extends State<SendTab> {
onClearButtonPressed: _clearMessage,
),
bottomWidget: ButtonsPanel(
buttonsDisabledBool: buttonsDisabledBool,
mainButtonDisabledBool: state is SendTabPreparingState || _msgEmptyBool,
emissionInProgressBool: emissionInProgressBool,
msgEmptyBool: _msgEmptyBool,
themeAssets: widget.themeAssets,
onSaveButtonPressed: _saveFile,
onPlayButtonPressed: () => widget.sendTabCubit.playSound(widget.messageTextController.text),
Expand Down
11 changes: 6 additions & 5 deletions lib/widgets/buttons_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import 'package:whisp/cubit/theme_cubit/theme_assets.dart';
import 'package:whisp/widgets/square_button.dart';

class ButtonsPanel extends StatelessWidget {
final bool buttonsDisabledBool;
final bool mainButtonDisabledBool;
final bool emissionInProgressBool;
final bool msgEmptyBool;
final ThemeAssets themeAssets;
final VoidCallback onSaveButtonPressed;
final VoidCallback onPlayButtonPressed;
final VoidCallback onStopButtonPressed;
final VoidCallback onShareButtonPressed;

const ButtonsPanel({
required this.buttonsDisabledBool,
required this.mainButtonDisabledBool,
required this.emissionInProgressBool,
required this.msgEmptyBool,
required this.themeAssets,
required this.onSaveButtonPressed,
required this.onPlayButtonPressed,
Expand All @@ -24,7 +26,6 @@ class ButtonsPanel extends StatelessWidget {

@override
Widget build(BuildContext context) {
bool buttonsDisabledBool = emissionInProgressBool || msgEmptyBool;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
Expand All @@ -47,15 +48,15 @@ class ButtonsPanel extends StatelessWidget {
flex: 10,
child: SquareButton.big(
backgroundColor: themeAssets.primaryColor,
onTap: msgEmptyBool
onTap: mainButtonDisabledBool
? null
: emissionInProgressBool
? onStopButtonPressed
: onPlayButtonPressed,
child: Icon(
emissionInProgressBool ? Icons.stop_rounded : Icons.play_arrow_rounded,
size: 80,
color: emissionInProgressBool ? const Color(0xff244064) : const Color(0xff396521),
color: buttonsDisabledBool ? const Color(0xff244064) : const Color(0xff396521),
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: whisp
description: "Demo application for mrumru."
publish_to: 'none'
version: 0.0.3
version: 0.0.4

environment:
sdk: "3.2.6"
Expand Down