fast time based event for generative music

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.
Post Reply
jw_otto
Prole
Posts: 2
Joined: Mon Feb 27, 2017 9:36 pm

fast time based event for generative music

Post by jw_otto »

Hi,
At first I want to thank the community for love2d, really like it!

I am a nerdy musician that likes to code.
Here is some of my music. The visuals where made with processing and a Kinect
https://vimeo.com/163467506

Now I am working on a game in love2d with generative music, but I can't get the timing exactly right.
musically I know what to do but programming wise I have al lot to learn.


the basic of my code looks something like this

Code: Select all


-- 16 note at 120 bpm
maxtime = 0.125

--measures
maxcounter = 4
switch = false

function update()

time = love.timer.getTime()%maxtime
if time >0 and time < maxtime/2 and switch == false then

-- play sound at first note 
if counter == 0 and switch == false then 
--playsound here
end

counter= (counter +1) %maxcounter

switch = true
end

if time > maxtime/2 then
	switch = false
end

end

download the full example here
https://www.dropbox.com/sh/ll5knmipjk1m ... F93Pa?dl=0

I know this isn't the perfect way of doing it but I don't know a other one..
So my question is how can I do fast time based event te proper way ?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: fast time based event for generative music

Post by zorg »

Hello and welcome to the forums!
...also, my man, i'm your man :3

First, you have two choices:
1. Use a conf.lua and turn off vsync, since that limits how often love.update is called.
2. Use a separate thread for audio timing.
Both may have downsides, but the second one may be the better choice, since processing music in its own dedicated thread will ensure that nothing else can mess with its timing... that said, my projects still use the first.

Second, you should not create sources all the time (as you are doing now), just create as many as you need at first, in your soundLoad function, for instance, then :play() them when you need to.

Some minor nitpicks:
- use # instead of table.getn(), since that's deprecated syntax: toonladderlength = #toonladder
- love.update has a dt parameter, you can use that in your sound.lua's soundUpdate function, just pass it in, instead of calling love.timer.getTime:
function soundUpdate(dt) time = time + dt % maxtime end -- in sound.lua
function love.update(dt) soundUpdate(dt)end -- in main.lua


Finally, it's not relevant YET, but the upcoming 0.11.0 version of löve will have a new QueueableSource object, that you can push audio data to in realtime; i've been using that to create a module player (among other things) myself, and others have been experimenting with similar things for a while now. With that, you can "correctly" time your music based on how many buffers you have filled with data, instead of relying on an external timer.
(But if you are interested, then this is something that can work like that)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
jw_otto
Prole
Posts: 2
Joined: Mon Feb 27, 2017 9:36 pm

Re: fast time based event for generative music

Post by jw_otto »

Thanks for looking at my code my man!

You gave me some helpful tips
I was thinking about turning of the vsync but I wanted to implement it in a platform jumper so turning of the vsyn would be kind of a hassle for me at the moment. thanks for the minor nitpicks the program would indeed run faster if do that! I have much to learn haha.
I'm gonna check that module out!

Do you have any sources about this subject for instance I really like the pico-8 and the sonicPI sound module/timing?

So much thank for you help!
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: fast time based event for generative music

Post by zorg »

Any game works fine even without vsync, platformer or not, you just need to use dt regardless; but even if you want a constant update rate, you could do something like this:

Code: Select all

local rate = 1/60
local acc = 0.0
function love.update(dt)
  acc = acc + dt
  if acc >= rate then
    acc = acc - rate
    -- Update stuff here
  end
end
The same code could be used for the draw function as well, also it's a bit more involved, since the screen's vsync may not be 60 Hz.

Sadly, i couldn't find any in-depth details about how pico-8's music system works, other than the fact that it uses a tracker-like interface, i did find a video tutorial series though: https://www.youtube.com/playlist?list=P ... IFZ8MLlhvg

For the sonicPI, i can only point you to their github, since i myself can't really comprehend ruby: https://github.com/samaaron/sonic-pi

Finally, i wouldn't worry about what kind of timing something uses; if this were C or assembly, i'd say to use the system's interrupt timer... but this isn't, and you can't reliably count on anything like that, so as i previously said, you have two coices, using löve's own timer, or counting how many sample(point)s you passed onto your soundcard (with 0.11, i mean); however it's still a minefield of mistakes if one's not careful. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

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