Playing music with 2 sources

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
samuelcable
Prole
Posts: 4
Joined: Sun Jul 14, 2019 5:36 am

Playing music with 2 sources

Post by samuelcable » Sun Jul 14, 2019 6:05 am

Hi, im new to the forum so im sorry if this has already been asked/discussed. In my game i am trying to make a song play an intro to the song once before playing the loop, but there is clear audio artifacts when it transitions from the intro to the loop. there is also a problem with lag or moving the window possibly effecting the audio in weird ways.

my current code is just getting when the intro ends by getting when the intro Source:tell equals zero and is checked as being started, i know this may not be the best way but im not sure how else to do this, i have tried many things messing with the source: options and none of them helped remove the seam. I put a love file below with my code in it, but it is just an example project, my current game does a slightly more complex way of doing this but the idea is the same and they both have the same exact problem.

in the love file, once the pink bar turns white it means the intro has stopped and the loop has started playing. the music will loop , but that is only a problem for this file i whipped up for here.

basic code is

Code: Select all

  if source1:tell() > 0 then
    started = true
  end
  if started == true and source1:tell() == 0 then
  source2:setLooping(false)
  source2:play()
  end

any help would be appreciated :) sorry if theres something obvious im missing
Attachments
audio3.love
(4.29 MiB) Downloaded 3 times

User avatar
ReFreezed
Citizen
Posts: 70
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Playing music with 2 sources

Post by ReFreezed » Sun Jul 14, 2019 6:50 pm

To make sure one sound is played exactly after another with no interruption you need to use a queueable source. (Your example actually plays seamlessly for me but that's just luck.)
Tools: LuaPreprocess, LuaHotLoader
Games: Momento Temporis: Light from the Deep, Momento Temporis: Arena, Energize!
"If each mistake being made is a new one, then progress is being made."

samuelcable
Prole
Posts: 4
Joined: Sun Jul 14, 2019 5:36 am

Re: Playing music with 2 sources

Post by samuelcable » Mon Jul 15, 2019 6:38 am

ReFreezed wrote:
Sun Jul 14, 2019 6:50 pm
To make sure one sound is played exactly after another with no interruption you need to use a queueable source. (Your example actually plays seamlessly for me but that's just luck.)

Thanks, i got it working using queueable sources, Also you and other people i ask also notice no seam with the old method, which is weird since i certainly do, but that's not a problem anymore

samuelcable
Prole
Posts: 4
Joined: Sun Jul 14, 2019 5:36 am

Re: Playing music with 2 sources

Post by samuelcable » Tue Jul 16, 2019 9:26 am

i have a new problem, I got it working but in a way that is not very optimal, im not sure how to get the duration of the song properly in order to only call it once for when the song is over in order to loop it. Source:getDuration is not accurate to the ending point of Source:tell, so im not sure how to get the exact time of when it finishes without manually adding in the time of each song loop myself. sorry if this isnt explained well, heres what im doing

Code: Select all

if qs:tell() >= source2:getDuration() -0.01 or qs:tell() == 0 then started = true 
where qs is the queuable source (both audio files), source2 is the looping song.

again sorry if im not explaining this well, this is a very placeholder-y method because im not sure if theres any other way to get this properly, due to the difference in source:tell ending point and source:getDuration

User avatar
zorg
Party member
Posts: 2616
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Playing music with 2 sources

Post by zorg » Tue Jul 16, 2019 11:35 am

If you're using queueablesources, then forget about source:tell(), and especially about source:getDuration; you don't need those since you should be manually queueing stuff into the qsource.

Instead, you should:
- query the qsource if it has free buffers or not, and if it doesn't, queue up another sounddata into it,
- keep track of how many samplepoints you pushed into it through sounddata:getSampleCount().

Now, usually, qsources have lots of internal buffers, so unless you're using a small intermediary sounddata and chunking the data into smaller pieces, feeding it constantly into the qsource, you might want to create the qsource with just 2 internal buffers, which you can with giving 2 as the last parameter to it when you create it (see the wiki); that way, you won't ever queue up more than the single instances of the two songs you want to queue up; if a buffer empties, it will automatically queue the other one up next.

And it doesn't matter when that queue-ing happens (with qsource:queue() ), as long as it's before the previous buffer's worth of data hasn't ran out.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

samuelcable
Prole
Posts: 4
Joined: Sun Jul 14, 2019 5:36 am

Re: Playing music with 2 sources

Post by samuelcable » Tue Jul 16, 2019 1:12 pm

zorg wrote:
Tue Jul 16, 2019 11:35 am
If you're using queueablesources, then forget about source:tell(), and especially about source:getDuration; you don't need those since you should be manually queueing stuff into the qsource.

Instead, you should:
- query the qsource if it has free buffers or not, and if it doesn't, queue up another sounddata into it,
- keep track of how many samplepoints you pushed into it through sounddata:getSampleCount().

Now, usually, qsources have lots of internal buffers, so unless you're using a small intermediary sounddata and chunking the data into smaller pieces, feeding it constantly into the qsource, you might want to create the qsource with just 2 internal buffers, which you can with giving 2 as the last parameter to it when you create it (see the wiki); that way, you won't ever queue up more than the single instances of the two songs you want to queue up; if a buffer empties, it will automatically queue the other one up next.

And it doesn't matter when that queue-ing happens (with qsource:queue() ), as long as it's before the previous buffer's worth of data hasn't ran out.

Ah, thank you, this actually clears up a lot of things. Sorry for not doing more research on sounddata

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests