voice_rb_sw.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*************************************************************************/
  2. /* voice_rb_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 VOICE_RB_SW_H
  30. #define VOICE_RB_SW_H
  31. #include "servers/audio_server.h"
  32. #include "os/os.h"
  33. class VoiceRBSW {
  34. public:
  35. enum {
  36. VOICE_RB_SIZE=1024
  37. };
  38. struct Command {
  39. enum Type {
  40. CMD_NONE,
  41. CMD_PLAY,
  42. CMD_STOP,
  43. CMD_SET_VOLUME,
  44. CMD_SET_PAN,
  45. CMD_SET_FILTER,
  46. CMD_SET_CHORUS,
  47. CMD_SET_REVERB,
  48. CMD_SET_MIX_RATE,
  49. CMD_SET_POSITIONAL,
  50. CMD_CHANGE_ALL_FX_VOLUMES
  51. };
  52. Type type;
  53. RID voice;
  54. struct {
  55. RID sample;
  56. } play;
  57. union {
  58. struct {
  59. float volume;
  60. } volume;
  61. struct {
  62. float pan,depth,height;
  63. } pan;
  64. struct {
  65. AS::FilterType type;
  66. float cutoff;
  67. float resonance;
  68. float gain;
  69. } filter;
  70. struct {
  71. float send;
  72. } chorus;
  73. struct {
  74. float send;
  75. AS::ReverbRoomType room;
  76. } reverb;
  77. struct {
  78. int mix_rate;
  79. } mix_rate;
  80. struct {
  81. bool positional;
  82. } positional;
  83. };
  84. Command() { type=CMD_NONE; }
  85. };
  86. private:
  87. Command voice_cmd_rb[VOICE_RB_SIZE];
  88. volatile int read_pos;
  89. volatile int write_pos;
  90. public:
  91. _FORCE_INLINE_ bool commands_left() const { return read_pos!=write_pos; }
  92. _FORCE_INLINE_ Command pop_command() {
  93. ERR_FAIL_COND_V( read_pos==write_pos, Command() );
  94. Command cmd=voice_cmd_rb[read_pos];
  95. read_pos=(read_pos+1)%VOICE_RB_SIZE;
  96. return cmd;
  97. }
  98. _FORCE_INLINE_ void push_command(const Command& p_command) {
  99. bool full = ((write_pos+1)%VOICE_RB_SIZE)==read_pos;
  100. if (full) {
  101. #ifdef DEBUG_ENABLED
  102. if (OS::get_singleton()->is_stdout_verbose()) {
  103. ERR_EXPLAIN("Audio Ring Buffer Full (too many commands");
  104. ERR_FAIL_COND( ((write_pos+1)%VOICE_RB_SIZE)==read_pos);
  105. }
  106. #endif
  107. return;
  108. }
  109. voice_cmd_rb[write_pos]=p_command;
  110. write_pos=(write_pos+1)%VOICE_RB_SIZE;
  111. }
  112. VoiceRBSW() { read_pos=write_pos=0; }
  113. };
  114. #endif // VOICE_RB_SW_H