<< Chapter < Page | Chapter >> Page > |
The predefined value for the variable named channels in Listing 13 is 1 for monaural. That is the value that is needed for this monaural program. Listing 9 contains a statement that sets the value of audioParams.channels to 1 simply to illustrate the syntax involved. That statement is superfluous. However, as you will see in Listing 10 , there is a statement that sets the value of audioParams.sampleRate replacing the default value of 16000.0F with 8000.0F. That statement is not superfluous. (Recall that the F suffix causes the value to be treated as type float instead of type double .)
Listing 10 contains several miscellaneous setup operations. The comments should be sufficient to describe the purpose of each of those operations.
Listing 10 . Miscellaneous setup operations. |
---|
//Each channel requires two 8-bit bytes per 16-bit sample.
int bytesPerSampPerChan = 2;//Override the default sample rate. Allowable sample rates are 8000,11025,
// 16000,22050,44100 samples per second.audioParams.sampleRate = 8000.0F;// Set the length of the melody in seconds
double lengthInSeconds = 3.0;//Create an output data array sufficient to contain the melody// at "sampleRate" samples per second, "bytesPerSampPerChan" bytes per
// sample per channel and "channels" channels.melody = new byte[(int)(lengthInSeconds*audioParams.sampleRate*
bytesPerSampPerChan*audioParams.channels)];
System.out.println("melody.length = " + melody.length); |
Audio data consisting of a sequence of random values is believed to be white, or at least pink. The code in Listing 11 uses a class named Random from the Java Standard Edition library to populate the array with byteshaving random values.
Listing 11 . Generating the white (random) noise. |
---|
Random generator = new Random(new Date().getTime());
for(int cnt = 0;cnt<melody.length; cnt++){
melody[cnt]= (byte)generator.nextInt();
}//end for loopreturn melody;
}//end method getMelody//-------------------------------------------------------------------------//
}//end class WhiteNoise |
According to the documentation, an instance of the Random class can be used to generate a stream of pseudorandom numbers. (The prefix pseudo means that the numbers in the stream are almost random but maynot be completely random.)
We need a stream of pseudorandom byte values. The Random class provides methods named nextBoolean , nextDouble , nextFloat , nextGaussian , nextInt , and nextLong that can be called to get one new value from the pseudorandom stream as the type indicated by the nameof the method. However, there is no method that returns the next value as type short or as type byte . (There is a method named nextBytes -- note the plural. It behaves differently from what is needed.)
Fortunately there is an easy workaround for this issue. We will simply get a stream of int values and use the (byte) cast operator to discard the 24 most significant bitsfrom each int value. It is probably safe to assume that if the int values are random, the eight least significant bits of each int value will also be random. Given that assumption, Listing 11 gets and populates the melody array with those values and then returns a reference to the melody array.
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?