Difference between revisions of "Decoder"
m (Add PrettyDeprecated) |
(Add example for streaming to a queueable Source) |
||
Line 27: | Line 27: | ||
== Supertypes == | == Supertypes == | ||
* [[parent::Object]] | * [[parent::Object]] | ||
+ | == Examples == | ||
+ | === Streaming music to a queueable Source === | ||
+ | <source lang="lua"> | ||
+ | local decoder = love.sound.newDecoder("music.ogg") | ||
+ | local queueableSource = love.audio.newQueueableSource(decoder:getSampleRate(), decoder:getBitDepth(), decoder:getChannelCount()) | ||
+ | |||
+ | local function buffer(loop) | ||
+ | local freeBufferCount = queueableSource:getFreeBufferCount() | ||
+ | for i = 1, freeBufferCount do | ||
+ | local soundData = decoder:decode() | ||
+ | if not soundData and loop then | ||
+ | -- the decoder reached the end of the file! | ||
+ | -- Reset to the start to loop the music and resume queueing | ||
+ | decoder:seek(0) | ||
+ | soundData = decoder:decode() | ||
+ | end | ||
+ | queueableSource:queue(soundData) | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function love.load() | ||
+ | -- when the queueable Source has no buffers left to process it stops playing | ||
+ | -- buffer some data before starting to play! | ||
+ | buffer(true) | ||
+ | queueableSource:play() | ||
+ | end | ||
+ | |||
+ | function love.update(dt) | ||
+ | -- keep buffering so it doesn't stop | ||
+ | buffer(true) | ||
+ | end | ||
+ | </source> | ||
+ | |||
== See Also == | == See Also == | ||
* [[parent::love.sound]] | * [[parent::love.sound]] |
Revision as of 00:52, 21 February 2025
An object which can gradually decode a sound file.
Contents
Constructors
love.sound.newDecoder | Attempts to find a decoder for the encoded sound data in the specified file. |
Functions
Decoder:clone | Create new copy of existing decoder. | ![]() |
|
Decoder:decode | Decodes a chunk of audio data to a SoundData. | ![]() |
|
Decoder:getBitDepth | Returns the number of bits per sample. | ![]() |
|
Decoder:getBits | Returns the number of bits per sample. | ![]() | |
Decoder:getChannelCount | Returns the number of channels in the stream. | ![]() |
|
Decoder:getChannels | Returns the number of channels in the stream. | ![]() | |
Decoder:getDuration | Gets the duration of the sound file. | ![]() |
|
Decoder:getSampleRate | Returns the sample rate of the Decoder. | ||
Decoder:seek | Sets the currently playing position of the Decoder. | ![]() |
|
Object:release | Immediately destroys the object's Lua reference. | ![]() |
|
Object:type | Gets the type of the object as a string. | ||
Object:typeOf | Checks whether an object is of a certain type. |
Supertypes
Examples
Streaming music to a queueable Source
local decoder = love.sound.newDecoder("music.ogg")
local queueableSource = love.audio.newQueueableSource(decoder:getSampleRate(), decoder:getBitDepth(), decoder:getChannelCount())
local function buffer(loop)
local freeBufferCount = queueableSource:getFreeBufferCount()
for i = 1, freeBufferCount do
local soundData = decoder:decode()
if not soundData and loop then
-- the decoder reached the end of the file!
-- Reset to the start to loop the music and resume queueing
decoder:seek(0)
soundData = decoder:decode()
end
queueableSource:queue(soundData)
end
end
function love.load()
-- when the queueable Source has no buffers left to process it stops playing
-- buffer some data before starting to play!
buffer(true)
queueableSource:play()
end
function love.update(dt)
-- keep buffering so it doesn't stop
buffer(true)
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info