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 (English improvements :3)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
This code snippet is basically shows the possibly of loading audio [[FileData]] then let the [[Decoder]] decode the audio in-memory.
+
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 can be memory and time consuming.
+
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 [[0.10.0|Super Toast]] and [[11.0|Mysterious Mysteries]] version of LÖVE. Probably also work in [[0.9.2|Baby Inspector]] (needs confirmation).
+
Works in [[0.10.2|Super Toast]] and [[11.0|Mysterious Mysteries]] versions of LÖVE. It probably also work in [[0.9.2|Baby Inspector]] (needs confirmation).
 
== Simple Snippet ==
 
== Simple Snippet ==
A very simple snippet which shows it works.
+
A very simple snippet which shows how it works.
 
<source lang="lua">
 
<source lang="lua">
 
-- Load audio file data into memory
 
-- Load audio file data into memory
Line 10: Line 10:
 
-- Create new "stream" source from FileData
 
-- Create new "stream" source from FileData
 
local source = love.audio.newSource(audioFile, "stream")
 
local source = love.audio.newSource(audioFile, "stream")
-- Ensure it's "stream". This line is just for checking purpose
+
-- Ensure it has "stream" as its type. This line is just for checking purposes
 
assert(source:getType() == "stream")
 
assert(source:getType() == "stream")
 
-- Play sound
 
-- Play sound
Line 16: Line 16:
 
</source>
 
</source>
 
== Full Snippet ==
 
== Full Snippet ==
This is reflect what is actually happend inside the LÖVE framework.
+
This reflects what is actually happening inside the LÖVE framework.
 
<source lang="lua">
 
<source lang="lua">
 
-- Open audio file
 
-- Open audio file
Line 22: Line 22:
 
-- Read the audio file contents. It's still in encoded form.
 
-- Read the audio file contents. It's still in encoded form.
 
local audioContents = audioHandle:read()
 
local audioContents = audioHandle:read()
-- Create new FileData based on the audio contents. Make sure to specify
+
-- Create a new FileData based on the audio contents. Make sure to specify
-- correct file extension in the second parameter! "Decoder" determines
+
-- a correct file extension in the second parameter! "Decoder"s determine
-- audio format by their extension and not by their contents!
+
-- audio formats by their extension, and not by their contents!
 
local audioData = love.filesystem.newFileData(audioContents, "_.ogg"))
 
local audioData = love.filesystem.newFileData(audioContents, "_.ogg"))
-- Create new Decoder to decode the audio
+
-- Create a new Decoder to decode the audio
 
local decoder = love.sound.newDecoder(audioData)
 
local decoder = love.sound.newDecoder(audioData)
-- Create new "stream" source from created Decoder
+
-- Create a new "stream" type source from the created Decoder
 
local source = love.audio.newSource(decoder, "stream")
 
local source = love.audio.newSource(decoder, "stream")
-- Ensure it's "stream". This line is just for checking purpose
+
-- Ensure it has "stream" as its type. This line is just for checking purposes
 
assert(source:getType() == "stream")
 
assert(source:getType() == "stream")
 
-- Play sound
 
-- Play sound
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|decodes]] it from RAM.}}

Latest revision as of 16:59, 7 April 2018

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