1 #include "common.h"
2 #include "encoder.h"
3 #include "encode.h"
4 #include "audio_read.h"
5 
6 /************************************************************************
7 *
8 * read_samples()
9 *
10 * PURPOSE:  reads the PCM samples from a file to the buffer
11 *
12 *  SEMANTICS:
13 * Reads #samples_read# number of shorts from #musicin# filepointer
14 * into #sample_buffer[]#.  Returns the number of samples read.
15 *
16 ************************************************************************/
17 
read_samples(FILE * musicin,long int * sample_buffer,long unsigned int num_samples,long unsigned int frame_size,int * byte_per_sample,int * aiff)18 unsigned long read_samples (FILE * musicin, long int *sample_buffer,
19 			    long unsigned int num_samples,
20 			    long unsigned int frame_size, int
21 			    *byte_per_sample, int *aiff)
22 {
23   unsigned long samples_read;
24   static unsigned long samples_to_read;
25   static char init = TRUE;
26   short pcm_sample_buffer[9216];	/*for correct reading of pcm-data */
27   int i;
28 
29   if (init) {
30     samples_to_read = num_samples;
31     init = FALSE;
32   }
33   if (samples_to_read >= frame_size)
34     samples_read = frame_size;
35   else
36     samples_read = samples_to_read;
37 
38   if ((*aiff == 1) && (*byte_per_sample != 2)) {
39 
40     if ((samples_read =
41 	 fread (sample_buffer, *byte_per_sample, (int) samples_read,
42 		musicin)) == 0)
43       if (verbosity >= 2)
44 	printf ("Hit end of audio data\n");
45 
46   } else {
47     if ((samples_read =
48 	 fread (pcm_sample_buffer, sizeof (short), (int) samples_read,
49 		musicin)) == 0)
50       if (verbosity >= 2)
51 	printf ("Hit end of audio data\n");
52     for (i = 0; i < 9216; ++i)
53       sample_buffer[i] = pcm_sample_buffer[i];
54   }
55 
56   samples_to_read -= samples_read;
57   if (samples_read < frame_size && samples_read > 0) {
58     if (verbosity >= 2)
59       printf ("Insufficient PCM input for one frame - fillout with zeros\n");
60     for (; samples_read < frame_size; sample_buffer[samples_read++] = 0);
61     samples_to_read = 0;
62   }
63   return (samples_read);
64 }
65 
66 
read_samples_new(FILE * musicin,long int * sample_buffer,long unsigned int num_samples,long unsigned int frame_size,int * byte_per_sample,int * aiff)67 unsigned long read_samples_new (FILE * musicin, long int *sample_buffer,
68 			    long unsigned int num_samples,
69 			    long unsigned int frame_size, int
70 			    *byte_per_sample, int *aiff)
71 {
72   unsigned long samples_read;
73   static unsigned long samples_to_read;
74   static char init = TRUE;
75   short pcm_sample_buffer[9216];	/*for correct reading of pcm-data */
76   int i;
77 
78   if (init) {
79     samples_to_read = num_samples;
80     init = FALSE;
81   }
82   if (samples_to_read >= frame_size)
83     samples_read = frame_size;
84   else
85     samples_read = samples_to_read;
86 
87 
88   if ((samples_read =
89        fread (pcm_sample_buffer, sizeof (short), (int) samples_read,
90 	      musicin)) == 0)
91     if (verbosity >= 2)
92       printf ("Hit end of audio data\n");
93   for (i = 0; i < 9216; ++i)
94     sample_buffer[i] = pcm_sample_buffer[i];
95 
96 
97   samples_to_read -= samples_read;
98   if (samples_read < frame_size && samples_read > 0) {
99     if (verbosity >= 2)
100       printf ("Insufficient PCM input for one frame - fillout with zeros\n");
101     for (; samples_read < frame_size; sample_buffer[samples_read++] = 0);
102     samples_to_read = 0;
103   }
104   return (samples_read);
105 }
106 
107 /************************************************************************
108 *
109 * get_audio()
110 *
111 * PURPOSE:  reads a frame of audio data from a file to the buffer,
112 *   aligns the data for future processing, and separates the
113 *   left and right channels
114 *
115 *  SEMANTICS:
116 * Calls read_samples() to read a frame of audio data from filepointer
117 * #musicin# to #insampl[]#.  The data is shifted to make sure the data
118 * is centered for the 1024pt window to be used by the psychoacoustic model,
119 * and to compensate for the 256 sample delay from the filter bank. For
120 * stereo, the channels are also demultiplexed into #buffer[0][]# and
121 * #buffer[1][]#
122 *
123 * 21/03/1995 JMZ Multimode adaptations
124 ************************************************************************/
125 
126 unsigned long
get_audio(FILE * musicin,double (* buffer)[1152],long unsigned int num_samples,int stereo,IFF_AIFF * aiff_ptr,int stereomc,int stereoaug,frame_params * fr_ps,int * aiff,int * byte_per_sample,double (* buffer_matr)[1152])127 get_audio (FILE * musicin,
128 	   double (*buffer)[1152],
129 	   long unsigned int num_samples,
130 	   int stereo,
131 	   IFF_AIFF * aiff_ptr,
132 	   int stereomc,
133 	   int stereoaug,
134 	   frame_params * fr_ps,
135 	   int *aiff, int *byte_per_sample, double (*buffer_matr)[1152]
136   )
137 {
138   int k, j, i;
139   long insamp[9216];
140   unsigned long samples_read;
141   int lay;
142   int lfe;
143   layer *info = fr_ps->header;
144 
145   lay = info->lay;
146   lfe = info->lfe;
147 
148  {
149    if (*aiff == 1) {
150      k = aiff_ptr->numChannels;
151      samples_read = read_samples (musicin, insamp, num_samples,
152 				  (unsigned long) (k * 1152), byte_per_sample,
153 				  aiff);
154 
155      for (i = 0; i < k; i++)
156        for (j = 0; j < 1152; j++)
157 	 buffer[i][j] = insamp[k * j + i];
158    } else {			/* layerII, stereo */
159      if (stereo == 2) {
160        samples_read = read_samples (musicin, insamp, num_samples,
161 				    (unsigned long) ((2 + lfe) * 1152),
162 				     byte_per_sample, aiff);
163 	for (j = 0; j < 1152; j++) {	/* fixed bug 28.6.93 S.R. */
164 	  buffer[0][j] = insamp[(2 + lfe) * j];
165 	  buffer[1][j] = insamp[(2 + lfe) * j + 1];
166 	  buffer[2][j] = 0;
167 	  buffer[3 + lfe][j] = 0;
168 	  buffer[4 + lfe][j] = 0;
169 	  if (lfe)
170 	    buffer[3][j] = insamp[(2 + lfe) * j + 2];	/* ########### */
171 	}
172       } else {			/* layer 2 (or 3), mono */
173 	samples_read = read_samples (musicin, insamp, num_samples,
174 				     (unsigned long) 1152, byte_per_sample,
175 				     aiff);
176 	for (j = 0; j < 1152; j++) {
177 	  buffer[0][j] = insamp[j];
178 	  buffer[1][j] = 0;
179 	  buffer[2][j] = 0;
180 	  buffer[3][j] = 0;
181 	  buffer[4][j] = 0;
182 	}
183       }
184     }
185   }
186 
187   /*
188    * If LFE is not enabled, "buffer" contains:
189    *  buffer[0]       L
190    *  buffer[1]       R
191    *  buffer[2]       C
192    *  buffer[3]       Ls
193    *  buffer[4]       Rs
194    *
195    * If LFE is enabled, "buffer" contains:
196    *  buffer[0]       L
197    *  buffer[1]       R
198    *  buffer[2]       C
199    *  buffer[3]       LFE
200    *  buffer[4]       Ls
201    *  buffer[5]       Rs
202    */
203 
204  matricing_fft (buffer, buffer_matr, fr_ps);
205 
206   /*
207    * After matrixing, "buffer_matr" contains:
208    *  buffer_matr[0]  Lo
209    *  buffer_matr[1]  Ro
210    *  buffer_matr[2]  C
211    *  buffer_matr[3]  Ls
212    *  buffer_matr[4]  Rs
213    *  buffer_matr[5]  L
214    *  buffer_matr[6]  R
215    */
216 
217   return (samples_read);
218 }
219 
220 
221 /***********************************
222 get_audio new
223 reads from 5 seperate files
224 ************************************/
225 
226 #define MAXCHANNELS 7
227 char soundfile[MAXCHANNELS][256] = {"left.pcm", "right.pcm", "centre.pcm", "left_surround.pcm", "right_surround.pcm"};
228 FILE *audioin[MAXCHANNELS];
229 int numinputs = 5;
230 
init_audio_inputs(void)231 void init_audio_inputs(void) {
232   int i;
233 
234   for (i=0;i<numinputs;i++) {
235     if ((audioin[i] = fopen(soundfile[i], "r")) == NULL) {
236       fprintf(stderr, "Can't open %s for input\n",soundfile[i]);
237       exit(99);
238     }
239   }
240 }
241 
242 unsigned long
get_audio_new(FILE * musicin,double (* buffer)[1152],long unsigned int num_samples,int stereo,IFF_AIFF * aiff_ptr,int stereomc,int stereoaug,frame_params * fr_ps,int * aiff,int * byte_per_sample,double (* buffer_matr)[1152])243 get_audio_new (FILE * musicin,
244 	   double (*buffer)[1152],
245 	   long unsigned int num_samples,
246 	   int stereo,
247 	   IFF_AIFF * aiff_ptr,
248 	   int stereomc,
249 	   int stereoaug,
250 	   frame_params * fr_ps,
251 	   int *aiff, int *byte_per_sample, double (*buffer_matr)[1152]
252   )
253 {
254   int j, i;
255   long insamp[9216];
256   unsigned long samples_read=0;
257   //MFC  int lay;
258   int lfe;
259   layer *info = fr_ps->header;
260   static int init=0;
261 
262   if (!init) {
263     init_audio_inputs();
264     init++;
265   }
266 
267 
268   lfe = info->lfe;
269 
270 #define NEWREAD
271 #ifdef NEWREAD
272   for (i=0;i<numinputs;i++) {
273     samples_read += read_samples(audioin[i], insamp, num_samples, 1152, byte_per_sample, aiff);
274     //fprintf(stdout,"%i samples read %i\n",i, samples_read);
275     for (j=0;j<1152;j++)
276       buffer[i][j] = insamp[j];
277   }
278 #else
279   if (stereo == 2) {
280     samples_read = read_samples (musicin, insamp, num_samples,
281 				 (unsigned long) ((2 + lfe) * 1152),
282 				 byte_per_sample, aiff);
283     for (j = 0; j < 1152; j++) {	/* fixed bug 28.6.93 S.R. */
284       buffer[0][j] = insamp[(2 + lfe) * j];
285       buffer[1][j] = insamp[(2 + lfe) * j + 1];
286       buffer[2][j] = 0;
287       buffer[3 + lfe][j] = 0;
288       buffer[4 + lfe][j] = 0;
289       if (lfe)
290 	buffer[3][j] = insamp[(2 + lfe) * j + 2];	/* ########### */
291     }
292   } else {			/* layer 2 (or 3), mono */
293     samples_read = read_samples (musicin, insamp, num_samples,
294 				 (unsigned long) 1152, byte_per_sample,
295 				 aiff);
296     for (j = 0; j < 1152; j++) {
297       buffer[0][j] = insamp[j];
298       buffer[1][j] = 0;
299       buffer[2][j] = 0;
300       buffer[3][j] = 0;
301       buffer[4][j] = 0;
302     }
303   }
304 #endif // NEWREAD
305   /*
306    * If LFE is not enabled, "buffer" contains:
307    *  buffer[0]       L
308    *  buffer[1]       R
309    *  buffer[2]       C
310    *  buffer[3]       Ls
311    *  buffer[4]       Rs
312    *
313    * If LFE is enabled, "buffer" contains:
314    *  buffer[0]       L
315    *  buffer[1]       R
316    *  buffer[2]       C
317    *  buffer[3]       LFE
318    *  buffer[4]       Ls
319    *  buffer[5]       Rs
320    */
321 
322     matricing_fft (buffer, buffer_matr, fr_ps);
323 
324   /*
325    * After matrixing, "buffer_matr" contains:
326    *  buffer_matr[0]  Lo
327    *  buffer_matr[1]  Ro
328    *  buffer_matr[2]  C
329    *  buffer_matr[3]  Ls
330    *  buffer_matr[4]  Rs
331    *  buffer_matr[5]  L
332    *  buffer_matr[6]  R
333    */
334 
335   return (samples_read);
336 }
337