From 657c860aa222fe4ffbf04ab4dcf68c1301559325 Mon Sep 17 00:00:00 2001 From: hap Date: Wed, 17 Jul 2024 14:23:39 +0200 Subject: beep: small cleanup --- src/devices/sound/beep.cpp | 53 ++++++++++++++++++---------------------------- src/devices/sound/beep.h | 18 ++++++---------- 2 files changed, 28 insertions(+), 43 deletions(-) diff --git a/src/devices/sound/beep.cpp b/src/devices/sound/beep.cpp index d2358d4cc93..77e3b431581 100644 --- a/src/devices/sound/beep.cpp +++ b/src/devices/sound/beep.cpp @@ -7,15 +7,12 @@ This is used for computers/systems which can only output a constant tone. This tone can be turned on and off. e.g. PCW and PCW16 computer systems - KT - 25-Jun-2000 ****************************************************************************/ #include "emu.h" #include "beep.h" -#define BEEP_RATE (384000) - // device type definition DEFINE_DEVICE_TYPE(BEEP, beep_device, "beep", "Beep") @@ -24,7 +21,7 @@ beep_device::beep_device(const machine_config &mconfig, const char *tag, device_ : device_t(mconfig, BEEP, tag, owner, clock) , device_sound_interface(mconfig, *this) , m_stream(nullptr) - , m_enable(0) + , m_enable(false) , m_frequency(clock) { } @@ -36,8 +33,10 @@ beep_device::beep_device(const machine_config &mconfig, const char *tag, device_ void beep_device::device_start() { - m_stream = stream_alloc(0, 1, BEEP_RATE); - m_enable = 0; + m_stream = stream_alloc(0, 1, 384000); + + m_enable = false; + m_incr = 0; m_signal = 1.0; // register for savestates @@ -55,37 +54,26 @@ void beep_device::device_start() void beep_device::sound_stream_update(sound_stream &stream, std::vector const &inputs, std::vector &outputs) { auto &buffer = outputs[0]; - int16_t signal = m_signal; - int clock = 0, rate = BEEP_RATE / 2; - - /* get progress through wave */ - int incr = m_incr; - - if (m_frequency > 0) - clock = m_frequency; - /* if we're not enabled, just fill with 0 */ - if (!m_enable || clock == 0) + // if we're not enabled, just fill with 0 + if (!m_enable || m_frequency == 0) { buffer.fill(0); return; } - /* fill in the sample */ + // fill in the sample for (int sampindex = 0; sampindex < buffer.samples(); sampindex++) { - buffer.put(sampindex, signal); - incr -= clock; - while( incr < 0 ) + m_incr -= m_frequency; + while (m_incr < 0) { - incr += rate; - signal = -signal; + m_incr += stream.sample_rate() / 2; + m_signal = -m_signal; } - } - /* store progress through wave */ - m_incr = incr; - m_signal = signal; + buffer.put(sampindex, m_signal); + } } @@ -95,15 +83,14 @@ void beep_device::sound_stream_update(sound_stream &stream, std::vectorupdate(); - m_enable = on; + m_enable = bool(state); - /* restart wave from beginning */ + // restart wave from beginning m_incr = 0; m_signal = 1.0; } @@ -120,6 +107,8 @@ void beep_device::set_clock(uint32_t frequency) m_stream->update(); m_frequency = frequency; - m_signal = 1.0; + + // restart wave from beginning m_incr = 0; + m_signal = 1.0; } diff --git a/src/devices/sound/beep.h b/src/devices/sound/beep.h index 4ea9064c49c..76b974bc7bf 100644 --- a/src/devices/sound/beep.h +++ b/src/devices/sound/beep.h @@ -15,14 +15,14 @@ // TYPE DEFINITIONS //************************************************************************** -// ======================> beep_device - -class beep_device : public device_t, - public device_sound_interface +class beep_device : public device_t, public device_sound_interface { public: beep_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock); + void set_state(int state); // enable/disable sound output + void set_clock(uint32_t frequency); // output frequency + protected: // device-level overrides virtual void device_start() override; @@ -30,15 +30,11 @@ protected: // sound stream update overrides virtual void sound_stream_update(sound_stream &stream, std::vector const &inputs, std::vector &outputs) override; -public: - void set_state(int state); // enable/disable sound output - void set_clock(uint32_t frequency); // output frequency - private: sound_stream *m_stream; // stream number - int m_enable; // enable beep - int m_frequency; // set frequency - this can be changed using the appropriate function - int m_incr; // initial wave state + bool m_enable; // enable beep + uint32_t m_frequency; // set frequency - this can be changed using the appropriate function + int32_t m_incr; // initial wave state stream_buffer::sample_t m_signal; // current signal }; -- cgit v1.2.3