<< Chapter < Page
  Accessible objected-oriented     Page 17 / 17
Chapter >> Page >

...

Listing 28 . The class named PlayerPiano01.
/*File PlayerPiano01.java Copyright 2014, R.G.BaldwinRevised 08/27/14 This class simulates an old-fashioned player piano and makes it possible tocompose a melody by specifying the sequence of notes and the duration of each note for both the treble and bass clefs.The program uses text files to store the notes. The notes are converted to frequencies, which are then used to produce mono orstereo sound. A file containing treble clef notes is required. Bass clef notes are optional.If a file is provided containing bass clef notes, the bass clef melody is emitted from the left speaker and the treble clef melody is emitted from theright speaker. If bass clef notes are not provided, the program defaults to mono with thetreble clef melody being emitted with equal volume from both speakers. Various operating parameters are provided by way of command-line parameters. One ofthe command-line parameters makes it possible to play the music immediately or to write it into an audio file of type AU for playback later.The notes for the melody are specified in one or two text files. A file containing treble clef notes is required. A file containing bass clef notes isoptional. The input file names and other parameters are specified on the command line as shown in the comments at the beginning of the source code filefor the class named MusicComposer09. Each line in the text file specifies the duration and one or more keys thatwould be struck on a piano simultaneously. For example, the following lines of text specify four instances of the A-Major chord for quarter, half,three-fourths, and whole notes with a whole note rest in between. (See http://www.true-piano-lessons.com/piano-chord-chart.html for chords.)Note that X is used as the symbol for silence or a musical rest. // This is a comment in the text file1,A3,C4#,E4 4,X2,A3,C4#,E4 4,X3,A3,C4#,E4 4,X4,A3,C4#,E4 Normally, the first character must be a number or a /. Comments must begin inthe first column with a forward slash. The program will ignore:Blank lines that result in a string with a zero length Lines that begin with a spaceThe range extends from A2 (110 Hz) to A7 (3520 Hz). Note, however, that many audio speakers cannot produce sound at the low or high end of that spectrum.Middle-C (261.63 Hz) is specified as C4. The amplitude versus time of each note is shaped by a scale factor. Theamplitude is maximum at the beginning and decays linearly to zero at the end. You should be able to play the audio file back with any standard media playerthat can handle the AU file type. ******************************************************************************/import java.io.*; import java.nio.*;import java.util.*; public class PlayerPiano01 extends AudioSignalGenerator02{//Used to store treble clef notes as a list of arrays.ArrayList trebleClef = null; //Used to store bass clef notes as a list of arrays.ArrayList bassClef = null;//Names of input text files are stored here. String trebleFileName;String bassFileName; //Routine working variablesint trebleLengthInBeats; int bassLengthInBeats;//Constructorpublic PlayerPiano01(AudioFormatParameters01 audioParams, String[]args, byte[]melody){ super(audioParams,args,melody);//Get names of files containing the durations and notes.if(args.length>= 3){ //Required trebleFileNametrebleFileName = args[2];}else{ System.out.println("No trebleFileName provided");}//end elseif(args.length>= 4){ //Optional bassFileNamebassFileName = args[3];}//end if }//end constructor//-------------------------------------------------------------------------////This method returns a melody array that will play piano-like sounds using // input nomenclature like A, A#, B, C, C#, D, D# etc.byte[] getMelody(){//Treble data must be provided. If bass data is also provided, the output// audio data will be interlaced in the output melody array so that the // bass will be played through the left speaker and the treble will be// played through the right speaker. if(bassFileName != null){//Specify stereo output. audioParams.channels = 2;}else{ //Specify mono output.audioParams.channels = 1;//superfluous - same as default }//end elseSystem.out.println("audioParams.channels = " + audioParams.channels); //Controls how fast or slow the notes are played.int beatsPerSec = Integer.parseInt(args[1]);//Read the input files to define the that is to be played or// filed. They must be stored in the subfolder named Music. try{trebleClef = new ArrayList(); BufferedReader in = new BufferedReader(new FileReader("Music/" + trebleFileName)); String str;while ((str = in.readLine()) != null) { //Split the input string into multiple substrings using the comma as// the delimiter and save them in an array object. String[]strArr = str.split(","); //Ignore:// Blank lines that result in a string with a zero length // Comments that begin with a /// Lines that begin with a space if(strArr[0].length() != 0&&!(strArr[0].substring(0,1).equals("/"))&&!(strArr[0].substring(0,1).equals(" "))){//Add the array to the end of the list. trebleClef.add(strArr);}//end if}//end while in.close();//close the input fileif(bassFileName != null){ //A file was specified for bass cleft data. Need to process it.bassClef = new ArrayList(); in = new BufferedReader(new FileReader("Music/" + bassFileName));while ((str = in.readLine()) != null) { String[]strArr = str.split(",");//Ignore: // Blank lines that result in a string with a zero length// Comments that begin with a / // Lines that begin with a spaceif(strArr[0].length() != 0&&!(strArr[0].substring(0,1).equals("/"))&&!(strArr[0].substring(0,1).equals(" "))){bassClef.add(strArr); }//end if}//end while}//end if }catch(Exception ex){ex.printStackTrace(); }//end catch//Compute length of trebleClef in beats//Get an iterator on the trebleClef Iterator iter = trebleClef.iterator();while(iter.hasNext()){ //Extract the next array of notes from the ArrayList.String[] array = (String[])iter.next(); //Get the duration in beats and add to the total. trebleLengthInBeats += Integer.parseInt(array[0]); }//end whileif(bassFileName != null){//Compute length of bassClef in beats //Get an iterator on the trebleClefiter = bassClef.iterator(); while(iter.hasNext()){//Extract the next array of notes from the ArrayList String[]array = (String[])iter.next();//Get the duration in beats and add to the total. bassLengthInBeats += Integer.parseInt(array[0]); }//end while//Check for the possibility that the treble clef and the bass clef // are different lengths. If so, will play using the shorter of the// two later. This could a portion of the end of the melody to not // be played.if(trebleLengthInBeats != bassLengthInBeats){ System.out.println("Treble and bass are different lengths.");System.out.println("Will use the shorter of the two."); }//end if}//end if //Each channel requires two 8-bit bytes per 16-bit sample.int bytesPerSampPerChan = 2;//Override the default sampleRate of 16000.0F. Allowable sample rates // are 8000,11025,16000,22050, and 44100 samples per second.audioParams.sampleRate = 8000.0F;//Create an array of sufficient size to contain the treble melody. Treat// as mono at this point. May combine with bass melody later to create // a stereo output. Add an extra one-half second of capacity to deal with// possible round off error at the end. byte[]trebleMelody = new byte[ (int)(audioParams.sampleRate/2 + trebleLengthInBeats*audioParams.sampleRate*bytesPerSampPerChan/beatsPerSec)];System.out.println("trebleMelody.length = " + trebleMelody.length); //Prepare a ByteBuffer for usebyteBuffer = ByteBuffer.wrap(trebleMelody); //Call a method that transforms the notes for the clef into an array of// amplitude values. makeMusic(trebleClef,beatsPerSec);//Process the bass clef if it exists.if(bassFileName != null){ //Create an array of sufficient size to contain the bass melody. Treat// as mono at this point. Will combine with the trebleMelody later to // create a stereo output. Add an extra one-half second of capacity to// deal with possible round off error at the end. byte[]bassMelody = new byte[ (int)(audioParams.sampleRate/2 + bassLengthInBeats*audioParams.sampleRate*bytesPerSampPerChan/beatsPerSec)];System.out.println("bassMelody.length = " + bassMelody.length);//Prepare a ByteBuffer for use byteBuffer = ByteBuffer.wrap(bassMelody);//Call a method that transforms the notes for the clef into an array// of amplitude values. makeMusic(bassClef,beatsPerSec);//Use the shorter of the two lengths if they don't match. Note the use// of a Java conditional operator to accomplish this. int lengthLimit = (trebleMelody.length<= bassMelody.length) ? trebleMelody.length : bassMelody.length;//Create an output array that is twice the length of the shorter of the // bass or treble melodies to accommodate a stereo representation of the// melody. The bass melody will be put in the left-channel bytes in the // array. Similarly, the treble melody will be put in the right-channel// bytes in the array. melody = new byte[2*lengthLimit]; //Interlace the bass and treble melody data in the array so that the// bass will be played through the left speaker and the treble will be // played through the right speaker.for(int cnt = 0;cnt<melody.length-4;cnt+=4){ melody[cnt]= bassMelody[cnt/2];melody[cnt+1] = bassMelody[1 + cnt/2]; melody[cnt+2]= trebleMelody[cnt/2];melody[cnt+3] = trebleMelody[1 + cnt/2]; }//end for loopreturn melody;//return the array and terminate the method }//end if//There was no bass clef data. Return the trebleMelody to be played mono// from both speakers equally. return trebleMelody;}//end method getMelody//-------------------------------------------------------------------------// //This method transforms the notes for a clef into an array of amplitude// values. void makeMusic(ArrayList clef,int beatsPerSec){double gain = 4000.0;//Set the output volume to a reasonable level. //Get a hashtable that maps note names into note frequencies from A2// through A7. Note that the name "X" is used to indicate a period of // silence or a rest, so it is appended onto the end of the hashtable with// a frequency of 0.0. //Frequency values can be checked against// http://www.phy.mtu.edu/~suits/notefreqs.html. Hashtable piano = getPiano();piano.put("X",0.0); //Miscellaneous variablesdouble freq = 0; int beats = 0;double scaleFactor = 0;//Get an iterator on the clef Iterator iter = clef.iterator();//Process each array containing duration and note names in the list. while(iter.hasNext()){//Get the next array containing duration and note names String[]array = (String[])iter.next();//Get the duration of the note in beats beats = Integer.parseInt(array[0]); //Transform the notes into sound data at the appropriate frequencies and// duration. for(int cnt = 0; cnt<beats*audioParams.sampleRate/beatsPerSec; cnt++){ //Compute the time for this iteration to use when evaluating the// cosine function. double time = cnt/audioParams.sampleRate;double sum = 0;//sum of values for this iteration //Process each note in the array.for(int element = 1;element<array.length;element++){ //Iterate on the notes defined in the array.//Get the name of the next note and make sure that it is upper-case. String noteName = array[element].toUpperCase(); try{//Use the noteName and get the corresponding frequency from the // hashtable. Note that results are retrieved from the list as// type Object and must be cast to the correct type. freq = (double)piano.get(noteName);}catch(java.lang.NullPointerException ex){ ex.printStackTrace();System.out.println("noteName: " + noteName); }//end catch//Compute the amplitude for this note at this time and add it to// the sum unless it is a musical rest. if(!noteName.equals("X")){//This is not a musical rest. sum += Math.cos(2*Math.PI*(freq)*time);}//end if //Go back to the top of the loop and get the next note from the// array, if any. }//end for loop//Amplitude values for all the notes at this time have been added into// the sum. //Scale the amplitude value so that each note has a maximum amplitude// at the beginning and a zero amplitude at the end of the note. Use // a linear scale factor.scaleFactor = gain*((beats*audioParams.sampleRate/beatsPerSec) - cnt) /(beats*audioParams.sampleRate/beatsPerSec);//Scale the amplitude value and put it into the output array. byteBuffer.putShort((short)(scaleFactor*sum));//Go back and compute the next sample values for this set of notes // until the note duration is satisfied.}//end for loop //Go back, retrieve, and process the next set of notes for a given// duration value. }//end while}//end makeMusic method//-------------------------------------------////This method creates and returns a hashtable containing the name and // frequency of every note from A2 (110 Hz) at the low end to A7 (3520 Hz)// at the high end. A7 is the highest A-note that I can hear on my computer.Hashtable getPiano(){ Hashtable piano = new Hashtable();double factor = 1.05946309436;//12th root of 2 double freq = 110;//Frequency of A2 at 110 Hz. Start with this.String note = "A2";//Name of note at 110 Hz. Start with this.//Used to parse a note into 3 single-character substrings such // as C, 5, and #.String sub1 = null; String sub2 = null;String sub3 = null;for(int cnt = 0;cnt<61;cnt++){ //This loop counts up through A7 at 3520 Hz.//Store the name and the frequency of the note in the next element in // the hashtable.piano.put(note,freq);//Compute the frequency of the next note freq *= factor;//Use logic to determine the name of the next note in a sequence such// as A2,A2#,B2,C3,C3#,D3,D3#,E3,F3,F3#,G3,G3#,A3 //Begin by parsing the current note name into three single-character// substrings, the third of which may be null. if(note.length() == 3){sub1 = note.substring(0,1); sub2 = note.substring(1,2);sub3 = note.substring(2,3); }else{sub1 = note.substring(0,1); sub2 = note.substring(1,2);sub3 = null; }//end if//Use the three substrings of the current note name to determine the// name of the next note. This is long and tedious but it works. if((sub1.equals("A"))&&(sub3 == null)){ sub1 = "A";sub3 = "#"; }else if((sub1.equals("A"))&&(sub3.equals("#"))){ sub1 = "B";sub3 = null; }else if((sub1.equals("B"))){sub1 = "C"; sub3 = null;//Increment the number sub2 = "" + (1 + Integer.parseInt(sub2));}else if((sub1.equals("C"))&&(sub3 == null)){ sub1 = "C";sub3 = "#"; }else if((sub1.equals("C"))&&(sub3.equals("#"))){ sub1 = "D";sub3 = null; }else if((sub1.equals("D"))&&(sub3 == null)){ sub1 = "D";sub3 = "#"; }else if((sub1.equals("D"))&&(sub3.equals("#"))){ sub1 = "E";sub3 = null; }else if((sub1.equals("E"))){sub1 = "F"; sub3 = null;}else if((sub1.equals("F"))&&(sub3 == null)){ sub1 = "F";sub3 = "#"; }else if((sub1.equals("F"))&&(sub3.equals("#"))){ sub1 = "G";sub3 = null; }else if((sub1.equals("G"))&&(sub3 == null)){ sub1 = "G";sub3 = "#"; }else if((sub1.equals("G"))&&(sub3.equals("#"))){ sub1 = "A";sub3 = null; }else{System.out.println("Can't reach this point."); }//end else//Construct the next note from the updated substrings. if(sub3 == null){note = sub1 + sub2; }else{note = sub1 + sub2 + sub3; }//end if}//end for loop return piano;}//end getPiano }//end class PlayerPiano01//===========================================================================//

...

Listing 29 . The file named GreensleevesTreble.txt
//GreensleevesTreble //03,A4 //16,C5 3,D5//2 5,E51,F5 3,E5//3 6,D53,B4 //45,G4 1,A43,B4 //56,C5 3,A4//6 5,A41,G4# 3,A4//7 6,B43,G4# //86,E4 3,A4//9 6,C53,D5 //105,E5 1,F53,E5 //116,D5 3,B4//12 5,G41,A4 3,B4//13 5,C51,B4 3,A4//14 5,G4#1,F4# 3,G4#//15 9,A4//16 9,A4//17

...

Listing 30 . The file named GreensleevesBass.txt
//GreensleevesBass //03,x //16,A3 3,A3//2 6,A33,E4 //36,G3 3,D4//4 6,G33,D4 //56,F3 3,C4//6 6,F33,C4 //76,E3 3,B3//8 6,E33,B3 //96,A3 3,E4//10 6,G33,D4 //116,G3 3,D4//12 6,G33,D4 //136,F3 3,C4//14 6,E33,B3 //153,A3 3,C43,E4 //169,A3,C4,E4 //17

...

Listing 31 . The file named Greensleeves.bat.
echo off del *.classdel Greensleeves.au echo onjavac MusicComposer09.java java MusicComposer09 play 8 GreensleevesTreble.txt GreensleevesBass.txtjava MusicComposer09 Greensleeves 8 GreensleevesTreble.txt GreensleevesBass.txt echo offdel *.class pause

-end-

Questions & Answers

What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Berger describes sociologists as concerned with
Mueller Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?

Ask