audio_driver_dummy.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*************************************************************************/
  2. /* audio_driver_dummy.cpp */
  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. #include "audio_driver_dummy.h"
  30. #include "globals.h"
  31. #include "os/os.h"
  32. Error AudioDriverDummy::init() {
  33. active=false;
  34. thread_exited=false;
  35. exit_thread=false;
  36. pcm_open = false;
  37. samples_in = NULL;
  38. mix_rate = 44100;
  39. output_format = OUTPUT_STEREO;
  40. channels = 2;
  41. int latency = GLOBAL_DEF("audio/output_latency",25);
  42. buffer_size = nearest_power_of_2( latency * mix_rate / 1000 );
  43. samples_in = memnew_arr(int32_t, buffer_size*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_size / float(ad->mix_rate))*1000000;
  51. while (!ad->exit_thread) {
  52. if (!ad->active) {
  53. } else {
  54. ad->lock();
  55. ad->audio_server_process(ad->buffer_size, ad->samples_in);
  56. ad->unlock();
  57. };
  58. OS::get_singleton()->delay_usec(usdelay);
  59. };
  60. ad->thread_exited=true;
  61. };
  62. void AudioDriverDummy::start() {
  63. active = true;
  64. };
  65. int AudioDriverDummy::get_mix_rate() const {
  66. return mix_rate;
  67. };
  68. AudioDriverSW::OutputFormat AudioDriverDummy::get_output_format() const {
  69. return output_format;
  70. };
  71. void AudioDriverDummy::lock() {
  72. if (!thread || !mutex)
  73. return;
  74. mutex->lock();
  75. };
  76. void AudioDriverDummy::unlock() {
  77. if (!thread || !mutex)
  78. return;
  79. mutex->unlock();
  80. };
  81. void AudioDriverDummy::finish() {
  82. if (!thread)
  83. return;
  84. exit_thread = true;
  85. Thread::wait_to_finish(thread);
  86. if (samples_in) {
  87. memdelete_arr(samples_in);
  88. };
  89. memdelete(thread);
  90. if (mutex)
  91. memdelete(mutex);
  92. thread = NULL;
  93. };
  94. AudioDriverDummy::AudioDriverDummy() {
  95. mutex = NULL;
  96. thread=NULL;
  97. };
  98. AudioDriverDummy::~AudioDriverDummy() {
  99. };