<< 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;

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
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
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
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.
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
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?