Compare commits

...

4 Commits

Author SHA1 Message Date
DaniTheSkunk 49f1a19af1 added Sine node 2022-10-13 06:19:59 +00:00
DaniTheSkunk e2d039f380 audio nodes store engine reference 2022-10-13 06:06:23 +00:00
DaniTheSkunk 6c9aa03341 started implementing audio node system 2022-10-13 06:05:01 +00:00
DaniTheSkunk f96b2802f6 added AudioBuffer 2022-10-13 06:04:48 +00:00
5 changed files with 138 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.AudioEngine; import com.danitheskunk.skunkworks.audio.AudioEngine;
import com.danitheskunk.skunkworks.audio.nodes.Sine;
import org.lwjgl.openal.AL; import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC; import org.lwjgl.openal.ALC;
@ -17,6 +18,7 @@ import static org.lwjgl.openal.ALC10.*;
public class TestSound { public class TestSound {
public static void main(String args[]) throws InterruptedException { public static void main(String args[]) throws InterruptedException {
var engine = new AudioEngine(44100, 256, 8); var engine = new AudioEngine(44100, 256, 8);
engine.setNode(new Sine(engine, 440));
for(int i = 0; i < 120; ++i) { for(int i = 0; i < 120; ++i) {
engine.refill(); engine.refill();
Thread.sleep(20); Thread.sleep(20);

View File

@ -0,0 +1,28 @@
package com.danitheskunk.skunkworks.audio;
public class AudioBuffer {
private double[] left;
private double[] right;
public AudioBuffer(int size) {
left = new double[size];
right = new double[size];
}
public int getSize() {
return left.length;
}
public void setSample(int pos, double left, double right) {
this.left[pos] = left;
this.right[pos] = right;
}
public double getLeft(int pos) {
return left[pos];
}
public double getRight(int pos) {
return right[pos];
}
}

View File

@ -1,11 +1,11 @@
package com.danitheskunk.skunkworks.audio; package com.danitheskunk.skunkworks.audio;
import com.danitheskunk.skunkworks.audio.nodes.Node;
import org.lwjgl.openal.AL; import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC; import org.lwjgl.openal.ALC;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.Random;
import static org.lwjgl.openal.AL10.*; import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.ALC10.*; import static org.lwjgl.openal.ALC10.*;
@ -17,8 +17,7 @@ public class AudioEngine {
private long device; private long device;
private long context; private long context;
private int source; private int source;
private int tick = 0; private Node node;
public AudioEngine(int sampleRate, int bufferSize, int bufferCount) { public AudioEngine(int sampleRate, int bufferSize, int bufferCount) {
this.sampleRate = sampleRate; this.sampleRate = sampleRate;
this.bufferSize = bufferSize; this.bufferSize = bufferSize;
@ -44,26 +43,25 @@ public class AudioEngine {
alSourcePlay(source); alSourcePlay(source);
} }
public int getBufferSize() {
return bufferSize;
}
public void close() { public void close() {
alcDestroyContext(context); alcDestroyContext(context);
alcCloseDevice(device); alcCloseDevice(device);
} }
private void fillBuffer(int buffer) { private void fillBuffer(int buffer) {
var buf = new double[bufferSize * 2];
for(int i = 0; i < bufferSize; ++i) {
double time = (double) tick / (double) sampleRate;
var s = Math.sin(2 * Math.PI * 440 * time);
buf[i * 2] = s;
buf[i * 2 + 1] = s;
++tick;
}
var shortBuffer = new short[bufferSize * 2]; var shortBuffer = new short[bufferSize * 2];
for(int i = 0; i < bufferSize * 2; ++i) {
shortBuffer[i] = (short) (buf[i] * 32760);
}
if(node != null) {
var buf = node.getBuffer(0);
for(int i = 0; i < bufferSize; ++i) {
shortBuffer[i * 2 + 1] = (short) (buf.getLeft(i) * 32760);
shortBuffer[i * 2 + 1] = (short) (buf.getRight(i) * 32760);
}
}
alBufferData(buffer, AL_FORMAT_STEREO16, shortBuffer, sampleRate); alBufferData(buffer, AL_FORMAT_STEREO16, shortBuffer, sampleRate);
} }
@ -91,4 +89,12 @@ public class AudioEngine {
public int getSampleRate() { public int getSampleRate() {
return sampleRate; return sampleRate;
} }
public Node getNode() {
return node;
}
public void setNode(Node node) {
this.node = node;
}
} }

View File

@ -0,0 +1,59 @@
package com.danitheskunk.skunkworks.audio.nodes;
import com.danitheskunk.skunkworks.audio.AudioBuffer;
import com.danitheskunk.skunkworks.audio.AudioEngine;
public abstract class Node {
private int inCount;
private int outCount;
private boolean[] isOutConnected;
private Node[] inConnections;
private int[] inConnectionSlots;
private AudioEngine engine;
public Node(AudioEngine engine, int inCount, int outCount) {
this.inCount = inCount;
this.outCount = outCount;
this.isOutConnected = new boolean[outCount];
this.inConnections = new Node[inCount];
this.inConnectionSlots = new int[inCount];
this.engine = engine;
if(outCount > 1) {
throw new RuntimeException("more than one out connection not yet" +
" " +
"allowed, needs handling in getBuffer to only process once");
}
}
public static void connect(Node src, int srcSlot, Node dst, int dstSlot) {
if(srcSlot < 0 || srcSlot >= src.outCount) {
throw new RuntimeException("invalid srcSlot");
}
if(dstSlot < 0 || dstSlot >= dst.inCount) {
throw new RuntimeException("invalid dstSlot");
}
if(src.isOutConnected[srcSlot]) {
throw new RuntimeException("src node slot already connected");
}
if(dst.inConnections[dstSlot] != null) {
throw new RuntimeException("dst node slot already connected");
}
src.isOutConnected[srcSlot] = true;
dst.inConnections[dstSlot] = src;
dst.inConnectionSlots[dstSlot] = srcSlot;
}
public AudioEngine getEngine() {
return engine;
}
public int getInCount() {
return inCount;
}
public int getOutCount() {
return outCount;
}
public abstract AudioBuffer getBuffer(int slot);
}

View File

@ -0,0 +1,28 @@
package com.danitheskunk.skunkworks.audio.nodes;
import com.danitheskunk.skunkworks.audio.AudioBuffer;
import com.danitheskunk.skunkworks.audio.AudioEngine;
public class Sine extends Node {
private int tick;
private double freq;
public Sine(AudioEngine engine, double freq) {
super(engine, 0, 1);
tick = 0;
this.freq = freq;
}
@Override
public AudioBuffer getBuffer(int slot) {
var bufsize = getEngine().getBufferSize();
var buf = new AudioBuffer(bufsize);
for(int i = 0; i < bufsize; ++i) {
var time = (double) tick / (double) getEngine().getSampleRate();
var s = Math.sin(Math.PI * 2 * freq * time);
buf.setSample(i, s, s);
++tick;
}
return buf;
}
}