Godot:RecordingWithMicrophone
lastmod 2023-05-25created 2023-05-253 edits
How to record from a microphone in Godot
How to use
Set up the default bus layout
Record_bus.png
Add a BUS named "Record" and check the "Record" checkbox.
Project settings
Record_project_setting.png
You need to add the default bus layout and check the "Enable Audio Input" checkbox.
Add an AudioStreamPlayer
Record_stream_player.png
Add an AudioStreamPlayer for recording to the scene and connect a microphone stream.
Record_stream_microphone.png
Then select the BUS named "Record" that you added earlier.
AudioEffectRecord
Get the bus index from the AudioServer, then get the AudioEffectRecord.
private AudioEffectRecord _effect;
private AudioStreamSample _recording;
public override void _Ready()
{
// We get the index of the "Record" bus.
int idx = AudioServer.GetBusIndex("Record");
// And use it to retrieve its first effect, which has been defined
// as an "AudioEffectRecord" resource.
_effect = (AudioEffectRecord)AudioServer.GetBusEffect(idx, 0);
}
Record button click event
Implementing record start/stop:
private void OnRecordButtonPressed()
{
if (_effect.IsRecordingActive())
{
_recording = _effect.GetRecording();
GetNode<Button>("PlayButton").Disabled = false;
GetNode<Button>("SaveButton").Disabled = false;
_effect.SetRecordingActive(false);
GetNode<Button>("RecordButton").Text = "Record";
GetNode<Label>("Status").Text = "";
}
else
{
GetNode<Button>("PlayButton").Disabled = true;
GetNode<Button>("SaveButton").Disabled = true;
_effect.SetRecordingActive(true);
GetNode<Button>("RecordButton").Text = "Stop";
GetNode<Label>("Status").Text = "Recording...";
}
}
Music playback click event
private void OnPlayButtonPressed()
{
GD.Print(_recording);
GD.Print(_recording.Format);
GD.Print(_recording.MixRate);
GD.Print(_recording.Stereo);
byte[] data = _recording.Data;
GD.Print(data.Length);
var audioStreamPlayer = GetNode<AudioStreamPlayer>("AudioStreamPlayer");
audioStreamPlayer.Stream = _recording;
audioStreamPlayer.Play();
}
Save a WAV file
private void OnSaveButtonPressed()
{
string savePath = GetNode<LineEdit>("SaveButton/Filename").Text;
_recording.SaveToWav(savePath);
GetNode<Label>("Status").Text = string.Format("Saved WAV file to: {0}\n({1})", savePath, ProjectSettings.GlobalizePath(savePath));
}
Android Permission
On Android, the following permission is required.
See also
Favorite site
- Recording with microphone — Godot Engine (stable) documentation in English
- godot-demo-projects/audio/mic_record at 3.2 · godotengine/godot-demo-projects · GitHub
Pages that link here