2 * 11/19/04 1.0 moved to LGPL.
4 * 12/12/99 Initial Version based on FileObuffer. mdm@techie.com.
7 * 15/02/99 Java Conversion by E.B ,javalayer@javazoom.net
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.
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.
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 *----------------------------------------------------------------------
27 * The <code>SampleBuffer</code> class implements an output buffer that provides
28 * storage for a fixed size block of samples.
32 public class SampleBuffer extends Obuffer {
34 private short[] buffer;
36 private int[] bufferp;
40 private int frequency;
44 static public long sampleNumber = 0;
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]
55 for ( int i = 0; i < number_of_channels; ++i) {
56 bufferp[i] = (short) i; // LOC(bufferp[i]) has indirect flows from the
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
65 public int getChannelCount() {
69 public int getSampleFrequency() {
70 return this.frequency;
73 public short[] getBuffer() {
77 public int getBufferLength() {
82 * Takes a 16 Bit PCM sample.
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]
90 bufferp[channel] += channels;
91 // for lhs, LOC(bufferp[channel]) = [local.D, SampleBuffer.BUFP]
92 // for rhs, LOC(channels) = [local.D, SampleBuffer.CON]
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]
105 for ( int i = 0; i < 32;) {
106 fs = f[i++]; // [IN] -> [D,BUFP]
110 // it has an indirect flow from LOC(fs)
111 // since LOC(fs) is a shared location, it's okay
113 if (fs < -32767.0f) {
119 * fs = (fs>32767.0f ? 32767.0f : (fs < -32767.0f ? -32767.0f : fs));
122 s = (short) fs; // it's okay since BUFP of [D,BUFP] is a shared location
125 // DEBUG_OUTPUT(pos, s);
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]
131 pos += channels; // [D,BUFP] -> [D,BUFP]
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
141 * Write the samples to the file (Random Acces).
143 public void write_buffer( int val) {
145 // for (int i = 0; i < channels; ++i)
146 // bufferp[i] = (short)i;
150 public void close() {
157 public void clear_buffer() {
158 for (idx = 0; idx < channels; ++idx)
159 bufferp[idx] = (short) idx;
165 public void set_stop_flag() {
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)
173 System.out.println(sampleNumber + " " + s);