From 402fc909fcebec1ed702e550c97617f51e4e9ed1 Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 22 Jan 2020 22:31:53 +0000 Subject: [PATCH] Further experimenting with interesting shapes --- src/AudioClient.java | 5 +++++ src/AudioPlayer.java | 29 ++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/AudioClient.java b/src/AudioClient.java index dfd30de2..15f368c4 100644 --- a/src/AudioClient.java +++ b/src/AudioClient.java @@ -10,6 +10,11 @@ public class AudioClient { player.start(); while (true) { float currentTime = System.nanoTime() - startTime; + + if (currentTime > 100000) { + startTime = System.nanoTime(); + } + AudioPlayer.setFrequency(0.1f); AudioPlayer.drawLine(0, 0, 0.5f * (float) Math.sin(2 * Math.PI * (currentTime / 100000f)), 0.5f * (float) Math.cos(2 * Math.PI * (currentTime / 100000f))); } } diff --git a/src/AudioPlayer.java b/src/AudioPlayer.java index d2718e5e..6ddff0d2 100644 --- a/src/AudioPlayer.java +++ b/src/AudioPlayer.java @@ -6,10 +6,12 @@ public class AudioPlayer extends Thread { private static float count = 1; private static volatile boolean stopped = false; - public static float x1; - public static float y1; - public static float x2; - public static float y2; + private static float x1; + private static float y1; + private static float x2; + private static float y2; + + private static float FREQUENCY = 440f; static void render(XtStream stream, Object input, Object output, int frames, double time, long position, boolean timeValid, long error, Object user) { @@ -18,15 +20,12 @@ public class AudioPlayer extends Thread { count++; for (int f = 0; f < frames; f++) { - float sine = nextSine(format.mix.rate, 440); - float cos = nextCos(format.mix.rate, 440); - for (int c = 0; c < format.outputs; c++) { float xMid = (x1 + x2) / 2f; float yMid = (y1 + y2) / 2f; - ((float[]) output)[f * format.outputs] = xMid + (Math.abs(x2 - x1) / 2f) * sine; - ((float[]) output)[f * format.outputs + 1] = yMid + (Math.abs(y2 - y1) / 2f) * sine; + ((float[]) output)[f * format.outputs] = xMid + (Math.abs(x2 - x1) / 2f) * nextSine(FREQUENCY); + ((float[]) output)[f * format.outputs + 1] = yMid + (Math.abs(y2 - y1) / 2f) * nextSine(FREQUENCY); } } } @@ -38,15 +37,19 @@ public class AudioPlayer extends Thread { AudioPlayer.y2 = y2; } - private static float nextSine(double sampleRate, double frequency) { - phase += frequency / sampleRate; + public static void setFrequency(float frequency) { + AudioPlayer.FREQUENCY = frequency; + } + + public static float nextSine(double frequency) { + phase += frequency / FORMAT.mix.rate; if (phase >= 1.0) phase = -1.0; return (float) Math.sin(phase * Math.PI); } - private static float nextCos(double sampleRate, double frequency) { - phase += frequency / sampleRate; + public static float nextCos(double frequency) { + phase += frequency / FORMAT.mix.rate; if (phase >= 1.0) phase = -1.0; return (float) Math.cos(phase * Math.PI);