Detecting the End of a Sound File

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
shatterblast
Party member
Posts: 136
Joined: Tue Dec 11, 2012 9:47 pm
Location: Dallas, Texas, USA

Detecting the End of a Sound File

Post by shatterblast »

Hello there. Thanks for any help!

Is there a function that detects the end of a sound file? Or maybe that returns how much time is left before a sound ends?

I am trying to create a shuffled play list of music. So far, using SLAM, I have created a shuffled playlist that will pick a random song during the launch of the software. If I set the music to loop, it will only play the beginning music over and over. I think I could get around that by detecting the end of when a song plays, and choose a different song randomly when one ends.

Anyhow, here is my LOVE file, and a copy of the main.lua. Please excuse my coding techniques. I know there are a lot of globals that I will one day nest into other functions.

LOVE file

Main.lua:

Code: Select all

function love.load()
  require("anal")
  require("slam")
  
  back_ground = love.graphics.newImage("background01.png")
    
  --______________________________________________________________________
  
  spritesheet1 = love.graphics.newImage("sparkle_set01.png")
  spritesheet1:setFilter("nearest", "nearest")
  animation1 = newAnimation(spritesheet1, 192, 192, 0.1, 110)
  
  --______________________________________________________________________

  spritesheet2 = love.graphics.newImage("sparkle_set02.png")
  spritesheet2:setFilter("nearest", "nearest")
  animation2 = newAnimation(spritesheet2, 200, 200, 0.1, 80)
  --______________________________________________________________________
    
  spritesheet3 = love.graphics.newImage("sparkle_set03.png")
  spritesheet3:setFilter("nearest", "nearest")
  animation3 = newAnimation(spritesheet3, 200, 200, 0.1, 80)
  
  --______________________________________________________________________  
  
  spritesheet4 = love.graphics.newImage("safir_test01.png")
  spritesheet4:setFilter("nearest", "nearest")
  --MAKE SURE THE LAST NUMBER IS THE NUMBER OF FRAMES!!!
  animation4 = newAnimation(spritesheet4, 120, 203, 0.1, 60)
  
  --______________________________________________________________________
  
  spritesheet5 = love.graphics.newImage("shell_burst_03.png")
  spritesheet5:setFilter("nearest", "nearest")
  --MAKE SURE THE LAST NUMBER IS THE NUMBER OF FRAMES!!!
  animation5 = newAnimation(spritesheet5, 159, 203, 0.1, 31)
  
  --______________________________________________________________________
  
  animationState_Effects = "Effect_0"
  animationState_Saphir = "Effect_Saphir_1"
  soundState = "Sound_1"
  
  --______________________________________________________________________
  
  --TO DO: Add more sound here.
  --______________________________________________________________________
  
  sound03 = love.audio.newSource("explosion03.ogg", "static")
  
  --______________________________________________________________________
    
  sound04 = love.audio.newSource("explosion04.ogg", "static")
  
  --______________________________________________________________________
 
  --Background music for battles.  
 
  battle_music_shuffle = table.shuffle{'battle_music_Derek_Oldman_-_Notch.ogg', 'battle_music_Melodie_en_sous-sol_-_azul_ul_inu.ogg', 'battle_music_mindthings_-_Eve_.ogg', 'battle_music_Outsider_-_Dangerous_Groups.ogg', 'battle_music_Piter_-_Reveranger__amp__Piter_-_HYMNOMYNA__chillout_.ogg', 'battle_music_Tripple_T_-_The_Chill_Area.ogg', 'battle_music_Wade_-_Pozitivli_Tuned_Up.ogg', 'battle_music_WoNd3RBoY_-_WoNd3RBoY_-_MilkyWay__original_mix_.ogg'}
  
  background_Music01 = love.audio.newSource(battle_music_shuffle[1])
  
  background_Music01:setVolume(0.25)
  background_Music01:play()

  --______________________________________________________________________
end

function table.shuffle(array)
  local arrayCount = #array
    for i = arrayCount, 2, -1 do
      local j = love.math.random(1, i)
        array[i], array[j] = array[j], array[i]
    end
    return array
end

function love.update(dt)
  animation1:update(dt)         
  animation2:update(dt)
  animation3:update(dt)
  animation4:update(dt)
  animation5:update(dt)

end

function love.draw()
  love.graphics.draw(back_ground, 0, 0)
  
  love.graphics.print("Press 1, 2, 3, or 4 to alternate between animations.", 50, 50)
  love.graphics.print(animationState_Effects, 50, 110)
  
  if animationState_Saphir == "Effect_Saphir_1" then
    animation4:setMode("loop")
    animation4:draw(600, 150)    
    
  elseif animationState_Saphir == "Effect_Saphir_2" then
    animation5:setMode("once")
    animation5:draw(580, 150)
    
    if animation5:getCurrentFrame() == 31 then
      animationState_Saphir = "Effect_Saphir_1"
      animation4:reset()
      animation4:play()
      
    end
  end

  --______________________________________________________________________

  if animationState_Effects == "Effect_1" then
    animation1:setMode("once")
    animation1:draw(250, 250)
    
    if animation1:getCurrentFrame() == 110 then
      animationState_Effects = "Effect_0"
      
    end
       
  elseif animationState_Effects == "Effect_2" then
    animation2:setMode("once")
    animation2:draw(250, 250)
    
    if animation2:getCurrentFrame() == 80 then
      animationState_Effects = "Effect_0"
      
    end
    
  elseif animationState_Effects == "Effect_3" then
    animation3:setMode("once")
    animation3:draw(250, 250)
    
    if animation3:getCurrentFrame() == 80 then
      animationState_Effects = "Effect_0"
      
    end
          
  elseif animationState_Effects == "Effect_4" then
    animationState_Saphir = "Effect_Saphir_2"         
          
  end
end

function love.keypressed(key)
  if key == "1" then
    animationState_Effects = "Effect_1"
    animation1:reset()
    animation1:play()
    
  elseif key == "2" then
    animationState_Effects = "Effect_2"
    animation2:reset()
    animation2:play()
    love.audio.play(sound03)
    
  elseif key == "3" then
    animationState_Effects = "Effect_3"
    animation3:reset()
    animation3:play()
    love.audio.play(sound04)
  
  elseif key == "4" then
    animationState_Saphir = "Effect_Saphir_2"
    animation5:reset()
    animation5:play()
       
  end
end

--Place more songs here as the list grows so that all background music ceases before starting a new one.
function stop_songs()
  love.audio.stop(background_Music01)
end
Last edited by shatterblast on Tue May 20, 2014 10:47 am, edited 1 time in total.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Detecting the End of a Sound File

Post by kikito »

If I'm reading it correctly, [wiki]Source:isPlaying[/wiki] will return "true" until the (un-looped) source finishes, and then start returning false. So you can detect that it's finished like this:

Code: Select all

if not currentSource:isPlaying() then
  -- choose a new source and play it
end
Last edited by kikito on Tue May 20, 2014 12:29 pm, edited 1 time in total.
When I write def I mean function.
User avatar
shatterblast
Party member
Posts: 136
Joined: Tue Dec 11, 2012 9:47 pm
Location: Dallas, Texas, USA

Re: Detecting the End of a Sound File

Post by shatterblast »

kikito wrote:If I'm reading it correctly, [wiki]Source:isPlaying[/wiki] will return "true" until the (un-looped) source finishes, and then start returning false. So you can detect that it's finished like this:
Great! I'll check into it. Thanks!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Detecting the End of a Sound File

Post by bartbes »

Well, technically it will stop when it's finished, so isStopped would return true. isPlaying also takes pausing into account, which may or may not matter.
User avatar
nuno
Party member
Posts: 137
Joined: Wed Dec 11, 2013 12:00 pm
Location: Portugal
Contact:

Re: Detecting the End of a Sound File

Post by nuno »

I miss a way to get the current position and total length of the audio
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Detecting the End of a Sound File

Post by bartbes »

[wiki]Source:tell[/wiki] has the current position, but length is only available after the audio has been completely decoded (see [wiki]SoundData:getDuration[/wiki]).
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 142 guests