Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding settings for mix_rate, stereo and format for Mic Record Demo 4.0-dev #711

Merged
merged 1 commit into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions audio/mic_record/MicRecord.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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():
var idx = AudioServer.get_bus_index("Record")
Expand All @@ -14,23 +19,26 @@ func _on_RecordButton_pressed():
$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 = "Recording..."
$Status.text = "Status: Recording..."


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()
print(data.size())
print("Size: %s" % data.size())
$AudioStreamPlayer.stream = recording
$AudioStreamPlayer.play()

Expand All @@ -47,4 +55,38 @@ func _on_Play_Music_pressed():
func _on_SaveButton_pressed():
var save_path = $SaveButton/Filename.text
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)
190 changes: 152 additions & 38 deletions audio/mic_record/MicRecord.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ anchor_left = 0.5
anchor_top = 0.5
anchor_right = 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" )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="AudioStreamRecord" type="AudioStreamPlayer" parent="."]
stream = SubResource( "1" )
Expand All @@ -31,51 +28,168 @@ autoplay = true
stream = ExtResource( "2" )
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="."]
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"
__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
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
text = "Save WAV To:"
__meta__ = {
"_edit_layout_mode": 0,
"_edit_use_custom_anchors": false
}

[node name="Filename" type="LineEdit" parent="SaveButton"]
offset_left = 180.0
offset_right = 340.0
offset_right = 507.0
offset_bottom = 40.0
text = "user://record.wav"
caret_blink = true
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="SaveButton" to="." method="_on_SaveButton_pressed"]
[connection signal="pressed" from="PlayButton" to="." method="_on_PlayButton_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"]
5 changes: 4 additions & 1 deletion audio/mic_record/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
This is an example showing how one can record audio from
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

Renderer: GLES 2
Renderer: Vulkan Mobile

Check out this demo on the asset library: https://godotengine.org/asset-library/asset/527

Expand Down
10 changes: 4 additions & 6 deletions audio/mic_record/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ enable_audio_input=true

[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/size/width=640
window/size/height=480

[rendering]

quality/driver/driver_name="GLES2"
vram_compression/import_etc=true
vram_compression/import_etc2=false
vulkan/rendering/back_end=1
Binary file modified audio/mic_record/screenshots/mic_record.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.