Skip to content

Play sounds

cl4cnam edited this page Nov 29, 2023 · 6 revisions

In this tutorial, we are going to play sounds, multiple sounds in the same time.

Prepare your environment

Do as in Hello World tutorial except that you change the names: playSounds, playSounds.html, playSounds.fg. Copy the files ducks.mp3, birds.mp3, barking.mp3 into the directory playSounds.

Write the program

Write these lines into playSounds.fg:

parallel ||
	playSoundFile('ducks.mp3')
||
	playSoundFile('birds.mp3')
||
	playSoundFile('barking.mp3')

displayNewMessage('The End')

Explanation

The program is made up of two parts:

parallel ||
	playSoundFile('ducks.mp3')
||
	playSoundFile('birds.mp3')
||
	playSoundFile('barking.mp3')

and

displayNewMessage('The End')

Note

The second part is executed only when the first part is finished.

The first part is a block (that I called 'parallel construct'):

parallel ||
	block1
||
	block2
||
	block3

executes "block1", "block2" and "block3" in parallel (just logical parallelism).

Note

Beware that the 'parallel construct' finished only when all the three blocks are finished.

So when you load playSounds.html, here is the timeline:

timeline2

Variant #1

If you change a little the program (add exitAfter 2 finished just after the parallel word):

parallel exitAfter 2 finished ||
	playSoundFile('ducks.mp3')
||
	playSoundFile('birds.mp3')
||
	playSoundFile('barking.mp3')

displayNewMessage('The End')

when the two blocks that finished first (playSoundFile('barking.mp3') and playSoundFile('ducks.mp3')) are finished,
all the remaining blocks (in this case, just playSoundFile('birds.mp3')) are discontinued and the parallel construct finishes.
So that the 'The End' message is displayed. Here is the timeline:

timeline3

Variant #2

If you change again a little the program (put exitWith branch 3 just after the parallel word):

parallel exitWith branch 3 ||
	playSoundFile('ducks.mp3')
||
	playSoundFile('birds.mp3')
||
	playSoundFile('barking.mp3')

displayNewMessage('The End')

when the third (in order of writing) block (playSoundFile('barking.mp3')) is finished,
all the remaining blocks (in this case, playSoundFile('ducks.mp3') and playSoundFile('birds.mp3')) are discontinued and the parallel construct finishes.
So that the 'The End' message is displayed. Here is the timeline:

timeline4