Package jm.music.data

Examples of jm.music.data.Phrase


        double[] mutationArray = new double[population.length];
        for (int i = 0; i < population.length; i++) {
            mutationArray[i] = population[i].getEndTime();
        }
        for (int i = 0; i < population.length; i++) {
            Phrase individual = population[i];
           
            //Change the seed portion of the extension
            if(modifyAll) {
                initialSize = 0;
                initialLength = 0.0;
            }
           
            // 1. Random Pitch Change

            int n = individual.size() - initialSize;
            double n2change1 = n * MUTATE_PERCENTAGE[0] / 100.0;
            int n2change = 0;
            if (n2change1 < 1.0) {
                if (Math.random() < n2change1) {
                    n2change = 1;
                } else {
                    n2change = 0;
                }
            } else {
                n2change = (int) Math.floor(n2change1);
            }
            for (int j = 0; j < n2change; j++) {
                int r = (int) (Math.random() * n);
                mutate(individual.getNote(initialSize + r));
            }

            // 2. Bar sequence mutations

            if (Math.random() < MUTATE_PERCENTAGE[1] / 100.0) {
                int previousNoteOnBar = 0;
                double beatCount = 0;
                for (int j = 0; j < individual.size(); j++) {
                    beatCount += individual.getNote(j).getRhythmValue();
                }
                int[] notesOnBars = new int[(int) beatCount];
                int[] barNumber = new int[(int) beatCount];
                int index = 0;
                beatCount = 0;
                for (int j = 0; j < individual.size(); j++) {
                    if (beatCount / (double) beatsPerBar
                            == Math.floor(beatCount / (double) beatsPerBar)) {
                        notesOnBars[index] = j;
                        barNumber[index++] = (int) (beatCount * beatsPerBar);
                    }
                    beatCount += individual.getNote(j).getRhythmValue();
                }
                int countOfEncapsulatedBars = 0;
                int[] notesBeginningEncapsulatedBars = new int[index];
                if (index > 0) {
                    for (int j = 1; j < index; j++) {
                        if (barNumber[j] == barNumber[j - 1] + 1) {
                            notesBeginningEncapsulatedBars[
                                    countOfEncapsulatedBars++] = notesOnBars[j - 1];
                        }
                    }
                }
                if (countOfEncapsulatedBars > 0) {
                    int r2 = 0;
                    while (r2 < (int) (initialLength / beatsPerBar) - 1) {
                        r2 = (int) (Math.random() * countOfEncapsulatedBars);
                    }
                    int r3 = (int) (Math.random() * 2 + 1);
                    switch(r3) {
                    case 1:
                        int transpose = 0;
                        if (Math.random() < 0.5) {
                            transpose = 2;
                        } else {
                            transpose = -2;
                        }
                        beatCount = 0;
                        index = notesBeginningEncapsulatedBars[r2];
                        while (beatCount < beatsPerBar) {
                            shiftPitch(individual.getNote(index), transpose);
                            beatCount += individual.getNote(index++).getRhythmValue();
                        }
                        break;
                    case 2:
                    default:
                        index = notesBeginningEncapsulatedBars[r2];
                        beatCount = 0;
                        while (beatCount < beatsPerBar) {
                            beatCount += individual.getNote(index++).getRhythmValue();
                        }
                        int notesInBar = index - notesBeginningEncapsulatedBars[r2];
                        index = notesBeginningEncapsulatedBars[r2];
                        if (notesInBar > 0) {
                            int[] tempPitches = new int[notesInBar];
                            double[] tempRhythmValues = new double[notesInBar];
                            for (int j = 0; j < notesInBar; j++) {
                                tempPitches[j] = individual.getNote(j + index).getPitch();
                                tempRhythmValues[j] = individual.getNote(j + index).getRhythmValue();
                            }
                            for (int j = 0; j < notesInBar; j++) {
                                individual.getNote(j + index).setPitch(tempPitches[notesInBar - j - 1]);
                                individual.getNote(j + index).setRhythmValue(tempRhythmValues[notesInBar - j - 1]);
                            }
                        }
                    }
                }
            }

            // 3. Split and Merge
           
            int n1 = individual.size() - initialSize;
            double n2change2 = n1 * MUTATE_PERCENTAGE[2] / 100.0;
            int n2change3 = 0;
            if (n2change2 < 1.0) {
                if (Math.random() < n2change2) {
                    n2change3 = 1;
                } else {
                    n2change3 = 0;
                }
            } else {
                n2change3 = (int) Math.floor(n2change2);
            }
            Vector vector = (Vector) individual.getNoteList().clone();
            for (int j = 0; j < n2change3; j++) {
                int r1 = (int) (Math.random() * (n1 - 1));
                Note note5 = (Note) vector.elementAt(initialSize + r1);
                int pitch5 = note5.getPitch();
                double rhythmValue5 = note5.getRhythmValue();
                if (rhythmValue5 >= 1.0 && rhythmValue5%1.0 == 0 &&
                        rhythmValue5 * 2.0 == Math.ceil(rhythmValue5 * 2.0)) {
                    vector.removeElementAt(initialSize + r1);
                    vector.insertElementAt(new Note(pitch5,
                                                    rhythmValue5 / 2.0),
                                           initialSize + r1);
                    vector.insertElementAt(new Note(pitch5,
                                                    rhythmValue5 / 2.0),
                                           initialSize + r1);
                    n1++;
                } else {
                    double rhythmValue6 = rhythmValue5 + ((Note)
                            vector.elementAt(initialSize + r1 + 1))
                                  .getRhythmValue();
                    if (rhythmValue6 <= 2.0) {
                        vector.removeElementAt(initialSize + r1);
                        vector.removeElementAt(initialSize + r1);
                        vector.insertElementAt(new Note(pitch5,
                                                        rhythmValue6),
                                               initialSize + r1);
                        n1--;
                    }
                }
            }
            individual.addNoteList(vector, false);
           
           
            // 4. Step interpolation
            vector = (Vector) individual.getNoteList().clone();
            int currentPitch;
            double currentRV;
            int previousPitch = (int)Note.REST;
            double previousRV = 0;
            int index1 = initialSize;
            while (index1 < vector.size() && previousPitch == Note.REST) {
                previousPitch = ((Note) vector.elementAt(index1)).getPitch();
                previousRV = ((Note) vector.elementAt(index1)).getRhythmValue();
                index1++;
            }
            int k = index1;
            while (k < vector.size()) {
                currentPitch = ((Note) vector.elementAt(k)).getPitch();
                currentRV = ((Note) vector.elementAt(k)).getRhythmValue();
                if (currentPitch != Note.REST) {
                    int interval = currentPitch - previousPitch;
                    if ((Math.abs(interval) == 4 || Math.abs(interval) == 3)
                            && Math.random() < (MUTATE_PERCENTAGE[3] / 100.0)) {
                        int scalePitch = 0;
                        if (interval > 0) {
                            scalePitch = currentPitch - 1;
                            if (!isScale(scalePitch)) {
                                scalePitch--;
                            }
                        } else {
                            scalePitch = currentPitch + 1;
                            if (!isScale(scalePitch)) {
                                scalePitch++;
                            }
                        }
                        if (currentRV > previousRV) {
                            if (currentRV >= 0.5
                                    && (int) Math.ceil(currentRV * 2)
                                       == (int) (currentRV * 2)) {
                                vector.removeElementAt(k);
                                vector.insertElementAt(new Note(currentPitch,
                                        currentRV / 2.0), k);
                                vector.insertElementAt(new Note(scalePitch,
                                        currentRV / 2.0), k);
                                k++;
                            }
                        } else {
                            if (previousRV >= 0.5
                                    && (int) Math.ceil(previousRV * 2)
                                       == (int) (previousRV * 2)) {
                                vector.removeElementAt(k - 1);
                                vector.insertElementAt(new Note(scalePitch,
                                        previousRV / 2.0), k - 1);
                                vector.insertElementAt(new Note(previousPitch,
                                        previousRV / 2.0), k - 1);
                                k++;
                            }
                        }
                    }
                    previousPitch = currentPitch;
                    previousRV = currentRV;
                }
                k++;
            }

            individual.addNoteList(vector, false);

            // 5. Tonal Pauses (make well positioned primary pitches longer
                // by adding the value of two notes together)

            individual.addNoteList(applyTonalPausesMutation(individual,
                                                            initialLength,
                                                            initialSize,
                                                            beatsPerBar),
                                   false);

            // 6. Pitch Clean Up

            double cumulativeRV = 0;
            for (int j = initialSize; j < individual.size(); j++) {
                int pitch = individual.getNote(j).getPitch();
                double rv = individual.getNote(j).getRhythmValue();
                if (pitch != Note.REST) {
                    if (!isScale(pitch)) {
                        if ((int) Math.ceil(cumulativeRV / 2.0)
                                == (int) (cumulativeRV / 2.0)) {
                            if (Math.random() < rv) {
                                if (Math.random() < 0.5) {
                                    individual.getNote(j).setPitch(pitch + 1);
                                } else {
                                    individual.getNote(j).setPitch(pitch - 1);
                                }
                            }
                        } else {
                            if (Math.random() < (rv / 2.0)) {
                                if (Math.random() < 0.5) {
                                    individual.getNote(j).setPitch(pitch + 1);
                                } else {
                                    individual.getNote(j).setPitch(pitch - 1);
                                }
                            }
                        }   
                    }
                }
View Full Code Here


        );
        panel.add(populationLabel);
    }       
   
    public Phrase[] initPopulation(final Phrase phrase, final int beatsPerBar) {
        Phrase seed = completeFinalBeat(phrase, beatsPerBar);
        int size;
        if(modifyAll) {
            size = 0;
        } else size = seed.size();
        double[][] beatRhythmArray = generateBeatRhythmArray(seed, beatsPerBar);
        int[] intervalArray = generateIntervalArray(seed);


        Phrase[] population = new Phrase[populationSize];
        for (int i = 0; i < populationSize; i++) {
            population[i] = seed.copy();

            Note target;
            int targetBeat;
            int climax = 0;

   
            // Set target
            if (isClimaxAccepted(seed, beatsPerBar)) {
                climax = findClimax(seed);
                target = new Note(TONIC, (double) beatsPerBar);
                targetBeat = 7 * beatsPerBar;
            } else {
                int lowestPitch = Note.MAX_PITCH;
                for (int j = 0; j < seed.size(); j++) {
                    int currentPitch = seed.getNote(j).getPitch();
                    if (currentPitch != Note.REST && currentPitch < lowestPitch) {
                        lowestPitch = currentPitch;
                    }
                }
                target = generateClimax(lowestPitch);
                climax = target.getPitch();
                targetBeat = 4 * beatsPerBar;
            }


            /* Find the absolute minimum pitch which is the lower of an octave
             * below the starting note or a fifth below middle C (53),.
             */
            int lowerlimit = -1;
            for (int j = 0; j < seed.size(); j++) {
                int pitch = seed.getNote(j).getPitch();
                if (pitch != Note.REST) {
                    lowerlimit = pitch - 12;
                    break;
                }
            }
View Full Code Here

       
        return population;
    }

    private Phrase completeFinalBeat(final Phrase phrase, int beatsPerBar) {
        Phrase returnPhrase = phrase.copy();

        double length = returnPhrase.getEndTime();

        // Because beats are integer multiples of a 1, a crotchet
        double rhythmValueToCompleteBeat = Math.ceil(length) - length;

        // If the melody's length isn't a whole number of beats
        if (rhythmValueToCompleteBeat > 0) {

            int[] intervals =  generateIntervalArray(phrase);
            int counter = returnPhrase.size() - 1;
            int pitch = (int)Note.REST;
            while (pitch == Note.REST) {
                pitch = returnPhrase.getNote(counter--)
                                        .getPitch();
            }
            pitch += intervals[(int) (Math.random() * intervals.length)];
            if (!isScale(pitch)) {
                if (Math.random() < 0.5) {
                    ++pitch;
                } else {
                    --pitch;
                }
            }

            // Complete the beat
            returnPhrase.addNote(new Note(pitch,
                                          rhythmValueToCompleteBeat));
        }
        return returnPhrase;
    }       
View Full Code Here

    private static int rand(final int left, final int right) {
        return left + (int) (Math.random() * (right - left)) + 1;
    }

    private void swap(final int i, final int j) {
        Phrase tempPhrase = population[i];
        population[i] = population[j];
        population[j] = tempPhrase;

        double tempDouble = fitness[i];
        fitness[i] = fitness[j];
View Full Code Here

      }
    }
    //need to create new phrase for a new voice?
    if(phrIndex == -1){
      phrIndex = phrVct.size();
      phrVct.addElement(new Phrase(startTime));
      currentLength[phrIndex] = startTime;
    }
    //Do we need to add a rest ?
    if((startTime > currentLength[phrIndex])&&
        (curNote[phrIndex] != null)){
View Full Code Here

    double scoreTempo = score.getTempo();
    double partTempoMultiplier = 1.0;
    double phraseTempoMultiplier = 1.0;
    int phraseNumb;
    Phrase phrase1, phrase2;

    //Add a tempo track at the start of top of the list
    //Add time sig to the tempo track
    Track smfT = new Track();
    smfT.addEvent(new TempoEvent(0, score.getTempo()));
    smfT.addEvent(new TimeSig(0, score.getNumerator(),score.getDenominator()));
    smfT.addEvent(new KeySig(0, score.getKeySignature()));
    smfT.addEvent(new EndTrack());
    smf.getTrackList().addElement(smfT);
    //---------------------------------------------------
    int partCount = 0;
    Enumeration aEnum = score.getPartList().elements();
    while(aEnum.hasMoreElements()){
      Track smfTrack = new Track();
      Part inst = (Part) aEnum.nextElement();
      System.out.print("    Part "+ partCount + " '" + inst.getTitle() +
          "' to SMF Track on Ch. " + inst.getChannel() + ": ");
      partCount++;

      // set up tempo difference between score and track - if any
      if(inst.getTempo() != Part.DEFAULT_TEMPO) partTempoMultiplier =
        scoreTempo / inst.getTempo();
      else partTempoMultiplier = 1.0;
      //System.out.println("partTempoMultiplier = " + partTempoMultiplier);

      //order phrases based on their startTimes
      phraseNumb = inst.getPhraseList().size();
      for(int i=0; i< phraseNumb; i++){
        phrase1 = (Phrase) inst.getPhraseList().elementAt(i);
        for(int j=0; j<phraseNumb; j++){
          phrase2 = (Phrase)inst.getPhraseList().elementAt(j);
          if(phrase2.getStartTime() > phrase1.getStartTime()){
            inst.getPhraseList().setElementAt( phrase2, i );
            inst.getPhraseList().setElementAt( phrase1, j );
            break;
          }
        }
      }
      //break Note objects into NoteStart's and NoteEnd's
      //as well as combining all phrases into one list
//      HashMap midiEvents = new HashMap();

      class EventPair{public double time; public Event ev; public EventPair(double t, Event e){time = t; ev = e;}};
      LinkedList<EventPair> midiEvents = new LinkedList<EventPair>();
     
      /*if(inst.getTempo() != Part.DEFAULT_TEMPO){
        //System.out.println("Adding part tempo");
        midiEvents.add(new EventPair(0, new TempoEvent(inst.getTempo())));
      } */
      //if this part has a Program Change value then set it     
      if(inst.getInstrument() != NO_INSTRUMENT){
        //System.out.println("Instrument change no. " + inst.getInstrument());
        midiEvents.add(new EventPair(0, new PChange((short) inst.getInstrument(),(short) inst.getChannel(),0)));
      }

      if(inst.getNumerator() != NO_NUMERATOR){
        midiEvents.add(new EventPair(0, new TimeSig(inst.getNumerator(),inst.getDenominator())));
      }

      if(inst.getKeySignature() != NO_KEY_SIGNATURE){
        midiEvents.add(new EventPair(0, new KeySig(inst.getKeySignature(),inst.getKeyQuality())));
      }

      Enumeration partEnum = inst.getPhraseList().elements();
      double max = 0;
      double startTime = 0.0;
      double offsetValue = 0.0;
      int phraseCounter = 0;
      while(partEnum.hasMoreElements()) {
        Phrase phrase = (Phrase) partEnum.nextElement();
        Enumeration phraseEnum = phrase.getNoteList().elements();
        startTime = phrase.getStartTime() * partTempoMultiplier; 
        if(phrase.getInstrument() != NO_INSTRUMENT){
          midiEvents.add(new EventPair(0, new PChange((short)phrase.getInstrument(),(short)inst.getChannel(),0)));
        }
        if(phrase.getTempo() != Phrase.DEFAULT_TEMPO) {
          phraseTempoMultiplier = scoreTempo / phrase.getTempo(); //(scoreTempo * partTempoMultiplier) / phrase.getTempo();
        } else {
          phraseTempoMultiplier = partTempoMultiplier;
        }

        ////////////////////////////////////////////////
View Full Code Here

     * @param amount -  The number of beats to add.
     */
    public static void addToRhythmValue(CPhrase cphrase, double amount) {
        Enumeration enum1 = cphrase.getPhraseList().elements();
        while(enum1.hasMoreElements()){
            Phrase phr = (Phrase) enum1.nextElement();
            addToRhythmValue(phr, amount);

        }
    }
View Full Code Here

     * @param amount - The number of beats to add.
     */
    public static void addToLength(CPhrase cphrase, double amount) {
        Enumeration enum1 = cphrase.getPhraseList().elements();
        while(enum1.hasMoreElements()){
            Phrase phr = (Phrase) enum1.nextElement();
            addToLength(phr, amount);
        }
    }
View Full Code Here

     * @param amount - The scaling multiplyer for the intervals, i.e., 2.0 doubles width.
     */
    public static void expandIntervals(CPhrase cphrase, double amount) {
        Enumeration enum1 = cphrase.getPhraseList().elements();
        while(enum1.hasMoreElements()){
            Phrase phr = (Phrase) enum1.nextElement();
            expandIntervals(phr, amount);
        }
    }
View Full Code Here

     * @param amount - The dynamic change possible either side of the curent dynamic.
     */
    public static void shake(CPhrase cphrase, int amount) {
        Enumeration enum1 = cphrase.getPhraseList().elements();
        while(enum1.hasMoreElements()){
            Phrase phr = (Phrase) enum1.nextElement();
            shake(phr, amount);
        }
    }
View Full Code Here

TOP

Related Classes of jm.music.data.Phrase

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.