import jm.music.data.*; import jm.util.*; public class SongPhrase { /* * Example of how to use the SongPhrase class. * You don't need to understand the guts of SongPhrase.java */ public static void main(String[] args) { // play a single array int [] notes = {64,62,60, 62, 64, 64, 64, 62, 62, 62}; SongPhrase.showSong(notes, notes.length); // play an array with a second array indicating times double [] times = {1, 1, 1, 1, 1, 1, 2, 1, 1, 2}; SongPhrase.showSong(1, notes, times, notes.length); } /* * The primary access point for a SongPhrase. Shows the music * notes in the song in a new window and allows the music to be played. * * @numArray - an array of the notes to be played * @length - the length of the array to actually play, should be * less than numArray.length */ public static void showSong(int [] numArray, int length) { // assume a default of piano for the instrument. showSong(1, numArray, length); } /* * Optional showSong method * * @instrument - an instrument from 0-127 * @numArray - an array of the notes to be played * @length - the length of the array to actually play, should be * less than numArray.length */ public static void showSong(int instrument, int[] numArray, int length) { double[] timesArray = new double[length]; for (int i = 0; i < length; i++) { timesArray[i] = 0.5; } showSong(1, numArray, timesArray, length); } /* * Internal method to test for errors in the input to showSong */ private static boolean testForInputErrors(int instrument, int[] numArray, double[] timesArray, int length) { boolean foundError = false; if((instrument > 127) || (instrument < 0)) { System.out.println("The instrument number: " + instrument + " is not valid. Must be 0-127"); foundError = true; } if (length > numArray.length) { System.out.println("The length provided: " + length + " is higher than the actual length of the variable numArray"); foundError = true; } if (length > timesArray.length) { System.out.println("The length provided: " + length + " is higher than the actual length of the variable timesArray"); foundError = true; } for (int i = 0; i < length; i++) { if ((numArray[i] > 127) || (numArray[i] < 0) || (numArray[i] == -2147483648)) { System.out.println("numArray[" + i + "] =" + numArray[i] + " which is not a valid value. Must be 0-127"); foundError = true; } double nextTime = timesArray[i]; if ((nextTime != 0.25) && (nextTime != 0.5) && (nextTime != 0.75) && (nextTime != 1.0) && (nextTime != 2.0) && (nextTime != 3.0) && (nextTime != 4.0)) { System.out.println("timesArray[" + i + "] =" + timesArray[i] + " which is not a normal value. It might be modified."); } } return foundError; } /* * You don't need to understand what is happening in this method! * But you might want to change the values for the tempo or time signature. * myScore.setTimeSignature(3,4); * myScore.setTempo(120.0); * * @instrument should be a value between 0 and 127 * @numArray is an array of notes to be played * @timesArray is an array of times for each corresponding note in @numArray * @length is the length of the numArray to play. @numarray and @timesArray must be at least @length long */ public static void showSong(int instrument, int[] numArray, double[] timesArray, int length){ if(testForInputErrors(instrument, numArray, timesArray, length)) { // there was an error - don't continue // return; } // Make the Score that we'll assemble the elements into // We'll set it up with a default time signature and tempo we like Score myScore = new Score("My Song"); myScore.setTimeSignature(4,4); myScore.setTempo(120.0); // Make the Part that we'll assemble things into Part myPart = new Part(instrument); // Make a new Phrase that will contain the notes from all the phrases Phrase collector = new Phrase(); Note [] allNotes = new Note[length]; // While we're not through... for (int i = 0; i < length; i++) { allNotes[i] = new Note(numArray[i], timesArray[i]); } collector.addNoteList(allNotes); // Now, construct the part and the score. myPart.addPhrase(collector); myScore.addPart(myPart); // At the end, let's see it! View.notate(myScore); } }