Skip to content

Improved sound streaming docs #536

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

Merged
merged 2 commits into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/en/manuals/project-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ This number should be larger than the number of loaded sound files times the str
Otherwise, you risk evicting new chunks each frame.

#### Stream Chunk Size
The size of each streamed chunk. `16384` bytes by default.
The size of each streamed chunk, `16384` bytes by default.

#### Stream Preload Size
Determines the size of the initial chunk for sound files read from the archive.
Determines the size of the initial chunk for sound files read from the archive, `16384` bytes by default.

---

Expand Down
53 changes: 18 additions & 35 deletions docs/en/manuals/sound-streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ brief: This manual explains how to stream sounds into the Defold game engine

While the default behaviour is to load sound data in full, it may also be beneficial to load the data in chunks, prior to their use. This is often called "streaming".

One benefit is that the runtime memory is kept low.

Another benefit is that, if you are streaming content from e.g. a http url, you can update the content at any time, and also avoid the initial download.
One benefit of sound streaming is that less runtime memory is required, another is if you are streaming content from e.g. a http url, you can update the content at any time, and also avoid the initial download.

### Example

There is an example project showcasing this setup: https://github.com/defold/example-sound-streaming
There is an example project showcasing this setup: [https://github.com/defold/example-sound-streaming](https://github.com/defold/example-sound-streaming)

## How to enable streaming sounds

### Easy way

The simplest way to use sound streaming, is by setting the `sound.stream_enabled` to true.
By simply switching on this flag, your project will start streaming the sounds.
The simplest way to use sound streaming, is by enabling the [`sound.stream_enabled` setting](https://defold.com/manuals/project-settings/#stream-enabled) in *game.project*. When this option is enabled the engine will start streaming the sounds.

Note: If you have lots of sound files loaded at the same time, you may need to up the `sound.stream_cache_size` value.
Note: If you have lots of sound files loaded at the same time, you may need to increase the `sound.stream_cache_size` value (see below).

### Runtime resources

Expand All @@ -31,13 +28,16 @@ You can also create a new sound data resource, and set it to a sound component.
You do this by:
* Load the initial part of the sound file data
* Note: This is the raw sound file, including the ogg/wav header
* Calling [resource.create_sound_data()](/ref/resource/#resource.create_sound_data) to get a resource
* Setting the resource to the sound component
* Create a new sound data resource by calling [`resource.create_sound_data()`](/ref/resource/#resource.create_sound_data).
* Set the new sound data resource to the sound component using [`go.set()`](/ref/go#go.set)

Here is an excerpt from the example project, using a `http.request()` to get the initial sound file.
Note that the web server you're loading content from has to support ranged requests.

```Lua
::: sidenote
The web server you're loading content from has to support [HTTP range requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Range_requests).
:::

```lua
local function parse_range(s)
local _, _, rstart, rend, size = string.find(s, "(%d+)-(%d+)/(%d+)") -- "bytes 0-16383/103277"
return rstart, rend, size
Expand Down Expand Up @@ -69,36 +69,19 @@ end

## Resource providers


You can of course use other means to load the initial chunk of the sound file.
The important thing to remember is that the rest of the chunks are loaded from the resource system and our resource providers.
You can of course use other means to load the initial chunk of the sound file. The important thing to remember is that the rest of the chunks are loaded from the resource system and it's resource providers.

In this example, we have added a new file provider by adding a live update mount, by calling using [liveupdate.add_mount()](/ref/liveupdate/#liveupdate.add_mount).


## Sound chunk cache

You can control how much memory will be consumed by the sounds at runtime, by setting the size of the "sound chunk cache".
Given this limit, the loaded sound data will never exceed this limit.

Some things to note:

* Sound files smaller than the sound chunk size, aren't streamed.
* If a new chunk doesn't fit into the cache, the oldest chunk is evicted
* If the cache is too small, chunks may get evicted the same frame, and the sound won't play properly.

The initial chunk of each sound file cannot be evicted, so they will occupy the cache as long as the resources are loaded.
You can also control the size of each sound chunk. This may help you get the sound cache size down even further if you have many sound files loaded at the same time.

## Configuration

Currently, the streaming is enabled on all sound resources.
We may improve upon this in the future, allowing settings on individual sound files.
The amount of memory consumed by the sounds at runtime is controlled by the [`sound.stream_cache_size` setting](https://defold.com/manuals/project-settings/#stream-cache-size) in *game.project*. Given this limit, the loaded sound data will never exceed this limit.

The game project supports these settings:
The initial chunk of each sound file cannot be evicted and they will occupy the cache for as long as the resources are loaded. The size of the initial chunk is controlled by the [`sound.stream_preload_size` setting](https://defold.com/manuals/project-settings/#stream-preload-size) in *game.project*.

* `sound.stream_enabled` (default 0) - If enabled, enables streaming of all sound files
* `sound.stream_cache_size` (default 2097152 bytes) - The max size of the cache containing _all_ chunks.
* `sound.stream_chunk_size` (default 16384 bytes) - Determines size of each chunk that is loaded from a file at a time
* `sound.stream_preload_size` (default 16384 bytes) - Determines size of initial chunk read from each found file in the archive
You can also control the size of each sound chunk by changing the [`sound.stream_chunk_size` setting](https://defold.com/manuals/project-settings/#stream-chunk-size) in *game.project*. This may help you get the sound cache size down even further if you have many sound files loaded at the same time. Sound files smaller than the sound chunk size, aren't streamed and if a new chunk doesn't fit into the cache, the oldest chunk is evicted

::: important
The total size of the sound chunk cache should be larger than the number of loaded sound files times the stream chunk size. Otherwise, you risk evicting new chunks each frame and sounds won't play properly
:::
Loading