audio_driver_media_kit.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 = nullptr;
  34. Error AudioDriverMediaKit::init() {
  35. active = false;
  36. mix_rate = GLOBAL_GET("audio/mix_rate");
  37. speaker_mode = SPEAKER_MODE_STEREO;
  38. channels = 2;
  39. int latency = GLOBAL_GET("audio/output_latency");
  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. nullptr,
  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 == nullptr, ERR_CANT_OPEN);
  58. }
  59. player->Start();
  60. return OK;
  61. }
  62. void AudioDriverMediaKit::PlayBuffer(void *cookie, void *buffer, size_t size, const media_raw_audio_format &format) {
  63. AudioDriverMediaKit *ad = (AudioDriverMediaKit *)cookie;
  64. int32_t *buf = (int32_t *)buffer;
  65. if (!ad->active) {
  66. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  67. AudioDriverMediaKit::samples_in[i] = 0;
  68. }
  69. } else {
  70. ad->lock();
  71. ad->audio_server_process(ad->buffer_size, AudioDriverMediaKit::samples_in);
  72. ad->unlock();
  73. }
  74. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  75. buf[i] = AudioDriverMediaKit::samples_in[i];
  76. }
  77. }
  78. void AudioDriverMediaKit::start() {
  79. active = true;
  80. }
  81. int AudioDriverMediaKit::get_mix_rate() const {
  82. return mix_rate;
  83. }
  84. AudioDriverMediaKit::SpeakerMode AudioDriverMediaKit::get_speaker_mode() const {
  85. return speaker_mode;
  86. }
  87. void AudioDriverMediaKit::lock() {
  88. if (!mutex)
  89. return;
  90. mutex.lock();
  91. }
  92. void AudioDriverMediaKit::unlock() {
  93. if (!mutex)
  94. return;
  95. mutex.unlock();
  96. }
  97. void AudioDriverMediaKit::finish() {
  98. delete player;
  99. if (samples_in) {
  100. memdelete_arr(samples_in);
  101. };
  102. }
  103. AudioDriverMediaKit::AudioDriverMediaKit() {
  104. player = nullptr;
  105. }
  106. AudioDriverMediaKit::~AudioDriverMediaKit() {
  107. }
  108. #endif