Search in sources :

Example 1 with Gridding

use of spacegraph.space2d.container.Gridding 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;
}
Also used : Loop(jcog.exe.Loop) FloatSlider(spacegraph.space2d.widget.slider.FloatSlider) Surface(spacegraph.space2d.Surface) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneDHaar(jcog.math.OneDHaar) Draw(spacegraph.video.Draw) Util(jcog.Util) FloatArrayList(org.eclipse.collections.impl.list.mutable.primitive.FloatArrayList) Gridding(spacegraph.space2d.container.Gridding) Topic(jcog.event.Topic) Plot2D(spacegraph.space2d.widget.meter.Plot2D) ListTopic(jcog.event.ListTopic) BitmapMatrixView(spacegraph.space2d.widget.meter.BitmapMatrixView) FloatSlider(spacegraph.space2d.widget.slider.FloatSlider) BitmapMatrixView(spacegraph.space2d.widget.meter.BitmapMatrixView) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Gridding(spacegraph.space2d.container.Gridding) Plot2D(spacegraph.space2d.widget.meter.Plot2D) FloatArrayList(org.eclipse.collections.impl.list.mutable.primitive.FloatArrayList)

Example 2 with Gridding

use of spacegraph.space2d.container.Gridding in project narchy by automenta.

the class AllOrNothingSlider method AllOrNothingSlider.

public static Gridding AllOrNothingSlider(FloatSlider f) {
    PushButton zeroButton = new PushButton("-").click((cb) -> f.valueRelative(0f));
    PushButton oneButton = new PushButton("+").click((cb) -> f.valueRelative(1f));
    return new Gridding(Gridding.HORIZONTAL, f, new Gridding(Gridding.VERTICAL, zeroButton, oneButton));
}
Also used : Gridding(spacegraph.space2d.container.Gridding) PushButton(spacegraph.space2d.widget.button.PushButton)

Example 3 with Gridding

use of spacegraph.space2d.container.Gridding in project narchy by automenta.

the class SSHSurface method main.

public static void main(String[] args) {
    PhyWall w = SpaceGraph.wall(800, 600);
    w.put(new Gridding(new SSHSurface()), 8, 6);
    w.put(new Gridding(new AWTSurface(new JColorChooser(), 200, 200)), 3, 3);
}
Also used : Gridding(spacegraph.space2d.container.Gridding) PhyWall(spacegraph.space2d.widget.windo.PhyWall)

Example 4 with Gridding

use of spacegraph.space2d.container.Gridding in project narchy by automenta.

the class ImageTexture method main.

public static void main(String[] args) throws MalformedURLException {
    // pngquant 2 wrench.png --speed 1 --quality 0 --nofs
    String file = "/home/me/Font-Awesome-SVG-PNG/white/png/x128/wrench-fs8.png";
    SpaceGraph.window(new Gridding(// new ImageTexture(new File(file)).view(),
    new ImageTexture("fontawesome://wrench").view(), new ImageTexture("fontawesome://feed").view(), new ImageTexture("fontawesome://space-shuttle").view(), new ImageTexture("fontawesome://youtube").view(), new ImageTexture(new File(file)).view()), 500, 500);
}
Also used : Gridding(spacegraph.space2d.container.Gridding) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 5 with Gridding

use of spacegraph.space2d.container.Gridding in project narchy by automenta.

the class TensorGlow method main.

public static void main(String[] args) {
    PhyWall p = SpaceGraph.wall(1200, 1000);
    p.W.setGravity(new v2(0, -2.8f));
    staticBox(p.W, -5, -8, +5, 2f, false, true, true, true);
    for (int j = 0; j < 3; j++) {
        BodyDef bodyDef2 = new BodyDef();
        bodyDef2.type = BodyType.DYNAMIC;
        // otocenie
        bodyDef2.angle = -0.6f;
        // smer pohybu
        bodyDef2.linearVelocity = new v2(0.0f, 0.0f);
        // rotacia (rychlost rotacie)
        bodyDef2.angularVelocity = 0.0f;
        Body2D newBody = p.W.addBody(bodyDef2);
        PolygonShape shape2 = new PolygonShape();
        shape2.setAsBox(0.25f, 0.25f);
        Fixture f = newBody.addFixture(shape2, 1.0f);
        // trenie
        f.friction = 0.5f;
        // odrazivost
        f.restitution = 0.0f;
        f.material = new Uniform();
        f.material.m_rigidity = 1.0f;
    }
    // //ceiling rack
    // addBox(p.W, -1, +0.4f, 0, +0.65f, false, true, true, true);
    // new Pacman(p.W);
    {
        p.W.setContactListener(new Explosives.ExplosionContacts());
        TheoJansen t = new TheoJansen(p.W, 0.35f);
        PhyWall.PhyWindow pw = p.put(new Gridding(0.5f, new Port((float[] v) -> {
            // System.out.println(v);
            t.motorJoint.setMotorSpeed(v[0] * 2 - v[1] * 2);
            t.motorJoint.setMaxMotorTorque(v[2]);
            t.motorJoint.enableLimit(true);
            t.motorJoint.setLimits((float) (-v[3] * Math.PI), (float) (+v[4] * Math.PI));
            if (v[5] > 0.5f) {
                t.gun.fire();
            }
            t.turretJoint.setLimits((float) (+Math.PI / 2 + v[6] * Math.PI - 0.1f), (float) (+Math.PI / 2 + v[6] * Math.PI + 0.1f));
        })), 0.8f, 0.4f);
        p.W.addJoint(new RevoluteJoint(p.W, new RevoluteJointDef(pw.body, t.chassis)));
    }
    {
        p.W.setParticleRadius(0.05f);
        p.W.setParticleDamping(0.1f);
        CircleShape shape = new CircleShape();
        shape.center.set(0, 10);
        shape.radius = 2f;
        ParticleGroupDef pd = new ParticleGroupDef();
        pd.flags = ParticleType.b2_waterParticle;
        // b2_viscousParticle;
        // b2_elasticParticle;
        // b2_springParticle;
        // b2_powderParticle;
        pd.color = new ParticleColor(0.7f, 0.1f, 0.1f, 0.8f);
        pd.shape = shape;
        p.W.addParticles(pd);
    }
    HaiQae q = new HaiQae(8, 2);
    float[] in = new float[q.ae.inputs()];
    final Tensor randomVector = Tensor.randomVectorGauss(in.length, 0, 1, rng);
    final FloatRange lerpRate = new FloatRange(0.01f, 0, 1f);
    final TensorLERP lerpVector = new TensorLERP(randomVector, lerpRate);
    PhyWall.PhyWindow w = p.put(new Gridding(0.25f, new AutoUpdateMatrixView(lerpVector.data), new LabeledPane("lerp", new XYSlider().on((x, y) -> {
        lerpRate.set(x);
    })), new LabeledPane("out", new Port((x) -> {
    }) {

        @Override
        public void prePaint(int dtMS) {
            super.prePaint(dtMS);
            out(lerpVector.data);
        }
    })), 0.5f, 0.5f);
    p.put(new TogglePort(), 0.25f, 0.25f);
    PhyWall.PhyWindow qw = p.put(new Gridding(new Label("HaiQ"), new AutoSurface<>(q), new LabeledPane("input", new Port((float[] i) -> {
        System.arraycopy(i, 0, in, 0, i.length);
    })), new Gridding(VERTICAL, new AutoUpdateMatrixView(in), new AutoUpdateMatrixView(q.ae.x), new AutoUpdateMatrixView(q.ae.W), new AutoUpdateMatrixView(q.ae.y)), new Gridding(VERTICAL, new AutoUpdateMatrixView(q.q), new AutoUpdateMatrixView(q.et))), 1, 1);
    Loop.of(() -> {
        lerpVector.update();
        q.act((((float) Math.random()) - 0.5f) * 2, in);
    }).runFPS(25);
}
Also used : Fixture(spacegraph.space2d.phys.dynamics.Fixture) ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) Explosives(spacegraph.space2d.phys.explosive.Explosives) Tensor(jcog.math.tensor.Tensor) HaiQae(jcog.learn.ql.HaiQae) Random(java.util.Random) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) Port(spacegraph.space2d.widget.windo.Port) ParticleColor(spacegraph.space2d.phys.particle.ParticleColor) AutoSurface(spacegraph.space2d.widget.meta.AutoSurface) AutoUpdateMatrixView(spacegraph.space2d.widget.meter.AutoUpdateMatrixView) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) PhyWall(spacegraph.space2d.widget.windo.PhyWall) Gridding(spacegraph.space2d.container.Gridding) BodyType(spacegraph.space2d.phys.dynamics.BodyType) ParticleType(spacegraph.space2d.phys.particle.ParticleType) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef) XoRoShiRo128PlusRandom(jcog.math.random.XoRoShiRo128PlusRandom) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) LabeledPane(spacegraph.space2d.widget.text.LabeledPane) Loop(jcog.exe.Loop) TogglePort(spacegraph.space2d.widget.windo.TogglePort) XYSlider(spacegraph.space2d.widget.slider.XYSlider) Dynamics2D.staticBox(spacegraph.space2d.phys.dynamics.Dynamics2D.staticBox) Uniform(spacegraph.space2d.phys.fracture.materials.Uniform) FloatRange(jcog.math.FloatRange) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) spacegraph.util.math.v2(spacegraph.util.math.v2) TensorLERP(jcog.math.tensor.TensorLERP) VERTICAL(spacegraph.space2d.container.Gridding.VERTICAL) SpaceGraph(spacegraph.SpaceGraph) Label(spacegraph.space2d.widget.text.Label) Body2D(spacegraph.space2d.phys.dynamics.Body2D) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Port(spacegraph.space2d.widget.windo.Port) TogglePort(spacegraph.space2d.widget.windo.TogglePort) Label(spacegraph.space2d.widget.text.Label) Uniform(spacegraph.space2d.phys.fracture.materials.Uniform) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) TogglePort(spacegraph.space2d.widget.windo.TogglePort) FloatRange(jcog.math.FloatRange) AutoSurface(spacegraph.space2d.widget.meta.AutoSurface) LabeledPane(spacegraph.space2d.widget.text.LabeledPane) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) HaiQae(jcog.learn.ql.HaiQae) Tensor(jcog.math.tensor.Tensor) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) TensorLERP(jcog.math.tensor.TensorLERP) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef) Gridding(spacegraph.space2d.container.Gridding) AutoUpdateMatrixView(spacegraph.space2d.widget.meter.AutoUpdateMatrixView) XYSlider(spacegraph.space2d.widget.slider.XYSlider) PhyWall(spacegraph.space2d.widget.windo.PhyWall) ParticleColor(spacegraph.space2d.phys.particle.ParticleColor) spacegraph.util.math.v2(spacegraph.util.math.v2) Body2D(spacegraph.space2d.phys.dynamics.Body2D)

Aggregations

Gridding (spacegraph.space2d.container.Gridding)14 GL2 (com.jogamp.opengl.GL2)4 List (java.util.List)4 Util (jcog.Util)4 Loop (jcog.exe.Loop)4 Surface (spacegraph.space2d.Surface)4 AutoSurface (spacegraph.space2d.widget.meta.AutoSurface)4 Random (java.util.Random)3 FloatRange (jcog.math.FloatRange)3 XoRoShiRo128PlusRandom (jcog.math.random.XoRoShiRo128PlusRandom)3 RectFloat2D (jcog.tree.rtree.rect.RectFloat2D)3 Label (spacegraph.space2d.widget.text.Label)3 Draw (spacegraph.video.Draw)3 BufferedImage (java.awt.image.BufferedImage)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IntStream (java.util.stream.IntStream)2 HaiQae (jcog.learn.ql.HaiQae)2 FasterList (jcog.list.FasterList)2 TensorLERP (jcog.math.tensor.TensorLERP)2 CheckBox (spacegraph.space2d.widget.button.CheckBox)2