Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
880 stars 575 forks source link

[BUG]: #1050

Open BubuKunz opened 5 months ago

BubuKunz commented 5 months ago

Flutter Sound Version :


Severity


Platforms you faced the error


Describe the bug I'm working with elevenlabs TTS API. I want to have

To Reproduce Steps to reproduce the behavior:

  1. Get TTS response from elevenlabs for pcm16_22050
  2. Try to play it using startPlayerFromStream

Actual result: without 500ms delay it trims audio (kind of next line after 'await player.feedFromStream()' is reachebale before audio exactly finished)

` final List<Future> _audioPartsQueue = [];

Future _playFlutterSoundStream( VoidCallback onStartPlaying, ) async { await _myPlayer.startPlayerFromStream( codec: flutter_sound.Codec.pcm16, numChannels: 1, sampleRate: 22050, ); var startedPlaying = false; while (_audioPartsQueue.isNotEmpty) { final bytes = await _audioPartsQueue.removeAt(0); if (!startedPlaying) { onStartPlaying(); startedPlaying = true; } await _myPlayer.feedFromStream(bytes); if (_audioPartsQueue.isEmpty) { // need to add delay because the end of audio is trimmed if stop player without delay await Future.delayed(const Duration(milliseconds: 500)); } } await _myPlayer.stopPlayer(); } }`

Larpoux commented 5 months ago

Yes, this problem is known. There is a long string of processes working for delivering audio stream. Every body does buffering so they don’t lose audio packets and be sure to give packets in time to the lower process.

This is not easy to control all these buffers. The last buffers are managed by the os and we don’t have control on them.

‘But on iOS we have a layer managing several buffers (5?) just above the os. Very recently I implemented a callback "when finished " which is fired when those (5?) buffers are consumed by the os. This feature has been implemented and tested for app feeding the sink stream without waiting for a back pressure. I am not sure that it will do something useful for app waiting for each future completed (as you do if I understand correctly). You can try and tell us if this solves your problem.

‘If not, I will try to find with you another solution. Be sure to use the last version of flutter sound.

Larpoux commented 5 months ago

Also, if after waiting for the (5?) buffers consumed you still have truncation of the last buffers, you can try to lower the buffer size. But be careful: if too low, you may have some audio packets lost. Also, this can increase the cpu load a little bit.

Larpoux commented 5 months ago
player.strartPlayer( …. whenFinished: (){ do something;} … );