Difference between revisions of "In-Memory Audio Streaming"

(Created page with "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 [...")
 
m (Tutorial and snippet at same time.)
Line 37: Line 37:
  
 
[[Category:Snippets]]
 
[[Category:Snippets]]
 +
[[Category:Tutorials]]
 
{{#set:Author=User:AuahDark}}
 
{{#set:Author=User:AuahDark}}
 
{{#set:LOVE Version=[[0.10.0]]}}
 
{{#set:LOVE Version=[[0.10.0]]}}
 
{{#set:Description=In-memory [[Source]] "stream". Stores the encoded audio data and [[Decoder|decode]] it in-memory.}}
 
{{#set:Description=In-memory [[Source]] "stream". Stores the encoded audio data and [[Decoder|decode]] it in-memory.}}

Revision as of 15:09, 7 April 2018

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()