Is there a nice way in Android to generate and then play a frequency (eg. 1000hz, 245hz)?
Frequency Sound Generator is a simple wave form sound generator and oscillator. It is easy to use tool so you can create high variety of sounds and signals in just few seconds. All controls are in real time so you can dynamically change the sound signals.
To use Frequency Generator, you could swipe your finger up and down to raise or lower the frequency of the tones. The user interface is very simple and clear. You could adjust the frequency up or down exactly in 1Hz with arrow button, and directly check the frequency by professional wave visualisation.
Signal Generator is an app that produces audio test tones. The basic app produces sine waves, and it can be extended and customized (via in-app purchases) to produce white noise, pink noise, frequency sweeps, and (on iPhone 4 and better) square waves, sawtooth waves, and triangle waves.
Spectrum analyser gives insight into your sounds and deconstructs the audio spectrum, showing the levels of the various frequencies in your audio signal. The built-in sound meter provides high-quality measurement results when measuring ambient noise levels in a multitude of real-life scenarios.
Take a look at android.media.ToneGenerator.
You can also try following Code.
public class PlaySound extends Activity {
// originally from http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html
// and modified by Steve Pomeroy <[email protected]>
private final int duration = 3; // seconds
private final int sampleRate = 8000;
private final int numSamples = duration * sampleRate;
private final double sample[] = new double[numSamples];
private final double freqOfTone = 440; // hz
private final byte generatedSnd[] = new byte[2 * numSamples];
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
super.onResume();
// Use a new tread as this can take a while
final Thread thread = new Thread(new Runnable() {
public void run() {
genTone();
handler.post(new Runnable() {
public void run() {
playSound();
}
});
}
});
thread.start();
}
void genTone(){
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
void playSound(){
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, numSamples,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With