1 /***********************************************************************************2 * Generate and Play A Tone in Android hacking3 * Disclaimer:4 * 1. Source code URL:http://stackoverflow.com/questions/2413426/playing-an-arbitrary-tone-with-android5 * 2. The main function is to allow the Android phone to emit a certain frequency of sound;6 * 3. Of course we can also be used to do all kinds of waveform generator, this is just a simple demo source code program;7 *8 * 2015-4-27 Monday Sunny Shenzhen Ping Shan village Zengjianfeng9 **********************************************************************************/Ten Public classPlaySound extends Activity { One //originally fromhttp://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html A //and modified by Steve Pomeroy <[email protected]> - /*sound lasts 3 seconds*/ - PrivateFinalintDuration =3;//seconds the /*Adoption rate is 8000*/ - PrivateFinalintSamplerate =8000; - /*total number of points sampled*/ - PrivateFinalintNumSamples = Duration *samplerate; + /*generating an array of data using a total number of sampled points*/ - PrivateFinalDoubleSample[] =New Double[NumSamples]; + /*Sound Frequency*/ A PrivateFinalDoubleFreqoftone = the;//Hz at - /*mainly because of the 16-bit data used*/ - PrivateFinalbyteGeneratedsnd[] =New byte[2*NumSamples]; - -Handler Handler =NewHandler (); - in @Override - Public voidonCreate (Bundle savedinstancestate) { to super.oncreate (savedinstancestate); + Setcontentview (r.layout.main); - } the * @Override $ protected voidOnresume () {Panax Notoginseng Super.onresume (); - the //Use a new tread as this can take a while +Final thread thread =NewThread (NewRunnable () { A Public voidrun () { the Gentone (); +Handler.post (NewRunnable () { - $ Public voidrun () { $ PlaySound (); - } - }); the } - });Wuyi Thread.Start (); the } - Wu /** - * Generate sound data corresponding to the above parameters, stored in the array About */ $ voidGentone () { - //fill out the array - for(inti =0; i < numsamples; ++i) { - /** A * Algorithm Analysis: + * 1. Samplerate/freqoftone: The number of points per cycle; the * 2. I/(Samplerate/freqoftone): The current I-point in the entire cycle of - * The specific gravity of the sampling point (I do not know how to express:) ); $ * 3.2 * Math.PI * I/(Samplerate/freqoftone): The radian system of the current point I; the * 4. Math.sin (2 * Math.PI * I/(Samplerate/freqoftone)): 1 to 1 of the value of the SIN function; the */ theSample[i] = Math.sin (2* Math.PI * I/(samplerate/freqoftone)); the } - in //convert to + bit PCM sound array the //assumes the sample buffer is normalised. the /** About * Convert the value of the SIN function from the range 1 to 1 above, to a value of 32767 to 32767 range, the * This value is a 16-bit value, placed in the corresponding array the */ the intIDX =0; + for(FinalDoubledval:sample) { - //Scale to maximum amplitude theFinal Shortval = ( Short) ((Dval *32767));Bayi //in-bit WAV PCM, first byte is the low order byte thegeneratedsnd[idx++] = (byte) (Val &0x00ff); thegeneratedsnd[idx++] = (byte) (Val &0xff00) >>>8); - - } the } the the voidPlaySound () { theFinal Audiotrack Audiotrack =NewAudiotrack (Audiomanager.stream_music, - samplerate, Audioformat.channel_out_mono, theAudioformat.encoding_pcm_16bit/*16-bit data*/, Generatedsnd.length, the audiotrack.mode_static); the //write out all the data, which is equivalent to making a sound94Audiotrack.write (GENERATEDSND,0, generatedsnd.length); the Audiotrack.play (); the } the}
Generate and Play A Tone in Android hacking