llfbandit / record

Audio recorder from microphone to a given file path. No external dependencies, MediaRecorder is used for Android an AVAudioRecorder for iOS.
https://pub.dev/packages/record
233 stars 196 forks source link

save audio from stream as wav #365

Closed zamirszn closed 2 months ago

zamirszn commented 2 months ago

hello @llfbandit , thanks for the great package

im implementing a feature where i send the audio stream to STT service , ive been able to do this but im trying to convert the raw audio into a wav file so i can play it later on

from the docs in the code it says "you must rely on stream close event to get full recorded data" which i did here

 audioStream.listen(
      (audioInUint8List) {
        audioDataChunk.add(audioInUint8List);
      },
      onDone: () {
        // When the stream is closed, close the controller
        audioStreamController.close();
        // now lets process the audio stream data
        processAndSaveAudio(audioDataChunk);
      },
      onError: (error) {
        // Handle any errors
        if (kDebugMode) {
          print('Error: $error');
        }
        audioStreamController.close();
      },
    );

but im stuck at processing the audio and saving it , im kinda new to working with audio can you help me see what im doing wrong heres a function i wrote to process the audio but im pretty sure im not doing it right ?

  Future<void> processAndSaveAudio(List<Uint8List> audioChunks) async {
    // Combine all chunks into a single Uint8List
    int totalLength =
        audioChunks.fold<int>(0, (total, chunk) => total + chunk.length);
    Uint8List combinedData = Uint8List(totalLength);
    int offset = 0;
    for (Uint8List chunk in audioChunks) {
      combinedData.setRange(offset, offset + chunk.length, chunk);
      offset += chunk.length;
    }

    // Save the audio as a temporary file
    Directory tempDir = await getApplicationDocumentsDirectory();
    String tempPath =
        '${tempDir.path}/recorded_audio.wav'; // Change the extension to match the audio format

    File tempFile = File(tempPath);
    await tempFile.writeAsBytes(combinedData);

    audioPath = tempPath;
    showPlayer = true;

    print('Audio saved to temporary file: $tempPath');
    setState(() {});
  }
llfbandit commented 2 months ago

You don't save your file as WAVE format and you'll have hard times if you continue this way. WAVE can't be streamed. You must compose WAVE header on top of PCM data if you want to read it easily later.

zamirszn commented 2 months ago

You don't save your file as WAVE format and you'll have hard times if you continue this way. WAVE can't be streamed. You must compose WAVE header on top of PCM data if you want to read it easily later.

thanks for the reply , i was able to write a wave header for my pcm data after doing some research and getting the recorded audio to play , though it only works on windows for now doesnt play on android , ill be closing this issue now once again thank you for your response