ASMixer.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/BufferStream.h>
  27. #include <AK/NumericLimits.h>
  28. #include <AudioServer/ASClientConnection.h>
  29. #include <AudioServer/ASMixer.h>
  30. #include <pthread.h>
  31. ASMixer::ASMixer()
  32. : m_device(Core::File::construct("/dev/audio", this))
  33. , m_sound_thread(
  34. [this] {
  35. mix();
  36. return 0;
  37. },
  38. "AudioServer[mixer]")
  39. {
  40. if (!m_device->open(Core::IODevice::WriteOnly)) {
  41. dbgprintf("Can't open audio device: %s\n", m_device->error_string());
  42. return;
  43. }
  44. pthread_mutex_init(&m_pending_mutex, nullptr);
  45. pthread_cond_init(&m_pending_cond, nullptr);
  46. m_zero_filled_buffer = (u8*)malloc(4096);
  47. bzero(m_zero_filled_buffer, 4096);
  48. m_sound_thread.start();
  49. }
  50. ASMixer::~ASMixer()
  51. {
  52. }
  53. NonnullRefPtr<ASBufferQueue> ASMixer::create_queue(ASClientConnection& client)
  54. {
  55. auto queue = adopt(*new ASBufferQueue(client));
  56. pthread_mutex_lock(&m_pending_mutex);
  57. m_pending_mixing.append(*queue);
  58. pthread_cond_signal(&m_pending_cond);
  59. pthread_mutex_unlock(&m_pending_mutex);
  60. return queue;
  61. }
  62. void ASMixer::mix()
  63. {
  64. decltype(m_pending_mixing) active_mix_queues;
  65. for (;;) {
  66. if (active_mix_queues.is_empty()) {
  67. pthread_mutex_lock(&m_pending_mutex);
  68. pthread_cond_wait(&m_pending_cond, &m_pending_mutex);
  69. active_mix_queues.append(move(m_pending_mixing));
  70. pthread_mutex_unlock(&m_pending_mutex);
  71. }
  72. active_mix_queues.remove_all_matching([&](auto& entry) { return !entry->client(); });
  73. Audio::Sample mixed_buffer[1024];
  74. auto mixed_buffer_length = (int)(sizeof(mixed_buffer) / sizeof(Audio::Sample));
  75. // Mix the buffers together into the output
  76. for (auto& queue : active_mix_queues) {
  77. if (!queue->client()) {
  78. queue->clear();
  79. continue;
  80. }
  81. for (int i = 0; i < mixed_buffer_length; ++i) {
  82. auto& mixed_sample = mixed_buffer[i];
  83. Audio::Sample sample;
  84. if (!queue->get_next_sample(sample))
  85. break;
  86. mixed_sample += sample;
  87. }
  88. }
  89. bool muted = m_muted;
  90. // output the mixed stuff to the device
  91. u8 raw_buffer[4096];
  92. auto buffer = ByteBuffer::wrap(muted ? m_zero_filled_buffer : raw_buffer, sizeof(raw_buffer));
  93. BufferStream stream(buffer);
  94. if (!muted) {
  95. for (int i = 0; i < mixed_buffer_length; ++i) {
  96. auto& mixed_sample = mixed_buffer[i];
  97. mixed_sample.scale(m_main_volume);
  98. mixed_sample.clip();
  99. i16 out_sample;
  100. out_sample = mixed_sample.left * NumericLimits<i16>::max();
  101. stream << out_sample;
  102. ASSERT(!stream.at_end()); // we should have enough space for both channels in one buffer!
  103. out_sample = mixed_sample.right * NumericLimits<i16>::max();
  104. stream << out_sample;
  105. }
  106. }
  107. if (stream.offset() != 0) {
  108. buffer.trim(stream.offset());
  109. }
  110. m_device->write(buffer);
  111. }
  112. }
  113. void ASMixer::set_muted(bool muted)
  114. {
  115. if (m_muted == muted)
  116. return;
  117. m_muted = muted;
  118. ASClientConnection::for_each([muted](ASClientConnection& client) {
  119. client.did_change_muted_state({}, muted);
  120. });
  121. }
  122. ASBufferQueue::ASBufferQueue(ASClientConnection& client)
  123. : m_client(client.make_weak_ptr())
  124. {
  125. }
  126. void ASBufferQueue::enqueue(NonnullRefPtr<Audio::Buffer>&& buffer)
  127. {
  128. m_remaining_samples += buffer->sample_count();
  129. m_queue.enqueue(move(buffer));
  130. }