audio_driver_dummy.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************************************/
  2. /* audio_driver_dummy.cpp */
  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. #include "audio_driver_dummy.h"
  31. #include "os/os.h"
  32. #include "project_settings.h"
  33. Error AudioDriverDummy::init() {
  34. active = false;
  35. thread_exited = false;
  36. exit_thread = false;
  37. samples_in = NULL;
  38. mix_rate = DEFAULT_MIX_RATE;
  39. speaker_mode = SPEAKER_MODE_STEREO;
  40. channels = 2;
  41. int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
  42. buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
  43. samples_in = memnew_arr(int32_t, buffer_frames * channels);
  44. mutex = Mutex::create();
  45. thread = Thread::create(AudioDriverDummy::thread_func, this);
  46. return OK;
  47. };
  48. void AudioDriverDummy::thread_func(void *p_udata) {
  49. AudioDriverDummy *ad = (AudioDriverDummy *)p_udata;
  50. uint64_t usdelay = (ad->buffer_frames / float(ad->mix_rate)) * 1000000;
  51. while (!ad->exit_thread) {
  52. if (ad->active) {
  53. ad->lock();
  54. ad->audio_server_process(ad->buffer_frames, ad->samples_in);
  55. ad->unlock();
  56. };
  57. OS::get_singleton()->delay_usec(usdelay);
  58. };
  59. ad->thread_exited = true;
  60. };
  61. void AudioDriverDummy::start() {
  62. active = true;
  63. };
  64. int AudioDriverDummy::get_mix_rate() const {
  65. return mix_rate;
  66. };
  67. AudioDriver::SpeakerMode AudioDriverDummy::get_speaker_mode() const {
  68. return speaker_mode;
  69. };
  70. void AudioDriverDummy::lock() {
  71. if (!thread || !mutex)
  72. return;
  73. mutex->lock();
  74. };
  75. void AudioDriverDummy::unlock() {
  76. if (!thread || !mutex)
  77. return;
  78. mutex->unlock();
  79. };
  80. void AudioDriverDummy::finish() {
  81. if (!thread)
  82. return;
  83. exit_thread = true;
  84. Thread::wait_to_finish(thread);
  85. if (samples_in) {
  86. memdelete_arr(samples_in);
  87. };
  88. memdelete(thread);
  89. if (mutex)
  90. memdelete(mutex);
  91. thread = NULL;
  92. };
  93. AudioDriverDummy::AudioDriverDummy() {
  94. mutex = NULL;
  95. thread = NULL;
  96. };
  97. AudioDriverDummy::~AudioDriverDummy(){
  98. };