<< Chapter < Page Chapter >> Page >

Map the sinusoidal function values into pulse frequencies

Listing 5 begins with a for loop that eventually deposits audio outputvalues in the melody array.

Listing 5 . Map the sinusoidal function values into pulse frequencies.
for(int cnt = 0; cnt<sampLength; cnt++){ //Compute the time in seconds for this sample.double time = cnt/audioParams.sampleRate;if(cnt%pulseLengthInSamples == 0){ //Time to create a new pulse at a different pitch. Compute the// frequency for the next pulse to represent a point on a sinusoidal // function of time. This section of code could easily be modified// to create audio graphs of many different functions.//Evaluate and scale the function double val = 0.35 * Math.sin(2*Math.PI*0.5*time);//Compute the frequency for the next pulse as a deviation from the// center frequency. For this scaled sinusoidal function, the Range // is from 0.65*centerFreq to 1.35*centerFreq or from 170.05 Hz// to 353.2 Hz. freq = (1+val)*centerFreq;}//end if

Compute the current time

The code inside the for loop in Listing 5 begins by computing the time in secondsfor the current sample. This value will be used later to evaluate the sinusoidal function that is being graphed using audio. It will also be used to compute thevalues of the higher-frequency sinusoids used to control the pitch of the pulses.

Compute the pitch of the pulse

An if statement is used with the modulus operator to cause the pitch to change from one pulse to the next. The code inside the if statement computes the amplitude of the sinusoidal function being graphed at thecurrent point in time. This value will range from -1.0 to +1.0 for a sinusoidal function, but the range may be different if you apply this approach to otherfunctions such as a parabola. It then uses that amplitude value to compute the required pitch of the pulse, at, above, or below the center frequency torepresent the amplitude of the sinusoidal function at, above, or below 0.0. The new pitch value is stored in the variable named freq .

Shape the pulse amplitude from beginning to end

When I first wrote this program, I noticed there were a lot of pops and clicks as the pulse frequency changed abruptly from one pulse to the next. Toeliminate that problem, I applied a shaping scale factor to the amplitude of each pulse to cause it to:

  • Start at zero at the beginning of the pulse.
  • Increase linearly to a maximum value at the center of the pulse.
  • Decrease linearly back to zero at the end of the pulse.

This is accomplished using the gain factor that is computed in Listing 6 . The gain factor ranges from 0.0 at the ends to 1.0 in the center of the pulse. I will leave it as an exercise for the student todecipher this code. (Hint: It would help to know the equation for a straight line.)

Listing 6 . Shape the pulse amplitude from beginning to end.
gain = (cnt%pulseLengthInSamples)/(double)pulseLengthInSamples;if(gain>0.5){ //Change to a negative slope.gain = (pulseLengthInSamples - cnt%pulseLengthInSamples)/(double)pulseLengthInSamples;}//end if //Set the gain to a value that is compatible with 16-bit audio data.gain = 8000*gain;

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