File: dwmixqa_c.c

package info (click to toggle)
ocp 1%3A0.1.21-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 5,344 kB
  • ctags: 7,862
  • sloc: ansic: 91,449; cpp: 9,729; sh: 3,119; makefile: 2,482
file content (303 lines) | stat: -rw-r--r-- 8,839 bytes parent folder | download | duplicates (3)
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
static int16_t (*myvoltab)[2][256];
static int16_t (*myinterpoltabq)[32][256][2];
static int16_t (*myinterpoltabq2)[16][256][4];

void mixqSetupAddresses(int16_t (*voltab)[2][256], int16_t (*interpoltabq)[32][256][2], int16_t (*interpoltabq2)[16][256][4])
{
	myvoltab=voltab;
	myinterpoltabq=interpoltabq;
	myinterpoltabq2=interpoltabq2;
}

static void playquiet(int16_t *buf, uint32_t len, struct channel *chan)
{
}

static inline int32_t interp_none8(const struct channel *chan, const uint32_t pos, uint32_t fpos)
{
	return chan->realsamp.bit8[pos]<<8;
}

static inline int32_t interp_none16(const struct channel *chan, const uint32_t pos, uint32_t fpos)
{
	return chan->realsamp.bit16[pos];
}

static inline int32_t interp_i8(const struct channel *chan, const uint32_t pos, uint32_t fpos)
{
	uint8_t cache = fpos>>11;
	return myinterpoltabq[0][cache][(uint8_t)chan->realsamp.bit8[pos]][0]
	        +
	       myinterpoltabq[0][cache][(uint8_t)chan->realsamp.bit8[pos+1]][1];
}

static inline int32_t interp_i16(const struct channel *chan, const uint32_t pos, uint32_t fpos)
{
	uint8_t cache = fpos>>11;

	return myinterpoltabq[0][cache][(uint8_t)(chan->realsamp.bit16[pos]>>8)][0]
	        +
	       myinterpoltabq[0][cache][(uint8_t)(chan->realsamp.bit16[pos+1]>>8)][1]
	        +
	       myinterpoltabq[1][cache][(uint8_t)(chan->realsamp.bit16[pos]&0xff)][0]
	        +
	       myinterpoltabq[1][cache][(uint8_t)(chan->realsamp.bit16[pos+1]&0xff)][1];
}

static inline int32_t interp_i28(const struct channel *chan, const uint32_t pos, uint32_t fpos)
{
	uint8_t cache = fpos>>12;

	return myinterpoltabq2[0][cache][(uint8_t)(chan->realsamp.bit8[pos])][0]
	        +
	        myinterpoltabq2[0][cache][(uint8_t)(chan->realsamp.bit8[pos+1])][1]
	        +
	        myinterpoltabq2[0][cache][(uint8_t)(chan->realsamp.bit8[pos+2])][2];
}

static inline int32_t interp_i216(const struct channel *chan, const uint32_t pos, uint32_t fpos)
{
	uint8_t cache = fpos>>12;

	return myinterpoltabq2[0][cache][(uint8_t)(chan->realsamp.bit16[pos]>>8)][0]
	        +
	       myinterpoltabq2[0][cache][(uint8_t)(chan->realsamp.bit16[pos+1]>>8)][1]
	        +
	       myinterpoltabq2[0][cache][(uint8_t)(chan->realsamp.bit16[pos+2]>>8)][2]
	        +
	       myinterpoltabq2[1][cache][(uint8_t)(chan->realsamp.bit16[pos]&0xff)][0]
	        +
	       myinterpoltabq2[1][cache][(uint8_t)(chan->realsamp.bit16[pos+1]&0xff)][1]
	       +
	       myinterpoltabq2[1][cache][(uint8_t)(chan->realsamp.bit16[pos+2]&0xff)][2];
}

#define MIX_TEMPLATE(NAME, INTERP)                                      \
static void                                                             \
NAME(int16_t *buf,                                                      \
     uint32_t len,                                                      \
     struct channel *chan)                                              \
{                                                                       \
    uint32_t pos=chan->pos;                                             \
    uint32_t fpos=chan->fpos;                                           \
    uint32_t fadd=chan->step&0xffff;                                    \
    uint32_t posadd=(int16_t)(chan->step>>16);                          \
                                                                        \
    while (len)                                                         \
        {                                                               \
            *(buf++) = interp_##INTERP(chan, pos, fpos);                \
            fpos+=fadd;                                                 \
            if (fpos&0xffff0000)                                        \
            {                                                           \
                pos++;                                                  \
                fpos&=0xffff;                                           \
            }                                                           \
            pos+=posadd;                                                \
            len--;                                                      \
        }                                                               \
}

//#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused"
MIX_TEMPLATE(playmono, none8)
MIX_TEMPLATE(playmono16, none16)
MIX_TEMPLATE(playmonoi, i8)
MIX_TEMPLATE(playmonoi16, i16)
MIX_TEMPLATE(playmonoi2, i28)
MIX_TEMPLATE(playmonoi216, i216)
//#pragma GCC diagnostic pop

void mixqPlayChannel(int16_t *buf, uint32_t len, struct channel *chan, int quiet)
{
	uint32_t fillen=0;
	uint32_t mixlen;
	int inloop;

	void (*playrout)(int16_t *buf, uint32_t len, struct channel *chan);

	if (quiet)
	{
		playrout=playquiet;
	} else {
		if (chan->status&MIXQ_INTERPOLATE)
		{
			if (chan->status&MIXQ_INTERPOLATEMAX)
			{
				if (chan->status&MIXQ_PLAY16BIT)
				{
					playrout=playmonoi216;
				} else {
					playrout=playmonoi2;
				}
			} else {
				if (chan->status&MIXQ_PLAY16BIT)
				{
					playrout=playmonoi16;
				} else {
					playrout=playmonoi;
				}
			}
		} else {
			if (chan->status&MIXQ_PLAY16BIT)
			{
				playrout=playmono16;
			} else {
				playrout=playmono;
			}
		}
	}

mixqPlayChannel_bigloop:
	inloop=0;
	mixlen=len;

	if (chan->step)
	{
		uint32_t abs_step;       /* abs of chan->step */
		uint32_t data_left;
		uint16_t data_left_fraction;

		if (chan->step<0)
		{
			abs_step=-chan->step;
			data_left          = chan->pos;
			data_left_fraction = chan->fpos;
			if (chan->status&MIXQ_LOOPED)
			{
				if (chan->pos >= chan->loopstart)
				{
					data_left -= chan->loopstart;
					inloop = 1;
				}
			}
		} else {
			abs_step=chan->step;
			data_left = chan->length - chan->pos - (!chan->fpos);
			data_left_fraction = -chan->fpos;
			if (chan->status&MIXQ_LOOPED)
			{
				if (chan->pos < chan->loopend)
				{
					data_left -= chan->length - chan->loopend;
					inloop = 1;
				}
			}
		}
		/* data_left should now be the end of the loop envelope */
		{
			/* fprintf(stderr, "Samples available to use: %d/0x%08x\n", data_left, data_left_fraction); */
			uint64_t tmppos=((((uint64_t)data_left)<<16)|data_left_fraction)+((uint32_t)abs_step)-1;
			/* fprintf(stderr, "Samples available to use hidef: %lld/0x%012llx\n", tmppos, tmppos);*/
			if ((tmppos>>32)<abs_step)
			{/* this is the safe check to avoid overflow in div */
				uint32_t tmplen;
				tmplen=tmppos/abs_step;
				/* fprintf(stderr, "Samples at current playrate would be output into %d samples\n", tmplen); */
				if (mixlen>=tmplen)
				{
					/* fprintf(stderr, "world wants more data than we can provide, limit output\n");*/
					mixlen=tmplen;
					if (!inloop)
					{
						/* fprintf(stderr, "We are not in loop, configure fillen\n");*/
						chan->status&=~MIXQ_PLAYING;
						fillen=(len-tmplen); /* the gap that is left */
						len=mixlen;
					}
				}
			}
		}
	}
	playrout(buf, mixlen, chan);
	buf+=mixlen;
	len-=mixlen;
	{
		int64_t tmp64=((int64_t)chan->step)*mixlen + (uint16_t)chan->fpos;
		chan->fpos=tmp64&0xffff;
		chan->pos+=(tmp64>>16);
	}

	if (inloop)
	{
		int32_t mypos = chan->pos; /* eax */
		if (chan->step<0)
		{
			if (mypos>=(int32_t)chan->loopstart)
				return;
			if (!(chan->status&MIXQ_PINGPONGLOOP))
			{
				mypos+=chan->replen;
			} else {
				chan->step=-chan->step;
				if ((chan->fpos=-chan->fpos))
					mypos++;
				mypos=chan->loopstart+chan->loopstart-mypos;
			}
		} else {
			if (mypos<chan->loopend)
				return;
			if (!(chan->status&MIXQ_PINGPONGLOOP))
			{
				mypos-=chan->replen;
			} else {
				chan->step=-chan->step;
				if ((chan->fpos=-chan->fpos))
					mypos++;
				mypos=chan->loopend+chan->loopend-mypos;
			}
		}
		chan->pos=mypos;
		if (len)
			goto mixqPlayChannel_bigloop;
	}
	if (fillen)
	{
		int16_t sample;
		int count;
		chan->pos=chan->length;
		if (!(chan->status&MIXQ_PLAY16BIT))
		{
			sample=chan->realsamp.bit8[chan->pos]<<8;
		} else {
			sample=chan->realsamp.bit16[chan->pos];
		}
		for (count=0;count<fillen;count++)
			*(buf++)=sample;
	}
}

void mixqAmplifyChannel(int32_t *buf, int16_t *src, uint32_t len, int32_t vol, uint32_t step)
{
	while (len)
	{
		(*buf) += myvoltab[vol][0][(uint8_t)((*src)>>8)] + myvoltab[vol][1][(uint8_t)((*src)&0xff)];
		src++;
		len--;
		buf+=step/sizeof(uint32_t);
	}
}

void mixqAmplifyChannelUp(int32_t *buf, int16_t *src, uint32_t len, int32_t vol, uint32_t step)
{
	while (len)
	{
		(*buf) += myvoltab[vol][0][(uint8_t)((*src)>>8)] + myvoltab[vol][1][(uint8_t)((*src)&0xff)];
		src++;
		vol++;
		len--;
		buf+=step/sizeof(uint32_t);
	}
}

void mixqAmplifyChannelDown(int32_t *buf, int16_t *src, uint32_t len, int32_t vol, uint32_t step)
{
	while (len)
	{
		(*buf) += myvoltab[vol][0][(uint8_t)((*src)>>8)] + myvoltab[vol][1][(uint8_t)((*src)&0xff)];
		src++;
		vol--;
		len--;
		buf+=step/sizeof(uint32_t);
	}
}