Files
godot-demo-projects/audio/mic_record/MicRecord.gd
Hugo Locurcio bac1e69164 Use static typing in all demos (#1063)
This leads to code that is easier to understand and runs
faster thanks to GDScript's typed instructions.

The untyped declaration warning is now enabled on all projects
where type hints were added. All projects currently run without
any untyped declration warnings.

Dodge the Creeps and Squash the Creeps demos intentionally don't
use type hints to match the documentation, where type hints haven't
been adopted yet (given its beginner focus).
2024-06-01 12:12:18 +02:00

100 lines
2.7 KiB
GDScript

extends Control
var effect: AudioEffect
var recording: AudioStreamWAV
var stereo := true
var mix_rate := 44100 # This is the default mix rate on recordings.
var format := AudioStreamWAV.FORMAT_16_BITS # This is the default format on recordings.
func _ready() -> void:
var idx := AudioServer.get_bus_index("Record")
effect = AudioServer.get_bus_effect(idx, 0)
func _on_record_button_pressed() -> void:
if effect.is_recording_active():
recording = effect.get_recording()
$PlayButton.disabled = false
$SaveButton.disabled = false
effect.set_recording_active(false)
recording.set_mix_rate(mix_rate)
recording.set_format(format)
recording.set_stereo(stereo)
$RecordButton.text = "Record"
$Status.text = ""
else:
$PlayButton.disabled = true
$SaveButton.disabled = true
effect.set_recording_active(true)
$RecordButton.text = "Stop"
$Status.text = "Status: Recording..."
func _on_play_button_pressed() -> void:
print_rich("\n[b]Playing recording:[/b] %s" % recording)
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"))
print_rich("[b]Mix rate:[/b] %s Hz" % recording.mix_rate)
print_rich("[b]Stereo:[/b] %s" % ("Yes" if recording.stereo else "No"))
var data := recording.get_data()
print_rich("[b]Size:[/b] %s bytes" % data.size())
$AudioStreamPlayer.stream = recording
$AudioStreamPlayer.play()
func _on_play_music_pressed() -> void:
if $AudioStreamPlayer2.playing:
$AudioStreamPlayer2.stop()
$PlayMusic.text = "Play Music"
else:
$AudioStreamPlayer2.play()
$PlayMusic.text = "Stop Music"
func _on_save_button_pressed() -> void:
var save_path: String = $SaveButton/Filename.text
recording.save_to_wav(save_path)
$Status.text = "Status: Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
func _on_mix_rate_option_button_item_selected(index: int) -> void:
match index:
0:
mix_rate = 11025
1:
mix_rate = 16000
2:
mix_rate = 22050
3:
mix_rate = 32000
4:
mix_rate = 44100
5:
mix_rate = 48000
if recording != null:
recording.set_mix_rate(mix_rate)
func _on_format_option_button_item_selected(index: int) -> void:
match index:
0:
format = AudioStreamWAV.FORMAT_8_BITS
1:
format = AudioStreamWAV.FORMAT_16_BITS
2:
format = AudioStreamWAV.FORMAT_IMA_ADPCM
if recording != null:
recording.set_format(format)
func _on_stereo_check_button_toggled(button_pressed: bool) -> void:
stereo = button_pressed
if recording != null:
recording.set_stereo(stereo)
func _on_open_user_folder_button_pressed() -> void:
OS.shell_open(ProjectSettings.globalize_path("user://"))