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.
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 {
34 private short[] buffer;
36 private int[] bufferp;
40 private int frequency;
47 public SampleBuffer(@LOC("IN") int sample_frequency, @LOC("IN") int number_of_channels) {
48 buffer = new short[OBUFFERSIZE];
49 bufferp = new int[MAXCHANNELS];
50 channels = number_of_channels; // [IN] -> [D]
51 frequency = sample_frequency; // [IN] -> [D]
53 for (@LOC("C") int i = 0; i < number_of_channels; ++i) {
54 bufferp[i] = (short) i; // LOC(bufferp[i]) has indirect flows from the
56 // also, it has a direct flow from C
57 // anyway, LOC(bufferp[i])=[D,SampleBuffer.BUFP] is lower than all
58 // locations that have in-flows
63 public int getChannelCount() {
67 public int getSampleFrequency() {
68 return this.frequency;
71 public short[] getBuffer() {
75 public int getBufferLength() {
80 * Takes a 16 Bit PCM sample.
82 public void append(@LOC("IN") int channel, @LOC("IN") short value) {
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]
88 bufferp[channel] += channels;
89 // for lhs, LOC(bufferp[channel]) = [local.D, SampleBuffer.BUFP]
90 // for rhs, LOC(channels) = [local.D, SampleBuffer.CON]
94 @LATTICE("D<IN,IN<C,THISLOC=D,C*")
95 public void appendSamples(@LOC("IN") int channel, @LOC("IN") float[] f) {
96 @LOC("D,SampleBuffer.BUFP") int pos = bufferp[channel];
97 // LOC(bufferp[channel])=[D,SampleBuffer.BUFP]
98 // LOC(pos)=[D,SampleBuffer.BUFP]
100 @LOC("D,SampleBuffer.BUFP") short s;
101 @LOC("D,SampleBuffer.BUFP") float fs;
103 for (@LOC("C") int i = 0; i < 32;) {
104 fs = f[i++]; // [IN] -> [D,BUFP]
108 // it has an indirect flow from LOC(fs)
109 // since LOC(fs) is a shared location, it's okay
111 if (fs < -32767.0f) {
117 * fs = (fs>32767.0f ? 32767.0f : (fs < -32767.0f ? -32767.0f : fs));
120 s = (short) fs; // it's okay since BUFP of [D,BUFP] is a shared location
122 // for LHS, LOC(buffer[pos])= GLB( [D,BUF] , [D,BUFP] ) = [D,BUF]
123 // for RHS, LOC(s) = [D,BUFP]
124 // so it's okay: [D,BUFP] -> [D,BUF]
126 pos += channels; // [D,BUFP] -> [D,BUFP]
129 bufferp[channel] = pos;
130 // for lhs, LOC(bufferp[channel])=[D,BUFP]
131 // for rhs, LOC(pos)=[D,BUFP]
132 // since BUFP is a shared location, the assignment is okay
136 * Write the samples to the file (Random Acces).
138 public void write_buffer(@LOC("IN") int val) {
140 // for (int i = 0; i < channels; ++i)
141 // bufferp[i] = (short)i;
145 public void close() {
152 public void clear_buffer() {
153 for (idx = 0; idx < channels; ++idx)
154 bufferp[idx] = (short) idx;
160 public void set_stop_flag() {