summaryrefslogtreecommitdiffstats
path: root/drivers/sound/softoss_rs.c
blob: 2fa95f748b355d9df16aaaa24a527ea9e08e16ad (plain)
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

/*
 * sound/softoss_rs.c
 *
 * Software based MIDI synthsesizer driver, the actual mixing loop.
 * Keep the loop as simple as possible to make it easier to rewrite this 
 * routine in assembly.
 *
 *
 * Copyright (C) by Hannu Savolainen 1993-1997
 *
 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
 * Version 2 (June 1991). See the "COPYING" file distributed with this software
 * for more info.
 */
#include <linux/config.h>


#include "sound_config.h"

#ifdef CONFIG_SOFTOSS
#include "softoss.h"

void softsynth_resample_loop(short *buf, int loops)
{
	int iloop, voice;
	volatile voice_info *v;

#ifdef OSS_BIG_ENDIAN
	unsigned char  *cbuf = (unsigned char *) buf;

#endif

	for (iloop = 0; iloop < loops; iloop++)
	{			/* Mix one sample */
		int accum, left = 0, right = 0;
		int ix, position;

		for (voice = 0; voice < devc->maxvoice; voice++)
		{
			if (voice_active[voice])
			{	/* Compute voice */
				v = &softoss_voices[voice];
#ifdef SOFTOSS_TEST
				ix = iloop << 3;
				position = v->ptr;
#else
				ix = (position = v->ptr) >> 9;
#endif
				/* Interpolation (resolution of 512 steps) */
				{
					int fract = v->ptr & 0x1ff;	/* 9 bits */

					/* This method works with less arithmetic operations */
					register int v1 = v->wave[ix];
					accum = v1 + ((((v->wave[ix + 1] - v1)) * (fract)) >> 9);
				}

				left += (accum * v->leftvol);
				right += (accum * v->rightvol);

				/* Update sample pointer */
				position += v->step;
				if (position <= v->endloop)
					v->ptr = position;
				else if (v->mode & WAVE_LOOPING)
				{
					if (v->mode & WAVE_BIDIR_LOOP)
					{							v->mode ^= WAVE_LOOP_BACK;	/* Turn around */
						v->step *= -1;
					}
					else
					{
						position -= v->looplen;
						v->ptr = position;
					}
				}
				/*  else leave the voice looping the current sample */

				if (v->mode & WAVE_LOOP_BACK && position < v->startloop)
				{
					if (v->mode & WAVE_BIDIR_LOOP)
					{							v->mode ^= WAVE_LOOP_BACK;	/* Turn around */
						v->step *= -1;
					}
					else
					{
						position += v->looplen;
						v->ptr = position;
					}
				}
			}	/* Compute voice */
		}
#if 1				/* Delay */
		left += left_delay[delayp];
		right += right_delay[delayp];

		left_delay[delayp] = right >> 2;
		right_delay[delayp] = left >> 2;
		delayp = (delayp + 1) % devc->delay_size;
#endif

#define AFTERSCALE devc->afterscale;

		left >>= AFTERSCALE;
		right >>= AFTERSCALE;

		if (left > 32767)
			left = 32767;
		if (left < -32768)
			left = -32768;
		if (right > 32767)
			right = 32767;
		if (right < -32768)
			right = -32768;

#ifdef OSS_BIG_ENDIAN
		*cbuf++ = left & 0xff;
		*cbuf++ = (left >> 8) & 0xff;
		*cbuf++ = right & 0xff;
		*cbuf++ = (right >> 8) & 0xff;
#else
		*buf++ = left;
		*buf++ = right;
#endif
		if (devc->control_counter++ >= devc->control_rate)
		{
			devc->control_counter = 0;
			softsyn_control_loop();
		}
	}			/* Mix one sample */
}
#endif