开发者

Playing sound of a particular frequency in android

I want to play a sound of a particular frequency in android based on user input, somewhat similar to guitar application. Can an开发者_JAVA技巧yone tell me how to do that? Do i need to have all freq sounds in the res\raw folder?


No, you do not need to store that in the resources. I think better approach would be to synthesize the tones in a real time. For that purpose you would need the following:

  • java.lang.Math for trigonometric functions calculations to fill buffer (after conversion from float to PCM (int)) with correct tone
  • android.media.AudioTrack for audio playing (from buffer). I suppose static mode would be what you need - faster response and easier to operate. However, audio playing is not trivial task at all so you would need to learn more about audio system on Android. Of course, some basic knowledge about signal processing is more than welcome.
  • you may even add some cool effects using android.media.audiofx


Send the sound to an AudioTrack instance and change its sample rate on the fly.


This is an extremely complex question even though it doesn't seem like it.

When you say 'sound' do you simply mean a tone (sine, square, triangle, sawtooth waves for example) or do you wish to play sounds of 'real' instruments such as guitar, piano and so on?

If it's the first one (simple tones) then Desiderio's answer may be on the right track but if you want 'real' insrument sounds then you will need to have a short 'sample' in either /res/raw or in your /assets directory.

You can load them into a SoundPool instance and use it to playback at different 'rates', e.g., a 'rate' of 2 will be twice the speed and double the frequency thus increasing by an octave. A 'rate' of 0.5 will be half the speed so lowering the sound by an octave.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜