MicRecord.gd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. extends Control
  2. var effect # See AudioEffect in docs
  3. var recording # See AudioStreamSample in docs
  4. var stereo := true
  5. var mix_rate := 44100 # This is the default mix rate on recordings
  6. var format := 1 # This equals to the default format: 16 bits
  7. func _ready():
  8. var idx = AudioServer.get_bus_index("Record")
  9. effect = AudioServer.get_bus_effect(idx, 0)
  10. func _on_RecordButton_pressed():
  11. if effect.is_recording_active():
  12. recording = effect.get_recording()
  13. $PlayButton.disabled = false
  14. $SaveButton.disabled = false
  15. effect.set_recording_active(false)
  16. recording.set_mix_rate(mix_rate)
  17. recording.set_format(format)
  18. recording.set_stereo(stereo)
  19. $RecordButton.text = "Record"
  20. $Status.text = ""
  21. else:
  22. $PlayButton.disabled = true
  23. $SaveButton.disabled = true
  24. effect.set_recording_active(true)
  25. $RecordButton.text = "Stop"
  26. $Status.text = "Status: Recording..."
  27. func _on_PlayButton_pressed():
  28. print_rich("\n[b]Playing recording:[/b] %s" % recording)
  29. print_rich("[b]Format:[/b] %s" % ("8-bit uncompressed" if recording.format == 0 else "16-bit uncompressed" if recording.format == 1 else "IMA ADPCM compressed"))
  30. print_rich("[b]Mix rate:[/b] %s Hz" % recording.mix_rate)
  31. print_rich("[b]Stereo:[/b] %s" % ("Yes" if recording.stereo else "No"))
  32. var data = recording.get_data()
  33. print_rich("[b]Size:[/b] %s bytes" % data.size())
  34. $AudioStreamPlayer.stream = recording
  35. $AudioStreamPlayer.play()
  36. func _on_Play_Music_pressed():
  37. if $AudioStreamPlayer2.playing:
  38. $AudioStreamPlayer2.stop()
  39. $PlayMusic.text = "Play Music"
  40. else:
  41. $AudioStreamPlayer2.play()
  42. $PlayMusic.text = "Stop Music"
  43. func _on_SaveButton_pressed():
  44. var save_path = $SaveButton/Filename.text
  45. recording.save_to_wav(save_path)
  46. $Status.text = "Status: Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
  47. func _on_MixRateOptionButton_item_selected(index: int) -> void:
  48. if index == 0:
  49. mix_rate = 11025
  50. elif index == 1:
  51. mix_rate = 16000
  52. elif index == 2:
  53. mix_rate = 22050
  54. elif index == 3:
  55. mix_rate = 32000
  56. elif index == 4:
  57. mix_rate = 44100
  58. elif index == 5:
  59. mix_rate = 48000
  60. if recording != null:
  61. recording.set_mix_rate(mix_rate)
  62. func _on_FormatOptionButton_item_selected(index: int) -> void:
  63. if index == 0:
  64. format = AudioStreamWAV.FORMAT_8_BITS
  65. elif index == 1:
  66. format = AudioStreamWAV.FORMAT_16_BITS
  67. elif index == 2:
  68. format = AudioStreamWAV.FORMAT_IMA_ADPCM
  69. if recording != null:
  70. recording.set_format(format)
  71. func _on_StereoCheckButton_toggled(button_pressed: bool) -> void:
  72. stereo = button_pressed
  73. if recording != null:
  74. recording.set_stereo(stereo)
  75. func _on_open_user_folder_button_pressed():
  76. OS.shell_open(ProjectSettings.globalize_path("user://"))