recording_with_microphone.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. .. _doc_recording_with_microphone:
  2. Recording with microphone
  3. =========================
  4. Godot supports in-game audio recording for Windows, macOS, Linux, Android and
  5. iOS.
  6. A simple demo is included in the official demo projects and will be used as
  7. support for this tutorial:
  8. `<https://github.com/godotengine/godot-demo-projects/tree/master/audio/mic_record>`_.
  9. You will need to enable audio input in the project settings, or you'll just get empty audio files.
  10. The structure of the demo
  11. -------------------------
  12. The demo consists of a single scene. This scene includes two major parts: the
  13. GUI and the audio.
  14. We will focus on the audio part. In this demo, a bus named ``Record`` with the
  15. effect ``Record`` is created to handle the audio recording.
  16. An ``AudioStreamPlayer`` named ``AudioStreamRecord`` is used for recording.
  17. .. image:: img/record_bus.png
  18. .. image:: img/record_stream_player.png
  19. .. tabs::
  20. .. code-tab:: gdscript GDScript
  21. var effect
  22. var recording
  23. func _ready():
  24. # We get the index of the "Record" bus.
  25. var idx = AudioServer.get_bus_index("Record")
  26. # And use it to retrieve its first effect, which has been defined
  27. # as an "AudioEffectRecord" resource.
  28. effect = AudioServer.get_bus_effect(idx, 0)
  29. .. code-tab:: csharp
  30. private AudioEffectRecord _effect;
  31. private AudioStreamSample _recording;
  32. public override void _Ready()
  33. {
  34. // We get the index of the "Record" bus.
  35. int idx = AudioServer.GetBusIndex("Record");
  36. // And use it to retrieve its first effect, which has been defined
  37. // as an "AudioEffectRecord" resource.
  38. _effect = (AudioEffectRecord)AudioServer.GetBusEffect(idx, 0);
  39. }
  40. The audio recording is handled by the :ref:`class_AudioEffectRecord` resource
  41. which has three methods:
  42. :ref:`get_recording() <class_AudioEffectRecord_method_get_recording>`,
  43. :ref:`is_recording_active() <class_AudioEffectRecord_method_is_recording_active>`,
  44. and :ref:`set_recording_active() <class_AudioEffectRecord_method_set_recording_active>`.
  45. .. tabs::
  46. .. code-tab:: gdscript GDScript
  47. func _on_RecordButton_pressed():
  48. if effect.is_recording_active():
  49. recording = effect.get_recording()
  50. $PlayButton.disabled = false
  51. $SaveButton.disabled = false
  52. effect.set_recording_active(false)
  53. $RecordButton.text = "Record"
  54. $Status.text = ""
  55. else:
  56. $PlayButton.disabled = true
  57. $SaveButton.disabled = true
  58. effect.set_recording_active(true)
  59. $RecordButton.text = "Stop"
  60. $Status.text = "Recording..."
  61. .. code-tab:: csharp
  62. public void OnRecordButtonPressed()
  63. {
  64. if (_effect.IsRecordingActive())
  65. {
  66. _recording = _effect.GetRecording();
  67. GetNode<Button>("PlayButton").Disabled = false;
  68. GetNode<Button>("SaveButton").Disabled = false;
  69. _effect.SetRecordingActive(false);
  70. GetNode<Button>("RecordButton").Text = "Record";
  71. GetNode<Label>("Status").Text = "";
  72. }
  73. else
  74. {
  75. GetNode<Button>("PlayButton").Disabled = true;
  76. GetNode<Button>("SaveButton").Disabled = true;
  77. _effect.SetRecordingActive(true);
  78. GetNode<Button>("RecordButton").Text = "Stop";
  79. GetNode<Label>("Status").Text = "Recording...";
  80. }
  81. }
  82. At the start of the demo, the recording effect is not active. When the user
  83. presses the ``RecordButton``, the effect is enabled with
  84. ``set_recording_active(true)``.
  85. On the next button press, as ``effect.is_recording_active()`` is ``true``,
  86. the recorded stream can be stored into the ``recording`` variable by calling
  87. ``effect.get_recording()``.
  88. .. tabs::
  89. .. code-tab:: gdscript GDScript
  90. func _on_PlayButton_pressed():
  91. print(recording)
  92. print(recording.format)
  93. print(recording.mix_rate)
  94. print(recording.stereo)
  95. var data = recording.get_data()
  96. print(data)
  97. print(data.size())
  98. $AudioStreamPlayer.stream = recording
  99. $AudioStreamPlayer.play()
  100. .. code-tab:: csharp
  101. public void OnPlayButtonPressed()
  102. {
  103. GD.Print(_recording);
  104. GD.Print(_recording.Format);
  105. GD.Print(_recording.MixRate);
  106. GD.Print(_recording.Stereo);
  107. byte[] data = _recording.Data;
  108. GD.Print(data);
  109. GD.Print(data.Length);
  110. var audioStreamPlayer = GetNode<AudioStreamPlayer>("AudioStreamPlayer");
  111. audioStreamPlayer.Stream = _recording;
  112. audioStreamPlayer.Play();
  113. }
  114. To playback the recording, you assign the recording as the stream of the
  115. ``AudioStreamPlayer`` and call ``play()``.
  116. .. tabs::
  117. .. code-tab:: gdscript GDScript
  118. func _on_SaveButton_pressed():
  119. var save_path = $SaveButton/Filename.text
  120. recording.save_to_wav(save_path)
  121. $Status.text = "Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
  122. .. code-tab:: csharp
  123. public void OnSavebuttonPressed()
  124. {
  125. string savePath = GetNode<LineEdit>("SaveButton/Filename").Text;
  126. _recording.SaveToWav(savePath);
  127. GetNode<Label>("Status").Text = string.Format("Saved WAV file to: {0}\n({1})", savePath, ProjectSettings.GlobalizePath(savePath));
  128. }
  129. To save the recording, you call ``save_to_wav()`` with the path to a file.
  130. In this demo, the path is defined by the user via a ``LineEdit`` input box.