audio_driver_media_kit.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*************************************************************************/
  2. /* audio_driver_media_kit.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_media_kit.h"
  31. #ifdef MEDIA_KIT_ENABLED
  32. #include "core/project_settings.h"
  33. int32_t *AudioDriverMediaKit::samples_in = NULL;
  34. Error AudioDriverMediaKit::init() {
  35. active = false;
  36. mix_rate = 44100;
  37. speaker_mode = SPEAKER_MODE_STEREO;
  38. channels = 2;
  39. int latency = GLOBAL_DEF_RST("audio/output_latency", 25);
  40. buffer_size = next_power_of_2(latency * mix_rate / 1000);
  41. samples_in = memnew_arr(int32_t, buffer_size * channels);
  42. media_raw_audio_format format;
  43. format = media_raw_audio_format::wildcard;
  44. format.frame_rate = mix_rate;
  45. format.channel_count = channels;
  46. format.format = media_raw_audio_format::B_AUDIO_INT;
  47. format.byte_order = B_MEDIA_LITTLE_ENDIAN;
  48. format.buffer_size = buffer_size * sizeof(int32_t) * channels;
  49. player = new BSoundPlayer(
  50. &format,
  51. "godot_sound_server",
  52. AudioDriverMediaKit::PlayBuffer,
  53. NULL,
  54. this);
  55. if (player->InitCheck() != B_OK) {
  56. fprintf(stderr, "MediaKit ERR: can not create a BSoundPlayer instance\n");
  57. ERR_FAIL_COND_V(player == NULL, ERR_CANT_OPEN);
  58. }
  59. mutex = Mutex::create();
  60. player->Start();
  61. return OK;
  62. }
  63. void AudioDriverMediaKit::PlayBuffer(void *cookie, void *buffer, size_t size, const media_raw_audio_format &format) {
  64. AudioDriverMediaKit *ad = (AudioDriverMediaKit *)cookie;
  65. int32_t *buf = (int32_t *)buffer;
  66. if (!ad->active) {
  67. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  68. AudioDriverMediaKit::samples_in[i] = 0;
  69. }
  70. } else {
  71. ad->lock();
  72. ad->audio_server_process(ad->buffer_size, AudioDriverMediaKit::samples_in);
  73. ad->unlock();
  74. }
  75. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  76. buf[i] = AudioDriverMediaKit::samples_in[i];
  77. }
  78. }
  79. void AudioDriverMediaKit::start() {
  80. active = true;
  81. }
  82. int AudioDriverMediaKit::get_mix_rate() const {
  83. return mix_rate;
  84. }
  85. AudioDriverMediaKit::SpeakerMode AudioDriverMediaKit::get_speaker_mode() const {
  86. return speaker_mode;
  87. }
  88. void AudioDriverMediaKit::lock() {
  89. if (!mutex)
  90. return;
  91. mutex->lock();
  92. }
  93. void AudioDriverMediaKit::unlock() {
  94. if (!mutex)
  95. return;
  96. mutex->unlock();
  97. }
  98. void AudioDriverMediaKit::finish() {
  99. delete player;
  100. if (samples_in) {
  101. memdelete_arr(samples_in);
  102. };
  103. if (mutex) {
  104. memdelete(mutex);
  105. mutex = NULL;
  106. }
  107. }
  108. AudioDriverMediaKit::AudioDriverMediaKit() {
  109. mutex = NULL;
  110. player = NULL;
  111. }
  112. AudioDriverMediaKit::~AudioDriverMediaKit() {
  113. }
  114. #endif