How to choose source?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Joliplex
Prole
Posts: 7
Joined: Wed Dec 07, 2011 12:17 pm

How to choose source?

Post by Joliplex »

Hi all!

I was thinking about ranom music. I tried at least these:

Code: Select all

source = love.audio.newSource("music1.mp3" or "music2.mp3" or "music3.mp3")

Code: Select all

source = love.audio.newSource(choose("music1.mp3", "music2.mp3", "music3.mp3"))

Code: Select all

source = love.audio.newSource(math.random("music1.mp3", "music2.mp3", "music3.mp3"))
I just can't figure it out! So if you can help, it will be super!

Thanks! :ultrahappy:
dar0n123
Prole
Posts: 7
Joined: Fri Nov 25, 2011 6:10 pm

Re: How to choose source?

Post by dar0n123 »

+
Last edited by dar0n123 on Thu Oct 30, 2014 4:38 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to choose source?

Post by bartbes »

Something along the lines of:

Code: Select all

local music = {"music1.mp3", "music2.mp3", "music3.mp3"}
local song = music[math.random(1, #music)]
source = love.audio.newSource(song)
Joliplex
Prole
Posts: 7
Joined: Wed Dec 07, 2011 12:17 pm

Re: How to choose source?

Post by Joliplex »

Thaaank you! This will help me alot! :3

EDIT: It doesn't work! Can someone take a look at it?

I used mp3 files for testing, so it's... Big just now.

Here (cannot attach it): http://www.mediafire.com/?du7u9n2w2i02p2b
User avatar
Metalcookie
Prole
Posts: 16
Joined: Sat Dec 10, 2011 2:57 pm
Location: Netherlands

Re: How to choose source?

Post by Metalcookie »

I poked your code a bit and.. math.random() ain't random at all for your program, even after using math.randomseed(os.time())..

Sorry, but that's all I can do for now, good luck.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How to choose source?

Post by kikito »

Metalcookie wrote:I poked your code a bit and.. math.random() ain't random at all for your program, even after using math.randomseed(os.time())..

Sorry, but that's all I can do for now, good luck.
On the contrary, adding math.randomseed(os.time()) seemed to just do the trick for me...

Joliplex, try adding that line to your program, like this:

Code: Select all

function love.load()		--Load stuff!
  math.randomseed(os.time())
  local source = {"Platform.mp3", "Push.mp3", "Prog_House.mp3"}
  local song = source[math.random(1, #source)]
  ...
I tried it in my computer and it worked - the song was selected randomly every time I opened the game.
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to choose source?

Post by bartbes »

It has been reported many times that this is not sufficient for everyone, but generally, calling math.random() twice after math.randomseed() gives pretty good results.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: How to choose source?

Post by Taehl »

Code: Select all

love.load()
	require"TEsound"
	math.randomseed(os.time()) math.random() math.random()
	TEsound.playLooping( {"music1.mp3", "music2.mp3", "music3.mp3"} )
end

love.update(dt)
	TEsound.cleanup()
end
:3
Last edited by Taehl on Sat Dec 10, 2011 7:56 pm, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to choose source?

Post by bartbes »

Disregarding your non-working source, I am left with the impression that you try to plug your library, which seems to happen a bit often, especially considering there already was a solution here.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How to choose source?

Post by kikito »

Independently of that, maybe we could "patch" randomseed to fix the occasional initial non-randomness?

I mean doing something like this directly in LÖVE:

Code: Select all

local prevseed = math.randomseed
math.randomseed = function(seed)
  prevseed(seed)
  for 1,10 math.random() end
end
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests