LAB 31 – Making Sounds by Combining Pieces (part 3 – Modern Music Synthesis)
Lab Exercises
Topics
n To choose between sound formats such as MIDI and MP3 for different purposes
n To introduce private (helper methods)
Exercises
10.6.2 Midi
i) Type in the code for a main method
import java.util.Scanner;
/** program that plays the first two verses and refrain of Jingle Bells using the MidiPlayer class
* developed at GaTech
*
* revised by Wayne Summers
* November 2, 2007
*/
public class PlayMidi
{
public static void main (String[] args)
{
MidiPlayer player = new MidiPlayer();
Scanner scan = new Scanner(System.in);
int instrument;
do
{
System.out.println("Enter an instrument number: 0 - PIANO, 10 - MUSICBOX, 24 - GUITAR, 32 - BASS");
instrument = scan.nextInt();
}
while (instrument != 0 && instrument != 10 && instrument != 24 && instrument != 32);
player.setInstrument(instrument);
playJingleBellsV1(player);
playJingleBellsRefrain(player);
playJingleBellsV2(player);
playJingleBellsRefrain(player);
}
ii) Type in the following code in the Program window below the main method and before the last }:
/* Method to play the first/third/eighth/tenth measure of Jingle Bells */
private static void playJingleBellsM1(MidiPlayer player)
{
player.playNote(52,250); // e eighth note
player.playNote(60,250); // c eighth note
player.playNote(58,250); // b flat eighth note
player.playNote(56,250); // a flat eighth note
}
/* Method to play the measure 2 of Jingle Bells */
private static void playJingleBellsM2(MidiPlayer player)
{
player.playNote(52,500); // e quarter note
player.rest(250); // rest
player.playNote(52,125); // e sixteenth note
player.playNote(52,125); // e sixteenth note
}
/* Method to play measure 5 of Jingle Bells */
private static void playJingleBellsM5(MidiPlayer player)
{
player.playNote(53,250); // f eighth note
player.playNote(61,250); // d flat eighth note
player.playNote(60,250); // c eighth note
player.playNote(58,250); // b flat eighth note
}
/* Method to play measure 7 of Jingle Bells */
private static void playJingleBellsM7(MidiPlayer player)
{
player.playNote(55,250); // g eighth note
player.playNote(65,250); // f eighth note
player.playNote(63,250); // e flat eighth note
player.playNote(61,250); // d flat eighth note
}
/* Method to play measures 13-16 of Jingle Bells */
private static void playJingleBellsM13To16(MidiPlayer player)
{
// measure 13
player.playNote(53,250); // f eighth note
player.playNote(61,250); // d flat eighth note
player.playNote(60,250); // c eighth note
player.playNote(58,250); // b flat eighth note
// measure 14
player.playNote(55,250); // g eighth note
player.playNote(63,250); // e flat eighth note
player.playNote(62,250); // d eighth note
player.playNote(63,250); // e flat eighth note
// measure 15
player.playNote(65,250); // f eighth note
player.playNote(63,250); // e flat eighth note
player.playNote(61,250); // d flat eighth note
player.playNote(58,250); // b flat eighth note
// measure 16
player.playNote(56,1000); // a flat half note
}
/** Method to play the first verse of jingle bells with each measure
* taking 1000 milliseconds (1 second). It is in 2/4 time
*/
public static void playJingleBellsV1(MidiPlayer player)
{
playJingleBellsM1(player); // play measure 1
playJingleBellsM2(player); // play measure 2
playJingleBellsM1(player); // play measure 3
player.playNote(53,1000); // measure 4
playJingleBellsM5(player); // play measure 5
player.playNote(55,1000); // measure 6
playJingleBellsM7(player); // play measure 7
player.playNote(60,1000); // measure 8
playJingleBellsM1(player); // play measure 9
player.playNote(52,1000); // play measure 10
playJingleBellsM1(player); // play measure 11
player.playNote(53,1000); // play measure 12
playJingleBellsM13To16(player); // play measure 13 to 16
}
/** Method to play the second verse of jingle bells with each measure
* taking 1000 milliseconds (1 second) It is in 2/4 time
*/
public static void playJingleBellsV2(MidiPlayer player)
{ // measure 1
player.rest(750);
player.playNote(52,250); // e eighth note
playJingleBellsM1(player); // measure 2
playJingleBellsM2(player); // measure 3
playJingleBellsM1(player); // measure 4
// measure 5
player.playNote(53,750); // f dotted quarter note
player.playNote(53,250); // f eighth note
playJingleBellsM5(player); // measure 6
// measure 7
player.playNote(55,750); // g dotted quarter note
player.playNote(55,250); // g eighth note
playJingleBellsM7(player); // measure 8
// measure 9
player.playNote(60,750); // c dotted quarter note
player.playNote(52,250); // e eighth note
playJingleBellsM1(player); // measure 10
// measure 11
player.playNote(52,750); // e dotted quarter note
player.playNote(52,250); // e eighth note
playJingleBellsM1(player); // measure 12
// measure 13
player.playNote(53,750); // f dotted quarter note
player.playNote(53,250); // f eighth note
playJingleBellsM13To16(player); // measure 14-17
}
/* Method to play the first/second measures of Refrain */
private static void playJingleBellsRefrainM1(MidiPlayer player)
{
player.playNote(64,250); // e eighth note
player.playNote(64,250); // e eighth note
player.playNote(64,500); // e quarter note
}
/* Method to play the third/eleventh measures of Refrain */
private static void playJingleBellsRefrainM3(MidiPlayer player)
{
player.playNote(64,250); // e eighth note
player.playNote(67,250); // g eighth note
player.playNote(60,250); // c eighth note
player.playNote(62,250); // d eighth note
}
/* Method to play the fifth/thirteenth measures of Refrain */
private static void playJingleBellsRefrainM5(MidiPlayer player)
{
for (int i = 1; i <= 4; i++)
player.playNote(65,250); // f eighth note
}
/* Method to play the sixth/fourteenth measures of Refrain */
private static void playJingleBellsRefrainM6(MidiPlayer player)
{
player.playNote(65,250); // f eighth note
player.playNote(64,250); // e eighth note
player.playNote(64,250); // e eighth note
player.playNote(64,250); // e eighth note
}
/* Method to play refrain of Jingle Bells */
public static void playJingleBellsRefrain(MidiPlayer player)
{
playJingleBellsRefrainM1(player); // measure 1
playJingleBellsRefrainM1(player); // measure 2
playJingleBellsRefrainM3(player); // measure 3
player.playNote(64,1000); // measure 4
playJingleBellsRefrainM5(player); // measure 5
playJingleBellsRefrainM6(player); // measure 6
// measure 7
player.playNote(64,250); // e note
player.playNote(62,250); // d eighth note
player.playNote(62,250); // d eighth note
player.playNote(64,250); // e eighth note
// measure 8
player.playNote(62,500); // d half note
player.playNote(67,500); // g half note
playJingleBellsRefrainM1(player); // measure 9
playJingleBellsRefrainM1(player); // measure 10
playJingleBellsRefrainM3(player); // measure 11
player.playNote(64,1000); // measure 12
playJingleBellsRefrainM5(player); // measure 13
playJingleBellsRefrainM6(player); // measure 14
// measure 15
player.playNote(67,250); // g eighth note
player.playNote(67,250); // g eighth note
player.playNote(65,250); // f eighth note
player.playNote(62,250); // d eighth note
// measure 16
player.playNote(60,500); // c quarter note
player.rest(500); // rest
}
iii) Test your program with different instruments to confirm that the methods work.
QUESTIONS:
Nothing to submit.