piano_key.gd 685 B

123456789101112131415161718192021222324252627282930
  1. class_name PianoKey
  2. extends Control
  3. var pitch_scale: float
  4. @onready var key: ColorRect = $Key
  5. @onready var start_color: Color = key.color
  6. @onready var color_timer: Timer = $ColorTimer
  7. func setup(pitch_index: int):
  8. name = "PianoKey" + str(pitch_index)
  9. var exponent := (pitch_index - 69.0) / 12.0
  10. pitch_scale = pow(2, exponent)
  11. func activate():
  12. key.color = (Color.YELLOW + start_color) / 2
  13. var audio := AudioStreamPlayer.new()
  14. add_child(audio)
  15. audio.stream = preload("res://piano_keys/A440.wav")
  16. audio.pitch_scale = pitch_scale
  17. audio.play()
  18. color_timer.start()
  19. await get_tree().create_timer(8.0).timeout
  20. audio.queue_free()
  21. func deactivate():
  22. key.color = start_color