In-Memory Audio Streaming

This code snippet basically shows the possibly of loading audio FileData, then having a Decoder decode the audio from memory. This is different than loading the whole SoundData into memory, which would decode the whole file, which can be memory and time consuming.

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

Simple Snippet

A very simple snippet which shows how 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 has "stream" as its type. This line is just for checking purposes
assert(source:getType() == "stream")
-- Play sound
source:play()

Full Snippet

This reflects what is actually happening 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 a new FileData based on the audio contents. Make sure to specify
-- a correct file extension in the second parameter! "Decoder"s determine
-- audio formats by their extension, and not by their contents!
local audioData = love.filesystem.newFileData(audioContents, "_.ogg"))
-- Create a new Decoder to decode the audio
local decoder = love.sound.newDecoder(audioData)
-- Create a new "stream" type source from the created Decoder
local source = love.audio.newSource(decoder, "stream")
--  Ensure it has "stream" as its type. This line is just for checking purposes
assert(source:getType() == "stream")
-- Play sound
source:play()