summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/disc_inp.hxx
blob: 2b39825f9f34d0b1c78e5c093b1fa66eb0256707 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// license:BSD-3-Clause
// copyright-holders:K.Wilkins
/************************************************************************
 *
 *  MAME - Discrete sound system emulation library
 *
 *  Written by K.Wilkins (mame@esplexo.co.uk)
 *
 *  (c) K.Wilkins 2000
 *
 ***********************************************************************
 *
 * DSS_ADJUSTMENT        - UI Mapped adjustable input
 * DSS_CONSTANT          - Node based constant - Do we need this ???
 * DSS_INPUT_x           - Input devices
 * DSS_INPUT_STREAM      - Connects external streams to the discrete system
 *
 ************************************************************************/


#define DSS_INPUT__GAIN     DISCRETE_INPUT(0)
#define DSS_INPUT__OFFSET   DISCRETE_INPUT(1)
#define DSS_INPUT__INIT     DISCRETE_INPUT(2)

/************************************************************************
 *
 * DSS_ADJUSTMENT - UI Adjustable constant node to emulate trimmers
 *
 * input[0]    - Enable
 * input[1]    - Minimum value
 * input[2]    - Maximum value
 * input[3]    - Log/Linear 0=Linear !0=Log
 * input[4]    - Input Port number
 * input[5]    -
 * input[6]    -
 *
 ************************************************************************/
#define DSS_ADJUSTMENT__MIN     DISCRETE_INPUT(0)
#define DSS_ADJUSTMENT__MAX     DISCRETE_INPUT(1)
#define DSS_ADJUSTMENT__LOG     DISCRETE_INPUT(2)
#define DSS_ADJUSTMENT__PORT    DISCRETE_INPUT(3)
#define DSS_ADJUSTMENT__PMIN    DISCRETE_INPUT(4)
#define DSS_ADJUSTMENT__PMAX    DISCRETE_INPUT(5)

DISCRETE_STEP(dss_adjustment)
{
	int32_t  rawportval = m_port->read();

	/* only recompute if the value changed from last time */
	if (UNEXPECTED(rawportval != m_lastpval))
	{
		double portval   = (double)(rawportval - m_pmin) * m_pscale;
		double scaledval = portval * m_scale + m_min;

		m_lastpval = rawportval;
		if (DSS_ADJUSTMENT__LOG == 0)
			set_output(0,  scaledval);
		else
			set_output(0,  pow(10, scaledval));
	}
}

DISCRETE_RESET(dss_adjustment)
{
	double min, max;

	m_port = m_device->machine().root_device().ioport(m_device->siblingtag((const char *)this->custom_data()).c_str());
	if (m_port == nullptr)
		fatalerror("DISCRETE_ADJUSTMENT - NODE_%d has invalid tag\n", this->index());

	m_lastpval = 0x7fffffff;
	m_pmin     = DSS_ADJUSTMENT__PMIN;
	m_pscale   = 1.0 / (double)(DSS_ADJUSTMENT__PMAX - DSS_ADJUSTMENT__PMIN);

	/* linear scale */
	if (DSS_ADJUSTMENT__LOG == 0)
	{
		m_min   = DSS_ADJUSTMENT__MIN;
		m_scale = DSS_ADJUSTMENT__MAX - DSS_ADJUSTMENT__MIN;
	}

	/* logarithmic scale */
	else
	{
		/* force minimum and maximum to be > 0 */
		min = (DSS_ADJUSTMENT__MIN > 0) ? DSS_ADJUSTMENT__MIN : 1;
		max = (DSS_ADJUSTMENT__MAX > 0) ? DSS_ADJUSTMENT__MAX : 1;
		m_min   = log10(min);
		m_scale = log10(max) - log10(min);
	}

	this->step();
}


/************************************************************************
 *
 * DSS_CONSTANT - This is a constant.
 *
 * input[0]    - Constant value
 *
 ************************************************************************/
#define DSS_CONSTANT__INIT  DISCRETE_INPUT(0)

DISCRETE_RESET(dss_constant)
{
	set_output(0, DSS_CONSTANT__INIT);
}


/************************************************************************
 *
 * DSS_INPUT_x    - Receives input from discrete_sound_w
 *
 * input[0]    - Gain value
 * input[1]    - Offset value
 * input[2]    - Starting Position
 * input[3]    - Current data value
 *
 ************************************************************************/

DISCRETE_RESET(dss_input_data)
{
	m_gain = DSS_INPUT__GAIN;
	m_offset = DSS_INPUT__OFFSET;

	m_data = DSS_INPUT__INIT;
	set_output(0,  m_data * m_gain + m_offset);
}

void DISCRETE_CLASS_FUNC(dss_input_data, input_write)(int sub_node, uint8_t data )
{
	uint8_t new_data    = 0;

	new_data = data;

	if (m_data != new_data)
	{
		/* Bring the system up to now */
		m_device->update_to_current_time();

		m_data = new_data;

		/* Update the node output here so we don't have to do it each step */
		set_output(0,  m_data * m_gain + m_offset);
	}
}

DISCRETE_RESET(dss_input_logic)
{
	m_gain = DSS_INPUT__GAIN;
	m_offset = DSS_INPUT__OFFSET;

	m_data = (DSS_INPUT__INIT == 0) ? 0 : 1;
	set_output(0,  m_data * m_gain + m_offset);
}

void DISCRETE_CLASS_FUNC(dss_input_logic, input_write)(int sub_node, uint8_t data )
{
	uint8_t new_data    = 0;

	new_data =  data ? 1 : 0;

	if (m_data != new_data)
	{
		/* Bring the system up to now */
		m_device->update_to_current_time();

		m_data = new_data;

		/* Update the node output here so we don't have to do it each step */
		set_output(0,  m_data * m_gain + m_offset);
	}
}

DISCRETE_RESET(dss_input_not)
{
	m_gain = DSS_INPUT__GAIN;
	m_offset = DSS_INPUT__OFFSET;

	m_data = (DSS_INPUT__INIT == 0) ? 1 : 0;
	set_output(0,  m_data * m_gain + m_offset);
}

void DISCRETE_CLASS_FUNC(dss_input_not, input_write)(int sub_node, uint8_t data )
{
	uint8_t new_data    = 0;

	new_data = data ? 0 : 1;

	if (m_data != new_data)
	{
		/* Bring the system up to now */
		m_device->update_to_current_time();

		m_data = new_data;

		/* Update the node output here so we don't have to do it each step */
		set_output(0,  m_data * m_gain + m_offset);
	}
}

DISCRETE_STEP(dss_input_pulse)
{
	/* Set a valid output */
	set_output(0,  m_data);
	/* Reset the input to default for the next cycle */
	/* node order is now important */
	m_data = DSS_INPUT__INIT;
}

DISCRETE_RESET(dss_input_pulse)
{
	m_data = (DSS_INPUT__INIT == 0) ? 0 : 1;
	set_output(0,  m_data);
}

void DISCRETE_CLASS_FUNC(dss_input_pulse, input_write)(int sub_node, uint8_t data )
{
	uint8_t new_data    = 0;

	new_data =  data ? 1 : 0;

	if (m_data != new_data)
	{
		/* Bring the system up to now */
		m_device->update_to_current_time();
		m_data = new_data;
	}
}

/************************************************************************
 *
 * DSS_INPUT_STREAM    - Receives input from a routed stream
 *
 * input[0]    - Input stream number
 * input[1]    - Gain value
 * input[2]    - Offset value
 *
 ************************************************************************/
#define DSS_INPUT_STREAM__STREAM    DISCRETE_INPUT(0)
#define DSS_INPUT_STREAM__GAIN      DISCRETE_INPUT(1)
#define DSS_INPUT_STREAM__OFFSET    DISCRETE_INPUT(2)

void discrete_dss_input_stream_node::stream_generate(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	stream_sample_t *ptr = outputs[0];
	int samplenum = samples;

	while (samplenum-- > 0)
		*(ptr++) = m_data;
}
DISCRETE_STEP(dss_input_stream)
{
	/* the context pointer is set to point to the current input stream data in discrete_stream_update */
	if (EXPECTED(m_ptr))
	{
		set_output(0,  (*m_ptr) * m_gain + m_offset);
		m_ptr++;
	}
	else
		set_output(0,  0);
}

DISCRETE_RESET(dss_input_stream)
{
	m_ptr = nullptr;
	m_data = 0;
}

void DISCRETE_CLASS_FUNC(dss_input_stream, input_write)(int sub_node, uint8_t data )
{
	uint8_t new_data    = 0;

	new_data =  data;

	if (m_data != new_data)
	{
		if (m_is_buffered)
		{
			/* Bring the system up to now */
			m_buffer_stream->update();

			m_data = new_data;
		}
		else
		{
			/* Bring the system up to now */
			m_device->update_to_current_time();

			m_data = new_data;

			/* Update the node output here so we don't have to do it each step */
			set_output(0,  new_data * m_gain + m_offset);
		}
	}
}

DISCRETE_START(dss_input_stream)
{
	discrete_base_node::start();

	/* Stream out number is set during start */
	m_stream_in_number = DSS_INPUT_STREAM__STREAM;
	m_gain = DSS_INPUT_STREAM__GAIN;
	m_offset = DSS_INPUT_STREAM__OFFSET;
	m_ptr = nullptr;

	m_is_buffered = is_buffered();
	m_buffer_stream = nullptr;
}

void DISCRETE_CLASS_NAME(dss_input_stream)::stream_start(void)
{
	if (m_is_buffered)
	{
		/* stream_buffered input only supported for sound devices */
		discrete_sound_device *snd_device = downcast<discrete_sound_device *>(m_device);
		//assert(DSS_INPUT_STREAM__STREAM < snd_device->m_input_stream_list.count());

		m_buffer_stream = m_device->machine().sound().stream_alloc(*snd_device, 0, 1, this->sample_rate(), stream_update_delegate(&discrete_dss_input_stream_node::stream_generate,this));

		snd_device->get_stream()->set_input(m_stream_in_number, m_buffer_stream);
	}
}