sample_player.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*************************************************************************/
  2. /* sample_player.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SAMPLE_PLAYER_H
  30. #define SAMPLE_PLAYER_H
  31. #include "scene/main/node.h"
  32. #include "scene/resources/sample_library.h"
  33. class SamplePlayer : public Node {
  34. OBJ_TYPE( SamplePlayer, Node );
  35. OBJ_CATEGORY("Audio Nodes");
  36. public:
  37. enum FilterType {
  38. FILTER_NONE,
  39. FILTER_LOWPASS,
  40. FILTER_BANDPASS,
  41. FILTER_HIPASS,
  42. FILTER_NOTCH,
  43. FILTER_PEAK,
  44. FILTER_BANDLIMIT, ///< cutoff is LP resonace is HP
  45. FILTER_LOW_SHELF,
  46. FILTER_HIGH_SHELF,
  47. };
  48. enum ReverbRoomType {
  49. REVERB_SMALL,
  50. REVERB_MEDIUM,
  51. REVERB_LARGE,
  52. REVERB_HALL
  53. };
  54. enum {
  55. INVALID_VOICE_ID=0xFFFFFFFF
  56. };
  57. typedef uint32_t VoiceID;
  58. private:
  59. Ref<SampleLibrary> library;
  60. struct Voice {
  61. RID voice;
  62. uint32_t check;
  63. bool active;
  64. int sample_mix_rate;
  65. int mix_rate;
  66. float volume;
  67. float pan;
  68. float pan_depth;
  69. float pan_height;
  70. FilterType filter_type;
  71. float filter_cutoff;
  72. float filter_resonance;
  73. float filter_gain;
  74. float chorus_send;
  75. ReverbRoomType reverb_room;
  76. float reverb_send;
  77. void clear();
  78. Voice();
  79. ~Voice();
  80. };
  81. Vector<Voice> voices;
  82. struct Default {
  83. float reverb_send;
  84. float pitch_scale;
  85. float volume_db;
  86. float pan;
  87. float depth;
  88. float height;
  89. FilterType filter_type;
  90. float filter_cutoff;
  91. float filter_resonance;
  92. float filter_gain;
  93. float chorus_send;
  94. ReverbRoomType reverb_room;
  95. } _default;
  96. uint32_t last_id;
  97. uint16_t last_check;
  98. String played_back;
  99. protected:
  100. bool _set(const StringName& p_name, const Variant& p_value);
  101. bool _get(const StringName& p_name,Variant &r_ret) const;
  102. void _get_property_list(List<PropertyInfo> *p_list) const;
  103. static void _bind_methods();
  104. public:
  105. void set_sample_library(const Ref<SampleLibrary>& p_library);
  106. Ref<SampleLibrary> get_sample_library() const;
  107. void set_polyphony(int p_voice_count);
  108. int get_polyphony() const;
  109. VoiceID play(const String& p_name,bool unique=false);
  110. void stop(VoiceID p_voice);
  111. void stop_all();
  112. bool is_voice_active(VoiceID) const;
  113. bool is_active() const;
  114. void set_mix_rate(VoiceID p_voice, int p_mix_rate);
  115. void set_pitch_scale(VoiceID p_voice, float p_pitch_scale);
  116. void set_volume(VoiceID p_voice, float p_volume);
  117. void set_volume_db(VoiceID p_voice, float p_db);
  118. void set_pan(VoiceID p_voice, float p_pan,float p_pan_depth=0,float p_pan_height=0);
  119. void set_filter(VoiceID p_voice,FilterType p_filter,float p_cutoff,float p_resonance,float p_gain);
  120. void set_chorus(VoiceID p_voice,float p_send);
  121. void set_reverb(VoiceID p_voice,ReverbRoomType p_room,float p_send);
  122. int get_mix_rate(VoiceID p_voice) const;
  123. float get_pitch_scale(VoiceID p_voice) const;
  124. float get_volume(VoiceID p_voice) const;
  125. float get_volume_db(VoiceID p_voice) const;
  126. float get_pan(VoiceID p_voice) const;
  127. float get_pan_depth(VoiceID p_voice) const;
  128. float get_pan_height(VoiceID p_voice) const;
  129. FilterType get_filter_type(VoiceID p_voice) const;
  130. float get_filter_cutoff(VoiceID p_voice) const;
  131. float get_filter_resonance(VoiceID p_voice) const;
  132. float get_filter_gain(VoiceID p_voice) const;
  133. float get_chorus(VoiceID p_voice) const;
  134. ReverbRoomType get_reverb_room(VoiceID p_voice) const;
  135. float get_reverb(VoiceID p_voice) const;
  136. void set_default_pitch_scale(float p_pitch_scale);
  137. void set_default_volume(float p_volume);
  138. void set_default_volume_db(float p_db);
  139. void set_default_pan(float p_pan,float p_pan_depth=0,float p_pan_height=0);
  140. void set_default_filter(FilterType p_filter,float p_cutoff,float p_resonance,float p_gain);
  141. void set_default_chorus(float p_send);
  142. void set_default_reverb(ReverbRoomType p_room,float p_send);
  143. float get_default_volume() const;
  144. float get_default_volume_db() const;
  145. float get_default_pitch_scale() const;
  146. float get_default_pan() const;
  147. float get_default_pan_depth() const;
  148. float get_default_pan_height() const;
  149. FilterType get_default_filter_type() const;
  150. float get_default_filter_cutoff() const;
  151. float get_default_filter_resonance() const;
  152. float get_default_filter_gain() const;
  153. float get_default_chorus() const;
  154. ReverbRoomType get_default_reverb_room() const;
  155. float get_default_reverb() const;
  156. SamplePlayer();
  157. ~SamplePlayer();
  158. };
  159. VARIANT_ENUM_CAST( SamplePlayer::FilterType );
  160. VARIANT_ENUM_CAST( SamplePlayer::ReverbRoomType );
  161. #endif // SAMPLE_PLAYER_H