summaryrefslogtreecommitdiffstats
path: root/Graphic_Equalizer_v1.0/src/runfft.hcc
blob: 709807080df74caa2b8845301ef8bc082d9d8202 (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
/*! \file runfft.hcc
 *
 * \section generic	This module will handle the audio I/O. It will ensure the 
 *			audiosamples are correctly buffered and fed correctly to the FFT.\n
 *			This module will also handle the output of the modified audio samples.
 *
 * \section project Project information.
 * Project Graphic Equalizer\n
 * \author M. Lauwerijssen 
 * \date 20041110
 * \version 0.1
 *
 * \section copyright Copyright
 * Copyright ©2004 Koninklijke Philips Electronics N.V. All rights reserved
 *
 * \section history Change history
 * 20041110: M. Lauwerijssen\n	Initial version
 *
 ********************************************************************/
#include <stdlib.hch>
#include "pal_master.hch"

#include "configuration.hch"
#if USE_RUNFFT
#include "audio.hch"
#include "fft.hch"

#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


#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

/*! \fn		macro proc  audio_main(AUDIOIN, AUDIOOUT);
 * \brief	Audio I/O component main.
 * 
 * \param	audiodata	Pointer to audiodata struct
 * \param	AUDIOIN		Handle to audio-input
 * \param	AUDIOOUT	Handle to audio-output
 *
 * \return	Never Returns.
 * \retval	void
 */
macro proc audio_main(audiodata, AUDIOIN, AUDIOOUT)
{
	signed 18 sample;
	unsigned 6 sample_count;
	unsigned 8 i,cycle;
	unsigned 4 eqinfo;

	unsigned 1 FFT_Sync, first;
	macro expr OW = PalAudioOutGetMaxDataWidthCT ();
	macro expr IW = PalAudioInGetMaxDataWidthCT  ();
	signed LeftNew, RightNew;
	signed Output_sample;

	ram unsigned 6 input[64];

  	//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
{
	/*
	 *	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.
	 */
	for(;;)
	{
		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;
	}	
	/*
	 *	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.
	 */
	for(sample_count=0;;)//store the samples in the inputbuffer
	{
		if (!FFT_Sync)
		{
			par
			{
				seq
				{
					PalAudioInRead(AUDIOIN, &LeftNew, &RightNew);
#if HARDWARE_MULTIPLY 					
					audioptr_in1[sample_count] = LeftNew;
#else
					audioptr_in1[sample_count] = (LeftNew\\2);//drop 2 LSB's
#endif
					sample_count++; 
    					if (!sample_count) 
					{
						FFT_Sync = 1; 
					}
				}
				seq
				{
					Output_sample = audioptr_out2[sample_count];
				}
			}
		}
		else
		{
			delay;
		}
	}
	/*
	 *	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));
	}
}//end par
}// end function
#endif