voice_rb_sw.h 4.0 KB

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