Drum Machine - Clocks and such

ghostoGamesghostoGames Posts: 9Member
edited April 27 in Tutorials

Another quick and dirty presentation. This is an experimental project to explore more of the sound capabilities built into Godot. Timers, as it turns out, don't make super precise beats. I've worked a bit more on this project and there is at least one more video's worth of exploration.

Tags :


  • ghostoGamesghostoGames Posts: 9Member

  • cyberealitycybereality Posts: 927Moderator

    I've tried to do something similar a long time ago in Shockwave (sorry if that dates me). It didn't really work great, but I have thought about this a bit.

    I think you may want to consider playing the sounds on "_process" or "_physics_process". Keep track of the elapsed time and the current bar and then play the sound if it passes that time point (you can use the modulus operator so it loops).

    Maybe in physics process, if you set the update to 1000 fps (1 ms) that should be more than enough resolution. If the frame rate is lower (likely 60 fps) it will just run multiple times per frame but that should be fine and I don't think the processing overhead would be that much considering the graphics are simple.

  • MegalomaniakMegalomaniak Posts: 2,603Admin

    @cybereality said:
    or "_physics_process".

    I was thinking the same, physics process since its likely to be more temporally stable and predictable.

  • TwistedTwiglegTwistedTwigleg Posts: 2,618Admin
    edited April 23

    I do not know if you've seen it, but this page on the documentation might be helpful: Sync the gameplay with audio and music

  • ghostoGamesghostoGames Posts: 9Member

    @Megalomaniak said:

    @cybereality said:
    or "_physics_process".

    I gave it a go:

    tldw; I get the same issue using the physics process and find out that the physics engine is capped some where south of 500. I find out that the OS has functions to use but the engine freezes when trying to use those values in computation. The timing is more solid though.

    I'm thinking if I pursue solid timing any further I may have to look into trying to get some c++ going and haven't the foggiest of how to get it working in Godot. I have a decent history of the language though so maybe I can check some of that out soon.

  • ghostoGamesghostoGames Posts: 9Member
    edited April 23

    @TwistedTwigleg said:
    I do not know if you've seen it, but this page on the documentation might be helpful: Sync the gameplay with audio and music

    I haven't seen this, thanks. I'm not sure if it can directly address my issue with the drum machine but I'll need to think about it a bit. Definitely some good info.

    edit: So just like in that second video, anytime I call an OS timing function to trigger playback the engine destabilizes. When it is in a triggered event like a button it freezes input (but plays back) and when it is in one of the process functions it quits out. Here is the code where it quits out that isn't in video.

    func _ready():

    time_begin = OS.get_ticks_usec()

    time_delay = AudioServer.get_time_to_next_mix() + AudioServer.get_output_latency()

    func _process(_delta):

    var time = (OS.get_ticks_usec() - time_begin) / 1000000.0
    time -= time_delay
    time = max(0, time)
    if running == true:

    if posmod(time, 3 / tempo) == 0:


  • cyberealitycybereality Posts: 927Moderator

    Here is how I imagined it. Not sure this is perfect, but it seems to work okay.

    extends Node2D
    var elapsed_ms = 0.0
    var beat_count = 0
    var ms_per_beat = bpm_to_ms(240.0)
    onready var sound = get_node("Sound")
    func _process(delta):
        elapsed_ms += delta * 1000.0
        while elapsed_ms > beat_count * ms_per_beat:
            beat_count += 1
    func bpm_to_ms(bpm):
        return (60.0 * 1000.0) / bpm
    func play_beat():
  • ghostoGamesghostoGames Posts: 9Member

    tldw; I did some statistical (and audial) analysis on the different models of producing a clock for the drum machine in Godot. Turns out some ways are better than others but none are accurate enough for what I need. From here on I will be pursuing a way to run the clock with C++ code, which I need to learn how to do first.

    Any thoughts or suggestion are very welcome and thanks to all

    This will probably be the last video post in this thread as the project will no longer solely be a timer based drum machine. I'll link to the new thread here when I get to it. For etiquette purposes, where would be the best place to put this new thread? Would it still be a Tutorial or perhaps a W.I.P?

  • cyberealitycybereality Posts: 927Moderator

    You can just rename the title of this thread to something else. Would probably make more sense than making a new one. If you can't rename it, send me a PM I should be able to do it.

Sign In or Register to comment.