summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/audio/dai.c
blob: 89ba1beec620ce975548d8770da01beacd48d93c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/***************************************************************************

  audio/dai.c

  Functions to emulate sound hardware of DAI Personal Computer

  Krzysztof Strzecha

****************************************************************************/

#include "emu.h"
#include "includes/dai.h"

// device type definition
const device_type DAI_SOUND = &device_creator<dai_sound_device>;


//-------------------------------------------------
//  dai_sound_device - constructor
//-------------------------------------------------

dai_sound_device::dai_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, DAI_SOUND, "DAI Custom Sound", tag, owner, clock),
	  device_sound_interface(mconfig, *this)
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void dai_sound_device::device_start()
{
	m_mixer_channel = machine().sound().stream_alloc(*this, 0, 2, machine().sample_rate(), this);
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void dai_sound_device::device_reset()
{
	memset(m_dai_input, 0, sizeof(m_dai_input));
	memset(m_osc_volume, 0, sizeof(m_osc_volume));
	m_noise_volume = 0;
}


//-------------------------------------------------
//  channels 0/1/2 volume table
//-------------------------------------------------

const UINT16 dai_sound_device::s_osc_volume_table[] = {
					   0,  500, 1000, 1500,
					2000, 2500, 3000, 3500,
					4000, 4500, 5000, 5500,
					6000, 6500, 7000, 7500
};

//-------------------------------------------------
//  noise volume table
//-------------------------------------------------

const UINT16 dai_sound_device::s_noise_volume_table[] = {
					   0,    0,    0,    0,
					   0,    0,    0,    0,
				     500, 1000, 1500, 2000,
					2500, 3000, 3500, 4000
};


//-------------------------------------------------
//  set_volume
//-------------------------------------------------

WRITE8_MEMBER(dai_sound_device::set_volume)
{
	m_mixer_channel->update();

	switch (offset & 1)
	{
	case 0x00:
		m_osc_volume[0] = data&0x0f;
		m_osc_volume[1] = (data&0xf0)>>4;
		break;

	case 0x01:
		m_osc_volume[2] = data&0x0f;
		m_noise_volume = (data&0xf0)>>4;
	}
}

//-------------------------------------------------
//  PIT callbacks
//-------------------------------------------------

WRITE_LINE_MEMBER(dai_sound_device::set_input_ch0)
{
	m_mixer_channel->update();
	m_dai_input[0] = state;
}

WRITE_LINE_MEMBER(dai_sound_device::set_input_ch1)
{
	m_mixer_channel->update();
	m_dai_input[1] = state;
}

WRITE_LINE_MEMBER(dai_sound_device::set_input_ch2)
{
	m_mixer_channel->update();
	m_dai_input[2] = state;
}

//-------------------------------------------------
//  sound_stream_update - handle update requests for
//  our sound stream
//-------------------------------------------------

void dai_sound_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	stream_sample_t *sample_left = outputs[0];
	stream_sample_t *sample_right = outputs[1];

	INT16 channel_0_signal = m_dai_input[0] ? s_osc_volume_table[m_osc_volume[0]] : -s_osc_volume_table[m_osc_volume[0]];
	INT16 channel_1_signal = m_dai_input[1] ? s_osc_volume_table[m_osc_volume[1]] : -s_osc_volume_table[m_osc_volume[1]];
	INT16 channel_2_signal = m_dai_input[2] ? s_osc_volume_table[m_osc_volume[2]] : -s_osc_volume_table[m_osc_volume[2]];

	while (samples--)
	{
		INT16 noise = machine().rand()&0x01 ? s_noise_volume_table[m_noise_volume] : -s_noise_volume_table[m_noise_volume];

		/* channel 0 + channel 1 + noise */
		*sample_left++ = channel_0_signal + channel_1_signal + noise;

		/* channel 1 + channel 2 + noise */
		*sample_right++ = channel_1_signal + channel_2_signal + noise;
	}
}