Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A hand with a Processing sketch

Options
  • 11-08-2011 9:51pm
    #1
    Registered Users Posts: 3,911 ✭✭✭


    Hello all!

    I am trying to make a basic pitch tone tester in Processing.

    It is working, I need to make a basic GUI for it, but I was just wondering if you guys could find places to optimise the code, which I am sure is almost everywhere :p

    I am not too sure if this is the right place for this though.

    Thanks all!
    bbk
    import ddf.minim.*;
    import ddf.minim.signals.*;

    Minim minim;
    AudioOutput out;
    SineWave sine;
    SineWave sine2;

    float n1;
    float n2;
    float s1;
    float s2;
    int lowmin;
    int lowmax;
    int highmin;
    int highmax;
    int a;
    int b;
    int c;
    int d;
    float low;
    float high;
    int level;


    void setup()
    {
    size(512, 200, P2D);

    low = 400;
    high = 600;
    println("Void Setup");
    println("Low = " + low);
    println("High = " + high);

    println(" ");
    n1 = low;
    n2 = high;


    float[] sinesource = new float [2];
    sinesource[0] = n1;
    sinesource[1] = n2;

    s1 = sinesource[int(random(0,2))];
    println("Sine Source 1 " + s1);


    if(s1 == n1) {
    s2 = n2;
    }
    else {
    s2 = n1;
    }

    println("Sine Source 2 " + s2);

    minim = new Minim(this);
    out = minim.getLineOut(Minim.STEREO);
    // println("Welcome to the pitch trainer app. The aim of this is to train your ears to detect tiny differences in pitch. Please press 'S' to start");
    }

    void draw()
    {
    background(0);
    stroke(255);
    if(keyPressed) {
    if (key == 's' || key == 'S') {
    println("Void Draw");
    println("Low = " + low);
    println("High = " + high);
    println("LEVEL " + level);
    println(" ");
    n1 = low;
    n2 = high;


    float[] sinesource = new float [2];
    sinesource[0] = n1;
    sinesource[1] = n2;

    s1 = sinesource[int(random(0,2))];
    println("Sine Source 1 " + s1);


    if(s1 == n1) {
    s2 = n2;
    }
    else {
    s2 = n1;
    }

    println("Sine Source 2 " + s2);

    sine = new SineWave(s1, 0, out.sampleRate());
    sine2 = new SineWave(s2, 0, out.sampleRate());
    out.addSignal(sine);
    out.addSignal(sine2);
    sine.setAmp(0.5);
    delay(1500);
    sine.setAmp(0);
    delay(800);
    sine2.setAmp(0.5);
    delay(1500);
    sine2.setAmp(0);
    }
    }
    else {
    }
    }

    void keyPressed() {

    if(key == '1') {
    if(s1 > s2) {
    println("Yay 1");
    level = level + 1;


    if (level == 2) {
    low = 494;
    high = 506;
    }

    if (level == 3) {
    low = 497;
    high = 503;
    }

    if (level == 4) {
    low = 497;
    high = 503;
    }
    if (level == 5) {
    low = 497;
    high = 503;
    }

    if (level == 6) {
    low = 498.5;
    high = 501.5;
    }

    if (level == 7) {
    low = 498.5;
    high = 501.5;
    }

    if (level == 8) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 9) {
    low = 499.25;
    high = 500.75;
    }

    if (level == 10) {
    low = 499.25;
    high = 500.75;
    }

    if (level == 11) {
    low = 499.25;
    high = 500.75;
    }

    if (level == 12) {
    low = 499.8125;
    high = 500.1875;
    }


    if (level == 13) {
    low = 499.8125;
    high = 500.1875;
    }


    if (level == 14) {
    low = 499.8125;
    high = 500.1875;
    }
    }
    else {
    println("Nay 1");
    level = level - 1;
    }
    }
    else {
    if(key == '2') {
    if(s1 < s2) {
    println("Yay 2");
    level = level + 1;

    if (level == 2) {
    low = 494;
    high = 506;
    }

    if (level == 3) {
    low = 497;
    high = 503;
    }

    if (level == 4) {
    low = 497;
    high = 503;
    }
    if (level == 5) {
    low = 497;
    high = 503;
    }

    if (level == 6) {
    low = 498.5;
    high = 501.5;
    }

    if (level == 7) {
    low = 498.5;
    high = 501.5;
    }

    if (level == 8) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 9) {
    low = 499.25;
    high = 500.75;
    }

    if (level == 10) {
    low = 499.25;
    high = 500.75;
    }

    if (level == 11) {
    low = 499.25;
    high = 500.75;
    }

    if (level == 12) {
    low = 499.8125;
    high = 500.1875;
    }


    if (level == 13) {
    low = 499.8125;
    high = 500.1875;
    }


    if (level == 14) {
    low = 499.8125;
    high = 500.1875;
    }
    }
    else {
    println("Nay 2");
    level = level - 1;
    }
    }
    }
    }


    void stop()
    {

    out.close();
    minim.stop();

    super.stop();
    }


Comments

  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Hmm, well is there any values there that don't change? Make them constant. Your variable names could be a little more meaningful. A few comments wouldn't go astray either.


  • Registered Users Posts: 3,911 ✭✭✭GTE


    Hmm, well is there any values there that don't change? Make them constant. Your variable names could be a little more meaningful. A few comments wouldn't go astray either.

    Thanks, I was silly not to comment it before I posted it here. Im just in the middle of doing it now having finished it.


    EDIT:

    This is a draft of the final thing.
    EDIT2:
    Found a wee spelling mistake when you finish all 16 levels.
    import ddf.minim.*;
    import ddf.minim.signals.*;
    Minim minim;
    AudioOutput out;
    SineWave sine;
    SineWave sine2; // Audio setup

    PFont font;
    PFont font2;
    PFont font3;

    float n1;
    float n2;
    float s1;
    float s2;
    float low;
    float high; // sets up floats for low sine and high sine wave information below

    int level;
    int levelview; // Sets up level progression information

    void setup()
    {
    size(800, 500,P2D);
    background(0);

    levelview = level + 1; // As level starts from 0, this adds 1 for user interface

    font = loadFont("CenturyGothic-Italic-32.vlw");
    font2 = loadFont("CenturyGothic-Italic-24.vlw");
    font3 = loadFont("CenturyGothic-Italic-16.vlw"); // Loads fonts of 3 different sizes


    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);
    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview, 0, 310, 800, 100);

    low = 400;
    high = 600;
    println("Void Setup");
    println("Low = " + low);
    println("High = " + high); // prints low and high frequency information

    println(" ");
    n1 = low;
    n2 = high; // turns low and high to n1 and s2 for the inital set of sine frequecies values


    float[] sinesource = new float [2];
    sinesource[0] = n1;
    sinesource[1] = n2; // n1 and n2 are placed in an array where they are randomly chosen to supply the frequency for sine 1 (s1) and sine2 (s2)

    s1 = sinesource[int(random(0,2))];
    println("Sine Source 1 " + s1);

    if(s1 == n1) {
    s2 = n2;
    }
    else {
    s2 = n1;
    } // when one piece of information has been taken out of the array to supply one sine wave, this insures the other is the unchosen value from the array.
    // this has the effect of randomising the s1 and s2 values so low or high are not always played in the same order.

    println("Sine Source 2 " + s2);

    minim = new Minim(this);
    out = minim.getLineOut(Minim.STEREO, 4048); // final audio output setup
    }

    void draw()
    {
    stroke(255);
    if(keyPressed) {

    if(key == 'e' || key == 'E') { // exit, reset and continue where left off features are here.
    exit();
    }
    if(key == 'r' || key == 'R') {
    level = 0;
    levelview = level;

    background(0);

    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);
    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview, 0, 310, 800, 100);
    }

    if(key == 'c' || key == 'C') {
    level = level;

    background(0);

    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);
    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview, 0, 310, 800, 100);
    }

    if (key == 's' || key == 'S') { // this plays the sound whenever the user wants.

    n1 = low;
    n2 = high; // This takes the new low and high values from the if statements below and converts them to n1 and n2 for clarity as above.

    float[] sinesource = new float [2];
    sinesource[0] = n1;
    sinesource[1] = n2;
    s1 = sinesource[int(random(0,2))];
    println("Sine Source 1 " + s1);

    if(s1 == n1) {
    s2 = n2;
    }
    else {
    s2 = n1;
    } // randomiser as seen above

    println("Sine Source 2 " + s2);

    sine = new SineWave(s1, 0, out.sampleRate()); // this plays the sine waves s1 and s2
    sine2 = new SineWave(s2, 0, out.sampleRate()); // Note there is no amplitude
    out.addSignal(sine);
    out.addSignal(sine2);
    sine.setAmp(0.5);

    delay(1500);
    sine.setAmp(0); // the first sinewave is raised and lowered in volume

    delay(800);
    sine2.setAmp(0.5);
    delay(1400); // as is the second sine wave

    fill(59,234,12);
    text("Which tone was higher in pitch?", 0, 400, 800, 100);
    text("Please choose 1 or 2", 0, 450, 800, 100);
    delay(100);
    sine2.setAmp(0); // User is given the choice. For visual integrety I have chosen the writing to come up 1 tenth of a second before sine2 is ramped down in amp.
    }
    }
    }

    void keyPressed() { // if the first sine was higher in pitch
    if(key == '1') {
    if(s1 > s2) { // checkes if true

    println("Yay 1"); // the following updates level information and resets the screen graphics while updating elements like the level counter.

    level = level + 1;
    levelview= level + 1;

    font = loadFont("CenturyGothic-Italic-32.vlw");
    font2 = loadFont("CenturyGothic-Italic-24.vlw");


    background(0);

    font = loadFont("CenturyGothic-Italic-32.vlw");
    font2 = loadFont("CenturyGothic-Italic-24.vlw");

    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);
    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview + ".", 0, 310, 800, 100);

    if (level == 1) { // low and high sine freq information spanning the levels.
    low = 450;
    high = 550;
    }
    if (level == 2) {
    low = 480;
    high = 520;
    }
    if (level == 3) {
    low = 490;
    high = 510;
    }
    if (level == 4) {
    low = 494;
    high = 506;
    }
    if (level == 5) {
    low = 497;
    high = 503;
    }
    if (level == 6) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 7) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 8) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 9) {
    low = 499.25;
    high = 500.75;
    }
    if (level == 10) {
    low = 499.25;
    high = 500.75;
    }
    if (level == 11) {
    low = 499.25;
    high = 500.75;
    }
    if (level == 12) {
    low = 499.8125;
    high = 500.1875;
    }
    if (level == 13) {
    low = 499.8125;
    high = 500.1875;
    }
    if (level == 14) {
    low = 499.8125;
    high = 500.1875;
    }
    if (level == 15) {
    background(0);
    text("You completed the 16 levels.", 5, 100, 800, 100);
    text("You can consistently difference two tones as little as " + (high - low)+ " Hz in difference! Well done!", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    else {
    println("Nay 1"); // Failure - User given options to reset, continure where they were, exit
    level = level;

    font = loadFont("CenturyGothic-Italic-32.vlw");
    font2 = loadFont("CenturyGothic-Italic-24.vlw");

    background(128, 0, 0);
    textFont(font);
    fill(255);
    text("You reached level " + levelview + ".", 5, 100, 800, 100);
    text("You can detect a pitch difference of as little as " + (high - low)+ " Hz.", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    if(levelview > 1) {
    textFont(font2);
    text("Press C to continue from level " + levelview + ".", 5, 400, 800, 100);
    }
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    else {
    if(key == '2') { // was sine 2 higher in pitch?

    if(s1 < s2) { // checks validity, updates level and resets screen graphics
    println("Yay 2");
    level = level + 1;
    levelview = level + 1;

    background(0);

    font = loadFont("CenturyGothic-Italic-32.vlw");
    font2 = loadFont("CenturyGothic-Italic-24.vlw");

    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);
    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview + ".", 0, 310, 800, 100);

    if (level == 1) { // high and low information for sine wave freq
    low = 450;
    high = 550;
    }
    if (level == 2) {
    low = 480;
    high = 520;
    }
    if (level == 3) {
    low = 490;
    high = 510;
    }
    if (level == 4) {
    low = 494;
    high = 506;
    }
    if (level == 5) {
    low = 497;
    high = 503;
    }
    if (level == 6) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 7) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 8) {
    low = 498.5;
    high = 501.5;
    }
    if (level == 9) {
    low = 499.25;
    high = 500.75;
    }
    if (level == 10) {
    low = 499.25;
    high = 500.75;
    }
    if (level == 11) {
    low = 499.25;
    high = 500.75;
    }
    if (level == 12) {
    low = 499.8125;
    high = 500.1875;
    }
    if (level == 13) {
    low = 499.8125;
    high = 500.1875;
    }
    if (level == 14) {
    low = 499.8125;
    high = 500.1875;
    }
    if (level == 15) {
    background(0);
    text("You completed the 16 levels.", 5, 100, 800, 100);
    text("You can consistently difference two tones as little as " + (high - low)+ " Hz in difference! Well done!", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    else {
    println("Nay 2"); //failure
    level = level;

    font = loadFont("CenturyGothic-Italic-32.vlw");
    font2 = loadFont("CenturyGothic-Italic-24.vlw");

    background(128, 0, 0);
    textFont(font);
    fill(255);
    text("You reached level " + levelview + ".", 5, 100, 800, 100);
    text("You can detect a pitch difference of as little as " + (high - low)+ " Hz.", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    if(levelview > 1) {
    textFont(font2);
    text("Press C to continue from level " + levelview + ".", 5, 400, 800, 100);
    }
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    }
    }


    void stop()
    {
    out.close();
    minim.stop();
    super.stop();
    }


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Your keyPressed function has duplicate code and FTLOG use a switch statement over all those if's.

    Where you have the following
    if (level == 1) { // high and low information for sine wave freq
    low = 450;
    high = 550;
    }
    if (level == 2) { 
    low = 480;
    high = 520;
    }
    if (level == 3) { 
    low = 490;
    high = 510;
    } 
    etc etc
    

    If the level is one it will continue to evaluate each and every if statement down the line.

    Also those two big sets of if's in relation to the level are identical. So write it once and call it twice i.e. stick it in a method and pass the method the level parameter and as I said change the if's into a simple switch. The below should point you in the right direction. If you use it you should end up cutting about 100 lines of code out!
    private void SetLevels(int lowLevel, int highLevel)
            {
                low = lowLevel;
                high = highLevel;
            }
    
            private void demo(int level)
            {
                switch (level)
                { 
                    case 1:
                        SetLevels(450, 550);
                        break;
                    case 2:
                        SetLevels(480, 520);
                        break;
                    case 3:
                        SetLevels(490, 510);
                        break;
                    case 4:
                        SetLevels(494, 506);
                        break;
                }
            }
    

    Also think about putting the High and low values per level into some form of dictionary/array which ever you are more comfortable with or available to you. This is more for maintainability than anything else. If you want to change your highs and lows you will be happy you did.
    public class test
        {
            private struct SineRange
            {
                public SineRange(int low, int high)
                {
                    this.Low = low;
                    this.High = high;
                }
    
                public int Low;
                public int High;
            }
    
            private Dictionary<int, SineRange> SineRanges = new Dictionary<int, SineRange>();
            private int low;
            private int high;
    
    
            private void InitRanges()
            {
                SineRanges.Add(1, new SineRange(450, 550));
                SineRanges.Add(2, new SineRange(480, 520));
                SineRanges.Add(3, new SineRange(490, 510));
                SineRanges.Add(4, new SineRange(4494, 506));
            }
    
            private void SetLevels(SineRange ranges)
            {
                low = ranges.Low;
                high = ranges.High;
            }
    
            private void demo(int level)
            {
                if (level < 15)
                    SetLevels(SineRanges[level]);
                else
                {
                    background(0);
                    text("You completed the 16 levels.", 5, 100, 800, 100);
                    text("You can consistently difference two tones as little as " + (high - low) + " Hz in difference! Well done!", 5, 200, 800, 100);
                    text("Press R to reset to level 1. ", 5, 300, 800, 100);
                    textFont(font3);
                    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
                    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
                    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
                    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
                    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
                    fill(255, 21, 0);
                    text("Press E to exit.", 600, 450, 250, 50);
                }
            }
        }
    

    There is some small pointers for a start :)


  • Registered Users Posts: 3,911 ✭✭✭GTE


    Sorted out some duplication of font errors and got rid of the ifs, made it shorter but then I added a load of printlns for clarity.


    [PHP]import ddf.minim.*;
    import ddf.minim.signals.*;
    Minim minim;
    AudioOutput out;
    SineWave sine;
    SineWave sine2; // Audio setup

    PFont font;
    PFont font2;
    PFont font3;

    float n1;
    float n2;
    float s1;
    float s2;

    float[] lowValues = {
    400, 450, 480, 490, 494, 497, 498.5, 498.5, 498.5, 499.25, 499.25, 499.25, 499.8125, 499.8125, 499.8125, 0
    };
    float[] highValues = {
    600, 550, 520, 510, 506, 503, 501.5, 501.5, 501.5, 500.75, 500.75, 500.75, 500.1875, 500.1875, 500.1875, 0
    };

    int level;
    int levelview; // Sets up level progression information

    void setup()
    {
    println("Void Setup");
    size(800, 500,P2D);
    background(0);

    level = 0;
    levelview = level + 1; // As level starts from 0, this adds 1 for user interface

    println("Level = " + level);
    println("Levelview = " + levelview);
    println(" ");
    font = loadFont("CenturyGothic-Italic-32.vlw");
    font2 = loadFont("CenturyGothic-Italic-24.vlw");
    font3 = loadFont("CenturyGothic-Italic-16.vlw"); // Loads fonts of 3 different sizes


    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);

    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);

    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview + ".", 0, 310, 800, 100);

    minim = new Minim(this);
    out = minim.getLineOut(Minim.STEREO, 4048); // final audio output setup
    }

    void draw()
    {
    }

    void keyPressed() {

    if(keyPressed) {
    if(key == 'e' || key == 'E') { // exit, reset and continue where left off features are here.
    exit();
    }

    if(key == 'r' || key == 'R') {

    println("User has reset the game");
    level = 0;
    levelview = level + 1;
    println("Level = " + level);
    println("Levelview = " + levelview);
    println(" ");

    background(0);
    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);

    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);

    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview, 0, 310, 800, 100);
    }

    if(key == 'c' || key == 'C') {

    println("User has continued the game after an incorrect answer ");

    println(" ");
    s1 = lowValues[level];
    s2 = highValues[level];
    println("s1 and s2 after being reset to the value array ");
    println("s1 = " + s1 + " s2 = " + s2);
    println(" ");

    background(0);
    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);

    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);

    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview, 0, 310, 800, 100);
    }

    if (key == 's' || key == 'S') { // this plays the sound whenever the user wants.

    println("User has started playback for level " + levelview);
    n1 = lowValues[level];
    n2 = highValues[level]; // This takes the the level dependant low and high values from the array towards the beginning

    println("n1 before randomising = " + n1);
    println("n2 before randomising = " + n2);

    float[] sinesource = new float [2]; //It converts these values to n1 and n2, which are placed in an array and randomised
    sinesource[0] = n1; // This randomisation stops low or high values always being placed first or last.
    sinesource[1] = n2;
    s1 = sinesource[int(random(0,2))];


    if(s1 == n1) { // if s1 = n1 then s2 is n2.
    s2 = n2;
    }
    else { // is s1 = n2 then s2 = n1
    s2 = n1;
    }

    println("s1, After n1/n2 randomisation = " + s1);
    println("s2, After n1/n2 randomisation = " + s2);
    println(" ");


    sine = new SineWave(s1, 0, out.sampleRate()); // this plays the sine waves s1 and s2
    sine2 = new SineWave(s2, 0, out.sampleRate()); // Note there is no amplitude
    out.addSignal(sine);
    out.addSignal(sine2);
    sine.setAmp(0.5);

    delay(1500);
    sine.setAmp(0); // the first sinewave is raised and lowered in volume

    delay(800);
    sine2.setAmp(0.5);
    delay(1400); // as is the second sine wave

    fill(59,234,12);
    text("Which tone was higher in pitch?", 0, 400, 800, 100);
    text("Please choose 1 or 2", 0, 450, 800, 100);
    delay(100);
    sine2.setAmp(0); // User is given the choice. For visual integrety I have chosen the writing to come up 1 tenth of a second before sine2 is ramped down in amp.
    }
    }




    if(key == '1') {
    if(s1 > s2) { // if the first sine was higher in pitch
    println("Yay 1");

    println("User has chosen 1, the correct answer"); // the following updates level information and resets the screen graphics while updating elements like the level counter.
    println("Level and levelview were " + level + " and " + levelview);

    level = level + 1;
    levelview = level + 1;
    println("Level and levelview are now " + level + " and " + levelview);

    s1 = s2; // stops people continually pressing 1 or 2 to advance through the levels with no audio.
    println("s1 and s2 have been equalised to avoid cheating. They will be reset to value array later. s1 and s2 = " + s1 + "and " + s2);
    println(" ");
    background(0);

    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);
    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview + ".", 0, 310, 800, 100);

    if (level == 15) { //16th level is a congrat screen

    println("User has completed the application");
    background(0);
    text("You completed the 15 levels.", 5, 100, 800, 100);
    text("You can consistently difference two tones as little as 0.375Hz in difference! Well done!", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    else {
    println("Nay 1"); // Failure - User given options to reset, continure where they were, exit



    if (s1 == s2) {
    println("User has pressed 1 or 2 after getting a correct answer, before hearing new set of sine waves either by accident or to cheat.");
    println(" ");
    }
    else {
    println("User has chosen 1, the incorrect answer");
    println(" ");
    background(128, 0, 0);
    textFont(font);
    fill(255);
    text("You reached level " + levelview + ".", 5, 100, 800, 100);
    text("You can detect a pitch difference of as little as " + (highValues[level] - lowValues[level])+ " Hz.", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    if(levelview > 1) {
    textFont(font2);
    text("Press C to continue from level " + levelview + ".", 5, 400, 800, 100);
    }
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    }
    else {
    if(key == '2') { // was sine 2 higher in pitch?

    if(s1 < s2) { // if the first sine was higher in pitch
    println("Yay 1");

    println("User has chosen 1, the correct answer"); // the following updates level information and resets the screen graphics while updating elements like the level counter.
    println("Level and levelview were " + level + " and " + levelview);

    level = level + 1;
    levelview = level + 1;
    println("Level and levelview are now " + level + " and " + levelview);

    s1 = s2; // stops people continually pressing 1 or 2 to advance through the levels with no audio.
    println("s1 and s2 have been equalised to avoid cheating. They will be reset to value array later. s1 and s2 = " + s1 + "and " + s2);
    println(" ");
    background(0);

    textFont(font);
    fill(0, 102, 153);
    text("Welcome to the pitch tester application.", 0, 0, 800, 100);
    textFont(font2);
    fill(234, 12, 12);
    text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800);
    fill(255);
    text("How to use:", 0, 170, 800, 100);
    text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100);
    fill(134);
    text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    fill(12,229,234);
    text("This is level " + levelview + ".", 0, 310, 800, 100);

    if (level == 15) { //16th level is a congrat screen


    println("User has completed the application.");
    background(0);
    text("You completed the 15 levels.", 5, 100, 800, 100);
    text("You can consistently difference two tones as little as 0.375Hz in difference! Well done!", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    else {
    println("Nay 2"); // Failure - User given options to reset, continure where they were, exit



    if (s1 == s2) {
    println("User has pressed 1 or 2 after getting a correct answer, before hearing new set of sine waves either by accident or to cheat.");
    println(" ");
    }
    else {
    println("User has chosen 2, the incorrect answer");
    println(" ");
    background(128, 0, 0);
    textFont(font);
    fill(255);
    text("You reached level " + levelview + ".", 5, 100, 800, 100);
    text("You can detect a pitch difference of as little as " + (highValues[level] - lowValues[level])+ " Hz.", 5, 200, 800, 100);
    text("Press R to reset to level 1. ", 5, 300, 800, 100);
    if(levelview > 1) {
    textFont(font2);
    text("Press C to continue from level " + levelview + ".", 5, 400, 800, 100);
    }
    textFont(font3);
    text("Less than .75 Hz is exceptional.", 450, 320, 250, 200);
    text("Less than 1.5 Hz is very good.", 450, 340, 250, 50);
    text("Less than 6 Hz is normal.", 450, 360, 250, 50);
    text("Less than 12 Hz is below normal.", 450, 380, 250, 50);
    text("Less than 16 Hz is evidence of possible perception problems.", 450, 400, 250, 50);
    textFont(font2);
    fill(255, 21, 0);
    text("Press E to exit.", 600, 450, 250, 50);
    }
    }
    }
    }
    }


    void stop()
    {
    out.close();
    minim.stop();
    super.stop();
    }
    [/PHP]


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Still a lot of duplicate code take a look at the test your writing out, break the different pieces that you write out a lot and stick them in a function and call that. The below appears 5 times in a single function.... Thats not a code smell its a code stink.
    textFont(font);  
      fill(0, 102, 153); 
      text("Welcome to the pitch tester application.", 0, 0, 800, 100); 
    
      textFont(font2); 
      fill(234, 12, 12); 
      text("This app is designed to test your ability to tell the which of two tones is higher pitched then the other.", 0, 105, 800, 800); 
      fill(255); 
      text("How to use:", 0, 170, 800, 100); 
      text("S - Press this to play the tone at the start of a new level. Use as much as you need during each level.", 0, 190, 800, 100); 
      fill(134); 
      text("To choose which tone is higher in pitch, press 1 or 2 for the 1st or 2nd.", 0, 247, 800, 100); 
    
      textFont(font2); 
      fill(255, 21, 0); 
      text("Press E to exit.", 600, 450, 250, 50); 
      fill(12,229,234); 
      text("This is level " + levelview + ".", 0, 310, 800, 100);
    

    Your keypressed handler is still nearly 300 lines long. This just reduces maintainability. Break it into smaller functions pretty please with sugar on top.

    Again with the ifs......
    if(key == 'r' || key == 'R')
    

    I don't know what lang you are using be check to see if there is a ToUpper for strings and you can remove the OR part of your clause.

    Also you still have the if's where a switch would offer a better way of doing things...
    switch (key.ToUpper())
    {
    case "E":
        exit();
        break;
    case "R":
        Reset();
        break;
    case "C":
        Continue();
        break;
    ....
    .....
    }
    


  • Advertisement
  • Registered Users Posts: 3,911 ✭✭✭GTE


    Thanks,

    Ill look into that.


    With regards to ToUpper I do not believe Processing has provisions for that. Certainly I have never come across it or anything that looks familiar to what you typed there.


  • Registered Users Posts: 3,805 ✭✭✭Setun


    bbk wrote: »
    Thanks,

    Ill look into that.


    With regards to ToUpper I do not believe Processing has provisions for that. Certainly I have never come across it or anything that looks familiar to what you typed there.

    If java has it then processing can use it. If toUpper is part of a specific java library then you may have to import it, same as the way you are importing minim at the top of your sketch.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    I remember doing some Processing before. I just checked the API there for String.

    http://processing.org/reference/String.html

    Seems to have that method alright.


  • Registered Users Posts: 3,911 ✭✭✭GTE


    Thanks for those replies.

    Great tips for the beginner!

    Thanks =D


Advertisement