Difference between revisions of "In-Memory Audio Streaming"
m (Tutorial and snippet at same time.) |
m (English improvements :3) |
||
Line 1: | Line 1: | ||
− | This code snippet | + | 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. | + | 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 | + | -- 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 | + | 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" | + | -- a correct file extension in the second parameter! "Decoder"s determine |
− | -- audio | + | -- 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 | + | -- 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 40: | Line 40: | ||
{{#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| | + | {{#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()