audio_effect_record.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*************************************************************************/
  2. /* audio_effect_record.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 AUDIOEFFECTRECORD_H
  31. #define AUDIOEFFECTRECORD_H
  32. #include "core/io/marshalls.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/os.h"
  35. #include "core/os/thread.h"
  36. #include "editor/import/resource_importer_wav.h"
  37. #include "scene/resources/audio_stream_sample.h"
  38. #include "servers/audio/audio_effect.h"
  39. #include "servers/audio_server.h"
  40. class AudioEffectRecord;
  41. class AudioEffectRecordInstance : public AudioEffectInstance {
  42. GDCLASS(AudioEffectRecordInstance, AudioEffectInstance);
  43. friend class AudioEffectRecord;
  44. Ref<AudioEffectRecord> base;
  45. bool is_recording;
  46. Thread *io_thread;
  47. bool thread_active;
  48. Vector<AudioFrame> ring_buffer;
  49. Vector<float> recording_data;
  50. unsigned int ring_buffer_pos;
  51. unsigned int ring_buffer_mask;
  52. unsigned int ring_buffer_read_pos;
  53. void _io_thread_process();
  54. void _io_store_buffer();
  55. static void _thread_callback(void *_instance);
  56. void _init_recording();
  57. void _update_buffer();
  58. static void _update(void *userdata);
  59. public:
  60. void init();
  61. void finish();
  62. virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count);
  63. virtual bool process_silence() const;
  64. AudioEffectRecordInstance() :
  65. thread_active(false) {}
  66. ~AudioEffectRecordInstance();
  67. };
  68. class AudioEffectRecord : public AudioEffect {
  69. GDCLASS(AudioEffectRecord, AudioEffect);
  70. friend class AudioEffectRecordInstance;
  71. enum {
  72. IO_BUFFER_SIZE_MS = 1500
  73. };
  74. bool recording_active;
  75. Ref<AudioEffectRecordInstance> current_instance;
  76. AudioStreamSample::Format format;
  77. void ensure_thread_stopped();
  78. protected:
  79. static void _bind_methods();
  80. static void debug(uint64_t time_diff, int p_frame_count);
  81. public:
  82. Ref<AudioEffectInstance> instance();
  83. void set_recording_active(bool p_record);
  84. bool is_recording_active() const;
  85. void set_format(AudioStreamSample::Format p_format);
  86. AudioStreamSample::Format get_format() const;
  87. Ref<AudioStreamSample> get_recording() const;
  88. AudioEffectRecord();
  89. };
  90. #endif // AUDIOEFFECTRECORD_H