Page 1 of 2

play 1 sound 2 times?

Posted: Mon Jan 05, 2015 7:55 pm
by Tomonline
hi there :)
I have a problem with playing sounds.

the purpose: making a text to speech program that talks with my voice.

i recorded a lot of phonemes (.wav files with parts of speech) that need to play after each other in the right order.
each sound must play ONLY if the previous sound finished playing.
so if my program needs to say 'hello' then it does this:

play("h.wav")
if "h.wav":isStopped() then:
play("e.wav")
if "e.wav":isStopped() then... etc ...


for test purposes i want to play "a.wav" twice but the problem is that i only hear 'A' once...
if i play e.g. 'A' and then 'B', it works fine..

any help would be mush appreciated! :)

this is the test code i currently have:

Code: Select all

function love.load()
	voice = {"a","b","e","f", ......}
	for i in pairs(voice) do
		voice[i] = love.audio.newSource("VOICE/" .. voice[i] .. ".wav", "static")
	end
end

function love.keypressed(key)
	if key == "a" then
		voice[1]:play()
		while voice[1]:isPlaying() do end
		voice[1]:play()
		while voice[1]:isPlaying() do end
	end
end

Re: play 1 sound 2 times?

Posted: Mon Jan 05, 2015 7:58 pm
by Doctory
perhaps this will help: viewtopic.php?f=4&t=79357

Re: play 1 sound 2 times?

Posted: Mon Jan 05, 2015 8:13 pm
by Tomonline
Doctory wrote:perhaps this will help: viewtopic.php?f=4&t=79357
Doctory, Thanks for the quick reply!

The link you gave doesn't help very much because the guy wants to play 2 sounds at the same time (the opposite of what i want)
i want to play 1 sound file 2 times: if the first one is finished then play the same again (or some other sound if needed).

maybe i missed something?
you can point me in the right direction?

Re: play 1 sound 2 times?

Posted: Mon Jan 05, 2015 11:22 pm
by zorg
Tomonline wrote:

Code: Select all

function love.keypressed(key)
	if key == "a" then
		voice[1]:play()
		while voice[1]:isPlaying() do end
		voice[1]:play()
		while voice[1]:isPlaying() do end
	end
end
That's highly inefficient, because the code flow won't exit those while loops until the sounds stop playing, which takes a long time; if you want to use it in a game like this, it will choke.

A better idea would be to use love.keypressed to add the letters to a queue (or stack, not sure which would be better atm), and using love.update to check periodically if it has elements, and if the topmost is playing or not; if not, toss that out, and play the next.

If you have problems with the implementation, i can code an example tomorrow.

Re: play 1 sound 2 times?

Posted: Tue Jan 06, 2015 12:00 am
by kikito
Instead of using a bunch of "ifs", put your sounds in a table, and use a loop. You will also need to use a variable to "keep track of what word are you in" (I put it inside the phrase table, it could also be outside). You will need to use use [wiki]Source:isPlaying[/wiki] to detect the end of one sound, so you can trigger the next one.

Here's a one-phrase example. I've left the relevant code in a function so you can have more than one phrase easily.

Code: Select all

local words

function love.load()
  local word1 = love.audio.newSource(...)
  local word2 = love.audio.newSource(...)
  local word3 = love.audio.newSource(...)
  words = { word1, word2, word3 }
end

function updatePhrase(phrase)
  phrase.index = phrase.index or 0
  if phrase.index == 0 or not phrase[phrase.index]:isPlaying() then
    phrase.index = phrase.index + 1
    if phrase.index <= #phrase then
      phrase[phrase.index]:play()
    end
  end
end

function love.update(dt)
  updatePhrase(words)
end

Re: play 1 sound 2 times?

Posted: Tue Jan 06, 2015 12:05 am
by Tomonline
Thank you!
its strange that it won't play a sound 2 times, but 2 different sound works fine...
also if i do this strange things happen:

Code: Select all

function love.load()
	voice = {"1","2"}
	for i in pairs(voice ) do
		voice [i] = love.audio.newSource("VOICE/" .. voice [i] .. ".wav", "static")
	end
	speel(voice [1])
	speel(voice [1])
	speel(voice [2])
end

function speel(geluid)
	geluid:play()
	repeat until geluid:isStopped()
	geluid:rewind()
end
it plays the sound twice and then only one time but at the same time as the next sound???
man i'm confused S:

I would love to see an example if you want to do that, zorg :)

Re: play 1 sound 2 times?

Posted: Tue Jan 06, 2015 12:16 am
by zorg
In all actuality, the example kikito gave is almost exactly what i would have written.

Re: play 1 sound 2 times?

Posted: Tue Jan 06, 2015 11:24 am
by Tomonline
ow, didn't saw the second replay yesterday, i think we submitted it the same time...

i'm not going to use key presses eventually (that was for testing purposes), the sentences are going to be build in, and the only things my
voice has to say differently are the points and the names in my game..

well, i'm going to try your example but still i find it strange that mine won't work correctly.
can someone say me why isStopped is so buggy in my example?

i let you know if yours it works correctly in a sec kikito :)

Re: play 1 sound 2 times?

Posted: Tue Jan 06, 2015 11:31 am
by Tomonline
error in function 'updatePhrase', attempt to index a nill value.
also playing e.g. word1, word2, word2 won't work correctly.. \:

Re: play 1 sound 2 times?

Posted: Tue Jan 06, 2015 12:43 pm
by kikito
Please post your .love file.