Package jm.music.data

Examples of jm.music.data.Part


    private Random random = new Random();

    @Override
    public void handleScore(Score score, ScoreContext ctx) {
        Part timpaniPart = ctx.getParts().get(PartType.TIMPANI);
        if (timpaniPart == null) {
            return;
        }

        Phrase phrase = new Phrase();
        for (int i = 0; i < ctx.getMeasures(); i++) {
            if (Chance.test(20)) {
                phrase.add(getNote(ctx, ToneType.TONIC));
                phrase.add(getNote(ctx, ToneType.MEDIANT));
                if (Chance.test(20)) {
                    phrase.add(getNote(ctx, ToneType.TONIC));
                } else {
                    phrase.addRest(new Rest(ctx.getNormalizedMeasureSize() / 4));
                }
                phrase.addRest(new Rest(ctx.getNormalizedMeasureSize() / 4));
            } else {
                phrase.addRest(new Rest(ctx.getNormalizedMeasureSize()));
            }
        }
        phrase.setDynamic(100);
        timpaniPart.add(phrase);
    }
View Full Code Here


        // http://www.solomonsmusic.net/vartech.htm
        // http://dolmetsch.com/form.pdf

        logger.debug("Generating piece with ctx=" + ctx);

        Part mainPart = ctx.getParts().get(PartType.MAIN);

        MainPartContext lCtx = initLocalContext(score, ctx);

        // used for representing the structure of the phrase, for debugging purposes
        StringBuilder structureItem = new StringBuilder();

        // some empty initial measures
        addInitialMeasures(lCtx, mainPart);

        // TODO add upBeat
        while(lCtx.getTotalMeasures() + lCtx.getCurrentPhraseMeasuresCounter() < ctx.getMeasures()) {

            if (lCtx.getCurrentPhrase() == null) {
                createNewPhrase(lCtx, mainPart);
            }
            initializeMeasure(lCtx);

            boolean downBeat = lCtx.isNextNoteDownBeat(); //getting here, because it will get overridden when we calculate the length

            if (lCtx.getCurrentPhraseMeasuresCounter() >= lCtx.getPhraseMeasures() && lCtx.getCurrentMeasureSize() == 0 && lCtx.getTotalLength() > 0) {
                //stop repeating the theme and end the phrase
                lCtx.getCurrentPhrase().setNotes(lCtx.getPitches().size());
                resetPhrase(lCtx);
                continue;
            }

            // in-phrase repetitions (motifs)
            if (lCtx.getCurrentPhraseMeasuresCounter() > 0 && lCtx.getCurrentPhraseMeasuresCounter() < lCtx.getPhraseMeasures()) {
                if (!lCtx.getCurrentPhraseMotifs().isEmpty() && Chance.test(40) && lCtx.getCurrentMeasureSize() == 0) {
                    lCtx.getCurrentPhrase().getStructure().add(structureItem.toString());
                    structureItem = new StringBuilder();

                    repeatExistingMotifWithinPhrase(lCtx);
                    lCtx.setMeasuresSinceLastMotif(0);
                } else if (shouldRepeatNewMotif(lCtx)) {
                    lCtx.getCurrentPhrase().getStructure().add(structureItem.toString());
                    structureItem = new StringBuilder();

                    repeatNewMotifWithinPhrase(lCtx);
                    lCtx.setMeasuresSinceLastMotif(0);
                } else {
                    // count the number of measures since the last motif repetition
                    if (lCtx.getCurrentMeasureSize() == 0) {
                        lCtx.setMeasuresSinceLastMotif(lCtx.getMeasuresSinceLastMotif() + 1);
                    }
                    if (Chance.test(17)) {
                        repeatNotesWithinPhrase(lCtx);
                        structureItem.append("--Repeat--");
                    }
                }
            }

            handleSpecialNotes(lCtx);

            handleContour(lCtx);

            boolean useInterval = false;
            boolean terraceContourChange = lCtx.getContour() == Contour.TERRACE && lCtx.getCurrentMeasureSize() == 0 && lCtx.getCurrentPhraseMeasuresCounter() % 2 == 0 && lCtx.getCurrentPhraseMeasuresCounter() != 0;
            if (terraceContourChange) {
                useInterval = true;
                lCtx.setDirectionUp(!lCtx.isDirectionUp());
            }

            //TODO add two-note chords (simultaneous intervals). The logic should contain resolving dissonant and unstable sim.intervals to consonant and stable.
            lCtx.setNotePitch(getNextNotePitch(lCtx, useInterval));

            // restore the direction after the terrace jump
            if (terraceContourChange) {
                lCtx.setDirectionUp(!lCtx.isDirectionUp());
            }
            //TODO climax - around 2/3 and 3/4

            double length = getLength(lCtx);
            lCtx.setLength(length);
            lCtx.getPitches().add(lCtx.getNotePitch());
            lCtx.getUniquePitches().add(lCtx.getNotePitch());

            // use pause in a couple of cases at random, and also in the middle and at the end of the theme
            boolean usePause = Chance.test(11)
                    || (lCtx.isEndOfMeasure() == true
                            && lCtx.getCurrentPhraseMeasuresCounter() == lCtx.getPhraseMeasures() / 2 && Chance
                                .test(70))
                    || (lCtx.getCurrentPhraseMeasuresCounter() == lCtx.getPhraseMeasures() && Chance
                            .test(75));

            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) {
            lCtx.getCurrentPhrase().getStructure().add(structureItem.toString());
            int endingMeasures = addEnding(lCtx.getCurrentPhrase(), ctx);
            lCtx.setCurrentPhraseMeasuresCounter(lCtx.getCurrentPhraseMeasuresCounter() + endingMeasures);
            lCtx.setTotalMeasures(lCtx.getTotalMeasures() + lCtx.getCurrentPhraseMeasuresCounter());
            lCtx.getCurrentPhrase().setMeasures(lCtx.getCurrentPhraseMeasuresCounter());
        }

        List<ExtendedPhrase> phrases = repeatPhrases(lCtx);

        // override with the possibly changed list of phrases
        lCtx.setPhrases(phrases);

        // if any (slight) modification has been carried out to the measure count, update it here
        ctx.setMeasures(lCtx.getTotalMeasures());

        // add the phrases to the main part
        mainPart.addPhraseList(lCtx.getPhrases().toArray(new ExtendedPhrase[lCtx.getPhrases().size()]));
        // transpose to the desired key
        Mod.transpose(mainPart, ctx.getKeyNote());

        if (ctx.getParts().containsKey(PartType.MAIN_DUPLICATE)) {
            Part duplicateMainPart = ctx.getParts().get(PartType.MAIN_DUPLICATE);
            duplicateMainPart.addPhraseList(mainPart.getPhraseArray());
            if (Chance.test(45) || duplicateMainPart.getInstrument() == mainPart.getInstrument()) {
                Mod.transpose(duplicateMainPart, -12); // an octave lower
            }
        }

        applyExtras(ctx);
View Full Code Here

    private int[] SECONDARY_DRUM_PERCENTAGES = { 15, 5, 80, 5, 20, 15, 80, 15 };
    private int[] AVAILABLE_PERCUSSIONS = {36, 38, 42, 43, 44, 46, 48, 49, 51};

    @Override
    public void handleScore(Score score, ScoreContext ctx) {
        Part drumPart = ctx.getParts().get(PartType.PERCUSSIONS);
        if (drumPart == null) {
            return;
        }

        Part mainPart = ctx.getParts().get(PartType.MAIN);
        Phrase[] phrases = mainPart.getPhraseArray();
        for (Phrase phrase : phrases) {
            if (!(phrase instanceof ExtendedPhrase)) {
                continue;
            }
View Full Code Here

public class Arpeggiator implements ScoreManipulator {
    private Random random = new Random();

    @Override
    public void handleScore(Score score, ScoreContext ctx) {
        Part part = ctx.getParts().get(PartType.ARPEGGIO);
        if (part == null) {
            return;
        }
        Part mainPart = ctx.getParts().get(PartType.MAIN);
        double currentMeasureSize = 0;
        double normalizedMeasureSize = ctx.getNormalizedMeasureSize();
        SpecialNoteType specialNoteType = null;
        for (Phrase phrase : mainPart.getPhraseArray()) {
            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;
View Full Code Here

        if (classical) {
            mainInstrument = Instruments.PIANO;
        }

        Part main = new Part(PartType.MAIN.getTitle(), mainInstrument , partIdx++);
        score.add(main);
        ctx.getParts().put(PartType.MAIN, main);

        if (Chance.test(12)) {
            Part duplicateMainPart = new Part(PartType.MAIN_DUPLICATE.getTitle(), MAIN_PART_ONLY_INSTRUMENTS[random.nextInt(MAIN_PART_ONLY_INSTRUMENTS.length)], partIdx++);
            score.add(duplicateMainPart);
            ctx.getParts().put(PartType.MAIN_DUPLICATE, duplicateMainPart);
        }

        if (accompaniment) {
            if (Chance.test(55) && !electronic) {
                Part accompanimentPart = new Part(PartType.ACCOMPANIMENT.getTitle(), ACCOMPANIMENT_INSTRUMENTS[random.nextInt(ACCOMPANIMENT_INSTRUMENTS.length)], partIdx++);
                score.add(accompanimentPart);
                ctx.getParts().put(PartType.ACCOMPANIMENT, accompanimentPart);
            } else if (isRegularMetre(ctx)){
                Part arpegioPart = new Part(PartType.ARPEGGIO.getTitle(), ARPEGGIO_INSTRUMENTS[random.nextInt(ARPEGGIO_INSTRUMENTS.length)], partIdx++);
                score.add(arpegioPart);
                ctx.getParts().put(PartType.ARPEGGIO, arpegioPart);
            }
        }

        boolean hasBass = Chance.test(22) && !classical;
        if (hasBass) {
            Part bassPart = new Part(PartType.BASS.getTitle(), BASS_INSTRUMENTS[random.nextInt(BASS_INSTRUMENTS.length)], partIdx++);
            score.add(bassPart);
            ctx.getParts().put(PartType.BASS, bassPart);
        }

        if (!hasBass && isRegularMetre(ctx) && !classical && Chance.test(16)) {
            Part dronePart = new Part(PartType.DRONE.getTitle(), DRONE_INSTRUMENTS[random.nextInt(DRONE_INSTRUMENTS.length)], partIdx++);
            score.add(dronePart);
            ctx.getParts().put(PartType.DRONE, dronePart);
        }

        boolean useSimpleBeat = Chance.test(33) && !classical;
        if (useSimpleBeat) {
            Part extraPart = new Part(PartType.SIMPLE_BEAT.getTitle(), SIMPLE_BEAT_INSTRUMENTS[random.nextInt(SIMPLE_BEAT_INSTRUMENTS.length)], partIdx++);
            score.add(extraPart);
            ctx.getParts().put(PartType.SIMPLE_BEAT, extraPart);
        }

        boolean usePercussions = Chance.test(46);
        if (!useSimpleBeat && usePercussions && isRegularMetre(ctx) && !classical) {
            Part extraPart = new Part(PartType.PERCUSSIONS.getTitle(), 0, 9);
            extraPart.setDynamic(35); //quieter, in the background
            score.add(extraPart);
            ctx.getParts().put(PartType.PERCUSSIONS, extraPart);
        }

        //TODO beats (ala drum & bass). no cymbals?

        if (!classical && Chance.test(30)) {
            Part extraPart = new Part(PartType.EFFECTS.getTitle(), SPORADIC_EFFECTS_INSTRUMENTS[random.nextInt(SPORADIC_EFFECTS_INSTRUMENTS.length)], partIdx++);
            score.add(extraPart);
            ctx.getParts().put(PartType.EFFECTS, extraPart);
        }

        if (Chance.test(30) && !classical) {
            Part padPart1 = new Part(PartType.PAD1.getTitle(), PAD_INSTRUMENTS[random.nextInt(PAD_INSTRUMENTS.length)], partIdx++);
            score.add(padPart1);
            ctx.getParts().put(PartType.PAD1, padPart1);

            if (Chance.test(46)) {
                Part padPart2 = new Part(PartType.PAD2.getTitle(), PAD_INSTRUMENTS[random.nextInt(PAD_INSTRUMENTS.length)], partIdx++);
                score.add(padPart2);
                ctx.getParts().put(PartType.PAD2, padPart2);
            }
        }
        // TODO add more dramatic features other than the timpani
        if (Chance.test(6) && isRegularMetre(ctx) && !classical) {
            Part timpaniPart = new Part(PartType.TIMPANI.getTitle(), Instruments.TIMPANI, partIdx++);
            score.add(timpaniPart);
            ctx.getParts().put(PartType.TIMPANI, timpaniPart);
        }
    }
View Full Code Here

public class DroneGenerator implements ScoreManipulator {
    private final Random random = new Random();

    @Override
    public void handleScore(Score score, ScoreContext ctx) {
        Part dronePart = ctx.getParts().get(PartType.DRONE);
        if (dronePart == null) {
            return;
        }
        Note[] droneNotes = null;
        Part mainPart = ctx.getParts().get(PartType.MAIN);
        Phrase[] phrases = mainPart.getPhraseArray();
        for (Phrase phrase : phrases) {
            Phrase dronePhrase = new Phrase();
            if (!(phrase instanceof ExtendedPhrase) || Chance.test(17)) { // skip the drone for some phrases
                continue;
            }
View Full Code Here

public class EffectsGenerator implements ScoreManipulator {
    private Random random = new Random();

    @Override
    public void handleScore(Score score, ScoreContext ctx) {
        Part effectsPart = ctx.getParts().get(PartType.EFFECTS);
        if (effectsPart == null) {
            return;
        }
        Phrase phrase = new Phrase();
        int restPercentage = Chance.test(65) ? 75 : 15; //sporadic vs regular
        for (int i = 0; i < ctx.getMeasures(); i++) {
            handleEffects(ctx, phrase, restPercentage);
        }
        effectsPart.add(phrase);
    }
View Full Code Here

public class SimpleBeatGenerator implements ScoreManipulator {
    private Random random = new Random();

    @Override
    public void handleScore(Score score, ScoreContext ctx) {
        Part drumPart = ctx.getParts().get(PartType.SIMPLE_BEAT);
        if (drumPart == null) {
            return;
        }
        Phrase phrase = new Phrase();
        boolean needsBass = Chance.test(80);
        boolean hasBass = ctx.getParts().get(PartType.BASS) != null;
        // "four on the floor" in case of faster pieces that (in most cases) have a bass
        if (PartConfigurer.isRegularMetre(ctx) && Chance.test(50) && ctx.getParts().get(PartType.PERCUSSIONS) == null && score.getTempo() > 115 && (needsBass ? hasBass : true)) {
            ctx.setFourToTheFloor(true);
            drumPart.setChannel(9);
            for (int i = 0; i < ctx.getMeasures(); i++) {
                for (int k = 0; k < 4; k++) {
                    Note note = NoteFactory.createNote(35 + k % 2, ctx.getNormalizedMeasureSize() / 4);
                    phrase.add(note);
                }
            }
        } else {
            for (int i = 0; i < ctx.getMeasures(); i++) {
                Note note = NoteFactory.createNote(40 + random.nextInt(30), ctx.getNormalizedMeasureSize() / 4);
                phrase.add(note);
                phrase.add(new Rest(ctx.getNormalizedMeasureSize() - note.getRhythmValue()));
            }
        }

        drumPart.add(phrase);

    }
View Full Code Here

    private static Random random = new Random();

    @Override
    public void handleScore(Score score, ScoreContext ctx) {
        Part bassPart = ctx.getParts().get(PartType.BASS);
        if (bassPart == null) {
            return;
        }

        Part mainPart = ctx.getParts().get(PartType.MAIN);

        int degreePercentages[] = new int[]{27, 13, 21, 6, 19, 10, 4};
        int dullBassPercentages[] = new int[]{45, 11, 22, 0, 22, 0, 0};

        boolean dullBass = Chance.test(38);
        ctx.setDullBass(dullBass);

        Phrase[] phrases = mainPart.getPhraseArray();

        double durationModifier = Chance.test(30) ? SpecialNoteType.STACCATO.getValue() : 0;

        for (Phrase phrase : phrases) {
            ExtendedPhrase extPhrase = ((ExtendedPhrase) phrase);
View Full Code Here

            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(0, 0, 500, 500);
                frame.setVisible(true);
                Part part = ctx.getParts().get(PartType.MAIN);
                Note[] notes = part.getPhrase(0).getNoteArray();
                List<Integer> pitches = new ArrayList<Integer>();
                for (Note note : notes) {
                    if (!note.isRest()) {
                        pitches.add(note.getPitch());
                    }
                }
                GraphicsPanel gp = new GraphicsPanel(pitches);
                frame.setContentPane(gp);
            }
        }).start();

        DecimalFormat df = new DecimalFormat("#.##");

        for (Part part : score.getPartArray()) {
            StringBuilder sb = new StringBuilder();
            printStatistics(ctx, part);
            System.out.println("------------ " + part.getTitle() + "-----------------");
            Phrase[] phrases = part.getPhraseArray();
            for (Phrase phr : phrases) {
                if (phr instanceof ExtendedPhrase) {
                    sb.append("Contour=" + ((ExtendedPhrase) phr).getContour() + " ");
                }
                double measureSize = 0;
                int measures = 0;
                double totalLength = 0;
                List<String> pitches = new ArrayList<String>();
                List<String> lengths = new ArrayList<String>();
                System.out.println("((Phrase notes: " + phr.getNoteArray().length + ")");
                for (Note note : phr.getNoteArray()) {
                    if (!note.isRest()) {
                        int degree = 0;
                        if (phr instanceof ExtendedPhrase) {
                            degree = Arrays.binarySearch(((ExtendedPhrase) phr).getScale().getDefinition(), note.getPitch() % 12);
                        }
                        pitches.add(String.valueOf(note.getPitch() + " (" + degree + ") "));
                    } else {
                        pitches.add(" R ");
                    }
                    lengths.add(df.format(note.getRhythmValue()));
                    measureSize += note.getRhythmValue();
                    totalLength += note.getRhythmValue();;
                    if (measureSize >= ctx.getNormalizedMeasureSize()) {
                        pitches.add(" || ");
                        lengths.add(" || " + (measureSize > ctx.getNormalizedMeasureSize() ? "!" : ""));
                        measureSize = 0;
                        measures++;
                    }
                }
                sb.append(pitches.toString() + "\r\n");
                sb.append(lengths.toString() + "\r\n");
                if (part.getTitle().equals(PartType.MAIN.getTitle())) {
                    sb.append("\r\n");
                }
                System.out.println("Phrase measures: " + measures);
                System.out.println("Phrase length: " + totalLength);
            }
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.