recording_with_microphone.rst 5.3 KB

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