Thursday, 16 April 2015

Ren'Py Multiple Audio Channels or Tracks

I keep meaning to do this post about audio channels in Ren'Py.

It was when I was working on my Nanoreno game, New Pisces, that I realised I would need to dig a bit deeper than just the Music, Sound and Voice channels that are standard in a new Ren'Py project. I wanted to have at least 12 loops of audio playing at the same time.

(This might seem like a lot, but the loops are quite stripped back - not full songs - and the 'gameplay' focuses on the interactive audio.)

After saving my 24+ audio files as mostly wav and ogg depending on the filesize (I tend to convert my wav files to ogg if they are over 1mb in the interest of keeping overall filesize low - I've been using http://media.io/ to convert the files) I started to look at how to get multiple music channels.

The Ren'Py documentation on Audio is here http://www.renpy.org/doc/html/audio.html where I saw:
 "New channels can be registered with renpy.music.register_channel()."

I think I first tried putting that code into the options.rpy file which didn't work and eventually I had a go with it in the script.rpy file - which works just fine! I also added "init python:" which I think I must've discovered on the lemmasoft forums ( http://lemmasoft.renai.us/forums/ ) when searching for solutions. In case it's helpful to anybody here is my code from the very top of my script.rpy file:

# You can place the script of your game in this file.

init python:
    renpy.music.register_channel("one", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("two", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("three", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("four", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("five", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("six", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("seven", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("eight", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("nine", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("ten", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("eleven", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("twelve", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("zero", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)
    renpy.music.register_channel("zeroone", mixer=None, loop=None, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True)

#my variables

define slowdis = Dissolve(2.0, alpha=False, time_warp=None)
define vslowdis = Dissolve(3.0, alpha=False, time_warp=None)
define pixelfast = Pixellate(0.3, 8)


Then to get my file playing on the desired channel I used:
    play one "00.ogg"

I also wanted to change the volume of certain channels at various points of the game, for that I used:
    $ renpy.music.set_volume(volume=0.2, delay=0.8, channel='one')

Here the volume is expressed as a decimal with 1.0 being full volume and 0.0 being silent, I believe this is accurate to more than one decimal place, but didn't do lots of testing to be sure that it doesn't round up or down. The delay property is the amount of time in seconds the change in volume takes, so the fade between the old and new volume.

Hopefully that has been helpful, if you'd like to ask me questions on this (or anything really!) the best way to get in touch is via twitter ( @sleepyagents )

4 comments:

  1. Thank you so much! I've been struggling with this for the past couple of hours! No where did I read about adding the "init python:" and that just fixed it. I appreciate you sharing this!

    ReplyDelete
    Replies
    1. No problem! I'm glad it was helpful ^^

      Delete
  2. Thank you so much, that was very usefull.

    ReplyDelete