1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| /*
* This file is part of Plants-Growth-2
* Plants-Growth-2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plants-Growth-2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Plants-Growth-2. If not, see <http://www.gnu.org/licenses/>.
*/
package be.manudahmen.mylittlesynth;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.NoSuchElementException;
public class AudioViewer extends Thread {
int numberOfSamplesPerPixel = 1;
private final LinkedList<Double> doubles = new LinkedList<>();
private LinkedList<Double> getDoubles() {
return doubles;
}
private void addDouble(Double value) {
this.doubles.add(value);
}
private final int channels;
private final float sampleRate;
private final Canvas canvas;
private double[] oldValues;
private double min = 0;
private double max = Short.MAX_VALUE;
private double position;
private final LinkedList<Double> values;
private boolean running;
public AudioViewer(float sampleRate, int channels, Canvas canvas) {
this.sampleRate = sampleRate;
this.channels = channels;
this.canvas = canvas;
oldValues = new double[channels];
running = true;
values = new LinkedList<>();
new CanvasRedrawTask().start();
}
void render(long timeElapsed, int numberOfSamples) {
GraphicsContext context2D = canvas.getGraphicsContext2D();
int maxWidth = (int) canvas.getWidth();
drawBorder(canvas.getGraphicsContext2D(), Color.RED);
Enveloppe enveloppe = new Enveloppe(1);
final double[] yPoints = new double[enveloppe.points.length];
final double[] xPoints = new double[enveloppe.points.length];
final double[] xPoints2 = new double[maxWidth];
final double[] yPoints2 = new double[maxWidth];
final double maxHeight = canvas.getHeight() / 2;
//System.out.println("maxHeight:" + maxHeight);
for (int i = 0; i < enveloppe.points.length; i++) {
double y = enveloppe.points[i].getY() * maxHeight;
double z = enveloppe.points[i].getZ() * canvas.getWidth();
xPoints[i] = z;
yPoints[i] = -y + maxHeight;
}
for (int i = 0; i < xPoints2.length; i++) {
double v = enveloppe.getForm(1.0 * i / maxWidth);
xPoints2[i] = i;
yPoints2[i] = -v * maxHeight + maxHeight;
}
LinkedList<Double> doubles2 = getDoubles();
if (doubles2.size() >= channels) {
//System.out.println("DOUBLES:" + doubles2.getFirst());
int size = (int) Math.min(canvas.getWidth() * 2, doubles2.size() / numberOfSamplesPerPixel);
final double[] samples = new double[size];
int total = 0;
do {
int i = 0;
try {
while ((doubles2.size() > 0) && (doubles2.getFirst() != null) && (i < size)) {
samples[i++] = doubles2.getFirst();
doubles2.removeFirst();
total++;
}
} catch (NoSuchElementException ex) {
}
} while ((sampleSpeed(timeElapsed, numberOfSamples) > treatmentSpeed(timeElapsed, total)));
final double[] xpoints;
final double[] xs = new double[size / 2];
final double[] ys1 = new double[size / 2];
final double[] ys2 = new double[size / 2];
xpoints = new double[size / 2];
for (int i = 0; i < size; ) {
xs[i / 2] = position++;
xpoints[i / 2] = xs[i / 2];
int k = 0;
ys1[i / 2] = 0;
ys2[i / 2] = 0;
while (k < numberOfSamplesPerPixel * 2) {
ys1[i / 2] += samples[i / 2 + k++] * maxHeight / max + maxHeight;
ys2[i / 2] += samples[i / 2 + k++] * maxHeight / max + maxHeight;
if (position >= canvas.getWidth()) {
position = 0;
}
}
ys1[i / 2] /= numberOfSamplesPerPixel;
ys2[i / 2] /= numberOfSamplesPerPixel;
i += 2;
}
context2D.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
context2D.strokePolyline(xPoints, yPoints, xPoints.length);
context2D.strokePolyline(xPoints2, yPoints2, xPoints2.length);
context2D.setFill(Color.BLACK);
context2D.setStroke(Color.BLUE);
context2D.setLineWidth(4.0);
context2D.strokePolyline(xpoints, ys1, size / 2);
context2D.strokePolyline(xpoints, ys2, size / 2);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println("Points: " + size / 2 + "(" + xpoints[0] + ", " + ys1[0] + ")");
}
}
private double treatmentSpeed(long time, int sample) {
return 1.0 * sample / time;
}
private double sampleSpeed(long time, int sample) {
return 1.0 * sample / time;
}
private void drawBorder(GraphicsContext g, Color selectedColor) {
final double canvasWidth = g.getCanvas().getWidth();
final double canvasHeight = g.getCanvas().getHeight();
g.setStroke(selectedColor);
g.setLineWidth(4);
g.strokeRect(0, 0, canvasWidth, canvasHeight);
//sets the color back to the currently selected ColorPicker color
}
public void sendDouble(Double value) {
addDouble(value);
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
class CanvasRedrawTask extends AnimationTimer {
long time = System.nanoTime();
long f = System.nanoTime();
@Override
public void handle(long now) {
render(f, getDoubles().size() / 2);
f = (System.nanoTime() - time) / 1000 / 1000;
System.out.println("Time since last redraw " + f + " ms");
time = System.nanoTime();
}
}
} |
Partager