3

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

Ashwin
  • 132
  • 3
  • 13

3 Answers3

4

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
Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • I'll try this out and get back to you in case of any difficulties, which m sure i will find.. :p.. Thanks a lot Desiderio... :) – Ashwin Dec 16 '10 at 06:02
2

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

BTR
  • 21
  • 1
1

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.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • 1
    Agreed. I tried to answer it as simple as possible. Just to mention that physical modelling of real instruments is possible, using e.g. Karplus-Strong algorithm. So, using current computational power of Android smartphones, it should be possible to implement real-time synthesis. – Zelimir Dec 13 '10 at 22:13
  • I was looking for playing sounds of instruments.. So for that there is no other way other than the one u suggested? In that case choice of the 'sample' would be very important right...? – Ashwin Apr 15 '11 at 10:01
  • @ashwin There is also MIDI, I believe. – ksoo Dec 19 '14 at 13:22