<< Chapter < Page
  Accessible objected-oriented     Page 5 / 17
Chapter >> Page >
  • // This is a comment in the text file
  • 1,A3,C4#,E4
  • 4,X
  • 2,A3,C4#,E4
  • 4,X
  • 3,A3,C4#,E4
  • 4,X
  • 4,A3,C4#,E4

Normally, the first character on each line must be a number or a /. Comments must begin in the first column with a forward slash (/).The program will ignore:

  • Comment lines that begin with /
  • Blank lines that result in a string with a zero length
  • Lines that begin with a space

The frequency range extends from A2 (110 Hz) to A7 (3520 Hz). Note, however, that someaudio speakers cannot produce sound at the low or high end of that spectrum. Middle-C (261.63 Hz) is specified as C4.

Shaping the notes

The amplitude versus time of each note is shaped by a scale factor. The amplitude is maximum at the beginning and decays linearly to zero at the end.

Beginning of the PlayerPiano01 class

The class named PlayerPiano01 begins in Listing 5 . Assuming that you have studied the previous modules in this series, there isnothing in Listing 5 that should require an explanation beyond the embedded comments.

Listing 5 . Beginning of the PlayerPiano01 class.
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;

The constructor for the PlayerPiano01 class

The constructor is shown in Listing 6 . The code in the constructor checks the length of the args array. Depending on the length, it gets and saves the file names for neither, either, or both of the text files that define the melody.

Listing 6 . The constructor for the PlayerPiano01 class.
public 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

Beginning of the getMelody method

This program uses the same structure as several programs in previous modules. By now, you should be familiar with that structure. Theoverridden getMelody method, which is inherited from the abstract class name AudioSignalGenerator02 and called from Listing 4 , begins in Listing 7 .

This method returns a melody array that will play piano-like sounds using input musical notation like

A, A#, B, C, C#, D, D# etc.

See Listing 29 for an example of a text file containing the treble clef notes for the Greensleeves melody.

Listing 7 . Beginning of the getMelody method.
byte[] getMelody(){if(bassFileName != null){ //Specify stereo output.audioParams.channels = 2; }else{//Specify mono output. audioParams.channels = 1;//superfluous - same as default}//end else System.out.println("audioParams.channels = " + audioParams.channels);//Controls how fast or slow the notes are played. int beatsPerSec = Integer.parseInt(args[1]);

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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