Difference between revisions of "Decoder"

(Add example for streaming to a queueable Source)
(Streaming music to a queueable Source: Fixed nil check)
 
Line 37: Line 37:
 
   for i = 1, freeBufferCount do
 
   for i = 1, freeBufferCount do
 
     local soundData = decoder:decode()
 
     local soundData = decoder:decode()
     if not soundData and loop then
+
     if not soundData then
 
       -- the decoder reached the end of the file!
 
       -- the decoder reached the end of the file!
       -- Reset to the start to loop the music and resume queueing
+
      if not loop then
 +
        return
 +
      end
 +
 
 +
       -- Reset the decoder to the start and decode from the start
 
       decoder:seek(0)
 
       decoder:seek(0)
 
       soundData = decoder:decode()
 
       soundData = decoder:decode()

Latest revision as of 00:58, 21 February 2025

An object which can gradually decode a sound file.

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. Added since 11.3
Decoder:decode Decodes a chunk of audio data to a SoundData. Added since 11.0
Decoder:getBitDepth Returns the number of bits per sample. Added since 0.9.0
Decoder:getBits Returns the number of bits per sample. Removed in 0.9.0
Decoder:getChannelCount Returns the number of channels in the stream. Added since 11.0
Decoder:getChannels Returns the number of channels in the stream. Deprecated in 11.0
Decoder:getDuration Gets the duration of the sound file. Added since 0.10.0
Decoder:getSampleRate Returns the sample rate of the Decoder.
Decoder:seek Sets the currently playing position of the Decoder. Added since 11.0
Object:release Immediately destroys the object's Lua reference. Added since 11.0
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 then
      -- the decoder reached the end of the file!
      if not loop then
        return
      end

      -- Reset the decoder to the start and decode from the start
      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