Just wondering if there was an easy way to play Sounds one after the other?
I'm trying to add speech to my game but have come across a few issues with creating the sound files. At the moment I'm creating a different sound for each required phrase, so, for example, the following text:
- Code: Select all
sTmp = "You have produced " .. iProduction .. " Commodities this turn! Please press the buttons below to allocate it into Storage and press OK when finished. NOTE: The RESET button will reset your Stored Commodities back to their original values."
would need multiple different versions depending on the value of iProduction:
IG221.ogg (iProduction = 1)
IG222.ogg (iProduction = 2)
IG223.ogg (iProduction = 3)
...
IG2210.ogg (iProduction = 10)
I'm currently limiting them to a maximum of 10, but until I test this part of the game, I'm not 100% sure what the maximum value would be
I would like to be able to play 3 different 2D Sounds to generate the above speech, as it would make my life so much easier:
IG22.ogg (First part of text)
NO1.ogg (Number 1 - i.e. iProduction = 1)
IG23.ogg (Second part of text)
then play them in sequence.
Short of writing something in the onUpdate function, I can't see a way around this? Also, I have rather a lot of speech to do, which would make using onUpdate almost unmanageable, so was hoping there would be some other way (maybe something like a Sound buffer that will only play the next item in the buffer if the previous one has finished?).
Any help/thoughts would be gratefully received.
Regards
Shando
[EDIT] I have managed to put together the code below, which seems to work OK, just a minor issue with timing, but not a problem for now:
- Code: Select all
tSB = { }
iSB = -1
iCurSB = 0
function onUpdate( td )
updateSoundBuffer ( )
end
function onKeyDown( key )
if ( key == "a" ) then
addToSoundBuffer ( "IG1.ogg" )
addToSoundBuffer ( "IG2.ogg" )
addToSoundBuffer ( "IG3.ogg" )
elseif ( key == "p" ) then
playSoundBuffer ( )
end
end
function addToSoundBuffer ( sIn )
local iLen = tableLength ( tSB )
iLen = iLen + 1
tSB[iLen] = sIn
end
function playSoundBuffer ( )
sound.setSoundFile ( tSB[1] )
sound.setIsStreamed ( 0 )
iSB = sound.create2DSound ( )
sound.play2DSound ( iSB )
iCurSB = 1
end
function updateSoundBuffer ( )
if ( iCurSB > 0 ) then
if ( sound.isPlaying ( iSB ) == 0 ) then
sound.freeSound ( iSB )
iCurSB = iCurSB + 1
local iLen = tableLength ( tSB )
if ( iCurSB <= iLen ) then
sound.setSoundFile ( tSB[iCurSB] )
sound.setIsStreamed ( 0 )
iSB = sound.create2DSound ( )
sound.play2DSound ( iSB )
else
tSB = { }
iSB = -1
iCurSB = 0
end
end
end
end
function tableLength ( inT )
local iC = 0
for _ in pairs ( inT ) do
iC = iC + 1
end
return iC
end