Page 1 of 1

Playing music with 2 sources

Posted: Sun Jul 14, 2019 6:05 am
by samuelcable
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

Re: Playing music with 2 sources

Posted: Sun Jul 14, 2019 6:50 pm
by ReFreezed
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.)

Re: Playing music with 2 sources

Posted: Mon Jul 15, 2019 6:38 am
by samuelcable
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

Re: Playing music with 2 sources

Posted: Tue Jul 16, 2019 9:26 am
by samuelcable
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

Re: Playing music with 2 sources

Posted: Tue Jul 16, 2019 11:35 am
by zorg
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.

Re: Playing music with 2 sources

Posted: Tue Jul 16, 2019 1:12 pm
by samuelcable
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