summaryrefslogtreecommitdiffstats
path: root/FFT_Test/Copy of reverb.hcc
blob: 7d21e6deba0797a610324b476ded16fe7e0fd12b (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
/****************************************************************
*                                                               *
* Copyright (C) 1991-2003 Celoxica Ltd. All rights reserved.    *
*                                                               *
*****************************************************************
*                                                               *
* Project   :   PAL                                             *
* Date      :   31 JAN 2003                                     *
* File      :   reverb.hcc                                      *
* Author    :   Matthew Aubury (MA)                             *
* Contributors:                                                 *
*                                                               *
* Description:                                                  *
*    Simple audio reverb.                                       *
*                                                               *
* Date         Version  Author  Reason for change               *
*                                                               *
* 29 OCT 2002    1.00    MA     Created                         *
*                                                               *
****************************************************************/
#define PAL_TARGET_CLOCK_RATE 25175000
 

#include <stdlib.hch>
#include "pal_master.hch"

#include "debug.hch"
#include "fft.hch"

#include "presets.hch"
#include "config.hch"
/*
 * Forward declarations
 */

static macro proc RunFFT (AudioIn, AudioOut);
static macro expr ClockRate = PAL_ACTUAL_CLOCK_RATE;

//input buffer
ram signed 16 audio_buffer_in[256] with { block = "BlockRAM"};
//output buffer
ram signed 16 audio_buffer_out[128] with { block = "BlockRAM"};	
//EQ settings for the FFT
ram unsigned 4 EQ_info[128] with { block = "BlockRAM"};	
//EQ settings received from the display
mpram 
{
  ram unsigned 4 write[768];	
  rom unsigned 4 read[768];	
}EQ_level with { block = "BlockRAM"};	

mpram 
{
  ram unsigned 7 write[256];	
  rom unsigned 7 read[256];	
}fft_info with { block = "BlockRAM"};

signed 16 *audioptr_in1,*audioptr_in2,*audioptr_in3,*audioptr_in4;
signed 16 *audioptr_out1,*audioptr_out2;
signed 16 *audioptr_temp;

unsigned 3 active_preset;

shared expr preset_address = (0@(active_preset-1))<<7;
/*
 * Main program
 */
void main(void)
{
    macro expr AudioIn  = PalAudioInCT  (0);
    macro expr AudioOut = PalAudioOutCT (0);
    
    /*
     * Check we've got everything we need
     */
    PalVersionRequire  (1, 2);
    PalAudioInRequire  (1);
    PalAudioOutRequire (1);
    PalDataPortRequire (1);

    
    /*
     * Run
     */
    par
    {
        PalAudioInRun  (AudioIn,  ClockRate);
        PalAudioOutRun (AudioOut, ClockRate);
		PalDataPortRun (PalRS232PortCT(0), PAL_ACTUAL_CLOCK_RATE );

        
        seq
        {
            par
            {
                PalAudioInEnable  (AudioIn);
                PalAudioOutEnable (AudioOut);
		PalDataPortEnable (PalRS232PortCT(0));
			
            }
	    RC200AudioInSetInput(RC200AudioInLineIn);
	    RC200AudioInSetGain(0,6,6);
            PalAudioInSetSampleRate  (AudioIn,  44100);
            PalAudioOutSetSampleRate (AudioOut, 44100);

	    RunFFT(AudioIn, AudioOut);		
        }
    }
}

void LoadPresets()
{
  unsigned 16 temp;
  unsigned 10 count;
  unsigned 8 index;
  
  count=0;
  do
  {
    par
    {
	temp = presets[index];
	index++;
    }
    par { EQ_level.write[count] = temp[7:4]; count++; }
    par { EQ_level.write[count] = temp[3:0]; count++; }
    par { EQ_level.write[count] = temp[15:12]; count++; }
    par { EQ_level.write[count] = temp[11:8]; count++; }
  } while (count<768);

}

/*
 * FFT routine
 */
static macro proc RunFFT (AudioIn, AudioOut)
{
	signed 16 sample;
	unsigned 6 sample_count;
	unsigned 8 i;
	unsigned 4 eqinfo;

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

	active_preset = 3;

  	//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];

  	FFT_Sync=0;

	LoadPresets();
par
{
	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;
				
				FFT_Sync=0;
			}

			// FFT calculation
			perform_fft(audioptr_in1);
			
	      	/*	for(i=0;i<NUMBER_OF_FREQUENCIES;i++)	  
        		{
				eqinfo = EQ_level.read[preset_address+(0@i)];
	      			EQ_info[i<-7]= eqinfo;
		    	}
*/
			// set volume of each individual frequency bar (equalizer)
		//	equalize_audio(&EQ_level.read[preset_address], &fft_info.write[0]);
   		   	
			// inverse FFT calculation
			perform_ifft(audioptr_out1);
/*
			for (i = 0; i != 64; i++)
			{
				sample = audioptr_in1[i];
				audioptr_out1[i] = sample;
			}
*/		
    		}
		else
			delay;
	}	

	for(sample_count=0;;)//store the samples in the inputbuffer
	{
		if (!FFT_Sync)
		{
			par
			{
				seq
				{
					PalAudioInRead (AudioIn, &LeftNew, &RightNew);
 					audioptr_in1[sample_count]=(LeftNew\\2);//drop 2 LSB's
					sample_count++; 
    					if (!sample_count) 
					{
						FFT_Sync = 1; 
					}
					else
						delay;

				}
				seq
				{
					Output_sample = audioptr_out2[sample_count];
				}
			}
		}
		else
		{
			delay;
		}
	}
	for(;;)
	{
		PalAudioOutWrite(AudioOut,(signed OW)(Output_sample @ 0),(signed OW)(Output_sample @ 0));
	}
}//end par
}// end function