IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaFX Discussion :

Graphique de son


Sujet :

JavaFX

  1. #1
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2018
    Messages : 12
    Points : 13
    Points
    13
    Par défaut Graphique de son
    Bonjour c'est la première fois que je développe vraiment (bien que ce soit encore fort amateur) en JavaFX.

    J'ai des soucis déjà j'avais fait un post avec la méthode setOnKeyPress des composants jfx qui me semble ne pas fonctionner comme il se devrait
    (c'est-à-dire de la manière qu'en awt). Je passe sur ce bug (si c'en est un?) que j'ai contourné.

    Maintenant j'essaie d'écrire le code pour une visualisation de son dans l'interface du synthé ce qui me permettra de faire des progrès dans le développement des nouvelles fonctionnalités.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
        public void run() {
     
            while (isRunning()) {
                GraphicsContext context2D = canvas.getGraphicsContext2D();
                drawBorder(canvas.getGraphicsContext2D(), Color.RED);    // FONCTIONNE
                Enveloppe enveloppe = new Enveloppe(1);
                double[] ypoints = new double[enveloppe.points.length];
                double[] xpoints = new double[enveloppe.points.length];
                double maxHeight = canvas.getHeight() / 2;
                for (int i = 0; i < enveloppe.points.length; i++) {
     
                    double y = enveloppe.points[i].getY() * maxHeight;
                    double z = enveloppe.points[i].getZ() * maxHeight;
     
                    xpoints[i] = z;
                    ypoints[i] = y;
     
     
                }
                context2D.strokePolyline(xpoints, ypoints, xpoints.length); // FONCTIONNE
     
    // A PARTIR D'ICI CA NE FONCTIONNE PLUS.
    LinkedList<Double> doubles2 = waitForData.getDoubles();
                if (doubles2.size() >= channels) {
                    //System.out.println("drawn");
                    double[] objects1 = new double[(int) (canvas.getWidth() * 2)];
     
                    int i = 0;
     
     
                    while (doubles2.getFirst() != null && i < objects1.length) {
                        objects1[i++] = doubles2.getFirst();
                        doubles2.removeFirst();
     
                    }
     
                    double[] xs = new double[objects1.length / 2];
                    double[] ys1 = new double[objects1.length / 2];
                    double[] ys2 = new double[objects1.length / 2];
     
     
                    xpoints = new double[objects1.length];
                    int length = ypoints.length;
                    for (; i < objects1.length; ) {
     
                        xs[i] = position++;
     
                        xpoints[i] = xs[i] / max + maxHeight / 2;
     
                        ys1[i] = objects1[i++] / max + maxHeight / 2;
     
                        ys2[i] = objects1[i++] / max + maxHeight / 2;
     
     
                        if (position >= canvas.getWidth()) {
                            position = 0;
                        }
                    }
                    context2D.setFill(Color.BLACK);
                    context2D.setStroke(Color.BLUE);
                    context2D.setLineWidth(4.0);
                    context2D.strokePolyline(xpoints, ys1, length);
                    context2D.strokePolyline(xpoints, ys2, length);
     
     
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    Le code n'est pas très complexe il s'agit juste d'afficher des échantillons sonores en graphique type "nuage de points reliés"

    Le code complet se trouve ici: https://gitlab.com/Graphics3D/MyLittleSynth

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Voir Heavy rendering task (in canvas) in JavaFX blocks GUI sur Stackoverflow.
    Je vais essayer de me pencher sur ça dès que possible. Je pensais aussi à dessiner dans une WriteableImage dans le thread et ensuite la composer sur le Canvas dans le JavaFX Application Thread.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2018
    Messages : 12
    Points : 13
    Points
    13
    Par défaut Rien ne s'affiche
    En fait le problème que j'ai ce n'est pas le thread d'affichage. (bien que j'aie suivi ton conseil et mis un AnimationTimer dont je ne comprends pas tout.

    Ce qui se passe (pas...) c'est que rien ne s'affiche à part le dessin des bordures (que j'ai mis pour tester). Les échantillons sonores ne se dessinent pas du tout?

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Il peut y avoir plusieurs soucis qui peuvent causer cela. Mais il va me falloir retélécharger tout le projet pour le recompiler pour tester cela (pas le temps ce soir) :

    • Soit les données ne sont pas correctement récupérées et donc rien ne se dessine et c'est normal. Là il faudrait passer dessus via le débogueur et voir ce qui se passe.
    • Voir carrément la méthode d'attente ne renvoie jamais quoi que ce soit et donc le thread se bloque indéfiniment. Là aussi il faudrait passer avec le débogueur.
    • Soit plus simplement compte tenu du fait qu'il y a du multi-thread mal synchronisé et ben juste c'est normal qu'on ne voit rien car l'affichage se fait à un moment ou la liste interne des actions de dessin est presque vide*.



    *Canvas ne permet pas un affichage en directe en dessinant dans des pixels. En fait elle conserve une liste des actions de dessins et celle liste est consommée en interne au moment de dessiner à l'écran.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  5. #5
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2018
    Messages : 12
    Points : 13
    Points
    13
    Par défaut Là je vais à la messe ;-)
    Ok bouye, j'ai fait un dernier commit/push et si tu peux regarder ça ce soir ce serait très cordial.
    Ca m'intéresse de savoir aussi comment ça se passe sur une autre machine, s'il y a des freeze ou quoi.

  6. #6
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Pas bien réveillé moi ce matin j'ai pas posté dans le bon sujet...

    Qu'est ce qu'est sensé faire AudioViewer.WaitForData ? Parce qu'en l’état cette classe ne sert strictement a rien.

    Ensuite le même soucis que d'hab : la boucle infinie dans AudioViewer bouffe 100% du temps CPU puisqu'elle ne ménage jamais aucune pause. Encore plus dans une simple app de test ou il n'y a jamais aucune note a jouer.

    Apres

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
        public boolean isRunning() {
            return running;
        }
     
        public void setRunning(boolean running) {
            this.running = running;
        }
    Ca sert a rien, la classe Thread dispose de toute ce qu'il faut pour tester si elle a ete interrompue et le faire si besoin (isInterrupted(), interrupt(), interrupted()* ).

    * bien lire la doc !
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  7. #7
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2018
    Messages : 12
    Points : 13
    Points
    13
    Par défaut
    Moi je n'ai pas de freeze.
    J'ai enlevé la classe WaitForData inutile.

    Voilà ce qui en sort:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    218
    219
    220
    /*
     * 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[] xpoints1 = 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();
     
                xpoints1[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;
     
     
            }
            context2D.strokePolyline(xpoints1, ypoints, xpoints1.length);
            context2D.strokePolyline(xpoints2, ypoints2, xpoints2.length);
            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[k++] * maxHeight + maxHeight;
     
                        ys2[i / 2] += samples[k++] * maxHeight + maxHeight;
     
     
                        if (position >= canvas.getWidth()) {
                            position = 0;
                        }
                    }
     
     
                    ys1[i / 2] /= numberOfSamplesPerPixel;
                    ys2[i / 2] /= numberOfSamplesPerPixel;
     
                    i += 2;
     
                }
                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] + ")");
     
            }
        }
     
     
        int sizeTreated = 0;
        Map<Long, Integer> treatmeentSpeed = new HashMap<>();
     
        private double treatmentSpeed(long time, int sample) {
            return 1.0 * sample / time;
        }
     
     
        Map<Long, Integer> samplesSpeed = new HashMap<>();
     
        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();
            }
        }
    }
    La ligne bleue des échantillons reste complètement plate. J'ai amélioré le comportement de la boucle de dessin qui fait que quand la taille des échantillons dans le buffer est plus grande que la taille des échantillons représentés dans la boucle: les échantillons sont sautés. J'ai aussi mis une variable "numberOfSamplesPerPixel" qui permet d'avoir un graphique condensé de la wave.

    Cependant je ne comprends pas que chez vous ça freeze tout le temps, moi c'est occasionnel avant que je débogue.

  8. #8
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2018
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2018
    Messages : 12
    Points : 13
    Points
    13
    Par défaut
    AudioViewer++++ Version déboguée. Reste à contrôler l'enveloppe des
    notes.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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();
            }
        }
    }
    Nom : 2018-04-23 (1).png
Affichages : 188
Taille : 96,7 Ko

  9. #9
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    ah mais j'ai pas dit que ca freeze tout le temps, j'ai juste sous entendu qu'une boucle qui se content de faire while(true){ [...] } (ou similaire) sans ménager un temps de pause c'est mal

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    AudioViewer extends Thread
    Du coup tu peux enlever le extends Thread
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Lier un fichier son à un objet graphique avec XPCE
    Par dreanlin dans le forum Prolog
    Réponses: 40
    Dernier message: 19/11/2006, 19h11
  2. Lier un fichier son à un objet graphique
    Par dreanlin dans le forum Multimédia
    Réponses: 15
    Dernier message: 06/11/2006, 23h04
  3. [GRAPHIQUE] Agrandir son affichage
    Par Jahprend dans le forum IHM
    Réponses: 2
    Dernier message: 15/06/2006, 16h56
  4. Réponses: 1
    Dernier message: 26/05/2006, 11h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo