summaryrefslogtreecommitdiffstats
path: root/arm-fm-22k/lib_src/eas_fmengine.c
blob: 9c3da66d9797050f106261ed39c1db6fc6c5f27e (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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*----------------------------------------------------------------------------
 *
 * File:
 * eas_fmengine.c
 *
 * Contents and purpose:
 * Implements the low-level FM synthesizer functions.
 *
 * Copyright Sonic Network Inc. 2004, 2005

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *----------------------------------------------------------------------------
 * Revision Control:
 *   $Revision: 795 $
 *   $Date: 2007-08-01 00:14:45 -0700 (Wed, 01 Aug 2007) $
 *----------------------------------------------------------------------------
*/

/* includes */
#include "eas_types.h"
#include "eas_math.h"
#include "eas_audioconst.h"
#include "eas_fmengine.h"

#if defined(EAS_FM_SYNTH) || defined(EAS_HYBRID_SYNTH) || defined(EAS_SPLIT_HYBRID_SYNTH) || defined(EAS_SPLIT_FM_SYNTH)
#include "eas_data.h"
#endif

/* externals */
extern const EAS_I16 sineTable[];
extern const EAS_U8 fmScaleTable[16];

// saturation constants for 32-bit to 16-bit conversion
#define _EAS_MAX_OUTPUT 32767
#define _EAS_MIN_OUTPUT -32767

static S_FM_ENG_VOICE voices[NUM_FM_VOICES];

/* local prototypes */
void FM_SynthMixVoice (S_FM_ENG_VOICE *p,  EAS_U16 gainTarget, EAS_I32 numSamplesToAdd, EAS_PCM *pInputBuffer, EAS_I32 *pBuffer);

/* used in development environment */
#if defined(_SATURATION_MONITOR)
static EAS_BOOL bSaturated = EAS_FALSE;

/*----------------------------------------------------------------------------
 * FM_CheckSaturation()
 *----------------------------------------------------------------------------
 * Purpose:
 * Allows the sound development tool to check for saturation at the voice
 * level. Useful for tuning the level controls.
 *
 * Inputs:
 *
 * Outputs:
 * Returns true if saturation has occurred since the last time the function
 * was called.
 *
 * Side Effects:
 * Resets the saturation flag
 *----------------------------------------------------------------------------
*/
EAS_BOOL FM_CheckSaturation ()
{
	EAS_BOOL bTemp;
    bTemp = bSaturated;
    bSaturated = EAS_FALSE;
    return bTemp;
}
#endif

/*----------------------------------------------------------------------------
 * FM_Saturate()
 *----------------------------------------------------------------------------
 * Purpose:
 * This inline function saturates a 32-bit number to 16-bits
 *
 * Inputs:
 * psEASData - pointer to overall EAS data structure
 *
 * Outputs:
 * Returns a 16-bit integer
 *----------------------------------------------------------------------------
*/
EAS_INLINE EAS_I16 FM_Saturate (EAS_I32 nValue)
{
	if (nValue > _EAS_MAX_OUTPUT)
	{
#if defined(_SATURATION_MONITOR)
		bSaturated = EAS_TRUE;
#endif
		return _EAS_MAX_OUTPUT;
	}
	if (nValue < _EAS_MIN_OUTPUT)
	{
#if defined(_SATURATION_MONITOR)
		bSaturated = EAS_TRUE;
#endif
		return _EAS_MIN_OUTPUT;
	}
	return (EAS_I16) nValue;
}

/*----------------------------------------------------------------------------
 * FM_Noise()
 *----------------------------------------------------------------------------
 * Purpose:
 * A 31-bit low-cost linear congruential PRNG algorithm used to
 * generate noise.
 *
 * Inputs:
 * pnSeed - pointer to 32-bit PRNG seed
 *
 * Outputs:
 * Returns a 16-bit integer
 *----------------------------------------------------------------------------
*/
EAS_INLINE EAS_I16 FM_Noise (EAS_U32 *pnSeed)
{
	*pnSeed = *pnSeed * 214013L + 2531011L;
	return (EAS_I16) ((*pnSeed >> 15) & 0xffff);
}

/*----------------------------------------------------------------------------
 * FM_PhaseInc()
 *----------------------------------------------------------------------------
 * Purpose:
 * Transform pitch cents to linear phase increment
 *
 * Inputs:
 * nCents - 	measured in cents
 * psEASData - pointer to overall EAS data structure
 *
 * Outputs:
 * nResult - int.frac result (where frac has NUM_DENTS_FRAC_BITS)
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_I32 FM_PhaseInc (EAS_I32 nCents)
{
	EAS_I32 nDents;
	EAS_I32 nExponentInt, nExponentFrac;
	EAS_I32 nTemp1, nTemp2;
	EAS_I32	nResult;

	/* convert cents to dents */
	nDents = FMUL_15x15(nCents, CENTS_TO_DENTS);
	nExponentInt = GET_DENTS_INT_PART(nDents) + (32 - SINE_TABLE_SIZE_IN_BITS - NUM_EG1_FRAC_BITS);
	nExponentFrac = GET_DENTS_FRAC_PART(nDents);

	/* implement 2^(fracPart) as a power series */
	nTemp1 = GN2_TO_X2 + MULT_DENTS_COEF(nExponentFrac, GN2_TO_X3);
	nTemp2 = GN2_TO_X1 + MULT_DENTS_COEF(nExponentFrac, nTemp1);
	nTemp1 = GN2_TO_X0 + MULT_DENTS_COEF(nExponentFrac, nTemp2);

	/*
	implement 2^(intPart) as 
	a left shift for intPart >= 0 or
	a left shift for intPart <  0
	*/
	if (nExponentInt >= 0)
	{
		/* left shift for positive exponents */
		/*lint -e{703} <avoid multiply for performance>*/
		nResult = nTemp1 << nExponentInt;
	}
	else
	{
		/* right shift for negative exponents */
		nExponentInt = -nExponentInt;
		nResult = nTemp1 >> nExponentInt;
	}

	return nResult;
}

#if	(NUM_OUTPUT_CHANNELS == 2)
/*----------------------------------------------------------------------------
 * FM_CalculatePan()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Assign the left and right gain values corresponding to the given pan value.
 *
 * Inputs: 
 * psVoice - ptr to the voice we have assigned for this channel
 * psArticulation - ptr to this voice's articulation
 * psEASData - pointer to overall EAS data structure
 * 
 * Outputs:
 *
 * Side Effects:
 * the given voice's m_nGainLeft and m_nGainRight are assigned
 *----------------------------------------------------------------------------
*/
static void FM_CalculatePan (EAS_I16 pan, EAS_U16 *pGainLeft, EAS_U16 *pGainRight)
{
	EAS_I32	nTemp;
	EAS_INT nNetAngle;

	/*
	Implement the following
	sin(x) = (2-4*c)*x^2 + c + x
	cos(x) = (2-4*c)*x^2 + c - x

	  where  c = 1/sqrt(2)
	using the a0 + x*(a1 + x*a2) approach
	*/

	/*
	Get the Midi CC10 pan value for this voice's channel
	convert the pan value to an "angle" representation suitable for
	our sin, cos calculator. This representation is NOT necessarily the same
	as the transform in the GM manuals because of our sin, cos calculator.
	"angle" = (CC10 - 64)/128
	*/
	/*lint -e{703} <avoid multiply for performance reasons>*/
	nNetAngle = ((EAS_I32) pan) << (NUM_EG1_FRAC_BITS -7);

	/* calculate sin */
	nTemp = EG1_ONE + FMUL_15x15(COEFF_PAN_G2, nNetAngle);
	nTemp = COEFF_PAN_G0 + FMUL_15x15(nTemp, nNetAngle);
	
	if (nTemp > SYNTH_FULL_SCALE_EG1_GAIN)
		nTemp = SYNTH_FULL_SCALE_EG1_GAIN;
	else if (nTemp < 0)
		nTemp = 0;

	*pGainRight = (EAS_U16) nTemp;

	/* calculate cos */
	nTemp = -EG1_ONE + FMUL_15x15(COEFF_PAN_G2, nNetAngle);
	nTemp = COEFF_PAN_G0 + FMUL_15x15(nTemp, nNetAngle);
	
	if (nTemp > SYNTH_FULL_SCALE_EG1_GAIN)
		nTemp = SYNTH_FULL_SCALE_EG1_GAIN;
	else if (nTemp < 0)
		nTemp = 0;
	
	*pGainLeft = (EAS_U16) nTemp;
}
#endif /* #if (NUM_OUTPUT_CHANNELS == 2) */

/*----------------------------------------------------------------------------
 * FM_Operator()
 *----------------------------------------------------------------------------
 * Purpose:
 * Synthesizes a buffer of samples based on passed parameters.
 *
 * Inputs:
 * nNumSamplesToAdd - number of samples to synthesize
 * psEASData - pointer to overall EAS data structure
 *
 * Outputs:
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
void FM_Operator (
		S_FM_ENG_OPER *p,
		EAS_I32 numSamplesToAdd,
		EAS_PCM *pBuffer,
		EAS_PCM *pModBuffer,
		EAS_BOOL mix,
		EAS_U16 gainTarget,
		EAS_I16 pitch,
		EAS_U8 feedback, 
		EAS_I16 *pLastOutput)
{
	EAS_I32 gain;
	EAS_I32 gainInc;
	EAS_U32 phase;
	EAS_U32 phaseInc;
	EAS_U32 phaseTemp;
	EAS_I32 temp;
	EAS_I32 temp2;

	/* establish local gain variable */
	gain = (EAS_I32) p->gain << 16;

	/* calculate gain increment */
	/*lint -e{703} use shift for performance */
	gainInc = ((EAS_I32) gainTarget - (EAS_I32) p->gain) << (16 - SYNTH_UPDATE_PERIOD_IN_BITS);

	/* establish local phase variables */
	phase = p->phase;

	/* calculate the new phase increment */
	phaseInc = (EAS_U32) FM_PhaseInc(pitch);

	/* restore final output from previous frame for feedback loop */
	if (pLastOutput)
		temp = *pLastOutput;
	else
		temp = 0;

	/* generate a buffer of samples */
	while (numSamplesToAdd--)
	{

		/* incorporate modulation */
		if (pModBuffer)
		{
			/*lint -e{701} use shift for performance */
			temp = *pModBuffer++ << FM_MODULATOR_INPUT_SHIFT;
		}
		
		/* incorporate feedback */
		else
		{
			/*lint -e{703} use shift for performance */
			temp = (temp * (EAS_I32) feedback) << FM_FEEDBACK_SHIFT;
		}
		
		/*lint -e{737} <use this behavior to avoid extra mask step> */
		phaseTemp = phase + temp;

		/* fetch sample from wavetable */
		temp = sineTable[phaseTemp >> (32 - SINE_TABLE_SIZE_IN_BITS)];

		/* increment operator phase */
		phase += phaseInc;

		/* internal gain for modulation effects */
		temp = FMUL_15x15(temp, (gain >> 16));

		/* output gain calculation */
		temp2 = FMUL_15x15(temp, p->outputGain);

		/* saturating add to buffer */
		if (mix)
		{
			temp2 += *pBuffer;
			*pBuffer++ = FM_Saturate(temp2);
		}
		
		/* output to buffer */
		else
			*pBuffer++ = (EAS_I16) temp2;

		/* increment gain */
		gain += gainInc;

	}

	/* save phase and gain */
	p->phase = phase;
	p->gain = gainTarget;

	/* save last output for feedback in next frame */
	if (pLastOutput)
		*pLastOutput = (EAS_I16) temp;
}

/*----------------------------------------------------------------------------
 * FM_NoiseOperator()
 *----------------------------------------------------------------------------
 * Purpose:
 * Synthesizes a buffer of samples based on passed parameters.
 *
 * Inputs:
 * nNumSamplesToAdd - number of samples to synthesize
 * psEASData - pointer to overall EAS data structure
 *
 * Outputs:
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
void FM_NoiseOperator (
		S_FM_ENG_OPER *p,
		EAS_I32 numSamplesToAdd,
		EAS_PCM *pBuffer,
		EAS_BOOL mix,
		EAS_U16 gainTarget,
		EAS_U8 feedback, 
		EAS_I16 *pLastOutput)
{
	EAS_I32 gain;
	EAS_I32 gainInc;
	EAS_U32 phase;
	EAS_I32 temp;
	EAS_I32 temp2;

	/* establish local gain variable */
	gain = (EAS_I32) p->gain << 16;

	/* calculate gain increment */
	/*lint -e{703} use shift for performance */
	gainInc = ((EAS_I32) gainTarget - (EAS_I32) p->gain) << (16 - SYNTH_UPDATE_PERIOD_IN_BITS);

	/* establish local phase variables */
	phase = p->phase;

	/* establish local phase variables */
	phase = p->phase;

	/* recall last sample for filter Z-1 term */
	temp = 0;
	if (pLastOutput)
		temp = *pLastOutput;

	/* generate a buffer of samples */
	while (numSamplesToAdd--)
	{

		/* if using filter */
		if (pLastOutput)
		{
			/* use PRNG for noise */
			temp2 = FM_Noise(&phase);
			
			/*lint -e{704} use shift for performance */
			temp += ((temp2 -temp) * feedback) >> 8;
		}
		else
		{
			temp = FM_Noise(&phase);
		}
		
		/* internal gain for modulation effects */
		temp2 = FMUL_15x15(temp, (gain >> 16));

		/* output gain calculation */
		temp2 = FMUL_15x15(temp2, p->outputGain);

		/* saturating add to buffer */
		if (mix)
		{
			temp2 += *pBuffer;
			*pBuffer++ = FM_Saturate(temp2);
		}
		
		/* output to buffer */
		else
			*pBuffer++ = (EAS_I16) temp2;

		/* increment gain */
		gain += gainInc;

	}

	/* save phase and gain */
	p->phase = phase;
	p->gain = gainTarget;
	
	/* save last output for feedback in next frame */
	if (pLastOutput)
		*pLastOutput = (EAS_I16) temp;
}

/*----------------------------------------------------------------------------
 * FM_ConfigVoice()
 *----------------------------------------------------------------------------
 * Purpose:
 * Receives parameters to start a new voice.
 *
 * Inputs:
 * voiceNum		- voice number to start
 * vCfg			- configuration data
 * pMixBuffer	- pointer to host supplied buffer
 *
 * Outputs:
 *
 * Side Effects:
 *
 * Notes:
 * pFrameBuffer is not used in the test version, but is passed as a
 * courtesy to split architecture implementations. It can be used as
 * as pointer to the interprocessor communications buffer when the
 * synthesis parameters are passed off to a DSP for synthesis.
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pFrameBuffer) pFrameBuffer not used in test version - see above */
void FM_ConfigVoice (EAS_I32 voiceNum, S_FM_VOICE_CONFIG *vCfg, EAS_FRAME_BUFFER_HANDLE pFrameBuffer)
{
	S_FM_ENG_VOICE *pVoice;
	EAS_INT i;

	/* establish pointer to voice data */
	pVoice = &voices[voiceNum];

	/* save data */
	pVoice->feedback = vCfg->feedback;
	pVoice->flags = vCfg->flags;
	pVoice->voiceGain = vCfg->voiceGain;

	/* initialize Z-1 terms */
	pVoice->op1Out = 0;
	pVoice->op3Out = 0;

	/* initialize operators */
	for (i = 0; i < 4; i++)
	{
		/* save operator data */
		pVoice->oper[i].gain = vCfg->gain[i];
		pVoice->oper[i].outputGain = vCfg->outputGain[i];
		pVoice->oper[i].outputGain = vCfg->outputGain[i];

		/* initalize operator */
		pVoice->oper[i].phase = 0;
	}

	/* calculate pan */
#if NUM_OUTPUT_CHANNELS == 2
	FM_CalculatePan(vCfg->pan, &pVoice->gainLeft, &pVoice->gainRight);
#endif	
}

/*----------------------------------------------------------------------------
 * FM_ProcessVoice()
 *----------------------------------------------------------------------------
 * Purpose:
 * Synthesizes a buffer of samples based on calculated parameters.
 *
 * Inputs:
 * nNumSamplesToAdd - number of samples to synthesize
 * psEASData - pointer to overall EAS data structure
 *
 * Outputs:
 *
 * Side Effects:
 *
 * Notes:
 * pOut is not used in the test version, but is passed as a
 * courtesy to split architecture implementations. It can be used as
 * as pointer to the interprocessor communications buffer when the
 * synthesis parameters are passed off to a DSP for synthesis.
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pOut) pOut not used in test version - see above */
void FM_ProcessVoice (
		EAS_I32 voiceNum,
		S_FM_VOICE_FRAME *pFrame,
		EAS_I32 numSamplesToAdd, 
		EAS_PCM *pTempBuffer, 
		EAS_PCM *pBuffer,
		EAS_I32 *pMixBuffer,
		EAS_FRAME_BUFFER_HANDLE pFrameBuffer)
{
	S_FM_ENG_VOICE *p;
	EAS_PCM *pOutBuf;
	EAS_PCM *pMod;
	EAS_BOOL mix;
	EAS_U8 feedback1; 
	EAS_U8 feedback3;
	EAS_U8 mode;

	/* establish pointer to voice data */
	p = &voices[voiceNum];
	mode = p->flags & 0x07;

	/* lookup feedback values */
	feedback1 = fmScaleTable[p->feedback >> 4];
	feedback3 = fmScaleTable[p->feedback & 0x0f];

	/* operator 3 is on output bus in modes 0, 1, and 3 */	
	if ((mode == 0) || (mode == 1) || (mode == 3))
		pOutBuf = pBuffer;
	else
		pOutBuf = pTempBuffer;
	
	if (p->flags & FLAG_FM_ENG_VOICE_OP3_NOISE)
	{
		FM_NoiseOperator(
				p->oper + 2,
				numSamplesToAdd,
				pOutBuf,
				EAS_FALSE,
				pFrame->gain[2],
				feedback3,
				&p->op3Out);
	}
	else
	{
		FM_Operator(
				p->oper + 2,
				numSamplesToAdd,
				pOutBuf,
				0,
				EAS_FALSE,
				pFrame->gain[2],
				pFrame->pitch[2],
				feedback3,
				&p->op3Out);
	}

	/* operator 4 is on output bus in modes 0, 1, and 2 */
	if (mode < 3)
		pOutBuf = pBuffer;
	else
		pOutBuf = pTempBuffer;

	/* operator 4 is modulated in modes 2, 4, and 5 */
	if ((mode == 2) || (mode == 4) || (mode == 5))
		pMod = pTempBuffer;
	else
		pMod = 0;
	
	/* operator 4 is in mix mode in modes 0 and 1 */
	mix = (mode < 2);

	if (p->flags & FLAG_FM_ENG_VOICE_OP4_NOISE)
	{
		FM_NoiseOperator(
				p->oper + 3,
				numSamplesToAdd,
				pOutBuf,
				mix,
				pFrame->gain[3],
				0,
				0);
	}
	else
	{
		FM_Operator(
				p->oper + 3,
				numSamplesToAdd,
				pOutBuf,
				pMod,
				mix,
				pFrame->gain[3],
				pFrame->pitch[3],
				0,
				0);
	}

	/* operator 1 is on output bus in mode 0 */
	if (mode == 0)
		pOutBuf = pBuffer;
	else
		pOutBuf = pTempBuffer;

	/* operator 1 is modulated in modes 3 and 4 */
	if ((mode == 3) || (mode == 4))
		pMod = pTempBuffer;
	else
		pMod = 0;
	
	/* operator 1 is in mix mode in modes 0 and 5 */
	mix = ((mode == 0) || (mode == 5));

	if (p->flags & FLAG_FM_ENG_VOICE_OP1_NOISE)
	{
		FM_NoiseOperator(
				p->oper,
				numSamplesToAdd,
				pOutBuf,
				mix,
				pFrame->gain[0],
				feedback1,
				&p->op1Out);
	}
	else
	{
		FM_Operator(
				p->oper,
				numSamplesToAdd,
				pOutBuf,
				pMod,
				mix,
				pFrame->gain[0],
				pFrame->pitch[0],
				feedback1,
				&p->op1Out);
	}

	/* operator 2 is modulated in all modes except 0 */
	if (mode != 0)
		pMod = pTempBuffer;
	else
		pMod = 0;
	
	/* operator 1 is in mix mode in modes 0 -3 */
	mix = (mode < 4);
	
	if (p->flags & FLAG_FM_ENG_VOICE_OP2_NOISE)
	{
		FM_NoiseOperator(
				p->oper + 1,
				numSamplesToAdd,
				pBuffer,
				mix,
				pFrame->gain[1],
				0,
				0);
	}
	else
	{
		FM_Operator(
				p->oper + 1,
				numSamplesToAdd,
				pBuffer,
				pMod,
				mix,
				pFrame->gain[1],
				pFrame->pitch[1],
				0,
				0);
	}

	/* mix voice output to synthesizer output buffer */
	FM_SynthMixVoice(p, pFrame->voiceGain, numSamplesToAdd, pBuffer, pMixBuffer);
}

/*----------------------------------------------------------------------------
 * FM_SynthMixVoice()
 *----------------------------------------------------------------------------
 * Purpose:
 * Mixes the voice output buffer into the final mix using an anti-zipper
 * filter.
 *
 * Inputs:
 * nNumSamplesToAdd - number of samples to synthesize
 * psEASData - pointer to overall EAS data structure
 *
 * Outputs:
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
void FM_SynthMixVoice(S_FM_ENG_VOICE *p,  EAS_U16 nGainTarget, EAS_I32 numSamplesToAdd, EAS_PCM *pInputBuffer, EAS_I32 *pBuffer)
{
	EAS_I32 nGain;
	EAS_I32 nGainInc;
	EAS_I32 nTemp;

	/* restore previous gain */
	/*lint -e{703} <use shift for performance> */
	nGain = (EAS_I32) p->voiceGain << 16;

	/* calculate gain increment */
	/*lint -e{703} <use shift for performance> */
	nGainInc = ((EAS_I32) nGainTarget - (EAS_I32) p->voiceGain) << (16 - SYNTH_UPDATE_PERIOD_IN_BITS);

	/* mix the output buffer */
	while (numSamplesToAdd--)
	{
		/* output gain calculation */
		nTemp = *pInputBuffer++;
		
		/* sum to output buffer */
#if (NUM_OUTPUT_CHANNELS == 2)

		/*lint -e{704} <use shift for performance> */
		nTemp = ((EAS_I32) nTemp * (nGain >> 16)) >> FM_GAIN_SHIFT;

		/*lint -e{704} <use shift for performance> */
		{
			EAS_I32 nTemp2;
			nTemp = nTemp >> FM_STEREO_PRE_GAIN_SHIFT;
			nTemp2 = (nTemp * p->gainLeft) >> FM_STEREO_POST_GAIN_SHIFT;
			*pBuffer++ += nTemp2;
			nTemp2 = (nTemp * p->gainRight) >> FM_STEREO_POST_GAIN_SHIFT;
			*pBuffer++ += nTemp2;
		}
#else
		/*lint -e{704} <use shift for performance> */
		nTemp = ((EAS_I32) nTemp * (nGain >> 16)) >> FM_MONO_GAIN_SHIFT;
		*pBuffer++ += nTemp;
#endif

		/* increment gain for anti-zipper filter */
		nGain += nGainInc;
	}

	/* save gain */
	p->voiceGain = nGainTarget;
}