beep: small cleanup

This commit is contained in:
hap 2024-07-17 14:23:39 +02:00
parent b5d80d85ae
commit 657c860aa2
2 changed files with 28 additions and 43 deletions

View File

@ -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<read_stream_view> const &inputs, std::vector<write_stream_view> &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::vector<read_str
void beep_device::set_state(int state)
{
/* only update if new state is not the same as old state */
int on = (state) ? 1 : 0;
if (m_enable == on)
// only update if new state is not the same as old state
if (m_enable == bool(state))
return;
m_stream->update();
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;
}

View File

@ -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<read_stream_view> const &inputs, std::vector<write_stream_view> &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
};