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