Page 1 of 1

Playing Sound and Music at the Same Time 2018

Posted: Fri Feb 23, 2018 5:51 pm
by veerashurra
Hello everyone!

I am a noob at Ren'Py and ran into an interesting issue while coding today. I want to play a sound effect and music at the same time, but for the life of me I cannot get it to work. I've tried using the audio channel, and I've tried registering a new channel, but I'm pretty sure I'm doing it wrong. No matter what I do, I cannot get the secondary sound effect to play over the music.

Any help would be much appreciated, or if someone could point me in the direction of an updated tutorial for this, I'd be very grateful. I can't find any new information past Feb. of 2017.

Cheers
Cas

Re: Playing Sound and Music at the Same Time 2018

Posted: Fri Feb 23, 2018 7:06 pm
by Morhighan
Hey, welcome!

I struggled with this at first, too! I ended up figuring it out, so I'll show you how I do it. :D

The first issue I ran into with audio files was the format. I was using MP3s, which are compatible with Ren'Py. But the files were large enough to make the game kind of clunky when it tried to play them. Also the MP3 format is not open source, so I don't use it so I can avoid any fees or copyright issues.

The good news is that MP3 is not the only audio file format that is supported! OPUS, OGG Vorbis, and WAV files are also compatible. Personally, I use the OGG Vorbis format. .ogg files are pretty lightweight, size-wise, but don't sacrifice much in the way of quality!

Ren'Py allows for the use of multiple audio channels. The three default channels are music, sound, and voice, but you can add others.

The music channel plays background music. The sound channel allows for sound effects to be played. This channel is ideal for short sound effects, like a gunshot or shattering glass. Sound effects generally do not loop (so you don't deal with the gunshots on repeat.) The voice channel allows for voice acted files to be synchronized with the text of the story. Additional channels are defined into three categories: music, sfx, and voice. This allows the volume mixers to be adjusted in the settings menu.

These usually work pretty well, but I also have made use of "longer" sound effects that play over the music, like the sound of cicadas or waves or the wind blowing. Most of these are sounds that would be found in nature, so that's what I call the new channel.

To add the nature, add the following into options.rpy:

Code: Select all

renpy.music.register_channel("nature", "sfx", True)
Where "nature" is the label, "sfx"is the volume mixer assignment, and "True" determines that the sound loops.

To play a nature sound in your script:

Code: Select all

play nature "nature.ogg"
To play a short sound effect in the script:

Code: Select all

play sound "gunshot.ogg"
To play a song in the bgm:

Code: Select all

play music "mozart.ogg"
To play a voice file with the written script:

Code: Select all

voice "line_0001.ogg"
"Hello World."
Hope this helps!