/* Called from chooseNote in Lickgen * Given an interval, the desired type of note, * the types of notes in the interval, and the occurences of each type of note, * looks up the probabilities for what type of note to choose in a table * and returns a note chosen based on the probabilities * attempts is a counter for the number of times we have tried to get all pitches within range */ public int getNote( int minPitch, int maxPitch, int low, int high, int type, int[] numTypes, int[] noteTypes, int attempts) { if (type == CHORD) type = 0; if (type == COLOR) type = 1; if (type == RANDOM) type = 2; if (type == SCALE) type = 3; Polylist prob = new Polylist(); // integers representing whether a certain type is present int haveChord, haveColor, haveRandom; if (numTypes[0] != 0) haveChord = 1; else haveChord = 0; if (numTypes[1] != 0) haveColor = 1; else haveColor = 0; if (numTypes[2] != 0) haveRandom = 1; else haveRandom = 0; // special case for chord tones - they are more important than staying // in the interval // if(type == 0 && haveChord == 0) { // for(int i = low - 4; i <= low + 4; i++) { // // } // } Polylist identifier = Polylist.list(type, haveChord, haveColor, haveRandom); // look for match for (Polylist L = probabilities; L.nonEmpty(); L = L.rest()) { // get first list in probabilities Polylist tempProb = (Polylist) L.first(); // chop off the probabilities so we just have the identifier Polylist tempIdentifier = tempProb.prefix(4); if (identifier.equals(tempIdentifier)) { prob = tempProb.coprefix(4); } } // put the matched probabilities into an array int[] probs = setProb(prob); Random rand = new Random(); // generate note type from probabilities int randNum = rand.nextInt(100) + 1; int newType = 0; // -1; for (int i = 0; i < probs.length; i++) { randNum = randNum - probs[i]; if (randNum <= 0) { newType = i; i = probs.length; } } // get note pitch randNum = rand.nextInt(numTypes[newType]) + 1; int pitchdiff = 0; for (int i = 0; i < noteTypes.length; i++) { if (noteTypes[i] == typeMap[newType] || (newType == 3 && (noteTypes[i] == CHORD || noteTypes[i] == COLOR))) { randNum--; } if (randNum <= 0) { pitchdiff = i; i = noteTypes.length; } } int finalPitch = low + pitchdiff; if (attempts >= LickGen.MELODY_GEN_LIMIT - 1 && doNotSwitchOctave == false) { // raise or lower by an octave if outside bounds while (finalPitch > maxPitch) { finalPitch -= 12; } while (finalPitch < minPitch) { finalPitch += 12; } } return finalPitch; }