extends Control var effect: AudioEffectRecord 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. # 2147483647 var noise_max_lvl := 2000000#10000000 func _ready() -> void: var idx := AudioServer.get_bus_index("Record") effect = AudioServer.get_bus_effect(idx, 2) %Controls.visible=false #region Record stuff func _on_load_words_pressed() -> void: if not FileAccess.file_exists(%SavePath.text.path_join("list.json")): return var txt = FileAccess.get_file_as_string(%SavePath.text.path_join("list.json")) words=JSON.parse_string(txt) for t in words: %WordList.add_item(t) func save_words(): var fl = FileAccess.open(%SavePath.text.path_join("list.json"),FileAccess.WRITE) var txt = JSON.stringify(words,"\t") fl.store_string(txt) fl.close() func _process(delta: float) -> void: if Input.is_action_just_pressed("ui_right"): if last_item_index!=null: run_swap_code(last_item_index+1) if Input.is_action_just_pressed("ui_left"): if last_item_index!=null: run_swap_code(last_item_index-1) func run_swap_code(new_index): if effect.is_recording_active(): start_new_record() await get_tree().process_frame _on_save_button_pressed() if words.size()<=new_index: new_index=0 if 0>new_index: new_index=words.size()-1 %WordList.select(new_index) %WordList.ensure_current_is_visible() _on_item_list_item_selected(new_index) var words :Array= [] func _on_add_word_btn_pressed() -> void: if (%SoundName.text=="" or words.has(%SoundName.text)): return %WordList.add_item(%SoundName.text) words.append(%SoundName.text) %SoundName.text="" save_words() var last_item_index :int = -1 var last_item_text := "" var last_item_path := "" func _on_item_list_item_selected(index: int) -> void: %Controls.visible=true last_item_text=%WordList.get_item_text(index) %WordName.text=last_item_text last_item_index=index last_item_path=%SavePath.text last_item_path=last_item_path.path_join(last_item_text+".wav") if FileAccess.file_exists(last_item_path): recording=load(last_item_path) 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) remove_noise() %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 start_new_record(): recording = effect.get_recording() %PlayButton.disabled = false %SaveButton.disabled = false if recording!=null: effect.set_recording_active(false) recording.set_mix_rate(mix_rate) recording.set_format(format) recording.set_stereo(stereo) remove_noise() await get_tree().process_frame effect.set_recording_active(true) func remove_noise(): return #var data := recording.data.to_int32_array() #var new_data := PackedInt32Array() # #var offset = 0 #var uc #for i in data: #if i < noise_max_lvl: #i=0 #new_data.append(i) #recording.data=new_data.to_byte_array() 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_save_button_pressed() -> void: if recording==null: return if not DirAccess.dir_exists_absolute(%SavePath.text): DirAccess.make_dir_absolute(%SavePath.text) if FileAccess.file_exists(last_item_path): DirAccess.remove_absolute(last_item_path) recording.save_to_wav(last_item_path) %Status.text = "Status: Saved WAV file to: %s" % [last_item_text+".wav"] func _on_delete_button_pressed() -> void: if (last_item_index==null): return if DirAccess.dir_exists_absolute(%SavePath.text) and FileAccess.file_exists(last_item_path): DirAccess.remove_absolute(last_item_path) words.erase(last_item_text) last_item_index=-1 last_item_text="" last_item_path="" %Controls.visible=false save_words() #endregion #region PlanText stuff func _on_load_text_btn_pressed() -> void: if not FileAccess.file_exists(%WordsTXTPath.text): return var txt = FileAccess.get_file_as_string(%WordsTXTPath.text) %TextWordsEdit.text=txt func _on_add_words_btn_pressed() -> void: var txt :String= %TextWordsEdit.text txt=txt.replace("\n"," ").replace("\t"," ") var wrds = txt.split(" ",false) for wrd in wrds: if !words.has(wrd): %WordList.add_item(wrd) words.append(wrd) var play = false func _on_play_text_btn_pressed() -> void: play=!play if play: var txt :String= %TextWordsEdit.text txt=txt.replace("\n"," ").replace("\t"," ") var wrds = txt.split(" ",false) for wrd in wrds: var path = %SavePath.text.path_join(wrd+".wav") if FileAccess.file_exists(path): var stream = load(path) $AudioStreamPlayer2.stream = stream $AudioStreamPlayer2.play() await $AudioStreamPlayer2.finished play=false else: $AudioStreamPlayer2.stop() #endregion #region Aditional stuff 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(%SavePath.text)) #endregion