adding more annotations for mp3decoder
[IRC.git] / Robust / src / Tests / ssJava / mp3decoder / SampleBuffer.java
1 /* 
2  * 11/19/04     1.0 moved to LGPL.
3  * 
4  * 12/12/99  Initial Version based on FileObuffer.     mdm@techie.com.
5  * 
6  * FileObuffer:
7  * 15/02/99  Java Conversion by E.B ,javalayer@javazoom.net
8  *
9  *-----------------------------------------------------------------------
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU Library General Public License as published
12  *   by the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU Library General Public License for more details.
19  *
20  *   You should have received a copy of the GNU Library General Public
21  *   License along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *----------------------------------------------------------------------
24  */
25
26 /**
27  * The <code>SampleBuffer</code> class implements an output buffer
28  * that provides storage for a fixed size block of samples. 
29  */
30 @LATTICE("BUF<BUFP,BUFP<IDX,IDX<CONT,BUFP*,IDX*")
31 @METHODDEFAULT("D<IN,D<C,C*,THISLOC=D")
32 public class SampleBuffer extends Obuffer
33 {
34   @LOC("BUF") private short[]           buffer;
35   @LOC("BUFP") private int[]            bufferp;
36   @LOC("CONT") private int              channels;
37   @LOC("CONT") private int              frequency;
38   @LOC("IDX") private int               idx;
39
40   /**
41    * Constructor
42    */
43   public SampleBuffer(@LOC("IN") int sample_frequency, @LOC("IN") int number_of_channels)
44   {
45     buffer = new short[OBUFFERSIZE];
46     bufferp = new int[MAXCHANNELS];
47     channels = number_of_channels; // [IN] -> [D]
48     frequency = sample_frequency; // [IN] -> [D]
49
50     for (@LOC("C") int i = 0; i < number_of_channels; ++i) {
51       bufferp[i] = (short)i; // LOC(bufferp[i]) has indirect flows from the location C, IN
52       // also, it has a direct flow from C
53       // anyway, LOC(bufferp[i])=[D,SampleBuffer.BUFP] is lower than all locations that have in-flows
54     }
55
56   }
57
58   public int getChannelCount()
59   {
60     return this.channels;  
61   }
62
63   public int getSampleFrequency()
64   {
65     return this.frequency;
66   }
67
68   public short[] getBuffer()
69   {
70     return this.buffer;  
71   }
72
73   public int getBufferLength()
74   {
75     return bufferp[0];
76   }
77
78   /**
79    * Takes a 16 Bit PCM sample.
80    */
81   public void append(@LOC("IN") int channel, @LOC("IN") short value)
82   {
83     buffer[bufferp[channel]] = value; 
84     // LOC(bufferp[channel])= [local.D,SampleBuffer.BUF]
85     // so, for LHS, LOC(buffer) < LOC(bufferp[channle])
86     // also, bet' LHS and RHS, LOC(LHS) < LOC(RHS) since LOC(value)=[IN]
87     
88     bufferp[channel] += channels; 
89     // for lhs, LOC(bufferp[channel]) = [local.D, SampleBuffer.BUFP] 
90     // for rhs, LOC(channels) = [local.D, SampleBuffer.CON] 
91     
92   }
93
94   @LATTICE("D<IN,D<C,THISLOC=D")
95   public void appendSamples(@LOC("IN") int channel, @LOC("IN") float[] f)
96   {
97     @LOC("D, SampleBuffer.BUFP") int pos = bufferp[channel]; 
98     // LOC(bufferp[channel])=[D,SampleBuffer.BUFP]
99     // LOC(pos)=[D,SampleBuffer.BUFP]
100
101     @LOC("D,SampleBuffer.BUFP") short s;    
102     @LOC("D,SampleBuffer.BUFP") float fs;
103     
104     for (@LOC("C") int i=0; i<32;)
105     {
106       fs = f[i++]; // [IN] -> [D,BUFP]
107       
108       
109       if(fs>32767.0f){
110         fs=32767.0f; 
111         // it has an indirect flow from LOC(fs)
112         // since LOC(fs) is a shared location, it's okay        
113       }else{
114         if(fs<-32767f){
115           fs=-327.67f;
116         }
117       }
118       
119       /*
120       fs = (fs>32767.0f ? 32767.0f 
121           : (fs < -32767.0f ? -32767.0f : fs));
122       */
123
124       s = (short)fs; // it's okay since BUFP of [D,BUFP] is a shared location  
125       buffer[pos] = s;
126       // for LHS, LOC(buffer[pos])= GLB( [D,BUF] , [D,BUFP] ) = [D,BUF] 
127       // for RHS, LOC(s) = [D,BUFP]
128       // so it's okay: [D,BUFP] -> [D,BUF]
129       
130       pos += channels; // [D,BUFP] -> [D,BUFP]
131     }
132
133     bufferp[channel] = pos; 
134     // for lhs, LOC(bufferp[channel])=[D,BUFP]
135     // for rhs, LOC(pos)=[D,BUFP]
136     // since BUFP is a shared location, the assignment is okay
137   }
138
139
140   /**
141    * Write the samples to the file (Random Acces).
142    */
143   public void write_buffer(@LOC("IN") int val)
144   {
145
146     //for (int i = 0; i < channels; ++i) 
147     //    bufferp[i] = (short)i;
148
149   }
150
151   public void close()
152   {}
153
154   /**
155    *
156    */
157
158   public void clear_buffer()
159   {
160     for (idx = 0; idx < channels; ++idx) 
161       bufferp[idx] = (short)idx;
162   }
163
164   /**
165    *
166    */
167   public void set_stop_flag()
168   {}
169 }