audio_effect_record.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**************************************************************************/
  2. /* audio_effect_record.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef AUDIO_EFFECT_RECORD_H
  31. #define AUDIO_EFFECT_RECORD_H
  32. #include "core/os/thread.h"
  33. #include "scene/resources/audio_stream_wav.h"
  34. #include "servers/audio/audio_effect.h"
  35. #include "servers/audio_server.h"
  36. class AudioEffectRecord;
  37. class AudioEffectRecordInstance : public AudioEffectInstance {
  38. GDCLASS(AudioEffectRecordInstance, AudioEffectInstance);
  39. friend class AudioEffectRecord;
  40. bool is_recording;
  41. Thread io_thread;
  42. Vector<AudioFrame> ring_buffer;
  43. Vector<float> recording_data;
  44. unsigned int ring_buffer_pos;
  45. unsigned int ring_buffer_mask;
  46. unsigned int ring_buffer_read_pos;
  47. void _io_thread_process();
  48. void _io_store_buffer();
  49. static void _thread_callback(void *_instance);
  50. void _init_recording();
  51. void _update_buffer();
  52. static void _update(void *userdata);
  53. public:
  54. void init();
  55. void finish();
  56. virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override;
  57. virtual bool process_silence() const override;
  58. };
  59. class AudioEffectRecord : public AudioEffect {
  60. GDCLASS(AudioEffectRecord, AudioEffect);
  61. friend class AudioEffectRecordInstance;
  62. enum {
  63. IO_BUFFER_SIZE_MS = 1500
  64. };
  65. Ref<AudioEffectRecordInstance> current_instance;
  66. AudioStreamWAV::Format format;
  67. void ensure_thread_stopped();
  68. protected:
  69. static void _bind_methods();
  70. public:
  71. Ref<AudioEffectInstance> instantiate() override;
  72. void set_recording_active(bool p_record);
  73. bool is_recording_active() const;
  74. void set_format(AudioStreamWAV::Format p_format);
  75. AudioStreamWAV::Format get_format() const;
  76. Ref<AudioStreamWAV> get_recording() const;
  77. AudioEffectRecord();
  78. ~AudioEffectRecord();
  79. };
  80. #endif // AUDIO_EFFECT_RECORD_H