summaryrefslogtreecommitdiff
path: root/Servers/AudioServer/ASMixer.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-11-22 21:44:02 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-11-22 21:44:02 +0100
commit107011f1190004b3a18f395c145ee6211301622c (patch)
treeeecce461dec6dac7bc5206f1a2e4b1be3f0a56b6 /Servers/AudioServer/ASMixer.cpp
parent2b9ec2257605335ec43aab01e2b7bd8107850f1b (diff)
downloadserenity-107011f1190004b3a18f395c145ee6211301622c.zip
AudioServer: Allow muting the system audio
This patch adds muting to ASMixer, which works by substituting what we would normally send to the sound card with zero-filled memory instead. We do it this way to ensure that the queued sample buffers keep getting played (silently.) This is obviously not the perfect way of doing this, and in the future we should improve on this, and also find a way to utilize any hardware mixing functions in the sound card.
Diffstat (limited to 'Servers/AudioServer/ASMixer.cpp')
-rw-r--r--Servers/AudioServer/ASMixer.cpp37
1 files changed, 24 insertions, 13 deletions
diff --git a/Servers/AudioServer/ASMixer.cpp b/Servers/AudioServer/ASMixer.cpp
index 15a317e4a4..2cd71e8bec 100644
--- a/Servers/AudioServer/ASMixer.cpp
+++ b/Servers/AudioServer/ASMixer.cpp
@@ -15,6 +15,8 @@ ASMixer::ASMixer()
return;
}
+ m_zero_filled_buffer = (u8*)malloc(4096);
+ bzero(m_zero_filled_buffer, 4096);
m_sound_thread.start();
}
@@ -66,33 +68,42 @@ void ASMixer::mix()
}
}
+ bool muted = m_muted;
+
// output the mixed stuff to the device
u8 raw_buffer[4096];
- auto buffer = ByteBuffer::wrap(raw_buffer, sizeof(raw_buffer));
- BufferStream stream(buffer);
+ auto buffer = ByteBuffer::wrap(muted ? m_zero_filled_buffer : raw_buffer, sizeof(raw_buffer));
- for (int i = 0; i < mixed_buffer_length; ++i) {
- auto& mixed_sample = mixed_buffer[i];
+ BufferStream stream(buffer);
+ if (!muted) {
+ for (int i = 0; i < mixed_buffer_length; ++i) {
+ auto& mixed_sample = mixed_buffer[i];
- mixed_sample.scale(m_main_volume);
- mixed_sample.clip();
+ mixed_sample.scale(m_main_volume);
+ mixed_sample.clip();
- i16 out_sample;
- out_sample = mixed_sample.left * std::numeric_limits<i16>::max();
- stream << out_sample;
+ i16 out_sample;
+ out_sample = mixed_sample.left * std::numeric_limits<i16>::max();
+ stream << out_sample;
- ASSERT(!stream.at_end()); // we should have enough space for both channels in one buffer!
- out_sample = mixed_sample.right * std::numeric_limits<i16>::max();
- stream << out_sample;
+ ASSERT(!stream.at_end()); // we should have enough space for both channels in one buffer!
+ out_sample = mixed_sample.right * std::numeric_limits<i16>::max();
+ stream << out_sample;
+ }
}
if (stream.offset() != 0) {
buffer.trim(stream.offset());
- m_device->write(buffer);
}
+ m_device->write(buffer);
}
}
+void ASMixer::set_muted(bool muted)
+{
+ m_muted = muted;
+}
+
ASBufferQueue::ASBufferQueue(ASClientConnection& client)
: m_client(client.make_weak_ptr())
{