Author Topic: Variable music.  (Read 140 times)

Minefran12

  • Newbie
  • *
  • Posts: 7
    • View Profile
Variable music.
« on: October 31, 2017, 08:18:32 pm »
Hi, Is posible to program a place to have more than 1 music? I want to do a specific place that when you enter the music will variate, one time the song great_fairy, other time sanctuary,...
« Last Edit: November 01, 2017, 12:18:03 pm by Minefran12 »

Diarandor

  • Hero Member
  • *****
  • Posts: 805
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Variable music.
« Reply #1 on: November 01, 2017, 12:49:44 am »
Yes, you can use the random Lua function to choose randomly the music that you will play on a map. You can do this from the map script.

Minefran12

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Variable music.
« Reply #2 on: November 01, 2017, 12:04:00 pm »
Thank you, but how i use it.
« Last Edit: November 01, 2017, 12:18:25 pm by Minefran12 »

Diarandor

  • Hero Member
  • *****
  • Posts: 805
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Variable music.
« Reply #3 on: November 01, 2017, 02:17:35 pm »
Thank you, but how i use it.

The "math.random()" function of Lua is explained here:
http://lua-users.org/wiki/MathLibraryTutorial

And the function to start a chosen music is in the Solarus Lua API, here:
http://www.solarus-games.org/doc/1.5/lua_api_audio.html#lua_api_audio_play_music

MetalZelda

  • Hero Member
  • *****
  • Posts: 511
    • View Profile
Re: Variable music.
« Reply #4 on: November 01, 2017, 11:55:42 pm »
I should go like this

In your music directory, name your music like so 'music_name_0, music_name_1', etc, etc

In your code

Code: Lua
  1. local music = math.random(0, x) -- Where X = number of music with as name "music_name_"
  2. local function play_music()
  3.   sol.audio.play_music("music_name_" .. music, function()
  4.     -- Re-random the music
  5.     -- Get the old number
  6.     local old = music
  7.  
  8.     -- Set a new random value
  9.     music = math.random(0, x)  -- Where X = number of music with as name "music_name_"
  10.  
  11.     -- Avoid repetition
  12.     if music == old then
  13.       -- Re-random the music number
  14.       music = math.random(0, x)  -- Where X = number of music with as name "music_name_"
  15.     end
  16.  
  17.     -- Call the function to play another music
  18.     -- Remark, if you use "LOOPSTART" in the music metadata, the sol.audio.play_music callback (this code essentially) will never be called.
  19.     play_music()
  20.   end)
  21. end
  22.  
  23. -- Start the music. The only way to cancel the phase is to use sol.audio.stop_music()
  24. play_music()
  25.  

That's basic stuff but that's the way to go essentially.

If you want to play specific tracks rather than random one, you can do the same thing, just replace music = math.random(0, x) by music = (music + 1) % x (replace x by the number you want, this is the number of tracks you wanna play)
« Last Edit: November 02, 2017, 12:02:59 am by MetalZelda »

Diarandor

  • Hero Member
  • *****
  • Posts: 805
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Variable music.
« Reply #5 on: November 02, 2017, 01:59:17 am »
There is no need to rename files, and that is a bad idea (it would be a loss of time and you wouldn't be able to guess from the filenames which one is each song). It is much better to define a list of strings with all the music names:
Code: Lua
  1. music_list = { ...blablabla... } --  <--- Put here the list of music names (strings).
and then just generate a random number between 1 and #music_list to choose the music. Something like this:
Code: Lua
  1. local index = math.random(1, #music_list)
  2. local music_id = music_list[index]
  3. sol.audio.play_music(music_id)

Minefran12

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Variable music.
« Reply #6 on: November 14, 2017, 06:27:00 pm »
Thanks, i was doing it with an if to each music.