started implement SamplePlayer

This commit is contained in:
DaniTheSkunk 2022-10-15 05:05:16 +00:00
parent 37e0b931ae
commit de325c72b5
3 changed files with 41 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.AudioEngine; import com.danitheskunk.skunkworks.audio.AudioEngine;
import com.danitheskunk.skunkworks.audio.nodes.Mixer; import com.danitheskunk.skunkworks.audio.nodes.Mixer;
import com.danitheskunk.skunkworks.audio.nodes.Node; import com.danitheskunk.skunkworks.audio.nodes.Node;
import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer;
import com.danitheskunk.skunkworks.audio.nodes.Sine; 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;
@ -25,18 +26,20 @@ public class TestSound {
var sin2 = new Sine(engine, 659.25); var sin2 = new Sine(engine, 659.25);
var sin3 = new Sine(engine, 523.25); var sin3 = new Sine(engine, 523.25);
engine.setNode(mix); engine.setNode(mix);
Node.connect(sin1, 0, mix, 0); //Node.connect(sin1, 0, mix, 0);
Node.connect(sin3, 0, mix, 1); //Node.connect(sin3, 0, mix, 1);
Node.connect(sin2, 0, mix, 2); //Node.connect(sin2, 0, mix, 2);
var sample = engine.loadSample("C:\\Users\\dani\\Downloads\\Untitled" + var sample = engine.loadSample("C:\\Users\\dani\\Downloads\\Untitled" +
".wav"); ".wav");
/* var player = new SamplePlayer(engine, sample);
for(int i = 0; i < 60; ++i) { Node.connect(player, 0, mix, 0);
for(int i = 0; i < 500; ++i) {
engine.refill(); engine.refill();
Thread.sleep(20); Thread.sleep(20);
} }
/*
for(int i = 0; i < 60; ++i) { for(int i = 0; i < 60; ++i) {
engine.refill(); engine.refill();
Thread.sleep(20); Thread.sleep(20);

View File

@ -142,10 +142,11 @@ public class AudioEngine {
} }
var sample = new Samplei(bytes.length / 2 / channels, channels == 2); var sample = new Samplei(bytes.length / 2 / channels, channels == 2);
for(int i = 0; i < bytes.length / 2 / channels; ++i) { for(int i = 0; i < bytes.length / 2 / channels; ++i) {
var l = bytes[i * 2 * channels] + bytes[i * 2 * channels + 1] << 8; var l =
bytes[i * 2 * channels] + bytes[i * 2 * channels + 1] * 256;
if(channels == 2) { if(channels == 2) {
var r = bytes[i * 2 * channels + 2] + var r = bytes[i * 2 * channels + 2] +
bytes[i * 2 * channels + 3] << 8; bytes[i * 2 * channels + 3] * 256;
sample.setSamplei(i, (short) l, (short) r); sample.setSamplei(i, (short) l, (short) r);
} else { } else {
sample.setSamplei(i, (short) l); sample.setSamplei(i, (short) l);

View File

@ -0,0 +1,29 @@
package com.danitheskunk.skunkworks.audio.nodes;
import com.danitheskunk.skunkworks.audio.AudioBuffer;
import com.danitheskunk.skunkworks.audio.AudioEngine;
import com.danitheskunk.skunkworks.audio.ISample;
public class SamplePlayer extends Node {
private ISample sample;
private int tick;
public SamplePlayer(AudioEngine engine, ISample sample) {
super(engine, 0, 1);
this.sample = sample;
this.tick = 0;
}
@Override
public AudioBuffer getBuffer(int slot) {
var bufsize = getEngine().getBufferSize();
var buf = new AudioBuffer(bufsize);
for(int i = 0; i < bufsize && tick < sample.getLength(); ++i) {
var l = sample.getSampleLeft(tick);
var r = sample.getSampleRight(tick);
buf.setSample(i, l, r);
++tick;
}
return buf;
}
}