Package jm.music.data

Examples of jm.music.data.Note


                // shuffle every time, so that we don't always get the same chord for a given note
                Collections.shuffle(scaleChords, random);
                Collections.shuffle(scaleSeventhChords, random);
                Collections.shuffle(scaleOtherChords, random);

                Note currentNote = notes[i];
                if (currentNote.getRhythmValue() == 0) {
                    continue; // rhythm value is 0 for the first notes of a (main-part) chord. So progress to the next
                }
                // inter-measure chords only for even-numbered, compound metres
                if (canHaveInterMeasureChords && currentMeasureSize == 0) {
                    interMeasureChord = Chance.test(18);
                }
                double chordLength = interMeasureChord ? normalizedMeasureSize / 2 : normalizedMeasureSize;
                boolean isHalfMeasure = currentMeasureSize == normalizedMeasureSize / 2;

                if (currentNote.getPitch() == 0) {
                    logger.warn("Pitch is 0 in main part.");
                    continue;
                }
                if (!currentNote.isRest() && (currentMeasureSize == 0 || (interMeasureChord && isHalfMeasure))) {
                    boolean preferStable = ToneResolver.needsContrastingChord(firstToneTypes, ToneGroups.UNSTABLE);
                    boolean preferUnstable = ToneResolver.needsContrastingChord(firstToneTypes, ToneGroups.STABLE);
                    Chord previous = chord;
                    chord = ChordUtils.getChord(ctx, currentNote.getPitch(), previous, scaleChords, scaleSeventhChords, scaleOtherChords, preferStable, preferUnstable);
                    if (chord != null && Chance.test(90)) {
                        if (Chance.test(20)) { // change the special note type
                            if (Chance.test(60)) { // to a new value
                                specialNoteType = SpecialNoteType.values()[random.nextInt(SpecialNoteType.values().length)];
                            } else { // reset
                                specialNoteType = null;
                            }
                        }
                        firstToneTypes.add(chord.getFirstToneType());
                        int[] chordPitches = chord.getPitches();
                        logger.debug(Arrays.toString(chordPitches) + " : " + currentNote.getPitch());

                        postProcessPitches(accompanimentPart, chordPitches);

                        int dynamics = InstrumentGroups.getInstrumentSpecificDynamics(65 + random.nextInt(20), accompanimentPart.getInstrument());
                        // in some cases repeat the chord
                        if (Chance.test(82)) {
                            if (Chance.test(75)) { //full chord
                                addChord(chordPitches, chordLength, dynamics, accompanimentPhrase,
                                        specialNoteType, preferChordNotesOffset, measureOffset);
                            } else { //partial chord + rest
                                // make the filling rest between 1/16 and 1/4
                                double restLength = 0.125 * Math.pow(2, random.nextInt(4));
                                if (restLength >= chordLength) {
                                    restLength = chordLength / 2;
                                }
                                addChord(chordPitches, chordLength - restLength, dynamics,
                                        accompanimentPhrase, specialNoteType, preferChordNotesOffset,
                                        measureOffset);
                                accompanimentPhrase.addRest(new Rest(restLength));
                            }
                        } else {
                            addChord(chordPitches, chordLength / 2, dynamics, accompanimentPhrase,
                                    specialNoteType, preferChordNotesOffset, measureOffset);
                            addChord(chordPitches, chordLength / 2, dynamics, accompanimentPhrase,
                                    specialNoteType, preferChordNotesOffset, measureOffset);
                        }
                    } else {
                        accompanimentPhrase.addRest(new Rest(chordLength));
                    }

                    measureChordLength += chordLength;
                }

                if (currentNote.isRest() && (currentMeasureSize == 0 || (interMeasureChord && isHalfMeasure))) {
                    accompanimentPhrase.addRest(new Rest(chordLength));
                    measureChordLength += chordLength;
                }

                currentMeasureSize += currentNote.getRhythmValue();
                if (currentMeasureSize >= normalizedMeasureSize) {
                    // when there's a long note and so no inter-measure chord is possible, fill the measure with a rest
                    if (measureChordLength != currentMeasureSize) {
                        double fillingSize = normalizedMeasureSize - measureChordLength;
                        accompanimentPhrase.addRest(new Rest(fillingSize));
View Full Code Here


        // in some cases add each subsequent note with a slight offset
        double offset = preferOffset && Chance.test(30) || Chance.test(5) ? 0.1 + random.nextInt(20) / 100d : 0;

        // all but the first note are set with length = 0, so that they all sound as a chord
        for (int i = 1; i < chordPitches.length; ++i) {
            Note localNote = NoteFactory.createNote(chordPitches[i], 0.0d);
            localNote.setOffset(measureOffset + i * offset);
            if (noteType == null) {
                localNote.setDuration(chordLength * 0.9d);
            } else {
                localNote.setDuration(chordLength * noteType.getValue());
            }
            localNote.setDynamic(dynamics);
            phrase.addNote(localNote);
        }
        // the first note is added with the right length
        Note note = NoteFactory.createNote(chordPitches[0], chordLength);
        note.setDynamic(dynamics);
        note.setOffset(measureOffset);
        phrase.addNote(note);

        // Add a supplementary octave chord in a lower octave
        if (Chance.test(10)) {
            Note startNote = NoteFactory.createNote(chordPitches[0] - 12, 0.0D);
            startNote.setDuration(chordLength * 0.9D);
            phrase.addNote(startNote);
            phrase.addNote(chordPitches[0] - 24, chordLength);
        }
    }
View Full Code Here

        timpaniPart.add(phrase);
    }

    private Note getNote(ScoreContext ctx, ToneType tone) {
        int pitch = Pitches.C3 + ctx.getScale().getDefinition()[tone.getDegree()];
        Note note = NoteFactory.createNote(pitch, ctx.getNormalizedMeasureSize() / 4);
        note.setDynamic(52 + random.nextInt(10));
        note.setDuration(1.2);
        return note;
    }
View Full Code Here

    @Test
    public void retrogradeTest() {
        MainPartGenerator gen = new MainPartGenerator();
        List<Note> list = new ArrayList<>();
        list.add(new Note(Pitches.C4, RhythmValues.EIGHTH_NOTE));
        list.add(new Note(Pitches.C5, RhythmValues.QUARTER_NOTE));
        list.add(new Note(Pitches.C6, RhythmValues.HALF_NOTE));
        gen.retrograde(list);
        Assert.assertEquals(RhythmValues.EIGHTH_NOTE, list.get(0).getRhythmValue(), 0);
        Assert.assertEquals(RhythmValues.QUARTER_NOTE, list.get(1).getRhythmValue(), 0);
        Assert.assertEquals(RhythmValues.HALF_NOTE, list.get(2).getRhythmValue(), 0);
View Full Code Here

            if (usePause) {
                lCtx.getCurrentPhrase().addRest(new Rest(length));
                structureItem.append("-R-");
            } else {
                Note note = createNote(lCtx, downBeat, length);

                if (lCtx.isAllowOrnaments() && Chance.test(6)) {
                    addOrnamentedNote(lCtx, length, note);
                } else {
                    lCtx.getCurrentPhrase().addNote(note);
                }

                structureItem.append("-" + note.getPitch() + "-");
            }
        }

        // add the cadences to the final phrase, if the above loop has been interrupted due to the end of the piece
        if (lCtx.getCurrentPhrase() != null) {
View Full Code Here

            }
        }
    }

    private Note createNote(MainPartContext lCtx, boolean downBeat, double length) {
        Note note = NoteFactory.createNote(lCtx.getNotePitch(), length);
        note.setDynamic(getNextNoteDynamics(lCtx, downBeat));

        if (lCtx.getSpecialNoteType() != null) {
            note.setDuration(note.getRhythmValue() * lCtx.getSpecialNoteType().getValue());
        } else if (Chance.test(8)) { // in 8% of the cases, choose this note to be special by its own
            note.setDuration(note.getRhythmValue() * SpecialNoteType.values()[random.nextInt(SpecialNoteType.values().length)].getValue());
        } else if (lCtx.getDominantSpecialNoteType() != null) {
            note.setDuration(note.getRhythmValue() * lCtx.getDominantSpecialNoteType().getValue());
        }
        return note;
    }
View Full Code Here

        if (length >= JMC.QUARTER_NOTE && Chance.test(60)) { // trill or tremolo
            boolean tremolo = Chance.test(40); // vary the velocity of the sound
            boolean mordents = Chance.test(20); // trill alternates 2 notes, mordents is alternation of 3 notes
            boolean changePitch = Chance.test(85);
            for (int i = 0; i < count; i++) {
                Note ornamentNote = cloneNote(note);
                ornamentNote.setLength(JMC.THIRTYSECOND_NOTE);
                if (i % 2 == 1) {
                    if (changePitch) {
                        ornamentNote.setPitch(ornamentNote.getPitch() + pitchChange);
                    }
                    if (tremolo) {
                        ornamentNote.setDynamic(ornamentNote.getDynamic() - 20);
                    }

                }
                if (mordents && i % 3 == 1 && changePitch) {
                    ornamentNote.setPitch(ornamentNote.getPitch() + pitchChange);
                } else if (mordents && changePitch && i % 3 == 2) {
                    ornamentNote.setPitch(ornamentNote.getPitch() + secondPitchChange);
                }

                lCtx.getCurrentPhrase().addNote(ornamentNote);
            }
        } else { //appoggiatura
            Note ornament = cloneNote(note);
            note.setLength(note.getRhythmValue() - JMC.THIRTYSECOND_NOTE);
            ornament.setLength(JMC.THIRTYSECOND_NOTE);
            lCtx.getCurrentPhrase().addNote(ornament);
            lCtx.getCurrentPhrase().addNote(note);
        }

        //TODO "turn"
View Full Code Here

    private void addInitialMeasures(MainPartContext lCtx, Part part) {
        if (Chance.test(43)) {
            int pitch = Pitches.C4 + lCtx.getScoreContext().getKeyNote();
            // initial note is needed, so that accompaniment can "hook" to it.
            Note note = NoteFactory.createNote(pitch, JMC.QUARTER_NOTE);
            double restLength = lCtx.getScoreContext().getNormalizedMeasureSize() - JMC.QUARTER_NOTE;
            ExtendedPhrase phrase = new ExtendedPhrase();
            phrase.setScale(lCtx.getScoreContext().getScale());
            phrase.setMeasures(2 + random.nextInt(3));
            phrase.add(note);
View Full Code Here

    private static double getVariationPerNote(Phrase phrase) {
        double variation = 0;
        int noteCount = 0;
        Note[] notes = phrase.getNoteArray();
        Note previousNote = null;
        for (Note note : notes) {
            if (note.isRest()) {
                continue;
            }
            if (previousNote != null) {
                variation += Math.abs(note.getPitch() - previousNote.getPitch());
            }
            previousNote = note;
            noteCount++;
        }
        if (noteCount == 0) {
View Full Code Here

            }
        }
    }

    private Note cloneNote(Note note) {
        Note newNote = NoteFactory.createNote(note.getPitch(), note.getRhythmValue(), note.getDynamic());
        newNote.setDuration(note.getDuration());
        return newNote;
    }
View Full Code Here

TOP

Related Classes of jm.music.data.Note

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.