Package jm.music.data

Examples of jm.music.data.Phrase


        } else if (XMLStyles.isValidPartTag(elements[0].getName())) {
            return elementToPart(elements[0]);
        } else if (XMLStyles.isValidPhraseTag(elements[0].getName())) {
            return new Part(elementToPhrase(elements[0]));
        } else if (XMLStyles.isValidNoteTag(elements[0].getName())) {
            return new Part(new Phrase(elementToNote(elements[0])));
        }
        throw new ConversionException("Unrecognised root element: "
                + elements[0].getName());
    }
View Full Code Here


                            + "xmlStringToPart(String) method instead."
            );
        } else if (XMLStyles.isValidPhraseTag(elements[0].getName())) {
            return elementToPhrase(elements[0]);
        } else if (XMLStyles.isValidNoteTag(elements[0].getName())) {
            return new Phrase(elementToNote(elements[0]));
        }
        throw new ConversionException("Unrecognised root element: "
                + elements[0].getName());
    }
View Full Code Here

            throw new ConversionException(
                    "Invalid element: " + element.getName() + ".  The only "
                            + "accepted tag name is '" + xmlStyle.getPhraseTagName() + "'."
            );
        }
        Phrase returnPhrase = new Phrase();
        String attributeValue;

        attributeValue = XMLStyles.getTitleAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPhrase.setTitle(attributeValue);
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getTitleAttributeName() + "' of element '"
                                + xmlStyle.getPhraseTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getStartTimeAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPhrase.setStartTime(
                        Double.valueOf(attributeValue).doubleValue());
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getStartTimeAttributeName() + "' of element '"
                                + xmlStyle.getPhraseTagName() + "' must represent a Java double."
                );
            }
        }
        attributeValue = XMLStyles.getInstrumentAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPhrase.setInstrument(Integer.parseInt(attributeValue));
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getInstrumentAttributeName() + "' of element '"
                                + xmlStyle.getPhraseTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getTempoAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPhrase.setTempo(
                        Double.valueOf(attributeValue).doubleValue());
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getTempoAttributeName() + "' of element '"
                                + xmlStyle.getPhraseTagName() + "' must represent a Java double."
                );
            }
        }
        attributeValue = XMLStyles.getAppendAttributeValue(element);
        if (!attributeValue.equals("")) {
            returnPhrase.setAppend(new Boolean(attributeValue).booleanValue());
        }
        attributeValue = XMLStyles.getPanAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPhrase.setPan(
                        Double.valueOf(attributeValue).doubleValue());
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getPanAttributeName() + "' of element '"
                                + xmlStyle.getPhraseTagName() + "' must represent a Java double."
                );
            }
        }
        Element[] elements = element.getChildren();
        for (int i = 0; i < elements.length; i++) {
            if (XMLStyles.isValidNoteTag(elements[i].getName())) {
                returnPhrase.addNote(elementToNote(elements[i]));
            }
        }
        return returnPhrase;
    }
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

        quicksort(left, last - 1);
        quicksort(last + 1, right);
    }

    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

        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

     * @param exit Hold program open for the duration of playback, then close ready to exit? true or false.
     */
    public static void midi(Note n, boolean exit) {
        //System.out.println("in midi(Note n, boolean exit)");
        Score s = new Score("One note score", 60);
        s.addPart(new Part(new Phrase(n)));
        midi(s, exit);
    }
View Full Code Here

     * @param n     The note to be played. See midiCycle(Score s)
     * @param index The midiCycle id to be used - default is 0.
     */
    public static void midiCycle(Note n, int index) {
        Score s = new Score("One note score");
        s.addPart(new Part(new Phrase(n)));
        midiCycle(s, index);
    }
View Full Code Here

     *
     * @param note The phrase to be played.
     * @param inst An instrument to play the note with
     */
    public static void audio(Note note, Instrument inst) {
        audio(new Phrase(note), inst);
    }
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.