recording_with_microphone.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. The audio recording is handled by the :ref:`class_AudioEffectRecord` resource
  29. which has three methods:
  30. :ref:`get_recording() <class_AudioEffectRecord_method_get_recording>`,
  31. :ref:`is_recording_active() <class_AudioEffectRecord_method_is_recording_active>`,
  32. and :ref:`set_recording_active() <class_AudioEffectRecord_method_set_recording_active>`.
  33. .. tabs::
  34. .. code-tab:: gdscript GDScript
  35. func _on_RecordButton_pressed():
  36. if effect.is_recording_active():
  37. recording = effect.get_recording()
  38. $PlayButton.disabled = false
  39. $SaveButton.disabled = false
  40. effect.set_recording_active(false)
  41. $RecordButton.text = "Record"
  42. $Status.text = ""
  43. else:
  44. $PlayButton.disabled = true
  45. $SaveButton.disabled = true
  46. effect.set_recording_active(true)
  47. $RecordButton.text = "Stop"
  48. $Status.text = "Recording..."
  49. At the start of the demo, the recording effect is not active. When the user
  50. presses the ``RecordButton``, the effect is enabled with
  51. ``set_recording_active(true)``.
  52. On the next button press, as ``effect.is_recording_active()`` is ``true``,
  53. the recorded stream can be stored into the ``recording`` variable by calling
  54. ``effect.get_recording()``.
  55. .. tabs::
  56. .. code-tab:: gdscript GDScript
  57. func _on_PlayButton_pressed():
  58. print(recording)
  59. print(recording.format)
  60. print(recording.mix_rate)
  61. print(recording.stereo)
  62. var data = recording.get_data()
  63. print(data)
  64. print(data.size())
  65. $AudioStreamPlayer.stream = recording
  66. $AudioStreamPlayer.play()
  67. To playback the recording, you assign the recording as the stream of the
  68. ``AudioStreamPlayer`` and call ``play()``.
  69. .. tabs::
  70. .. code-tab:: gdscript GDScript
  71. func _on_SaveButton_pressed():
  72. var save_path = $SaveButton/Filename.text
  73. recording.save_to_wav(save_path)
  74. $Status.text = "Saved WAV file to: %s\n(%s)" % [save_path, ProjectSettings.globalize_path(save_path)]
  75. To save the recording, you call ``save_to_wav()`` with the path to a file.
  76. In this demo, the path is defined by the user via a ``LineEdit`` input box.