JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
SoundBuffer.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistribution of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 */
32
33package com.jogamp.audio.windows.waveout;
34
35import java.io.*;
36
37class SoundBuffer {
38 private final byte[] data;
39 private final boolean needsByteSwap;
40 private int numBytes;
41 private final int bytesPerSample;
42 private int numSamples;
43 private boolean playing;
44 private boolean empty;
45
46 // Note: needsByteSwap argument makes assumptions about the format
47 SoundBuffer(final int size, final int bytesPerSample, final boolean needsByteSwap) {
48 this.bytesPerSample = bytesPerSample;
49 this.needsByteSwap = needsByteSwap;
50 data = new byte[size * bytesPerSample];
51 empty = true;
52 }
53
54 boolean playing() {
55 return playing;
56 }
57
58 void playing(final boolean playing) {
59 this.playing = playing;
60 }
61
62 boolean empty() {
63 return empty;
64 }
65
66 void empty(final boolean empty) {
67 this.empty = empty;
68 }
69
70 void fill(final InputStream input) throws IOException {
71 synchronized(this) {
72 if (playing) {
73 throw new IllegalStateException("Can not fill a buffer that is playing");
74 }
75 }
76
77 empty(true);
78 final int num = input.read(data);
79 if (num > 0) {
80 numBytes = num;
81 numSamples = numBytes / bytesPerSample;
82 empty(false);
83 if ((numBytes % bytesPerSample) != 0) {
84 System.out.println("WARNING: needed integral multiple of " + bytesPerSample +
85 " bytes, but read " + numBytes + " bytes");
86 }
87 } else {
88 numBytes = 0;
89 }
90 }
91
92 int numSamples() {
93 return numSamples;
94 }
95
96 // This is called by the mixer and must be extremely fast
97 // FIXME: may want to reconsider use of floating point at this point
98 // FIXME: assumes all sounds are of the same format to avoid normalization
99 float getSample(final int sample) {
100 final int startByte = sample * bytesPerSample;
101 // FIXME: assumes no more than 4 bytes per sample
102 int res = 0;
103 if (needsByteSwap) {
104 for (int i = startByte + bytesPerSample - 1; i >= startByte; i--) {
105 res <<= 8;
106 res |= (data[i] & 0xff);
107 }
108 } else {
109 final int endByte = startByte + bytesPerSample - 1;
110 for (int i = startByte; i <= endByte; i++) {
111 res <<= 8;
112 res |= (data[i] & 0xff);
113 }
114 }
115 // Sign extend
116 if (bytesPerSample == 2) {
117 res = (short) res;
118 } else if (bytesPerSample == 1) {
119 res = (byte) res;
120 }
121
122 return res;
123 }
124}