Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created May 24, 2026 00:14
Show Gist options
  • Select an option

  • Save amirrajan/a005bae0427b3ce045e338ee0d72b55b to your computer and use it in GitHub Desktop.

Select an option

Save amirrajan/a005bae0427b3ce045e338ee0d72b55b to your computer and use it in GitHub Desktop.
class AudioHash < Hash
alias_method :__original_indexor_set__, :[]= if !AudioHash.instance_methods.include?(:__original_indexor_set__)
def volume
@volume ||= if $gtk.platform?(:ios)
0.4
else
1.0
end
# do not mute if tick_count is 0
return @volume if Kernel.tick_count == 0
# do not mute if in dev mode
return @volume if !$args.gtk.production
# do not mute if platform is anything other than web
return @volume if !$args.gtk.platform? :web
# do not mute if game has focus
return @volume if $args.inputs.keyboard.has_focus
# mute volume if the platform is web in a production environment,
# and the game doesn't have focus
return 0.0
end
def volume= value
@volume = value
@volume = @volume.clamp(0.0, 1.0)
end
def sync!
GTK.update_simulation_audio_state
end
def []= key, value
# Redefine :[]= so that the following works:
# #+begin_src
# def boot args
# @sound = {
# input: "sounds/music.ogg",
# gain: 0.5
# }
# end
#
# def tick args
# if args.inputs.keyboard.key_down.j
# if args.audio[:sound]
# args.audio[:sound] = nil
# else
# args.audio[:sound] = @sound
# end
# end
# args.outputs.watch "#{@sound.keys}"
# end
# #+end_src
k_already_queued, v_already_queued = self.find { |k, v| v && v.object_id == value.object_id }
if k_already_queued && v_already_queued && k_already_queued == key && v_already_queued.object_id == value.object_id
value.delete :cptr if value
return __original_indexor_set__ key, value
elsif v_already_queued && v_already_queued.object_id == value.object_id
log <<-S
* WARNING: The instance you are attempting to send to ~args.audio~ is already queued at #{k_already_queued}.
The audio you are trying to queue is already playing. If you want to queue another instances of this audio,
then clone/dup it before adding it to the audio queue:
#+begin_src
# already queued/playing at #{k_already_queued}
#{v_already_queued}
#+end_src
S
return
else
current = self[key]
if current
current.gain = 0
current.looping = false
__original_indexor_set__ GTK.create_uuid, current
value.delete :cptr if value
return __original_indexor_set__ key, value
else
value.delete :cptr if value
return __original_indexor_set__ key, value
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment