In-Memory Audio Streaming

This code snippet is basically shows the possibly of loading audio FileData then let the Decoder decode the audio in-memory. This is different than loading the whole SoundData into memory, which can be memory and time consuming.

Works in Super Toast and Mysterious Mysteries version of LÖVE. Probably also work in Baby Inspector (needs confirmation).

Simple Snippet

A very simple snippet which shows it works.

-- Load audio file data into memory
local audioFile = love.filesystem.newFileData("file/to/audio.ogg")
-- Create new "stream" source from FileData
local source = love.audio.newSource(audioFile, "stream")
-- Ensure it's "stream". This line is just for checking purpose
assert(source:getType() == "stream")
-- Play sound
source:play()

Full Snippet

This is reflect what is actually happend inside the LÖVE framework.

-- Open audio file
local audioHandle = love.filesystem.newFile("file/to/audio.ogg", "r")
-- Read the audio file contents. It's still in encoded form.
local audioContents = audioHandle:read()
-- Create new FileData based on the audio contents. Make sure to specify
-- correct file extension in the second parameter! "Decoder" determines
-- audio format by their extension and not by their contents!
local audioData = love.filesystem.newFileData(audioContents, "_.ogg"))
-- Create new Decoder to decode the audio
local decoder = love.sound.newDecoder(audioData)
-- Create new "stream" source from created Decoder
local source = love.audio.newSource(decoder, "stream")
-- Ensure it's "stream". This line is just for checking purpose
assert(source:getType() == "stream")
-- Play sound
source:play()