Package jm.music.data

Examples of jm.music.data.Part


    public static void SMFToScore(Score score, SMF smf) {
        System.out.println("Convert SMF to JM");
        Enumeration aEnum = smf.getTrackList().elements();
        //Go through tracks
        while (aEnum.hasMoreElements()) {
            Part part = new Part();
            Track smfTrack = (Track) aEnum.nextElement();
            Vector evtList = smfTrack.getEvtList();
            Vector phrVct = new Vector();
            sortEvents(score, evtList, phrVct, smf, part);
            for (int i = 0; i < phrVct.size(); i++) {
                part.addPhrase((Phrase) phrVct.elementAt(i));
            }
            score.addPart(part);
            score.clean();
        }
    }
View Full Code Here


        //---------------------------------------------------
        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;
                }

                ////////////////////////////////////////////////
                int noteCounter = 0;
                //System.out.println();
                System.out.print(" Phrase " + phraseCounter++ + ":");
                // set a silly starting value to force and initial pan cc event
                double pan = -1.0;
                resetTicker(); // zero the ppqn error calculator
                while (phraseEnum.hasMoreElements()) {
                    Note note = (Note) phraseEnum.nextElement();
                    offsetValue = note.getOffset();
                    // add a pan control change if required
                    if (note.getPan() != pan) {
                        pan = note.getPan();
                        midiEvents.add(new EventPair(startTime + offsetValue, new CChange((short) 10, (short) (pan * 127), (short) inst.getChannel(), 0)));
                    }
                    //check for frequency rather than MIDI notes
                    int pitch = 0;
                    if (note.getPitchType() == Note.FREQUENCY) {
                        System.err.println("jMusic warning: converting note frequency to the closest MIDI pitch for SMF.");
                        //System.exit(1);
                        pitch = Note.freqToMidiPitch(note.getFrequency());
                    } else pitch = note.getPitch();
                    if (pitch != REST) {
                        midiEvents.add(new EventPair(new Double(startTime + offsetValue), new NoteOn((short) pitch, (short) note.getDynamic(), (short) inst.getChannel(), 0)));

                        // Add a NoteOn for the END of the note with 0 dynamic, as recommended.
                        //create a timing event at the end of the notes duration
                        double endTime = startTime + (note.getDuration() * phraseTempoMultiplier);
                        // Add the note-off time to the list
                        midiEvents.add(new EventPair(new Double(endTime + offsetValue), new NoteOn((short) pitch, (short) 0, (short) inst.getChannel(), 0)));
                    }
                    // move the note-on time forward by the rhythmic value
                    startTime += tickRounder(note.getRhythmValue() * phraseTempoMultiplier); //time between start times
                    System.out.print("."); // completed a note
                }
View Full Code Here

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

                            + "xmlStringToScore(String) method instead."
            );
        } 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

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

        attributeValue = XMLStyles.getTitleAttributeValue(element);
        if (!attributeValue.equals("")) {
            returnPart.setTitle(attributeValue);
        }
        attributeValue = XMLStyles.getChannelAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setChannel(Integer.parseInt(attributeValue));
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getChannelAttributeName() + "' of element '"
                                + xmlStyle.getPartTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getInstrumentAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setInstrument(Integer.parseInt(attributeValue));
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getInstrumentAttributeName() + "' of element '"
                                + xmlStyle.getPartTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getTempoAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setTempo(
                        Double.valueOf(attributeValue).doubleValue());
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getTempoAttributeName() + "' of element '"
                                + xmlStyle.getPartTagName() + "' must represent a Java double."
                );
            }
        }
        attributeValue = XMLStyles.getKeySignatureAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setKeySignature(Integer.parseInt(attributeValue));
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getKeySignatureAttributeName() + "' of element '"
                                + xmlStyle.getPartTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getKeyQualityAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setKeyQuality(Integer.parseInt(attributeValue));
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getKeyQualityAttributeName() + "' of element '"
                                + xmlStyle.getScoreTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getNumeratorAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setNumerator(Integer.parseInt(attributeValue));
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getNumeratorAttributeName() + "' of element '"
                                + xmlStyle.getPartTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getDenominatorAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setDenominator(Integer.parseInt(attributeValue));
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getDenominatorAttributeName() + "' of element '"
                                + xmlStyle.getPartTagName() + "' must represent a Java integer."
                );
            }
        }
        attributeValue = XMLStyles.getPanAttributeValue(element);
        if (!attributeValue.equals("")) {
            try {
                returnPart.setPan(Double.valueOf(attributeValue).doubleValue());
            } catch (NumberFormatException e) {
                throw new ConversionException(
                        "Invalid attribute value: " + attributeValue + ".  The "
                                + "attribute '" + xmlStyle.getPanAttributeName() + "' of element '"
                                + xmlStyle.getPartTagName() + "' must represent a Java double."
                );
            }
        }
        Element[] elements = element.getChildren();
        for (int i = 0; i < elements.length; i++) {
            if (XMLStyles.isValidPhraseTag(elements[i].getName())) {
                returnPart.addPhrase(elementToPhrase(elements[i]));
            }
        }
        return returnPart;
    }
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

    public static void midi(Phrase phr, boolean exit) {
        double tempo = 60;
        if (phr.getTempo() != Phrase.DEFAULT_TEMPO) tempo = phr.getTempo();
        Score s = new Score(phr.getTitle() + " score", tempo);
        if (phr.getTempo() != Phrase.DEFAULT_TEMPO) s.setTempo(phr.getTempo());
        s.addPart(new Part(phr));
        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 phr   The Phrase to be played. See midiCycle(Score s)
     * @param index The midiCycle id to be used - default is 0.
     */
    public static void midiCycle(Phrase phr, int index) {
        Score s = new Score(phr.getTitle() + " score");
        s.addPart(new Part(phr));
        midiCycle(s, index);
    }
View Full Code Here

     *
     * @param phrase The phrase to be played.
     * @param insts  An array of instruments to play the phrase with
     */
    public static void audio(Phrase phrase, Instrument[] insts) {
        audio(new Score(new Part(phrase)), insts);
    }
View Full Code Here

TOP

Related Classes of jm.music.data.Part

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.