kopia lustrzana https://github.com/jameshball/osci-render
Successfully implemented rotation and fixed drawing lines.
rodzic
3d25634618
commit
a0de24fc96
|
@ -8,15 +8,17 @@ public class AudioClient {
|
|||
AudioPlayer.FORMAT = new XtFormat(new XtMix(sampleRate, XtSample.FLOAT32), 0, 0, 2, 0);
|
||||
|
||||
// Draws a square.
|
||||
Line l1 = new Line(-0.5f, -0.5f, 0.5f, -0.5f);
|
||||
Line l2 = new Line(0.5f, -0.5f, 0.5f, 0.5f);
|
||||
Line l3 = new Line(0.5f, 0.5f, -0.5f, 0.5f);
|
||||
Line l4 = new Line(-0.5f, 0.5f, -0.5f, -0.5f);
|
||||
Line l1 = new Line(-0.5, -0.5, 0.5, -0.5);
|
||||
Line l2 = new Line(0.5, -0.5, 0.5, 0.5);
|
||||
Line l3 = new Line(0.5, 0.5, -0.5, 0.5);
|
||||
Line l4 = new Line(-0.5, 0.5, -0.5, -0.5);
|
||||
|
||||
AudioPlayer.addLine(l1);
|
||||
AudioPlayer.addLine(l2);
|
||||
AudioPlayer.addLine(l3);
|
||||
AudioPlayer.addLine(l4);
|
||||
|
||||
AudioPlayer.setRotateSpeed(0.4);
|
||||
|
||||
player.start();
|
||||
while (true) {
|
||||
|
|
|
@ -9,6 +9,7 @@ public class AudioPlayer extends Thread {
|
|||
private static List<Line> lines = new ArrayList<>();
|
||||
private static int currentLine = 0;
|
||||
private static float FREQUENCY = 440;
|
||||
private static double ROTATE_SPEED = 0;
|
||||
private static int framesDrawn = 0;
|
||||
private static double phase = 0;
|
||||
|
||||
|
@ -17,15 +18,21 @@ public class AudioPlayer extends Thread {
|
|||
XtFormat format = stream.getFormat();
|
||||
|
||||
for (int f = 0; f < frames; f++) {
|
||||
Line line = currentLine();
|
||||
line.rotate(nextTheta(stream.getFormat().mix.rate, 0.000001));
|
||||
Line line;
|
||||
|
||||
if (ROTATE_SPEED == 0) {
|
||||
line = currentLine();
|
||||
} else {
|
||||
line = currentLine().rotate(nextTheta(stream.getFormat().mix.rate, ROTATE_SPEED));
|
||||
}
|
||||
|
||||
int framesToDraw = (int) (line.length() * 100);
|
||||
int neg = line.getX1() < line.getX2() || line.getY1() < line.getY2() ? 1 : -1;
|
||||
int negX = line.getX1() < line.getX2()? 1 : -1;
|
||||
int negY = line.getY1() < line.getY2() ? 1 : -1;
|
||||
|
||||
for (int c = 0; c < format.outputs; c++) {
|
||||
((float[]) output)[f * format.outputs] = (float) (line.getX1() + Math.abs(line.getX2() - line.getX1()) * (neg * framesDrawn) / framesToDraw);
|
||||
((float[]) output)[f * format.outputs + 1] = (float) (line.getY1() + Math.abs(line.getY2() - line.getY1()) * (neg * framesDrawn) / framesToDraw);
|
||||
((float[]) output)[f * format.outputs] = (float) (line.getX1() + negX * Math.abs(line.getX2() - line.getX1()) * framesDrawn / framesToDraw);
|
||||
((float[]) output)[f * format.outputs + 1] = (float) (line.getY1() + negY * Math.abs(line.getY2() - line.getY1()) * framesDrawn / framesToDraw);
|
||||
}
|
||||
|
||||
framesDrawn++;
|
||||
|
@ -52,6 +59,10 @@ public class AudioPlayer extends Thread {
|
|||
return lines.get(currentLine % lines.size());
|
||||
}
|
||||
|
||||
public static void setRotateSpeed(double speed) {
|
||||
AudioPlayer.ROTATE_SPEED = speed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (XtAudio audio = new XtAudio(null, null, null, null)) {
|
||||
|
|
|
@ -16,16 +16,20 @@ public class Line {
|
|||
return Math.sqrt(Math.pow(getX1() - getX2(), 2) + Math.pow(getY1() - getY2(), 2));
|
||||
}
|
||||
|
||||
public void rotate(double theta) {
|
||||
public Line rotate(double theta) {
|
||||
double x1 = getX1();
|
||||
double y1 = getY1();
|
||||
double x2 = getX2();
|
||||
double y2 = getY2();
|
||||
|
||||
setX1(x1 * Math.cos(theta) - y1 * Math.sin(theta));
|
||||
setY1(x1 * Math.sin(theta) + y1 * Math.cos(theta));
|
||||
setX2(x2 * Math.cos(theta) - y2 * Math.sin(theta));
|
||||
setY2(x2 * Math.sin(theta) + y2 * Math.cos(theta));
|
||||
Line line = new Line(x1, y1, x2, y2);
|
||||
|
||||
line.setX1(x1 * Math.cos(theta) - y1 * Math.sin(theta));
|
||||
line.setY1(x1 * Math.sin(theta) + y1 * Math.cos(theta));
|
||||
line.setX2(x2 * Math.cos(theta) - y2 * Math.sin(theta));
|
||||
line.setY2(x2 * Math.sin(theta) + y2 * Math.cos(theta));
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
public Point getA() {
|
||||
|
|
|
@ -4,10 +4,27 @@ import static org.junit.Assert.*;
|
|||
|
||||
public class TestSuite {
|
||||
@Test
|
||||
public void lineRotationTest() {
|
||||
public void lineRotationTest1() {
|
||||
Line line = new Line(-0.5, 0.5, 0.5, 0.5);
|
||||
line.rotate(Math.PI / 2);
|
||||
|
||||
line.rotate(Math.PI / 2);
|
||||
assertEquals(new Line(-0.5, -0.5, -0.5, 0.5), line);
|
||||
|
||||
line.rotate(Math.PI / 2);
|
||||
assertEquals(new Line(0.5, -0.5, -0.5, -0.5), line);
|
||||
|
||||
line.rotate(Math.PI / 2);
|
||||
assertEquals(new Line(0.5, 0.5, 0.5, -0.5), line);
|
||||
|
||||
line.rotate(Math.PI / 2);
|
||||
assertEquals(new Line(-0.5, 0.5, 0.5, 0.5), line);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lineRotationTest2() {
|
||||
Line line = new Line(-0.5, -0.5, -0.25, 0.5);
|
||||
|
||||
line.rotate(Math.PI / 2);
|
||||
assertEquals(new Line(0.5, -0.5, -0.5, -0.25), line);
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue