summaryrefslogtreecommitdiffstats
path: root/Graphic_Equalizer/src/audio.hcc
blob: 8722ce7b59800d22b948ee17cfd6d62f8951bf42 (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
/*! @file audio.hcc
 *
 * @section generic Audio init and main loop
 *
 * @section project Project information.
 * Project Graphic Equalizer\n
 * @author O.M. Schinagl
 * @date 20041011
 * @version 0.1
 *
 * @section copyright Copyright
 * Copyright ©2004 Koninklijke Philips Electronics N.V. All rights reserved
 *
 * @section history Change history
 * 20041011: \n	Initial version
 *
 ********************************************************************/
#include <stdlib.hch>
#include "pal_master.hch"

#include "configuration.hch"
#include "smartmedia_shared.hch"
#include "display_shared.hch"
#if USE_RUNFFT
#include "audio.hch"
#include "fft.hch"
#include "smartmedia.hch"
#endif
#if HAVE_DEBUG
	#include "debug.hch"
#endif

/*
 * Forward declarations
 */
static macro expr ClockRate = PAL_ACTUAL_CLOCK_RATE;
#if HARDWARE_MULTIPLY
//input buffer
ram signed 18 audio_buffer_in[256] with { block = "BlockRAM"};
//output buffer
ram signed 18 audio_buffer_out[128] with { block = "BlockRAM"};	
#else
//input buffer
ram signed 16 audio_buffer_in[256] with { block = "BlockRAM"};
//output buffer
ram signed 16 audio_buffer_out[128] with { block = "BlockRAM"};	
#endif
//EQ settings for the FFT
ram unsigned 4 EQ_info[128] with { block = "BlockRAM"};	
//EQ settings received from the display

chan 1 event_notification;

#if HARDWARE_MULTIPLY
signed 18 *audioptr_in1,*audioptr_in2,*audioptr_in3,*audioptr_in4;

signed 18 *audioptr_out1,*audioptr_out2;

unsigned 6 *displayptr1,*displayptr2,*displayptr3,*displayptr4;
#else
signed 16 *audioptr_in1,*audioptr_in2,*audioptr_in3,*audioptr_in4;

signed 16 *audioptr_out1,*audioptr_out2;

unsigned 6 *displayptr1,*displayptr2,*displayptr3,*displayptr4;
#endif

unsigned 1 FFT_Sync;
signed Output_sample;

/*! \fn		macro proc	audio_init(gain_level, input_source, sample_rate, AUDIOIN, AUDIOOUT)
 *
 * \brief	Set some inital values to the audio hardware.
 *
 * \param	gain_level	Set the input amplifier to this amplification
 *				level.
 * \param	input_source	Choose between microphone input or linein
 *				input.
 * \param	sample_rate	Set the sample rate between 8000
 *					and 48000
 *
 * \return	void
 * \retval	void
 *
 */
macro proc audio_init(input_source, sample_rate, AUDIOIN, AUDIOOUT) {
	/*
	 * We simply call the appropiate handlers and pass values along.
	 * We have volume control to do this. Input and Output sampling rates
	 * are equal. We dont' need different rates.
	 */
	
	RC200AudioInSetInput(input_source);
	PalAudioInSetSampleRate(AUDIOIN, sample_rate);
	PalAudioOutSetSampleRate(AUDIOOUT, sample_rate);
} /* --- audio_init() --- */


#if !USE_RUNFFT
/*! /fn		macro proc audio_main(audiodata, AUDIOIN, AUDIOOUT);
 *
 * /brief	Main audiodriver. This function never returns! It copies the audio
 *		input directly to the audio output
 *
 * /param	*audiodata	pointer to audio information structure.
 * /param	AUDIOIN		Audio Input Handler
 * /param	AUDIOOUT	Audio Output Handler
 *
 * /return	Never Returns.
 * /retval	void
 */
macro proc audio_main(audiodata, AUDIOIN, AUDIOOUT) {
	/*
	 * Determin the data width for the current platform.
	 */
	macro expr IW = PalAudioInGetMaxDataWidthCT();
	macro expr OW = PalAudioOutGetMaxDataWidthCT();

	signed IW sample_left_in, sample_right_in;
	signed OW sample_left_out, sample_right_out;

	while (TRUE) {
		PalAudioInRead(AUDIOIN, &sample_left_in, &sample_right_in);
		PalAudioOutWrite(AUDIOOUT, (signed OW)(sample_left_in @ 0), (signed OW)(sample_right_in @ 0));
	}
} /* --- audio_main() --- */
#else

/*! /fn		macro proc audio_main(audiodata, AUDIOIN, AUDIOOUT);
 *
 * /brief	Main audiodriver. This function never returns! It calls the
 *		audiohandlers and stores samples into a global array. Once 64
 *		Samples are collected it raises a signal AUDIO_READY to let
 *		other processes know it's ready. We use quadruple buffering for
 *		audio input and double buffering for audio output.
 *
 * /param	audiodata	pointer to audio information structure.
 * /param	AUDIOIN		Audio Input Handler
 * /param	AUDIOOUT	Audio Output Handler
 *
 * /return	Never Returns.
 * /retval	void
 */
macro proc audio_main(audiodata, AUDIOIN, AUDIOOUT)
{
  	//pointers for double and quadruple buffering:
  	audioptr_in1 = &audio_buffer_in[0];
  	audioptr_in2 = &audio_buffer_in[64];
  	audioptr_in3 = &audio_buffer_in[128];
  	audioptr_in4 = &audio_buffer_in[192];

  	audioptr_out1 = &audio_buffer_out[0];
  	audioptr_out2 = &audio_buffer_out[64];

	displayptr1 = &audiodata.ifft_info.write[0];
	displayptr2 = &audiodata.ifft_info.write[64];
	displayptr3 = &audiodata.ifft_info.write[128];
	displayptr4 = &audiodata.ifft_info.write[192];

  	FFT_Sync=0;
	par
	{
		run_fft(audiodata);
		sample_audio(audiodata, AUDIOOIN);
		output_audio(AUDIOOOUT);
	}//end par
} /* --- audio_main() --- */
#endif

/*! /fn		macro proc run_fft(audiodata);
 *
 * /brief	FFT loop, waits until 64 samples are read from the audio input
 *		before switching the pointers needed for double and quadruple buffering,
 *		after that sequentially calling the perform_fft, equalize_audio and
 *		perform_ifft functions.
 *
 * /param	audiodata	pointer to audio information structure.
 *
 * /return	Never Returns.
 * /retval	void
 */
macro proc run_fft(audiodata)
{

	while(TRUE)
	{
		if (FFT_Sync)	//if 64 samples are read from ADC...
		{
			par
			{
				// switch pointers 
	    			audioptr_in1 = audioptr_in2;
				audioptr_in2 = audioptr_in3;
				audioptr_in3 = audioptr_in4;
				audioptr_in4 = audioptr_in1;
				
				audioptr_out1 = audioptr_out2;
				audioptr_out2 = audioptr_out1;

				displayptr1=displayptr2;
				displayptr2=displayptr3;
				displayptr3=displayptr4;
				displayptr4=displayptr1;
				
				
			}
		
			FFT_Sync = 0;
			// FFT calculation
			perform_fft(audioptr_in1);
			
#if PERFORM_FFT_CALCULATION
			equalize_audio(&audiodata);
#endif
   			// inverse FFT calculation
			perform_ifft(audioptr_out1,displayptr1);		
    		}
		else
			delay;
	}	
} /* --- run_fft() --- */

/*! /fn		macro proc sample_audio(ADUIOIN);
 *
 * /brief	Sampling loop, fills the audio input and output arrays and uses FFT_Sync
 *	 	to notify the FFT when 64 samples are read from the audio input.
 *
 * /param	audiodata	pointer to audio information structure.
 *
 * /return	Never Returns.
 * /retval	void
 */
macro proc sample_audio(audiodata, AUDIOOIN)
{
	macro expr IW = PalAudioInGetMaxDataWidthCT();
	signed IW LeftNew, RightNew;

	unsigned 27 blockcount;
	unsigned 8 not_saturated;
	unsigned 6 sample_count;
	unsigned 2 saturation;
	unsigned 1 result;
	
	par {
		while (TRUE)
		{
			if (FFT_Sync && audiodata.play)	//if 64 samples are read from ...
			{
				result = load_audio_samples(audioptr_in1, blockcount, WINDOW_SIZE);
				if (result) {
					blockcount = 0;
				} else {
					blockcount++;
				}
			}
			else
			{
				delay;
			}
		}
		while (TRUE)//store the samples in the inputbuffer
		{
			if (!FFT_Sync)
			{
				par
				{
					seq
					{
						PalAudioInRead(AUDIOIN, &LeftNew, &RightNew);
						
						if (!audiodata.play)
						{
#if HARDWARE_MULTIPLY 					
							audioptr_in1[sample_count] = LeftNew;
#else
							audioptr_in1[sample_count] = (LeftNew\\2);//drop 2 LSB's
#endif
							if (LeftNew > 130000) {
								saturation++;
								if (!saturation) {
									audiodata.saturated = TRUE;
									event_notification ! TRUE;
								} else {
									audiodata.saturated = FALSE;
								}
							} else {
								not_saturated++;
								if (!not_saturated) {
									audiodata.saturated = FALSE;
									saturation = 0;
									event_notification ! TRUE;
								}
							}
						}
						sample_count++;
	    					if (!sample_count) 
						{
							FFT_Sync = TRUE;
						}
					}
					seq
					{
						Output_sample = audioptr_out2[sample_count];
					}
				}
			}
			else
			{
				delay;
			}
		}
	}
}/* --- sample_audio() --- */

/*! /fn		macro proc output_audio(ADUIOOUT);
 *
 * /brief	Sampling loop, fills the audio input and output arrays and uses FFT_Sync
 *	 	to notify the FFT when 64 samples are read from the audio input.
 *
 * /param	audiodata	pointer to audio information structure.
 *
 * /return	Never Returns.
 * /retval	void
 */
macro proc output_audio(AUDIOOUT)
{
	macro expr OW = PalAudioOutGetMaxDataWidthCT();
	/*
	 *	Audio output loop, writes the modified audio samples to the audio output.	
	 */
	for(;;)
	{
		PalAudioOutWrite(AUDIOOUT,(signed OW)(Output_sample @ 0),(signed OW)(Output_sample @ 0));
	}
}/* --- output_audio() --- */