From 49f1a19af1d2bba7eb0a065d0f77fe7e634e5986 Mon Sep 17 00:00:00 2001 From: DaniTheSkunk Date: Thu, 13 Oct 2022 06:19:59 +0000 Subject: [PATCH] added Sine node --- com/danitheskunk/skunkworks/TestSound.java | 2 ++ .../skunkworks/audio/AudioEngine.java | 36 +++++++++++-------- .../skunkworks/audio/nodes/Node.java | 1 + .../skunkworks/audio/nodes/Sine.java | 28 +++++++++++++++ 4 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 com/danitheskunk/skunkworks/audio/nodes/Sine.java diff --git a/com/danitheskunk/skunkworks/TestSound.java b/com/danitheskunk/skunkworks/TestSound.java index 9f06c6d..5bbf9b0 100644 --- a/com/danitheskunk/skunkworks/TestSound.java +++ b/com/danitheskunk/skunkworks/TestSound.java @@ -1,6 +1,7 @@ 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; @@ -17,6 +18,7 @@ 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); diff --git a/com/danitheskunk/skunkworks/audio/AudioEngine.java b/com/danitheskunk/skunkworks/audio/AudioEngine.java index 26ccb23..fa525a1 100644 --- a/com/danitheskunk/skunkworks/audio/AudioEngine.java +++ b/com/danitheskunk/skunkworks/audio/AudioEngine.java @@ -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,8 +17,7 @@ public class AudioEngine { private long device; private long context; private int source; - private int tick = 0; - + private Node node; public AudioEngine(int sampleRate, int bufferSize, int bufferCount) { this.sampleRate = sampleRate; this.bufferSize = bufferSize; @@ -44,26 +43,25 @@ public class AudioEngine { alSourcePlay(source); } + public int getBufferSize() { + return bufferSize; + } + public void close() { alcDestroyContext(context); alcCloseDevice(device); } 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]; - 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); } @@ -91,4 +89,12 @@ public class AudioEngine { public int getSampleRate() { return sampleRate; } + + public Node getNode() { + return node; + } + + public void setNode(Node node) { + this.node = node; + } } diff --git a/com/danitheskunk/skunkworks/audio/nodes/Node.java b/com/danitheskunk/skunkworks/audio/nodes/Node.java index c17f777..9b64555 100644 --- a/com/danitheskunk/skunkworks/audio/nodes/Node.java +++ b/com/danitheskunk/skunkworks/audio/nodes/Node.java @@ -17,6 +17,7 @@ public abstract class Node { 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" + " " + diff --git a/com/danitheskunk/skunkworks/audio/nodes/Sine.java b/com/danitheskunk/skunkworks/audio/nodes/Sine.java new file mode 100644 index 0000000..44624b4 --- /dev/null +++ b/com/danitheskunk/skunkworks/audio/nodes/Sine.java @@ -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; + } +}