Implemented line translation

pull/35/head
James Ball 2020-02-02 21:46:11 +00:00
rodzic b988b60794
commit a7763faa24
3 zmienionych plików z 41 dodań i 16 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
package audio; package audio;
import shapes.Shapes; import shapes.Shapes;
import shapes.Vector;
public class AudioClient { public class AudioClient {
private static final int SAMPLE_RATE = 192000; private static final int SAMPLE_RATE = 192000;
@ -11,12 +12,13 @@ public class AudioClient {
AudioPlayer player = new AudioPlayer(); AudioPlayer player = new AudioPlayer();
AudioPlayer.FORMAT = AudioPlayer.defaultFormat(SAMPLE_RATE); AudioPlayer.FORMAT = AudioPlayer.defaultFormat(SAMPLE_RATE);
AudioPlayer.addLines(Shapes.generatePolygon(5, 0.25)); // AudioPlayer.addLines(Shapes.generatePolygon(5, 0.25));
// AudioPlayer.addLines(Shapes.generatePolygon(100, 0.5, 60)); AudioPlayer.addLines(Shapes.generatePolygon(100, 0.5, 60));
// AudioPlayer.addLines(Shapes.generatePolygram(5, 3, 0.5, 60)); AudioPlayer.addLines(Shapes.generatePolygram(5, 3, 0.5, 60));
AudioPlayer.setRotateSpeed(0.4); AudioPlayer.setRotateSpeed(0.4);
AudioPlayer.setTranslation(1, new Vector(0.25, 0.25));
player.start(); player.start();
} }

Wyświetl plik

@ -2,6 +2,7 @@ package audio;
import com.xtaudio.xt.*; import com.xtaudio.xt.*;
import shapes.Line; import shapes.Line;
import shapes.Vector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -11,22 +12,35 @@ public class AudioPlayer extends Thread {
private static volatile boolean stopped = false; private static volatile boolean stopped = false;
private static List<Line> lines = new ArrayList<>(); private static List<Line> lines = new ArrayList<>();
private static int currentLine = 0; private static int currentLine = 0;
private static float FREQUENCY = 440;
private static double ROTATE_SPEED = 0;
private static int framesDrawn = 0; private static int framesDrawn = 0;
private static double phase = 0;
private static double[] phases = new double[2];
private static double TRANSLATE_SPEED = 0;
private static Vector TRANSLATE_VECTOR;
private static final int TRANSLATE_PHASE_INDEX = 0;
private static double ROTATE_SPEED = 0.4;
private static final int ROTATE_PHASE_INDEX = 1;
static void render(XtStream stream, Object input, Object output, int frames, static void render(XtStream stream, Object input, Object output, int frames,
double time, long position, boolean timeValid, long error, Object user) { double time, long position, boolean timeValid, long error, Object user) {
XtFormat format = stream.getFormat(); XtFormat format = stream.getFormat();
for (int f = 0; f < frames; f++) { for (int f = 0; f < frames; f++) {
Line line; Line line = currentLine();
if (ROTATE_SPEED == 0) { if (ROTATE_SPEED != 0) {
line = currentLine(); line = currentLine().rotate(
} else { nextTheta(stream.getFormat().mix.rate, ROTATE_SPEED, TRANSLATE_PHASE_INDEX)
line = currentLine().rotate(nextTheta(stream.getFormat().mix.rate, ROTATE_SPEED)); );
}
if (TRANSLATE_SPEED != 0) {
line = line.translate(
TRANSLATE_VECTOR.scale(
Math.sin(nextTheta(stream.getFormat().mix.rate, TRANSLATE_SPEED, ROTATE_PHASE_INDEX))
)
);
} }
int framesToDraw = (int) (line.length * line.getWeight()); int framesToDraw = (int) (line.length * line.getWeight());
@ -45,11 +59,11 @@ public class AudioPlayer extends Thread {
} }
} }
static float nextTheta(double sampleRate, double frequency) { static float nextTheta(double sampleRate, double frequency, int phaseIndex) {
phase += frequency / sampleRate; phases[phaseIndex] += frequency / sampleRate;
if (phase >= 1.0) if (phases[phaseIndex] >= 1.0)
phase = -1.0; phases[phaseIndex] = -1.0;
return (float) (phase * Math.PI); return (float) (phases[phaseIndex] * Math.PI);
} }
public static void addLine(Line line) { public static void addLine(Line line) {
@ -68,6 +82,11 @@ public class AudioPlayer extends Thread {
AudioPlayer.ROTATE_SPEED = speed; AudioPlayer.ROTATE_SPEED = speed;
} }
public static void setTranslation(double speed, Vector translation) {
AudioPlayer.TRANSLATE_SPEED = speed;
AudioPlayer.TRANSLATE_VECTOR = translation;
}
@Override @Override
public void run() { public void run() {
try (XtAudio audio = new XtAudio(null, null, null, null)) { try (XtAudio audio = new XtAudio(null, null, null, null)) {

Wyświetl plik

@ -33,6 +33,10 @@ public class Vector {
return new Vector(getX() + vector.getX(), getY() + vector.getY()); return new Vector(getX() + vector.getX(), getY() + vector.getY());
} }
public Vector scale(double factor) {
return new Vector(getX() * factor, getY() * factor);
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == this) { if (obj == this) {