use of spacegraph.space2d.Surface in project narchy by automenta.
the class WaveCapture method view.
// private final boolean normalizeDisplayedWave = false;
public Surface view() {
final Plot2D.Series rawWave, wavelet1d;
rawWave = new Plot2D.Series("Audio", 1) {
@Override
public void update() {
clear();
float[] samples = WaveCapture.this.samples;
if (samples == null)
return;
// samples[0] = null;
int chans = WaveCapture.this.source.channelsPerSample();
int bufferSamples = Math.min(WaveCapture.this.bufferSamples, samples.length / chans);
switch(chans) {
case 1:
for (int i = 0; i < bufferSamples; i++) add(samples[i]);
break;
case 2:
for (int i = 0; i < bufferSamples; ) // to mono
add((samples[i++] + samples[i++]) / 2f);
break;
default:
throw new UnsupportedOperationException();
}
// minValue = -0.5f;
// maxValue = 0.5f;
// if (normalizeDisplayedWave) {
// autorange();
// } else {
// minValue = -1;
// maxValue = +1;
// }
// final FloatArrayList history = this.history;
//
// for (int i = 0; i < nSamplesRead; i++) {
// history.add((float) samples[i]);
// }
//
// while (history.size() > maxHistory)
// history.removeAtIndex(0);
// minValue = Float.POSITIVE_INFINITY;
// maxValue = Float.NEGATIVE_INFINITY;
//
// history.forEach(v -> {
// if (Double.isFinite(v)) {
// if (v < minValue) minValue = v;
// if (v > maxValue) maxValue = v;
// }
// //mean += v;
// });
}
};
wavelet1d = new Plot2D.Series("Wavelet", 1) {
final float[] transformedSamples = new float[Util.largestPowerOf2NoGreaterThan(bufferSamples)];
final AtomicBoolean busy = new AtomicBoolean();
{
frame.on((w) -> {
if (!busy.compareAndSet(false, true))
return;
FloatArrayList history = this;
// for (short s : ss) {
// history.add((float)s);
// }
//
//
// while (history.size() > maxHistory)
// history.removeAtIndex(0);
//
// while (history.size() < maxHistory)
// history.add(0);
final int bufferSamples = Math.min(samples.length, WaveCapture.this.bufferSamples);
float[] ss = transformedSamples;
// 1d haar wavelet transform
// OneDHaar.displayOrderedFreqsFromInPlaceHaar(x);
// the remainder will be zero
System.arraycopy(samples, 0, ss, 0, bufferSamples);
OneDHaar.inPlaceFastHaarWaveletTransform(ss);
sampleFrequency(ss);
// OneDHaar.displayOrderedFreqsFromInPlaceHaar(samples, System.out);
// //apache commons math - discrete cosine transform
// {
// double[] dsamples = new double[samples.length + 1];
// for (int i = 0; i < samples.length; i++)
// dsamples[i] = samples[i];
// dsamples = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I).transform(dsamples, TransformType.FORWARD);
// for (int i = 0; i < samples.length; i++)
// samples[i] = (float) dsamples[i];
// }
history.clear();
for (int i = 0; i < bufferSamples; i++) history.addAll(ss[i]);
// minValue = Short.MIN_VALUE;
// maxValue = Short.MAX_VALUE;
// if (normalizeDisplayedWave) {
// minValue = Float.POSITIVE_INFINITY;
// maxValue = Float.NEGATIVE_INFINITY;
//
// history.forEach(v -> {
// //if (Float.isFinite(v)) {
// if (v < minValue) minValue = v;
// if (v > maxValue) maxValue = v;
// //}
// //mean += v;
// });
// } else {
// minValue = -1f;
// maxValue = 1f;
// }
// System.out.println(maxHistory + " " + start + " " + end + ": " + minValue + " " + maxValue);
busy.set(false);
});
}
private void sampleFrequency(float[] freqSamples) {
int lastFrameIdx = data.length - freqSamplesPerFrame;
int samples = freqSamples.length;
float bandWidth = ((float) samples) / freqSamplesPerFrame;
float sensitivity = 1f;
final Envelope uniform = (i, k) -> {
float centerFreq = (0.5f + i) * bandWidth;
return 1f / (1f + Math.abs(k - centerFreq) / (bandWidth / sensitivity));
};
System.arraycopy(data, 0, data, freqSamplesPerFrame, lastFrameIdx);
float[] h = WaveCapture.this.data;
// int f = freqOffset;
// int freqSkip = 1;
// for (int i = 0; i < freqSamplesPerFrame; i++) {
// h[n++] = freqSamples[f];
// f+=freqSkip*2;
// }
float max = Float.NEGATIVE_INFINITY, min = Float.POSITIVE_INFINITY;
for (int i = 0; i < freqSamplesPerFrame; i++) {
float s = 0;
for (int k = 0; k < samples; k++) {
float fk = freqSamples[k];
s += uniform.apply(i, k) * fk;
}
if (s > max)
max = s;
if (s < min)
min = s;
h[i] = s;
}
if (max != min) {
// TODO epsilon check
float range = max - min;
for (int i = 0; i < freqSamplesPerFrame; i++) dataNorm[i] = (data[i] - min) / range;
}
// System.arraycopy(freqSamples, 0, history, 0, freqSamplesPerFrame);
}
};
rawWave.range(-1, +1);
wavelet1d.range(-1, +1);
// , bufferSamples, 450, 60);
Plot2D audioPlot = new Plot2D(bufferSamples, Plot2D.Line);
audioPlot.add(rawWave);
Plot2D audioPlot2 = new Plot2D(bufferSamples, Plot2D.Line);
audioPlot2.add(wavelet1d);
BitmapMatrixView freqHistory = new BitmapMatrixView(freqSamplesPerFrame, historyFrames, (x, y) -> {
if (data == null)
// HACK
return 0;
float kw = (data[y * freqSamplesPerFrame + x]);
// int kw = (int)(v*255);
return Draw.rgbInt(kw >= 0 ? kw : 0, kw < 0 ? -kw : 0, 0);
});
Gridding v = new Gridding(audioPlot, audioPlot2, freqHistory);
if (source instanceof AudioSource)
v.add(new FloatSlider(((AudioSource) source).gain));
frame.on(() -> {
freqHistory.update();
audioPlot.update();
audioPlot2.update();
// wav2.update();
});
return v;
}
use of spacegraph.space2d.Surface in project narchy by automenta.
the class Finger method on.
public synchronized Surface on(Surface root, float lx, float ly, short[] nextButtonDown) {
Fingering ff = this.fingering.get();
Fingering f0 = ff;
Surface touchedNext;
try {
this.pos.set(lx, ly);
arraycopy(this.buttonDown, 0, prevButtonDown, 0, buttonDown.length);
fill(this.buttonDown, false);
if (nextButtonDown != null) {
for (short s : nextButtonDown) {
if (// ignore -1 values
s > 0)
this.buttonDown[s - 1] = true;
}
for (int j = 0, jj = hitOnDown.length; j < jj; j++) {
if (!prevButtonDown[j] && buttonDown[j]) {
hitOnDown[j] = new v2(pos);
hitOnDownGlobal[j] = new v2(posGlobal);
}
}
} else {
Arrays.fill(hitOnDown, null);
}
if (ff == null || ff.escapes()) {
touchedNext = root.onTouch(this, nextButtonDown);
if (touchedNext instanceof Widget) {
if (!on((Widget) touchedNext))
touchedNext = null;
} else {
touchedNext = null;
}
} else {
touchedNext = null;
}
if (ff != null) {
if (!ff.update(this)) {
ff.stop(this);
ff = null;
}
}
for (int j = 0, jj = hitOnDown.length; j < jj; j++) {
if (!buttonDown[j] && hitOnDown[j] != null) {
// release
hitOnDown[j] = null;
}
}
if (touching != touchedNext && touching != null) {
touching.untouch();
touching = null;
}
} finally {
if (ff == null)
fingering.compareAndSet(f0, null);
}
return touchedNext;
}
use of spacegraph.space2d.Surface in project narchy by automenta.
the class Sketch2DBitmap method menu.
@Override
public Surface menu() {
ButtonSet<ColorToggle> colorMenu = new ButtonSet<>(ButtonSet.Mode.One, // black
new ColorToggle(0f, 0, 0), // red
new ColorToggle(1f, 0, 0), // orange
new ColorToggle(1f, 0.5f, 0), // yellow
new ColorToggle(0.75f, 0.75f, 0), // green
new ColorToggle(0f, 1, 0), // blue
new ColorToggle(0f, 0, 1), // purple
new ColorToggle(1f, 0, 1), // gray
new ColorToggle(0.5f, 0.5f, 0.5f), // white
new ColorToggle(1f, 1, 1));
colorMenu.on((cc, e) -> {
if (e) {
color(cc.r, cc.g, cc.b);
}
});
Surface toolMenu = grid(new XYSlider().on((_width, _alpha) -> {
brushWidth = Util.lerp(_width, 0.1f, 3f);
brushAlpha = Util.lerp(_alpha, 0.1f, 3f);
}).set(0.5f, 0.75f));
return grid(colorMenu, toolMenu);
}
use of spacegraph.space2d.Surface in project narchy by automenta.
the class MultiTrack method doLayout.
@Override
public void doLayout(int dtMS) {
float y = 0;
float w = w();
for (Surface t : content()) {
// HACK
float th = h() / 4;
if (t instanceof Track) {
((Track) t).update(state, 0, w, y, th);
}
y += th;
}
super.doLayout(dtMS);
}
use of spacegraph.space2d.Surface in project narchy by automenta.
the class Bordering method doLayout.
@Override
public void doLayout(int dtMS) {
float X = x();
float Y = y();
float W = w();
float H = h();
float w2, h2;
boolean aspectEqual = true;
if (aspectEqual) {
w2 = h2 = Math.min(W, H) / 2;
} else {
w2 = W / 2;
h2 = H / 2;
}
Surface[] children = children();
for (int i = 0, childrenLength = children.length; i < childrenLength; i++) {
Surface c = children[i];
if (c instanceof EmptySurface)
continue;
float x1, y1, x2, y2;
switch(i) {
case C:
x1 = borderWest * w2;
y1 = borderSouth * h2;
x2 = W - borderEast * w2;
y2 = H - borderNorth * h2;
break;
case N:
x1 = borderWest * w2;
y1 = H - borderNorth * h2;
x2 = W - borderEast * w2;
y2 = H;
break;
case S:
x1 = borderWest * w2;
y1 = 0;
x2 = W - borderEast * w2;
y2 = borderSouth * h2;
break;
case Bordering.W:
x1 = 0;
y1 = borderSouth * h2;
x2 = borderWest * w2;
y2 = H - borderNorth * h2;
break;
case E:
x1 = W - borderEast * w2;
y1 = borderSouth * h2;
x2 = W;
y2 = H - borderNorth * h2;
break;
case NE:
x1 = W - borderEast * w2;
y1 = H - borderNorth * h2;
x2 = W;
y2 = H;
break;
case SW:
x1 = 0;
y1 = 0;
x2 = borderWest * w2;
y2 = borderSouth * h2;
break;
default:
throw new TODO();
}
assert (x2 >= x1 && y2 >= y1);
c.pos(X + x1, Y + y1, X + x2, Y + y2);
}
}
Aggregations