From 55b0a0577d04a0a0246059227fc1cdecc22d926b Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Wed, 21 Jul 2021 22:58:20 +0700 Subject: [PATCH] Update bell - Don't add the bell to the archive to keep it small a simplify licensing. Also bells seem to be rarely used with terminal emulators. - Don't play the bell directly from the Terminal node by adding an AudioStreamPlayer, but make it easy to tune the "bell" signal behavior from the Terminal node so that only an AudioStreamPlayer node's play() method needs to be connected to it. - Keep the bell.wav sound around for testing/demo. --- .../terminal/editor_terminal.tscn | 3 ++ .../terminal/settings/terminal_settings.gd | 2 +- addons/godot_xterm/nodes/terminal/terminal.gd | 36 +++++++----------- examples/menu/menu.tscn | 4 +- examples/terminal/terminal.tscn | 9 ++++- test/unit/terminal.test.gd | 12 ++++++ .../themes => themes}/audio/bell.wav | Bin .../themes => themes}/audio/bell.wav.import | 6 +-- 8 files changed, 42 insertions(+), 30 deletions(-) rename {addons/godot_xterm/themes => themes}/audio/bell.wav (100%) rename {addons/godot_xterm/themes => themes}/audio/bell.wav.import (52%) diff --git a/addons/godot_xterm/editor_plugins/terminal/editor_terminal.tscn b/addons/godot_xterm/editor_plugins/terminal/editor_terminal.tscn index 94bbd9b..a94643a 100644 --- a/addons/godot_xterm/editor_plugins/terminal/editor_terminal.tscn +++ b/addons/godot_xterm/editor_plugins/terminal/editor_terminal.tscn @@ -24,4 +24,7 @@ env = { "TERM": "xterm-256color" } +[node name="Bell" type="AudioStreamPlayer" parent="."] + +[connection signal="bell" from="." to="Bell" method="play"] [connection signal="exited" from="PTY" to="." method="_on_PTY_exited"] diff --git a/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd b/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd index da25899..44d7608 100644 --- a/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd +++ b/addons/godot_xterm/editor_plugins/terminal/settings/terminal_settings.gd @@ -41,7 +41,7 @@ export var ctrl_scroll_to_resize_font := true # Bell settings. export var visual_bell := true export var audio_bell := true -export var bell_sound: AudioStream = preload("../../../themes/audio/bell.wav") +export var bell_sound: AudioStream # Exec args. export (FileType) var file_type := FileType.USE_SHELL_ENV diff --git a/addons/godot_xterm/nodes/terminal/terminal.gd b/addons/godot_xterm/nodes/terminal/terminal.gd index 318735b..48c6d9e 100644 --- a/addons/godot_xterm/nodes/terminal/terminal.gd +++ b/addons/godot_xterm/nodes/terminal/terminal.gd @@ -35,14 +35,16 @@ var rows = 2 export (bool) var copy_on_selection # Bell -# If enabled, bell_sound will play when the ASCII BELL "\u0007" character is printed. -export var bell_enabled := true -export var bell_sound: AudioStream -# Number of milliseconds that must pass before emitting a new bell sound. -# This important in cases where the bell character is being printed frequently -# such as `while true; do echo -e "\a"; done`, as adding additional AudioStreamPlayer -# nodes too frequently has a negative performance impact. -export var bell_cooldown_msec: int = 100 +# If muted, the "bell" signal will not be emitted when the bell "\u0007" character +# is written to the terminal. +export var bell_muted := false +# Amount of time in seconds that must pass before emitting a new "bell" signal. +# This can be useful in cases where the bell character is being written too +# frequently such as `while true; do echo -e "\a"; done`. +export var bell_cooldown: float = 0.1 + +export var blink_on_time: float = 0.6 +export var blink_off_time: float = 0.3 var _viewport: Viewport = preload("./viewport.tscn").instance() var _native_terminal: Control = _viewport.get_node("Terminal") @@ -229,20 +231,10 @@ func _on_size_changed(new_size: Vector2): func _on_bell(): - if bell_enabled and bell_sound and _bell_timer.time_left == 0: - var player := AudioStreamPlayer.new() - player.stream = bell_sound - player.autoplay = true - player.playing = true - player.connect("finished", self, "_on_player_finished", [player]) - add_child(player) - _bell_timer.start(0.001 * bell_cooldown_msec) - - emit_signal("bell") - - -func _on_player_finished(player: AudioStreamPlayer): - player.queue_free() + if not bell_muted and (bell_cooldown == 0 or _bell_timer.time_left == 0): + emit_signal("bell") + if bell_cooldown > 0: + _bell_timer.start(bell_cooldown) func _mouse_to_cell(pos: Vector2) -> Vector2: diff --git a/examples/menu/menu.tscn b/examples/menu/menu.tscn index 43990c6..771deff 100644 --- a/examples/menu/menu.tscn +++ b/examples/menu/menu.tscn @@ -1,8 +1,7 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://addons/godot_xterm/nodes/terminal/terminal.gd" type="Script" id=1] [ext_resource path="res://examples/menu/menu.gd" type="Script" id=2] -[ext_resource path="res://addons/godot_xterm/themes/audio/bell.wav" type="AudioStream" id=3] [node name="Menu" type="Control"] anchor_right = 1.0 @@ -21,6 +20,5 @@ script = ExtResource( 1 ) __meta__ = { "_edit_use_anchors_": false } -bell_sound = ExtResource( 3 ) [connection signal="key_pressed" from="Terminal" to="." method="_on_Terminal_key_pressed"] diff --git a/examples/terminal/terminal.tscn b/examples/terminal/terminal.tscn index 7c42c62..6b29757 100644 --- a/examples/terminal/terminal.tscn +++ b/examples/terminal/terminal.tscn @@ -1,5 +1,6 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] +[ext_resource path="res://themes/audio/bell.wav" type="AudioStream" id=1] [ext_resource path="res://addons/godot_xterm/nodes/pty/unix/pty_unix.gd" type="Script" id=2] [ext_resource path="res://examples/terminal/terminal.gd" type="Script" id=3] @@ -12,6 +13,7 @@ __meta__ = { "_edit_use_anchors_": false } copy_on_selection = true +bell_cooldown = 0.1 [node name="PTY" type="Node" parent="."] script = ExtResource( 2 ) @@ -20,3 +22,8 @@ env = { "COLORTERM": "truecolor", "TERM": "xterm-256color" } + +[node name="Bell" type="AudioStreamPlayer" parent="."] +stream = ExtResource( 1 ) + +[connection signal="bell" from="." to="Bell" method="play"] diff --git a/test/unit/terminal.test.gd b/test/unit/terminal.test.gd index 3d22006..7209103 100644 --- a/test/unit/terminal.test.gd +++ b/test/unit/terminal.test.gd @@ -10,6 +10,7 @@ func before_each(): func test_bell() -> void: + term.bell_cooldown = 0 term.write(char(7)) term.write(char(0x07)) term.write("\a") @@ -17,3 +18,14 @@ func test_bell() -> void: term.write("'Ask not for whom the \a tolls; it tolls for thee' - John Donne") yield(yield_to(term, "bell", 1), YIELD) assert_signal_emit_count(term, "bell", 5) + + +func test_bell_cooldown() -> void: + watch_signals(term) + term.bell_cooldown = 0.5 + term.write("\a") + term.write("\a") + yield(yield_for(0.5), YIELD) + term.write("\a") + yield(yield_to(term, "bell", 1), YIELD) + assert_signal_emit_count(term, "bell", 2) diff --git a/addons/godot_xterm/themes/audio/bell.wav b/themes/audio/bell.wav similarity index 100% rename from addons/godot_xterm/themes/audio/bell.wav rename to themes/audio/bell.wav diff --git a/addons/godot_xterm/themes/audio/bell.wav.import b/themes/audio/bell.wav.import similarity index 52% rename from addons/godot_xterm/themes/audio/bell.wav.import rename to themes/audio/bell.wav.import index 6853993..0737443 100644 --- a/addons/godot_xterm/themes/audio/bell.wav.import +++ b/themes/audio/bell.wav.import @@ -2,12 +2,12 @@ importer="wav" type="AudioStreamSample" -path="res://.import/bell.wav-54a2dcec0c35ce7bf9aa23071616df00.sample" +path="res://.import/bell.wav-0bb68f27c3e29a9aad676dc03d89b163.sample" [deps] -source_file="res://addons/godot_xterm/themes/audio/bell.wav" -dest_files=[ "res://.import/bell.wav-54a2dcec0c35ce7bf9aa23071616df00.sample" ] +source_file="res://themes/audio/bell.wav" +dest_files=[ "res://.import/bell.wav-0bb68f27c3e29a9aad676dc03d89b163.sample" ] [params]