audio_driver_dummy.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**************************************************************************/
  2. /* audio_driver_dummy.cpp */
  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. #include "audio_driver_dummy.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. AudioDriverDummy *AudioDriverDummy::singleton = nullptr;
  34. Error AudioDriverDummy::init() {
  35. active.clear();
  36. exit_thread.clear();
  37. samples_in = nullptr;
  38. if (mix_rate == -1) {
  39. mix_rate = _get_configured_mix_rate();
  40. }
  41. channels = get_channels();
  42. samples_in = memnew_arr(int32_t, (size_t)buffer_frames * channels);
  43. if (use_threads) {
  44. thread.start(AudioDriverDummy::thread_func, this);
  45. }
  46. return OK;
  47. }
  48. void AudioDriverDummy::thread_func(void *p_udata) {
  49. AudioDriverDummy *ad = static_cast<AudioDriverDummy *>(p_udata);
  50. uint64_t usdelay = (ad->buffer_frames / float(ad->mix_rate)) * 1000000;
  51. while (!ad->exit_thread.is_set()) {
  52. if (ad->active.is_set()) {
  53. ad->lock();
  54. ad->start_counting_ticks();
  55. ad->audio_server_process(ad->buffer_frames, ad->samples_in);
  56. ad->stop_counting_ticks();
  57. ad->unlock();
  58. }
  59. OS::get_singleton()->delay_usec(usdelay);
  60. }
  61. }
  62. void AudioDriverDummy::start() {
  63. active.set();
  64. }
  65. int AudioDriverDummy::get_mix_rate() const {
  66. return mix_rate;
  67. }
  68. AudioDriver::SpeakerMode AudioDriverDummy::get_speaker_mode() const {
  69. return speaker_mode;
  70. }
  71. void AudioDriverDummy::lock() {
  72. mutex.lock();
  73. }
  74. void AudioDriverDummy::unlock() {
  75. mutex.unlock();
  76. }
  77. void AudioDriverDummy::set_use_threads(bool p_use_threads) {
  78. use_threads = p_use_threads;
  79. }
  80. void AudioDriverDummy::set_speaker_mode(SpeakerMode p_mode) {
  81. speaker_mode = p_mode;
  82. }
  83. void AudioDriverDummy::set_mix_rate(int p_rate) {
  84. mix_rate = p_rate;
  85. }
  86. uint32_t AudioDriverDummy::get_channels() const {
  87. static const int channels_for_mode[4] = { 2, 4, 8, 16 };
  88. return channels_for_mode[speaker_mode];
  89. }
  90. void AudioDriverDummy::mix_audio(int p_frames, int32_t *p_buffer) {
  91. ERR_FAIL_COND(!active.is_set()); // If not active, should not mix.
  92. ERR_FAIL_COND(use_threads == true); // If using threads, this will not work well.
  93. uint32_t todo = p_frames;
  94. while (todo) {
  95. uint32_t to_mix = MIN(buffer_frames, todo);
  96. lock();
  97. audio_server_process(to_mix, samples_in);
  98. unlock();
  99. uint32_t total_samples = to_mix * channels;
  100. for (uint32_t i = 0; i < total_samples; i++) {
  101. p_buffer[i] = samples_in[i];
  102. }
  103. todo -= to_mix;
  104. p_buffer += total_samples;
  105. }
  106. }
  107. void AudioDriverDummy::finish() {
  108. if (use_threads) {
  109. exit_thread.set();
  110. thread.wait_to_finish();
  111. }
  112. if (samples_in) {
  113. memdelete_arr(samples_in);
  114. }
  115. }
  116. AudioDriverDummy::AudioDriverDummy() {
  117. singleton = this;
  118. }