Compare commits
No commits in common. "49f1a19af1d2bba7eb0a065d0f77fe7e634e5986" and "87b3faaa81a3d20f8b99aec6bc787df0a236459d" have entirely different histories.
49f1a19af1
...
87b3faaa81
|
@ -1,7 +1,6 @@
|
|||
package com.danitheskunk.skunkworks;
|
||||
|
||||
import com.danitheskunk.skunkworks.audio.AudioEngine;
|
||||
import com.danitheskunk.skunkworks.audio.nodes.Sine;
|
||||
import org.lwjgl.openal.AL;
|
||||
import org.lwjgl.openal.ALC;
|
||||
|
||||
|
@ -18,7 +17,6 @@ import static org.lwjgl.openal.ALC10.*;
|
|||
public class TestSound {
|
||||
public static void main(String args[]) throws InterruptedException {
|
||||
var engine = new AudioEngine(44100, 256, 8);
|
||||
engine.setNode(new Sine(engine, 440));
|
||||
for(int i = 0; i < 120; ++i) {
|
||||
engine.refill();
|
||||
Thread.sleep(20);
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
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];
|
||||
}
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
package com.danitheskunk.skunkworks.audio;
|
||||
|
||||
import com.danitheskunk.skunkworks.audio.nodes.Node;
|
||||
import org.lwjgl.openal.AL;
|
||||
import org.lwjgl.openal.ALC;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.lwjgl.openal.AL10.*;
|
||||
import static org.lwjgl.openal.ALC10.*;
|
||||
|
@ -17,7 +17,8 @@ public class AudioEngine {
|
|||
private long device;
|
||||
private long context;
|
||||
private int source;
|
||||
private Node node;
|
||||
private int tick = 0;
|
||||
|
||||
public AudioEngine(int sampleRate, int bufferSize, int bufferCount) {
|
||||
this.sampleRate = sampleRate;
|
||||
this.bufferSize = bufferSize;
|
||||
|
@ -43,25 +44,26 @@ public class AudioEngine {
|
|||
alSourcePlay(source);
|
||||
}
|
||||
|
||||
public int getBufferSize() {
|
||||
return bufferSize;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
alcDestroyContext(context);
|
||||
alcCloseDevice(device);
|
||||
}
|
||||
|
||||
private void fillBuffer(int buffer) {
|
||||
var shortBuffer = new short[bufferSize * 2];
|
||||
|
||||
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);
|
||||
}
|
||||
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];
|
||||
for(int i = 0; i < bufferSize * 2; ++i) {
|
||||
shortBuffer[i] = (short) (buf[i] * 32760);
|
||||
}
|
||||
|
||||
alBufferData(buffer, AL_FORMAT_STEREO16, shortBuffer, sampleRate);
|
||||
}
|
||||
|
||||
|
@ -89,12 +91,4 @@ public class AudioEngine {
|
|||
public int getSampleRate() {
|
||||
return sampleRate;
|
||||
}
|
||||
|
||||
public Node getNode() {
|
||||
return node;
|
||||
}
|
||||
|
||||
public void setNode(Node node) {
|
||||
this.node = node;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
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);
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue