Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save audio from stream as wav #365

Closed
zamirszn opened this issue Jul 7, 2024 · 2 comments
Closed

save audio from stream as wav #365

zamirszn opened this issue Jul 7, 2024 · 2 comments

Comments

@zamirszn
Copy link

zamirszn commented Jul 7, 2024

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
Copy link
Owner

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants