Bläddra i källkod

Merge pull request #711 from voylin/PR_Request_Mic_Record_Demo

Adding settings for mix_rate, stereo and format for Mic Record Demo 4.0-dev
Aaron Franke 3 år sedan
förälder
incheckning
713dfd064d

+ 51 - 9
audio/mic_record/MicRecord.gd

@@ -1,7 +1,12 @@
 extends Control
 extends Control
 
 
-var effect
-var recording
+var effect  # See AudioEffect in docs
+var recording  # See AudioStreamSample in docs
+
+var stereo := true
+var mix_rate := 44100  # This is the default mix rate on recordings
+var format := 1  # This equals to the default format: 16 bits
+
 
 
 func _ready():
 func _ready():
 	var idx = AudioServer.get_bus_index("Record")
 	var idx = AudioServer.get_bus_index("Record")
@@ -14,6 +19,9 @@ func _on_RecordButton_pressed():
 		$PlayButton.disabled = false
 		$PlayButton.disabled = false
 		$SaveButton.disabled = false
 		$SaveButton.disabled = false
 		effect.set_recording_active(false)
 		effect.set_recording_active(false)
+		recording.set_mix_rate(mix_rate)
+		recording.set_format(format)
+		recording.set_stereo(stereo)
 		$RecordButton.text = "Record"
 		$RecordButton.text = "Record"
 		$Status.text = ""
 		$Status.text = ""
 	else:
 	else:
@@ -21,16 +29,16 @@ func _on_RecordButton_pressed():
 		$SaveButton.disabled = true
 		$SaveButton.disabled = true
 		effect.set_recording_active(true)
 		effect.set_recording_active(true)
 		$RecordButton.text = "Stop"
 		$RecordButton.text = "Stop"
-		$Status.text = "Recording..."
+		$Status.text = "Status: Recording..."
 
 
 
 
 func _on_PlayButton_pressed():
 func _on_PlayButton_pressed():
-	print(recording)
-	print(recording.format)
-	print(recording.mix_rate)
-	print(recording.stereo)
+	print("Recording: %s" % recording)
+	print("Format: %s" % recording.format)
+	print("Mix rate: %s" % recording.mix_rate)
+	print("Stereo: %s" % recording.stereo)
 	var data = recording.get_data()
 	var data = recording.get_data()
-	print(data.size())
+	print("Size: %s" % data.size())
 	$AudioStreamPlayer.stream = recording
 	$AudioStreamPlayer.stream = recording
 	$AudioStreamPlayer.play()
 	$AudioStreamPlayer.play()
 
 
@@ -47,4 +55,38 @@ func _on_Play_Music_pressed():
 func _on_SaveButton_pressed():
 func _on_SaveButton_pressed():
 	var save_path = $SaveButton/Filename.text
 	var save_path = $SaveButton/Filename.text
 	recording.save_to_wav(save_path)
 	recording.save_to_wav(save_path)
-	$Status.text = "Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
+	$Status.text = "Status: Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
+
+
+func _on_MixRateOptionButton_item_selected(index: int) -> void:
+	if index == 0:
+		mix_rate = 11025
+	elif index == 1:
+		mix_rate = 16000
+	elif index == 2:
+		mix_rate = 22050
+	elif index == 3:
+		mix_rate = 32000
+	elif index == 4:
+		mix_rate = 44100
+	elif index == 5:
+		mix_rate = 48000
+	if recording != null:
+		recording.set_mix_rate(mix_rate)
+
+
+func _on_FormatOptionButton_item_selected(index: int) -> void:
+	if index == 0:
+		format = AudioStreamSample.FORMAT_8_BITS
+	elif index == 1:
+		format = AudioStreamSample.FORMAT_16_BITS
+	elif index == 2:
+		format = AudioStreamSample.FORMAT_IMA_ADPCM
+	if recording != null:
+		recording.set_format(format)
+
+
+func _on_StereoCheckButton_toggled(button_pressed: bool) -> void:
+	stereo = button_pressed
+	if recording != null:
+		recording.set_stereo(stereo)

+ 152 - 38
audio/mic_record/MicRecord.tscn

@@ -10,14 +10,11 @@ anchor_left = 0.5
 anchor_top = 0.5
 anchor_top = 0.5
 anchor_right = 0.5
 anchor_right = 0.5
 anchor_bottom = 0.5
 anchor_bottom = 0.5
-offset_left = -320.0
-offset_top = -240.0
-offset_right = 320.0
-offset_bottom = 240.0
+offset_left = -278.0
+offset_top = -224.0
+offset_right = 296.0
+offset_bottom = 226.0
 script = ExtResource( "1" )
 script = ExtResource( "1" )
-__meta__ = {
-"_edit_use_anchors_": false
-}
 
 
 [node name="AudioStreamRecord" type="AudioStreamPlayer" parent="."]
 [node name="AudioStreamRecord" type="AudioStreamPlayer" parent="."]
 stream = SubResource( "1" )
 stream = SubResource( "1" )
@@ -31,51 +28,168 @@ autoplay = true
 stream = ExtResource( "2" )
 stream = ExtResource( "2" )
 volume_db = -6.0
 volume_db = -6.0
 
 
+[node name="Status" type="Label" parent="."]
+anchor_right = 1.0
+offset_bottom = 26.0
+text = "Status: "
+horizontal_alignment = 1
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
 [node name="RecordButton" type="Button" parent="."]
 [node name="RecordButton" type="Button" parent="."]
-offset_left = 120.0
-offset_top = 100.0
-offset_right = 240.0
-offset_bottom = 140.0
+minimum_size = Vector2(130, 40)
+offset_left = 29.0
+offset_top = 77.0
+offset_right = 159.0
+offset_bottom = 117.0
 text = "Record"
 text = "Record"
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
 
 
-[node name="SaveButton" type="Button" parent="."]
-offset_left = 120.0
-offset_top = 180.0
-offset_right = 240.0
+[node name="PlayButton" type="Button" parent="."]
+minimum_size = Vector2(130, 40)
+offset_left = 209.0
+offset_top = 77.0
+offset_right = 339.0
+offset_bottom = 117.0
+disabled = true
+text = "Play"
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="PlayMusic" type="Button" parent="."]
+minimum_size = Vector2(130, 40)
+offset_left = 30.0
+offset_top = 395.0
+offset_right = 160.0
+offset_bottom = 435.0
+text = "Play Music"
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="FormatLabel" type="Label" parent="."]
+offset_left = 33.0
+offset_top = 153.0
+offset_right = 102.0
+offset_bottom = 179.0
+text = "Format:"
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="FormatOptionButton" type="OptionButton" parent="."]
+offset_left = 131.0
+offset_top = 150.0
+offset_right = 315.0
+offset_bottom = 181.0
+item_count = 3
+selected = 1
+popup/item_0/text = "8 Bit audio codec"
+popup/item_0/id = 0
+popup/item_1/text = "16 Bit audio codec"
+popup/item_1/id = 1
+popup/item_2/text = "IMA ADPCM Compression"
+popup/item_2/id = 2
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="MixRateLabel" type="Label" parent="."]
+offset_left = 33.0
+offset_top = 192.0
+offset_right = 102.0
+offset_bottom = 218.0
+text = "Mix rate:"
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="MixRateOptionButton" type="OptionButton" parent="."]
+offset_left = 131.0
+offset_top = 189.0
+offset_right = 220.0
 offset_bottom = 220.0
 offset_bottom = 220.0
+item_count = 6
+selected = 4
+popup/item_0/text = "11025"
+popup/item_0/id = 0
+popup/item_1/text = "16000"
+popup/item_1/id = 1
+popup/item_2/text = "22050"
+popup/item_2/id = 2
+popup/item_3/text = "32000"
+popup/item_3/id = 3
+popup/item_4/text = "44100"
+popup/item_4/id = 4
+popup/item_5/text = "48000"
+popup/item_5/id = 5
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="StereoLabel" type="Label" parent="."]
+offset_left = 33.0
+offset_top = 233.0
+offset_right = 102.0
+offset_bottom = 259.0
+text = "Stereo:"
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="StereoCheckButton" type="CheckButton" parent="."]
+offset_left = 126.0
+offset_top = 233.0
+offset_right = 170.0
+offset_bottom = 264.0
+button_pressed = true
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
+
+[node name="SaveButton" type="Button" parent="."]
+minimum_size = Vector2(130, 40)
+offset_left = 29.0
+offset_top = 284.0
+offset_right = 159.0
+offset_bottom = 324.0
 disabled = true
 disabled = true
 text = "Save WAV To:"
 text = "Save WAV To:"
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
 
 
 [node name="Filename" type="LineEdit" parent="SaveButton"]
 [node name="Filename" type="LineEdit" parent="SaveButton"]
 offset_left = 180.0
 offset_left = 180.0
-offset_right = 340.0
+offset_right = 507.0
 offset_bottom = 40.0
 offset_bottom = 40.0
 text = "user://record.wav"
 text = "user://record.wav"
 caret_blink = true
 caret_blink = true
 caret_blink_speed = 0.5
 caret_blink_speed = 0.5
-
-[node name="PlayButton" type="Button" parent="."]
-offset_left = 300.0
-offset_top = 100.0
-offset_right = 420.0
-offset_bottom = 140.0
-disabled = true
-text = "Play"
-
-[node name="PlayMusic" type="Button" parent="."]
-offset_left = 120.0
-offset_top = 260.0
-offset_right = 240.0
-offset_bottom = 300.0
-text = "Play Music"
-
-[node name="Status" type="Label" parent="."]
-offset_left = 120.0
-offset_top = 340.0
-offset_right = 520.0
-offset_bottom = 340.0
+__meta__ = {
+"_edit_layout_mode": 0,
+"_edit_use_custom_anchors": false
+}
 
 
 [connection signal="pressed" from="RecordButton" to="." method="_on_RecordButton_pressed"]
 [connection signal="pressed" from="RecordButton" to="." method="_on_RecordButton_pressed"]
-[connection signal="pressed" from="SaveButton" to="." method="_on_SaveButton_pressed"]
 [connection signal="pressed" from="PlayButton" to="." method="_on_PlayButton_pressed"]
 [connection signal="pressed" from="PlayButton" to="." method="_on_PlayButton_pressed"]
 [connection signal="pressed" from="PlayMusic" to="." method="_on_Play_Music_pressed"]
 [connection signal="pressed" from="PlayMusic" to="." method="_on_Play_Music_pressed"]
+[connection signal="item_selected" from="FormatOptionButton" to="." method="_on_FormatOptionButton_item_selected"]
+[connection signal="item_selected" from="MixRateOptionButton" to="." method="_on_MixRateOptionButton_item_selected"]
+[connection signal="toggled" from="StereoCheckButton" to="." method="_on_StereoCheckButton_toggled"]
+[connection signal="pressed" from="SaveButton" to="." method="_on_SaveButton_pressed"]

+ 4 - 1
audio/mic_record/README.md

@@ -3,9 +3,12 @@
 This is an example showing how one can record audio from
 This is an example showing how one can record audio from
 the microphone and later play it back or save it to a file.
 the microphone and later play it back or save it to a file.
 
 
+With an addition on how to change the format, mix rate and
+stereo settings of the recording.
+
 Language: GDScript
 Language: GDScript
 
 
-Renderer: GLES 2
+Renderer: Vulkan Mobile
 
 
 Check out this demo on the asset library: https://godotengine.org/asset-library/asset/527
 Check out this demo on the asset library: https://godotengine.org/asset-library/asset/527
 
 

+ 4 - 6
audio/mic_record/project.godot

@@ -24,13 +24,11 @@ enable_audio_input=true
 
 
 [display]
 [display]
 
 
-window/stretch/mode="2d"
+window/size/viewport_width=640
+window/size/viewport_height=480
+window/stretch/mode="canvas_items"
 window/stretch/aspect="expand"
 window/stretch/aspect="expand"
-window/size/width=640
-window/size/height=480
 
 
 [rendering]
 [rendering]
 
 
-quality/driver/driver_name="GLES2"
-vram_compression/import_etc=true
-vram_compression/import_etc2=false
+vulkan/rendering/back_end=1

BIN
audio/mic_record/screenshots/mic_record.png