play 1 sound 2 times?

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.
User avatar
Tomonline
Prole
Posts: 17
Joined: Sun Oct 20, 2013 12:29 am

play 1 sound 2 times?

Post 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
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: play 1 sound 2 times?

Post by Doctory »

perhaps this will help: viewtopic.php?f=4&t=79357
User avatar
Tomonline
Prole
Posts: 17
Joined: Sun Oct 20, 2013 12:29 am

Re: play 1 sound 2 times?

Post 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?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: play 1 sound 2 times?

Post 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.
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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: play 1 sound 2 times?

Post 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
When I write def I mean function.
User avatar
Tomonline
Prole
Posts: 17
Joined: Sun Oct 20, 2013 12:29 am

Re: play 1 sound 2 times?

Post 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 :)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: play 1 sound 2 times?

Post by zorg »

In all actuality, the example kikito gave is almost exactly what i would have written.
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.
User avatar
Tomonline
Prole
Posts: 17
Joined: Sun Oct 20, 2013 12:29 am

Re: play 1 sound 2 times?

Post 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 :)
User avatar
Tomonline
Prole
Posts: 17
Joined: Sun Oct 20, 2013 12:29 am

Re: play 1 sound 2 times?

Post by Tomonline »

error in function 'updatePhrase', attempt to index a nill value.
also playing e.g. word1, word2, word2 won't work correctly.. \:
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: play 1 sound 2 times?

Post by kikito »

Please post your .love file.
When I write def I mean function.
Post Reply

Who is online

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