implement Mixer

This commit is contained in:
DaniTheSkunk 2022-10-13 06:35:45 +00:00
parent 49f1a19af1
commit abc460e59b
3 changed files with 53 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.AudioEngine;
import com.danitheskunk.skunkworks.audio.nodes.Mixer;
import com.danitheskunk.skunkworks.audio.nodes.Node;
import com.danitheskunk.skunkworks.audio.nodes.Sine;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC;
@ -18,7 +20,14 @@ 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));
var mix = new Mixer(engine, 4);
var sin1 = new Sine(engine, 440);
var sin2 = new Sine(engine, 659.25);
var sin3 = new Sine(engine, 523.25);
Node.connect(sin1, 0, mix, 0);
Node.connect(sin2, 0, mix, 1);
Node.connect(sin3, 0, mix, 2);
engine.setNode(mix);
for(int i = 0; i < 120; ++i) {
engine.refill();
Thread.sleep(20);

View File

@ -0,0 +1,38 @@
package com.danitheskunk.skunkworks.audio.nodes;
import com.danitheskunk.skunkworks.audio.AudioBuffer;
import com.danitheskunk.skunkworks.audio.AudioEngine;
public class Mixer extends Node {
private int channels;
public Mixer(AudioEngine engine, int channels) {
super(engine, channels, 1);
this.channels = channels;
}
@Override
public AudioBuffer getBuffer(int slot) {
var bufs = new AudioBuffer[channels];
var bufferSize = getEngine().getBufferSize();
var buf = new AudioBuffer(bufferSize);
for(int channel = 0; channel < channels; ++channel) {
bufs[channel] = getBufferFromInput(channel);
}
for(int i = 0; i < bufferSize; ++i) {
double l = 0.0;
double r = 0.0;
for(var inBuf : bufs) {
if(inBuf != null) {
l += inBuf.getLeft(i);
r += inBuf.getRight(i);
}
}
//todo: dividing by channels just temporary mess
buf.setSample(i, l / channels, r / channels);
}
return buf;
}
}

View File

@ -56,4 +56,9 @@ public abstract class Node {
}
public abstract AudioBuffer getBuffer(int slot);
protected AudioBuffer getBufferFromInput(int inSlot) {
if(inConnections[inSlot] == null) return null;
return inConnections[inSlot].getBuffer(inConnectionSlots[inSlot]);
}
}