added Sine node
This commit is contained in:
parent
e2d039f380
commit
49f1a19af1
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ public abstract class Node {
|
||||||
this.isOutConnected = new boolean[outCount];
|
this.isOutConnected = new boolean[outCount];
|
||||||
this.inConnections = new Node[inCount];
|
this.inConnections = new Node[inCount];
|
||||||
this.inConnectionSlots = new int[inCount];
|
this.inConnectionSlots = new int[inCount];
|
||||||
|
this.engine = engine;
|
||||||
if(outCount > 1) {
|
if(outCount > 1) {
|
||||||
throw new RuntimeException("more than one out connection not yet" +
|
throw new RuntimeException("more than one out connection not yet" +
|
||||||
" " +
|
" " +
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue