summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/imaadpcm.cpp
blob: cd6f08166a3868e8d2275faf8149050093cae8fc (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
// license:BSD-3-Clause
// copyright-holders:Andrew Gardner,Aaron Giles
/***************************************************************************

    imaadpcm.cpp

    IMA ADPCM emulation.

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

#include "emu.h"
#include "imaadpcm.h"


//**************************************************************************
//  ADPCM STATE HELPER
//**************************************************************************

// ADPCM state and tables
bool ima_adpcm_state::s_tables_computed = false;
const int8_t ima_adpcm_state::s_index_shift[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
int ima_adpcm_state::s_diff_lookup[89*16];

//-------------------------------------------------
//  reset - reset the ADPCM state
//-------------------------------------------------

void ima_adpcm_state::reset()
{
	// reset the signal/step
	m_signal = m_loop_signal = 0;
	m_step = m_loop_step = 0;
	m_saved = false;
}


//-------------------------------------------------
//  clock - decode single nibble and update
//  ADPCM output
//-------------------------------------------------

int16_t ima_adpcm_state::clock(uint8_t nibble)
{
	// update the signal
	m_signal += s_diff_lookup[m_step * 16 + (nibble & 15)];

	// clamp to the maximumf
	if (m_signal > 32767)
		m_signal = 32767;
	else if (m_signal < -32768)
		m_signal = -32768;

	// adjust the step size and clamp
	m_step += s_index_shift[nibble & 7];
	if (m_step > 88)
		m_step = 88;
	else if (m_step < 0)
		m_step = 0;

	// return the signal
	return m_signal;
}


//-------------------------------------------------
//  save - save current ADPCM state to buffer
//-------------------------------------------------

void ima_adpcm_state::save()
{
	if (!m_saved)
	{
		m_loop_signal = m_signal;
		m_loop_step = m_step;
		m_saved = true;
	}
}


//-------------------------------------------------
//  restore - restore previous ADPCM state
//  from buffer
//-------------------------------------------------

void ima_adpcm_state::restore()
{
	m_signal = m_loop_signal;
	m_step = m_loop_step;
}


//-------------------------------------------------
//  compute_tables - precompute tables for faster
//  sound generation
//-------------------------------------------------

void ima_adpcm_state::compute_tables()
{
	// skip if we already did it
	if (s_tables_computed)
		return;
	s_tables_computed = true;

	// nibble to bit map
	static const int8_t nbl2bit[16][4] =
	{
		{ 1, 0, 0, 0}, { 1, 0, 0, 1}, { 1, 0, 1, 0}, { 1, 0, 1, 1},
		{ 1, 1, 0, 0}, { 1, 1, 0, 1}, { 1, 1, 1, 0}, { 1, 1, 1, 1},
		{-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
		{-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
	};

	// loop over all possible steps
	for (int step = -8; step <= 80; step++)
	{
		// compute the step value
		int stepval = std::min(floor(16.0 * pow(11.0 / 10.0, (double)step)), 32767.);

		// manual correction of some early values
		if (step == -5 || step == -4)
			stepval++;

		// loop over all nibbles and compute the difference
		for (int nib = 0; nib < 16; nib++)
		{
			s_diff_lookup[(step + 8)*16 + nib] = nbl2bit[nib][0] *
					(stepval * nbl2bit[nib][1] +
					stepval/2 * nbl2bit[nib][2] +
					stepval/4 * nbl2bit[nib][3] +
					stepval/8);
		}
	}
}