Work in progress

The content of this page was not yet updated for Godot 4.2 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

Recording with microphone

Godot supports in-game audio recording for Windows, macOS, Linux, Android and iOS.

A simple demo is included in the official demo projects and will be used as support for this tutorial: https://github.com/godotengine/godot-demo-projects/tree/master/audio/mic_record.

You will need to enable audio input in the project settings, or you’ll just get empty audio files.

The structure of the demo

The demo consists of a single scene. This scene includes two major parts: the GUI and the audio.

We will focus on the audio part. In this demo, a bus named Record with the effect Record is created to handle the audio recording. An AudioStreamPlayer named AudioStreamRecord is used for recording.

../../_images/record_bus.png ../../_images/record_stream_player.png

GDScriptC#

  1. var effect
  2. var recording
  3. func _ready():
  4. # We get the index of the "Record" bus.
  5. var idx = AudioServer.get_bus_index("Record")
  6. # And use it to retrieve its first effect, which has been defined
  7. # as an "AudioEffectRecord" resource.
  8. effect = AudioServer.get_bus_effect(idx, 0)
  1. private AudioEffectRecord _effect;
  2. private AudioStreamSample _recording;
  3. public override void _Ready()
  4. {
  5. // We get the index of the "Record" bus.
  6. int idx = AudioServer.GetBusIndex("Record");
  7. // And use it to retrieve its first effect, which has been defined
  8. // as an "AudioEffectRecord" resource.
  9. _effect = (AudioEffectRecord)AudioServer.GetBusEffect(idx, 0);
  10. }

The audio recording is handled by the AudioEffectRecord resource which has three methods: get_recording(), is_recording_active(), and set_recording_active().

GDScriptC#

  1. func _on_record_button_pressed():
  2. if effect.is_recording_active():
  3. recording = effect.get_recording()
  4. $PlayButton.disabled = false
  5. $SaveButton.disabled = false
  6. effect.set_recording_active(false)
  7. $RecordButton.text = "Record"
  8. $Status.text = ""
  9. else:
  10. $PlayButton.disabled = true
  11. $SaveButton.disabled = true
  12. effect.set_recording_active(true)
  13. $RecordButton.text = "Stop"
  14. $Status.text = "Recording..."
  1. private void OnRecordButtonPressed()
  2. {
  3. if (_effect.IsRecordingActive())
  4. {
  5. _recording = _effect.GetRecording();
  6. GetNode<Button>("PlayButton").Disabled = false;
  7. GetNode<Button>("SaveButton").Disabled = false;
  8. _effect.SetRecordingActive(false);
  9. GetNode<Button>("RecordButton").Text = "Record";
  10. GetNode<Label>("Status").Text = "";
  11. }
  12. else
  13. {
  14. GetNode<Button>("PlayButton").Disabled = true;
  15. GetNode<Button>("SaveButton").Disabled = true;
  16. _effect.SetRecordingActive(true);
  17. GetNode<Button>("RecordButton").Text = "Stop";
  18. GetNode<Label>("Status").Text = "Recording...";
  19. }
  20. }

At the start of the demo, the recording effect is not active. When the user presses the RecordButton, the effect is enabled with set_recording_active(true).

On the next button press, as effect.is_recording_active() is true, the recorded stream can be stored into the recording variable by calling effect.get_recording().

GDScriptC#

  1. func _on_play_button_pressed():
  2. print(recording)
  3. print(recording.format)
  4. print(recording.mix_rate)
  5. print(recording.stereo)
  6. var data = recording.get_data()
  7. print(data.size())
  8. $AudioStreamPlayer.stream = recording
  9. $AudioStreamPlayer.play()
  1. private void OnPlayButtonPressed()
  2. {
  3. GD.Print(_recording);
  4. GD.Print(_recording.Format);
  5. GD.Print(_recording.MixRate);
  6. GD.Print(_recording.Stereo);
  7. byte[] data = _recording.Data;
  8. GD.Print(data.Length);
  9. var audioStreamPlayer = GetNode<AudioStreamPlayer>("AudioStreamPlayer");
  10. audioStreamPlayer.Stream = _recording;
  11. audioStreamPlayer.Play();
  12. }

To playback the recording, you assign the recording as the stream of the AudioStreamPlayer and call play().

GDScriptC#

  1. func _on_save_button_pressed():
  2. var save_path = $SaveButton/Filename.text
  3. recording.save_to_wav(save_path)
  4. $Status.text = "Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
  1. private void OnSaveButtonPressed()
  2. {
  3. string savePath = GetNode<LineEdit>("SaveButton/Filename").Text;
  4. _recording.SaveToWav(savePath);
  5. GetNode<Label>("Status").Text = string.Format("Saved WAV file to: {0}\n({1})", savePath, ProjectSettings.GlobalizePath(savePath));
  6. }

To save the recording, you call save_to_wav() with the path to a file. In this demo, the path is defined by the user via a LineEdit input box.

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.