toxiclibs 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,162 +0,0 @@
1
- /*
2
- * __ .__ .__ ._____.
3
- * _/ |_ _______ __|__| ____ | | |__\_ |__ ______
4
- * \ __\/ _ \ \/ / |/ ___\| | | || __ \ / ___/
5
- * | | ( <_> > <| \ \___| |_| || \_\ \\___ \
6
- * |__| \____/__/\_ \__|\___ >____/__||___ /____ >
7
- * \/ \/ \/ \/
8
- *
9
- * Copyright (c) 2006-2011 Karsten Schmidt
10
- *
11
- * This library is free software; you can redistribute it and/or
12
- * modify it under the terms of the GNU Lesser General Public
13
- * License as published by the Free Software Foundation; either
14
- * version 2.1 of the License, or (at your option) any later version.
15
- *
16
- * http://creativecommons.org/licenses/LGPL/2.1/
17
- *
18
- * This library is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
- * Lesser General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU Lesser General Public
24
- * License along with this library; if not, write to the Free Software
25
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26
- * Modified and updated by Martin Prout December 2015
27
- */
28
-
29
- package toxi.audio;
30
-
31
- import java.util.logging.Level;
32
- import java.util.logging.Logger;
33
-
34
- /**
35
- * Implements a manager to play a number of shared samples in a multitimbral
36
- * manner without interrupting/restarting the playback of currently playing
37
- * sources. This is different to the default JOAL way of accessing sources
38
- * directly. The manager is keeping track of active sources and will attempt to
39
- * assign free ones for newly requested playbacks. If all voices are active, the
40
- * oldest one will be stopped and used as the newly requested voice.
41
- */
42
- public class MultiTimbralManager {
43
-
44
- static class SourceState {
45
-
46
- AudioSource src;
47
- boolean isActive;
48
- long startTime;
49
-
50
- SourceState(AudioSource src) {
51
- this.src = src;
52
- }
53
-
54
- void activate() {
55
- isActive = true;
56
- startTime = System.currentTimeMillis();
57
- }
58
-
59
- boolean updateStatus() {
60
- isActive = isActive
61
- && (src.isLooping() || src.getBuffersProcessed() == 0);
62
- return isActive;
63
- }
64
- }
65
-
66
- private static final Logger logger = Logger
67
- .getLogger(MultiTimbralManager.class.getName());
68
- protected SourceState[] pool;
69
- protected int maxSources;
70
-
71
- protected int currIndex;
72
-
73
- public MultiTimbralManager(JOALUtil liboal, int num) {
74
- logger.log(Level.INFO, "attempting to allocate {0} audio voices", num);
75
- AudioSource[] tmp = liboal.generateSources(num);
76
- maxSources = tmp.length;
77
- pool = new SourceState[maxSources];
78
- for (int i = 0; i < maxSources; i++) {
79
- AudioSource src = tmp[i];
80
- src.setReferenceDistance(100);
81
- src.setGain(1);
82
- pool[i] = new SourceState(src);
83
- }
84
- logger.info("done. all sources created.");
85
- }
86
-
87
- /**
88
- * Uses the class' logger to print out status information about the current
89
- * usage of the managed audio sources.
90
- */
91
- public void debug() {
92
- String sources = "";
93
- int numActive = 0;
94
- for (int i = 0; i < maxSources; i++) {
95
- if (pool[i].isActive) {
96
- sources += i + ",";
97
- numActive++;
98
- }
99
- }
100
- String info = "active sources: " + numActive;
101
- logger.info(info);
102
- logger.info(sources);
103
- }
104
-
105
- /**
106
- * Attempts to find an available, currently unused {@link AudioSource}
107
- * instance which can then be configured and played by the client
108
- * application. If no free source is available the oldest playing one will
109
- * be stopped and returned as free.
110
- *
111
- * @return a free AudioSource instance
112
- */
113
- public AudioSource getNextVoice() {
114
- boolean hasFreeSource = false;
115
- int numIterations = 0;
116
- int origID = currIndex;
117
- int id;
118
- // find first free slot...
119
- do {
120
- id = currIndex;
121
- currIndex = (currIndex + 1) % maxSources;
122
- if (pool[id].isActive) {
123
- if (!pool[id].updateStatus()) {
124
- hasFreeSource = true;
125
- }
126
- } else {
127
- hasFreeSource = true;
128
- }
129
- numIterations++;
130
- } while (!hasFreeSource && numIterations < maxSources);
131
- // use oldest slot if no free one is available
132
- if (!hasFreeSource) {
133
- long now = System.currentTimeMillis();
134
- id = origID;
135
- for (int i = 0; i < maxSources; i++) {
136
- if (pool[i].startTime < now) {
137
- id = i;
138
- now = pool[i].startTime;
139
- }
140
- }
141
- pool[id].src.stop();
142
- logger.log(Level.WARNING, "no free src, using oldest slot #{0}", id);
143
- currIndex = (id + 1) % maxSources;
144
- }
145
- pool[id].activate();
146
- return pool[id].src;
147
- }
148
-
149
- /**
150
- * Produces an array of status flags of used voices. Elements set to true
151
- * indicate the voice is currently active/playing.
152
- *
153
- * @return boolean array
154
- */
155
- public boolean[] getUsage() {
156
- boolean[] usage = new boolean[maxSources];
157
- for (int i = 0; i < maxSources; i++) {
158
- usage[i] = pool[i].isActive;
159
- }
160
- return usage;
161
- }
162
- }
@@ -1,154 +0,0 @@
1
- /*
2
- * __ .__ .__ ._____.
3
- * _/ |_ _______ __|__| ____ | | |__\_ |__ ______
4
- * \ __\/ _ \ \/ / |/ ___\| | | || __ \ / ___/
5
- * | | ( <_> > <| \ \___| |_| || \_\ \\___ \
6
- * |__| \____/__/\_ \__|\___ >____/__||___ /____ >
7
- * \/ \/ \/ \/
8
- *
9
- * Copyright (c) 2006-2011 Karsten Schmidt
10
- *
11
- * This library is free software; you can redistribute it and/or
12
- * modify it under the terms of the GNU Lesser General Public
13
- * License as published by the Free Software Foundation; either
14
- * version 2.1 of the License, or (at your option) any later version.
15
- *
16
- * http://creativecommons.org/licenses/LGPL/2.1/
17
- *
18
- * This library is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
- * Lesser General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU Lesser General Public
24
- * License along with this library; if not, write to the Free Software
25
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26
- * Modified and updated by Martin Prout December 2015
27
- */
28
-
29
- package toxi.audio;
30
-
31
- import com.jogamp.openal.AL;
32
- import toxi.geom.Vec3D;
33
-
34
- /**
35
- * The concept of a SoundListener refers directly to the user's instance in the
36
- * virtual (audio) world. By setting the 3D position, velocity and orientation
37
- * of the listener, the underlying audio hardware can produce a realistic 3D
38
- * spatial sound simulation (incl. doppler effect, volume falloff etc.).
39
- *
40
- * <p>
41
- * Like {@link AudioSource}, this class extends {@link Vec3D} and so if the
42
- * position of the listener is changed via the public x,y,z vector components,
43
- * the <code>updatePosition()</code> method needs to be called afterwards in
44
- * order to reflect these changes in the OpenAL context.
45
- */
46
- public final class SoundListener extends Vec3D {
47
-
48
- protected JOALUtil liboal;
49
-
50
- protected final float[] position = { 0.0f, 0.0f, 0.0f };
51
- protected final float[] velocity = { 0.0f, 0.0f, 0.0f };
52
- protected final float[] orient = { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
53
-
54
- protected SoundListener(JOALUtil lib) {
55
- super();
56
- liboal = lib;
57
- setGain(1f);
58
- setPosition(position);
59
- setVelocity(velocity);
60
- setOrientation(orient);
61
- }
62
-
63
- public final float[] getOrientation() {
64
- return orient;
65
- }
66
-
67
- public final float[] getPosition() {
68
- return position;
69
- }
70
-
71
- public final float[] getVelocity() {
72
- return velocity;
73
- }
74
-
75
- public final SoundListener setGain(float gain) {
76
- liboal.getAL().alListenerf(AL.AL_GAIN, gain);
77
- return this;
78
- }
79
-
80
- public SoundListener setOrientation(float upX, float upY, float upZ,
81
- float forwardX, float forwardY, float forwardZ) {
82
- orient[0] = upX;
83
- orient[1] = upY;
84
- orient[2] = upZ;
85
- orient[3] = forwardX;
86
- orient[4] = forwardY;
87
- orient[5] = forwardZ;
88
- liboal.getAL().alListenerfv(AL.AL_ORIENTATION, orient, 0);
89
- return this;
90
- }
91
-
92
- public SoundListener setOrientation(float[] o) {
93
- if (o.length == 6) {
94
- orient[0] = o[0];
95
- orient[1] = o[1];
96
- orient[2] = o[2];
97
- orient[3] = o[3];
98
- orient[4] = o[4];
99
- orient[5] = o[5];
100
- liboal.getAL().alListenerfv(AL.AL_ORIENTATION, orient, 0);
101
- } else {
102
- throw new IllegalArgumentException("wrong number of array elements");
103
- }
104
- return this;
105
- }
106
-
107
- public SoundListener setPosition(float xx, float yy, float zz) {
108
- x = position[0] = xx;
109
- y = position[1] = yy;
110
- z = position[2] = zz;
111
- liboal.getAL().alListenerfv(AL.AL_POSITION, position, 0);
112
- return this;
113
- }
114
-
115
- public final SoundListener setPosition(float[] p) {
116
- if (p.length == 3) {
117
- x = position[0] = p[0];
118
- y = position[1] = p[1];
119
- z = position[2] = p[2];
120
- liboal.getAL().alListenerfv(AL.AL_POSITION, position, 0);
121
- } else {
122
- throw new IllegalArgumentException("wrong number of elements");
123
- }
124
- return this;
125
- }
126
-
127
- public SoundListener setVelocity(float xx, float yy, float zz) {
128
- velocity[0] = xx;
129
- velocity[1] = yy;
130
- velocity[2] = zz;
131
- liboal.getAL().alListenerfv(AL.AL_VELOCITY, velocity, 0);
132
- return this;
133
- }
134
-
135
- public final SoundListener setVelocity(float[] v) {
136
- if (v.length == 3) {
137
- velocity[0] = v[0];
138
- velocity[1] = v[1];
139
- velocity[2] = v[2];
140
- liboal.getAL().alListenerfv(AL.AL_VELOCITY, velocity, 0);
141
- } else {
142
- throw new IllegalArgumentException("wrong number of elements");
143
- }
144
- return this;
145
- }
146
-
147
- public SoundListener updatePosition() {
148
- position[0] = x;
149
- position[1] = y;
150
- position[2] = z;
151
- liboal.getAL().alListenerfv(AL.AL_POSITION, position, 0);
152
- return this;
153
- }
154
- }
@@ -1,109 +0,0 @@
1
- /*
2
- * __ .__ .__ ._____.
3
- * _/ |_ _______ __|__| ____ | | |__\_ |__ ______
4
- * \ __\/ _ \ \/ / |/ ___\| | | || __ \ / ___/
5
- * | | ( <_> > <| \ \___| |_| || \_\ \\___ \
6
- * |__| \____/__/\_ \__|\___ >____/__||___ /____ >
7
- * \/ \/ \/ \/
8
- *
9
- * Copyright (c) 2006-2011 Karsten Schmidt
10
- *
11
- * This library is free software; you can redistribute it and/or
12
- * modify it under the terms of the GNU Lesser General Public
13
- * License as published by the Free Software Foundation; either
14
- * version 2.1 of the License, or (at your option) any later version.
15
- *
16
- * http://creativecommons.org/licenses/LGPL/2.1/
17
- *
18
- * This library is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
- * Lesser General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU Lesser General Public
24
- * License along with this library; if not, write to the Free Software
25
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26
- * Modified and updated by Martin Prout December 2015
27
- */
28
- package toxi.audio;
29
-
30
- import java.nio.ByteBuffer;
31
-
32
- /**
33
- * This class provides static conversion tools for translating normalized
34
- * floating point wave data into 16bit PCM.
35
- */
36
- public class SynthUtil {
37
-
38
- public static AudioBuffer floatArrayTo16bitBuffer(JOALUtil audioSys,
39
- float[] raw, int rate) {
40
- byte[] pcm = floatArrayTo16bitPCM(raw);
41
- AudioBuffer buffer = audioSys.generateBuffers(1)[0];
42
- buffer.configure(ByteBuffer.wrap(pcm), AudioBuffer.Format.MONO16, rate);
43
- return buffer;
44
- }
45
-
46
- public static byte[] floatArrayTo16bitPCM(float[] raw) {
47
- byte[] sample = new byte[raw.length * 2];
48
- for (int i = 0, j = 0; i < raw.length; i++) {
49
- int pcm = (int) (raw[i] * 0x8000);
50
- sample[j++] = (byte) (pcm & 0xff);
51
- sample[j++] = (byte) (pcm >> 8 & 0xff);
52
- }
53
- return sample;
54
- }
55
-
56
- public static AudioBuffer floatArrayTo16bitStereoBuffer(JOALUtil audioSys,
57
- float[] raw, int rate) {
58
- byte[] pcm = floatArrayTo16bitPCM(raw);
59
- AudioBuffer buffer = audioSys.generateBuffers(1)[0];
60
- buffer.configure(ByteBuffer.wrap(pcm), AudioBuffer.Format.STEREO16,
61
- rate);
62
- return buffer;
63
- }
64
-
65
- public static AudioBuffer floatArrayTo8bitBuffer(JOALUtil audioSys,
66
- float[] raw, int rate) {
67
- byte[] pcm = floatArrayTo8bitPCM(raw);
68
- AudioBuffer buffer = audioSys.generateBuffers(1)[0];
69
- buffer.configure(ByteBuffer.wrap(pcm), AudioBuffer.Format.MONO8, rate);
70
- return buffer;
71
- }
72
-
73
- public static byte[] floatArrayTo8bitPCM(float[] raw) {
74
- byte[] sample = new byte[raw.length];
75
- for (int i = 0; i < raw.length; i++) {
76
- sample[i] = (byte) (raw[i] * 0x7f + 0x80);
77
- }
78
- return sample;
79
- }
80
-
81
- public static AudioBuffer floatArrayTo8bitStereoBuffer(JOALUtil audioSys,
82
- float[] raw, int rate) {
83
- byte[] pcm = floatArrayTo8bitPCM(raw);
84
- AudioBuffer buffer = audioSys.generateBuffers(1)[0];
85
- buffer.configure(ByteBuffer.wrap(pcm), AudioBuffer.Format.STEREO8, rate);
86
- return buffer;
87
- }
88
-
89
- /**
90
- * Merges the two given mono arrays into an interleaved stereo array in
91
- * left-right order.
92
- *
93
- * @param left
94
- * @param right
95
- * @return stereo array
96
- */
97
- public static float[] joinMonoFloatArrays(float[] left, float[] right) {
98
- if (left.length != right.length) {
99
- throw new IllegalArgumentException(
100
- "left & right channels need to be of equal size");
101
- }
102
- float[] stereo = new float[left.length * 2];
103
- for (int i = 0, j = 0; i < left.length; i++, j += 2) {
104
- stereo[j] = left[i];
105
- stereo[j + 1] = right[i];
106
- }
107
- return stereo;
108
- }
109
- }