Thing which seemed very Thingish inside you is quite different when it gets out into the open and has other people looking at it

Sunday, October 9, 2011

Adding Chords to your melody using jMusic (Adding Accompaniments)


In my last post I showed how we can easily create a song (twinkle twinkle little star), which is basically the tune of that song or in western music language we call it the melody, in this post I am going to talk about how we can accompany this melody by adding chords.
Let’s see how we can accompany our melody ..

This is in our today’s TODO list
-          Adding Chords to our song
-          Arranging the chords in choral fashion
-          Adding  guitar chords (o any other instruments)

Adding Chords to the song “Twinkle twinkle”


 
Following diagram shows the chord arrangements for the song twinkle Twinkle however this is not static you can select appropriate chord of your choice depending on the mood and the sound you want. However let’s just use a very basic chord progression to our song.

Since we already created our song in my last post I will use the same code to add chord progression.

When we were creating the notes we needed a pitch array which is an integer array to store the notes(pitch classes), likewise we need Chord array to keep Chord structure.  

A Chord represents 3  or 4 notes together playing at once.  So I am going to create array of three notes for each of our chord. 

Here we need  3 Types of  Chords
Chord Notes
CMaj C E G
FMaj F A C
GMaj G B D

Adding  Chords is bit complicated than just creating notes therefore, I will explain simple as possible. 

Here we are going to have two parts. First one is our melody line(tune) which we created earlier. And the other part is the base part (the chords) to accompany our melody.

   Phrase phr = new Phrase("Twinkle twinkle", 0.0);
        Part treblePart = new Part("PIANO-Right", PIANO, 0);
        int[] pitchArray = {C4, C4, G4, G4, A4, A4, G4, F4, F4, E4, E4, D4, D4, C4, G4, G4, F4, F4, E4, E4, D4, G4, G4, F4, F4, E4, E4, D4};
        double[] rhythmArray = {C, C, C, C, C, C, M, C, C, C, C, C, C, M, C, C, C, C, C, C, M, C, C, C, C, C, C, M};
        phr.addNoteList(pitchArray, rhythmArray);
        treblePart.add(phr);

And then we create the base part
static Part bassPart = new Part("PIANO-Left", PIANO, 0);

To create chords we need to define each type of chord we are using. First we’l creates our three types of chords.

        Note cMaj[] = {new Note(C3, M), new Note(E3, M), new Note(G3, M)};
        Note fMaj[] = {new Note(F3, M), new Note(A3, M), new Note(C3, M)};
        Note gMaj[] = {new Note(G3, M), new Note(B3, M), new Note(D3, M)};


Earlier I created a Phrase  to store the pitch classes (notes) and their durations (time slots) here we need to create a  CPhrase chord = new CPhrase(); to store our chords. 
And we add each chord to the basePart to complete the phase.

So to make this code much efficient and elegant I am going to create a separate method which adds a given chord to our basePart.

public static void addChordsPart(Note chrd[]) {
        CPhrase chord = new CPhrase();
        chord.addChord(chrd);
        bassPart.addCPhrase(chord);
    }

Getting things all together
  1. Create two parts
  2. Add the melody to the first part by giving the pitch classes and durations
  3. Create the types of the chords
  4. Add each chord to the second part by giving the chord name and their duration per each chord
  5. Create the Score and add Part one and part two together
  6. Play/Save the midi

package mymusicapp;

import jm.JMC;
import jm.music.data.CPhrase;
import jm.music.data.Note;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.util.Play;
import jm.util.Write;


public class Main implements JMC {

    static Part bassPart = new Part("PIANO-Left", PIANO, 0);

    public static void main(String[] args) {
        Phrase phr = new Phrase("Twinkle twinkle", 0.0);
        Part treblePart = new Part("PIANO-Right", PIANO, 0);
        int[] pitchArray = {C4, C4, G4, G4, A4, A4, G4, F4, F4, E4, E4, D4, D4, C4, G4, G4, F4, F4, E4, E4, D4, G4, G4, F4, F4, E4, E4, D4};
        double[] rhythmArray = {C, C, C, C, C, C, M, C, C, C, C, C, C, M, C, C, C, C, C, C, M, C, C, C, C, C, C, M};
        phr.addNoteList(pitchArray, rhythmArray);
        treblePart.add(phr);

        Note cMaj[] = {new Note(C3, M), new Note(E3, M), new Note(G3, M)};
        Note fMaj[] = {new Note(F3, M), new Note(A3, M), new Note(C3, M)};
        Note gMaj[] = {new Note(G3, M), new Note(B3, M), new Note(D3, M)};

        addChordsPart(cMaj);
        addChordsPart(cMaj);
        addChordsPart(fMaj);
        addChordsPart(cMaj);
        addChordsPart(fMaj);
        addChordsPart(cMaj);
        addChordsPart(gMaj);
        addChordsPart(cMaj);
        addChordsPart(cMaj);
        addChordsPart(fMaj);
        addChordsPart(cMaj);
        addChordsPart(gMaj);
        addChordsPart(cMaj);
        addChordsPart(fMaj);
        addChordsPart(cMaj);
        addChordsPart(gMaj);

        Score score = new Score("Twinkle-Twinkle");
        score.addPart(treblePart);
        score.addPart(bassPart);

        Play.midi(score);
        Write.midi(score);

    }

    public static void addChordsPart(Note chrd[]) {
        CPhrase chord = new CPhrase();
        chord.addChord(chrd);
        bassPart.addCPhrase(chord);
    }
}


Now we added chords to our songs. You can try out the songs by simply playing the song.

Arranging  the chords.

Just playing the chord progression for a song can be lil boarding. To make the song more interesting we can arrange the base chords into different variations. 

Following diagram shows how we can arrange the chords using each note of the chord.

So for that we need to change our method a little bit by giving the notes of each chord separately and  arranging them with proper durations.

 public static void addbaseNotesPart(Note chrd[]) {
        Phrase chord = new Phrase();
        int[] pitchArray = {chrd[0].getPitch(), chrd[2].getPitch(), chrd[1].getPitch(), chrd[2].getPitch()};
        double[] rhythmArray = {Q, Q, Q, Q};
        chord.addNoteList(pitchArray, rhythmArray);
        bassPart.addPhrase(chord);
    }

We can use the same code, but instead of using addChordsPart, use addbaseNotesPart to get the styling of our chord progression.


Adding  guitar chords (o any other instruments)

This Chord Arrangements can be done using any instrument. All you need to do is change the  Part instrument to the instrument of your choice.
  static Part bassPart = new Part("PIANO-Left", GUITAR, 0);

You can also experiment by changing the chords/durations and adding new parts. 







No comments:

Post a Comment