notes_to_hz.gd 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. extends GraphNode
  2. @export var min_gap: float = 0.5 # editable value in inspector for the minimum gap between min and max
  3. signal open_help
  4. func _ready() -> void:
  5. #add button to title bar
  6. var titlebar = self.get_titlebar_hbox()
  7. var btn = Button.new()
  8. btn.text = "?"
  9. btn.tooltip_text = "Open help for " + self.title
  10. btn.connect("pressed", Callable(self, "_open_help")) #pass key (process name) when button is pressed
  11. titlebar.add_child(btn)
  12. var i = 0
  13. while i <= 6:
  14. $Note.set_item_tooltip_enabled(i, false)
  15. i += 1
  16. $Accidental.set_item_tooltip(0, "3/4 Flat")
  17. $Accidental.set_item_tooltip(1, "Flat")
  18. $Accidental.set_item_tooltip(2, "1/4 Flat")
  19. $Accidental.set_item_tooltip(3, "Natural")
  20. $Accidental.set_item_tooltip(4, "1/4 Sharp")
  21. $Accidental.set_item_tooltip(5, "Sharp")
  22. $Accidental.set_item_tooltip(6, "3/4 Sharp")
  23. $Note.select(0, true)
  24. $Accidental.select(3, true)
  25. calculate_freq()
  26. func _open_help():
  27. open_help.emit(self.get_meta("command"), self.title)
  28. func _on_item_list_item_selected(index: int) -> void:
  29. calculate_freq()
  30. func _on_item_list_2_item_selected(index: int) -> void:
  31. calculate_freq()
  32. const NOTE_TO_MIDI = {
  33. 0: 9, 1: 11, 2: 12, 3: 14, 4: 16, 5: 17, 6: 19,
  34. }
  35. const ACCIDENTAL_TO_MODIFIER = {
  36. 0: -1.5, 1: -1, 2: -0.5, 3: 0, 4: 0.5, 5: 1, 6: 1.5,
  37. }
  38. func calculate_freq():
  39. var note = $Note.get_selected_items()[0]
  40. var accidental = $Accidental.get_selected_items()[0]
  41. var freq
  42. var textout = ""
  43. $FreqOutput.text = ""
  44. note = NOTE_TO_MIDI.get(note, null)
  45. accidental = ACCIDENTAL_TO_MODIFIER.get(accidental, null)
  46. note = note + accidental
  47. freq = 440.0 * pow(2, (note - 69) / 12.0)
  48. var count = 0
  49. while count < 11:
  50. textout += "%.2f, " % freq
  51. freq = freq * 2
  52. count +=1
  53. $FreqOutput.text = textout