From 688d6e212814ad041c2d382020bf320c2dafabbf Mon Sep 17 00:00:00 2001 From: hap Date: Sat, 9 Sep 2023 21:19:24 +0200 Subject: sound: add configurable speaker panning --- src/emu/speaker.cpp | 62 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 14 deletions(-) (limited to 'src/emu/speaker.cpp') diff --git a/src/emu/speaker.cpp b/src/emu/speaker.cpp index 3cbb14355a4..0755aab5af3 100644 --- a/src/emu/speaker.cpp +++ b/src/emu/speaker.cpp @@ -37,6 +37,8 @@ speaker_device::speaker_device(const machine_config &mconfig, const char *tag, d , m_x(0.0) , m_y(0.0) , m_z(0.0) + , m_pan(1.0) + , m_defpan(1.0) , m_current_max(0) , m_samples_this_bucket(0) { @@ -52,6 +54,35 @@ speaker_device::~speaker_device() } +//------------------------------------------------- +// set_position - set speaker position +//------------------------------------------------- + +speaker_device &speaker_device::set_position(double x, double y, double z) +{ + // as mentioned in the header file, y and z params currently have no effect + m_x = x; + m_y = y; + m_z = z; + + // hard pan to left + if (m_x < 0.0) + set_pan(0.0f); + + // hard pan to right + else if (m_x > 0.0) + set_pan(2.0f); + + // center (mono) + else + set_pan(1.0f); + + m_defpan = m_pan; + + return *this; +} + + //------------------------------------------------- // mix - mix in samples from the speaker's stream //------------------------------------------------- @@ -89,24 +120,29 @@ void speaker_device::mix(stream_buffer::sample_t *leftmix, stream_buffer::sample // mix if sound is enabled if (!suppress) { - // if the speaker is centered, send to both left and right - if (m_x == 0) + // if the speaker is hard panned to the left, send only to the left + if (m_pan == 0.0f) for (int sample = 0; sample < expected_samples; sample++) - { - stream_buffer::sample_t cursample = view.get(sample); - leftmix[sample] += cursample; - rightmix[sample] += cursample; - } + leftmix[sample] += view.get(sample); - // if the speaker is to the left, send only to the left - else if (m_x < 0) + // if the speaker is hard panned to the right, send only to the right + else if (m_pan == 2.0f) for (int sample = 0; sample < expected_samples; sample++) - leftmix[sample] += view.get(sample); + rightmix[sample] += view.get(sample); - // if the speaker is to the right, send only to the right + // otherwise, send to both else + { + const float leftpan = (m_pan <= 1.0f) ? 1.0f : 2.0f - m_pan; + const float rightpan = (m_pan >= 1.0f) ? 1.0f : m_pan; + for (int sample = 0; sample < expected_samples; sample++) - rightmix[sample] += view.get(sample); + { + stream_buffer::sample_t cursample = view.get(sample); + leftmix[sample] += cursample * leftpan; + rightmix[sample] += cursample * rightpan; + } + } } } @@ -117,7 +153,6 @@ void speaker_device::mix(stream_buffer::sample_t *leftmix, stream_buffer::sample void speaker_device::device_start() { - // dummy save to make device.c happy } @@ -187,4 +222,3 @@ void speaker_device::device_stop() } } } - -- cgit v1.2.3