testing audio stuffs

This commit is contained in:
DaniTheSkunk 2022-10-12 06:23:19 +00:00
parent ca7a74f774
commit 4486bb67a9
3 changed files with 81 additions and 0 deletions

View File

@ -13,6 +13,8 @@
<element id="extracted-dir" path="$PROJECT_DIR$/lib/lwjgl/lwjgl.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jna/jna-5.12.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jna/jna-platform-5.12.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/lwjgl/lwjgl-openal-natives-windows.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/lwjgl/lwjgl-openal.jar" path-in-jar="/" />
</root>
</artifact>
</component>

View File

@ -9,12 +9,15 @@
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-stb-natives-windows.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-stb.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-openal-natives-windows.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-openal.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-glfw-javadoc.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-javadoc.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-opengl-javadoc.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-stb-javadoc.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/lwjgl/lwjgl-openal-javadoc.jar!/" />
</JAVADOC>
<SOURCES />
</library>

View File

@ -0,0 +1,76 @@
package com.danitheskunk.skunkworks;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC;
import javax.sound.sampled.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.ALC10.*;
public class TestSound {
public static void main(String args[]) {
var device = alcOpenDevice((ByteBuffer) null);
if(device == 0) {
throw new RuntimeException("couldn't init openal device");
}
var deviceCaps = ALC.createCapabilities(device);
var context = alcCreateContext(device, (IntBuffer) null);
if(context == 0) {
throw new RuntimeException("couldn't create openal context");
}
alcMakeContextCurrent(context);
AL.createCapabilities(deviceCaps);
var buf = alGenBuffers();
int sampleRate = 44100;
var samples = new short[sampleRate * 2];
for(int i = 0; i < samples.length; ++i) {
var s = Math.sin(2 * Math.PI * 220 * i / (double) sampleRate);
var s2 = Math.sin(2 * Math.PI * 440 * i / (double) sampleRate +
s * 10 * (88200 - i) / 88200);
//s = s * s2 * i / 88200;
s = s2 * (88200 - i) / 88200;
samples[i] = (short) (s * 32760);
}
var bb = ByteBuffer.allocate(samples.length * 2);
for(int i = 0; i < samples.length; ++i) {
bb.putShort(samples[i]);
}
var bytes = bb.array();
AudioInputStream audio;
var format = new AudioFormat(sampleRate, 16, 1, true, true);
audio = new AudioInputStream(
new ByteArrayInputStream(bytes),
format,
bytes.length
);
try {
AudioSystem.write(audio,
AudioFileFormat.Type.WAVE,
new File("meow.wav")
);
} catch(IOException e) {
throw new RuntimeException(e);
}
alBufferData(buf, AL_FORMAT_MONO16, samples, sampleRate);
var src = alGenSources();
alSourcei(src, AL_BUFFER, buf);
alSourcePlay(src);
try {
Thread.sleep(3000);
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
}