adding a test case
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3DecoderInfer / 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 that provides
28  * storage for a fixed size block of samples.
29  */
30
31 public class SampleBuffer extends Obuffer {
32
33   private short[] buffer;
34
35   private int[] bufferp;
36
37   private int channels;
38
39   private int frequency;
40
41   private int idx;
42
43   static public long sampleNumber = 0;
44
45   /**
46    * Constructor
47    */
48   public SampleBuffer(int sample_frequency, int number_of_channels) {
49     buffer = new short[OBUFFERSIZE];
50     bufferp = new int[MAXCHANNELS];
51     channels = number_of_channels; // [IN] -> [D]
52     frequency = sample_frequency; // [IN] -> [D]
53
54     for (int i = 0; i < number_of_channels; ++i) {
55       bufferp[i] = (short) i; // LOC(bufferp[i]) has indirect flows from the
56                               // location C, IN
57       // also, it has a direct flow from C
58       // anyway, LOC(bufferp[i])=[D,SampleBuffer.BUFP] is lower than all
59       // locations that have in-flows
60     }
61
62   }
63
64   public int getChannelCount() {
65     return this.channels;
66   }
67
68   public int getSampleFrequency() {
69     return this.frequency;
70   }
71
72   public short[] getBuffer() {
73     return this.buffer;
74   }
75
76   public int getBufferLength() {
77     return bufferp[0];
78   }
79
80   /**
81    * Takes a 16 Bit PCM sample.
82    */
83   public void append(int channel, short value) {
84     buffer[bufferp[channel]] = value;
85     // LOC(bufferp[channel])= [local.D,SampleBuffer.BUF]
86     // so, for LHS, LOC(buffer) < LOC(bufferp[channle])
87     // also, bet' LHS and RHS, LOC(LHS) < LOC(RHS) since LOC(value)=[IN]
88
89     bufferp[channel] += channels;
90     // for lhs, LOC(bufferp[channel]) = [local.D, SampleBuffer.BUFP]
91     // for rhs, LOC(channels) = [local.D, SampleBuffer.CON]
92
93   }
94
95   public void appendSamples(int channel, float[] f) {
96     int pos = bufferp[channel];
97     // LOC(bufferp[channel])=[D,SampleBuffer.BUFP]
98     // LOC(pos)=[D,SampleBuffer.BUFP]
99
100     short s;
101     float fs;
102
103     for (int i = 0; i < 32;) {
104       fs = f[i++]; // [IN] -> [D,BUFP]
105
106       if (fs > 32767.0f) {
107         fs = 32767.0f;
108         // it has an indirect flow from LOC(fs)
109         // since LOC(fs) is a shared location, it's okay
110       } else {
111         if (fs < -32767.0f) {
112           fs = -32767.0f;
113         }
114       }
115
116       /*
117        * fs = (fs>32767.0f ? 32767.0f : (fs < -32767.0f ? -32767.0f : fs));
118        */
119
120       s = (short) fs; // it's okay since BUFP of [D,BUFP] is a shared location
121       buffer[pos] = s;
122
123       // DEBUG_OUTPUT(pos, s);
124
125       // for LHS, LOC(buffer[pos])= GLB( [D,BUF] , [D,BUFP] ) = [D,BUF]
126       // for RHS, LOC(s) = [D,BUFP]
127       // so it's okay: [D,BUFP] -> [D,BUF]
128
129       pos += channels; // [D,BUFP] -> [D,BUFP]
130     }
131
132     bufferp[channel] = pos;
133     // for lhs, LOC(bufferp[channel])=[D,BUFP]
134     // for rhs, LOC(pos)=[D,BUFP]
135     // since BUFP is a shared location, the assignment is okay
136   }
137
138   /**
139    * Write the samples to the file (Random Acces).
140    */
141   public void write_buffer(int val) {
142
143     // for (int i = 0; i < channels; ++i)
144     // bufferp[i] = (short)i;
145
146   }
147
148   public void close() {
149   }
150
151   /**
152    *
153    */
154
155   public void clear_buffer() {
156     for (idx = 0; idx < channels; ++idx)
157       bufferp[idx] = (short) idx;
158   }
159
160   /**
161    *
162    */
163   public void set_stop_flag() {
164   }
165
166   @TRUST
167   private void DEBUG_OUTPUT(int pos, short s) {
168     // there is left and right channel interleaved into the
169     // output buffer, so only sample one channel (stride=2)
170     if (pos % 2 == 0) {
171       System.out.println(sampleNumber + " " + s);
172       sampleNumber++;
173     }
174   }
175 }