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

Audio buffer stream #148

Merged
merged 32 commits into from
Nov 24, 2024
Merged

Audio buffer stream #148

merged 32 commits into from
Nov 24, 2024

Conversation

alnitak
Copy link
Owner

@alnitak alnitak commented Nov 9, 2024

Description

close #144

This new type of AudioSource is created by subclassing the SoLoud C++ AudioSource class.

It works by initializing it with its parameters, then adding PCM audio data, and finally signaling that no more data is incoming:

  • setBufferStream returns the new AudioSource. Using its parameters, you can set the PCM audio format, the maximum memory usage, the buffering time required to resume playback (when paused to wait for more data), and a callback to handle buffering events.
  • addAudioDataStream* adds PCM audio data to the stream. Helper methods are available to add signed/unsigned 8-bit integers, signed 16- and 32-bit integers, and 32-bit floats.
  • setDataIsEnded marks the end of the data stream.

The returned AudioSource is compatible with filters, oscillators and all other tools.

Use-case

  • streaming PCM audio data from a websocket and playing it while receiving data: example/lib/buffer_stream/websocket.dart is used for testing. It receives PCM data from a local websocket, provided by the websocketd tool. The Flutter app uses ffmpeg to stream PCM data over the websocket via websocketd, allowing you to simulate slow download speeds and buffering behavior.
  • example/lib/generate.dart generates audio and fills it into the AudioBuffer.
  • the Cartesia TTS Flutter project demonstrates how to use Cartesia AI APIs to send text and receive voice audio data.
  • you can also use flutter_recorder to capture PCM audio from the microphone and load it into the AudioSource for flutter_soloud.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@alnitak alnitak mentioned this pull request Nov 9, 2024
2 tasks
@alnitak alnitak changed the title Websocket Audio buffer stream Nov 11, 2024
@alnitak alnitak marked this pull request as ready for review November 11, 2024 16:39
@alnitak alnitak requested a review from filiph November 11, 2024 16:39
Copy link
Collaborator

@filiph filiph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some suggestions but this looks great, thank you!

example/lib/buffer_stream/generate.dart Outdated Show resolved Hide resolved
lib/src/bindings/bindings_player.dart Outdated Show resolved Hide resolved
lib/src/bindings/bindings_player_web.dart Outdated Show resolved Hide resolved
lib/src/bindings/bindings_player_web.dart Outdated Show resolved Hide resolved
lib/src/bindings/bindings_player_web.dart Show resolved Hide resolved
src/enums.h Show resolved Hide resolved
src/player.cpp Show resolved Hide resolved
src/player.cpp Outdated Show resolved Hide resolved
src/player.cpp Show resolved Hide resolved
src/audiobuffer/audiobuffer.cpp Outdated Show resolved Hide resolved
@filiph filiph mentioned this pull request Nov 12, 2024
7 tasks
@alnitak
Copy link
Owner Author

alnitak commented Nov 13, 2024

I thought it would be interesting to have a new method to reset the BufferStream. Something like
void resetBufferStream(AudioSource source)
which removes the ended flag and frees up the old audio data allowing the developer to add new audio data from scratch?

@filiph
Copy link
Collaborator

filiph commented Nov 13, 2024

I thought it would be interesting to have a new method to reset the BufferStream. Something like void resetBufferStream(AudioSource source) which removes the ended flag and frees up the old audio data allowing the developer to add new audio data from scratch?

To be honest, I'm not the intended audience (I don't foresee needing buffer stream) so I'm not the right person to ask. Maybe the folks who're asking for the feature might tell you? Otherwise, in general, I'm a proponent of YAGNI.

@alnitak alnitak requested a review from filiph November 13, 2024 19:23
@prateekmedia
Copy link

@alnitak The resetBufferStream method can be useful when audio stream needs to be interuppted due to any reason and new audio stream needs to feed.

@alnitak
Copy link
Owner Author

alnitak commented Nov 15, 2024

Thanks @prateekmedia,

I was thinking of the resetBufferStream because I was trying to figure out how to do a walky_talky like app or any other workflow like this:

  1. setup an AudioSource with setBufferStream
  2. receive data from the net and add them to the AudioSource while playing it
  3. when data is ended call resetBufferStream and go back to 2 instead of disposing of the AudioSource and going to 1

Anyway, let's flush this PR and shortly we will think better about what will be useful to add.

@alnitak alnitak mentioned this pull request Nov 16, 2024
1 task
@alnitak alnitak merged commit 551f541 into main Nov 24, 2024
1 check passed
@alnitak alnitak deleted the websocket branch November 24, 2024 16:40
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

Successfully merging this pull request may close these issues.

feat: use PCM audio data to build a sound
3 participants