saveAudioFile method

Future<double> saveAudioFile(
  1. String filename, {
  2. Duration? silenceCutoffLength = const Duration(seconds: 3),
  3. Duration maxLength = const Duration(seconds: 30),
})

Saves the recorded audio to a file.

Args: filename (String): The name of the file to save the audio to. silenceCutoffLength (Duration?): The length of silence to detect before stopping the recording automatically. Defaults to 3 seconds. maxLength (Duration): The maximum length of the recording. Defaults to 30 seconds.

Returns: Future

Throws: ArgumentError: If no audio data is recorded.

Implementation

Future<double> saveAudioFile(
  String filename, {
  Duration? silenceCutoffLength = const Duration(seconds: 3),
  Duration maxLength = const Duration(seconds: 30),
}) async {
  final audioData = await recordAudio(
    silenceCutoffLength: silenceCutoffLength,
    maxLength: maxLength,
  );

  if (audioData.isEmpty) {
    throw ArgumentError('No audio data recorded');
  }

  final file = File(filename);
  await file.writeAsBytes(_bytesToWav(audioData, bitDepth, sampleRate));

  final lengthInSeconds = audioData.length / (_bitDepth ~/ 8) / _sampleRate;
  return lengthInSeconds;
}