Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synthesize piano sounds in android/java

I have made a few simple apps on android, and thought it was time for something a bit more complex. So, i thought I'd try something that's already out there, but build it from scratch.

The idea is to create an app that allows user to play piano by pressing virtual keys on the display. But I'm not sure how to go about synthesizing the sound of each note, is it best to have copies of of each note stored on file, or is there a more dynamic way of synthesising notes and chords on the fly.

I have worked with C++ so NDK stuff is also okay. Thanks for any help.

like image 277
Abid Ullah Avatar asked Dec 03 '10 17:12

Abid Ullah


2 Answers

Sound playback (handing off buffers) pretty much has to be done from the Android java apis

Synthesis could be done in native or java, whichever it preferred.

Short (uncompressed) samples could be played back repeatedly, but you probably also want an attack transient. Perhaps you could have an attack, a sustain, and release, repeating the sustain as long as the key is down. Ideally each sample should be an integral number of periods of its fundamental component long so that you don't get a transient when you change between the attack to sustain or sustain to decay.

I'm sure you can find code somewhere for an FM or other synthesizer... this you might well want to implement in a native library that hands off buffers to java code to pass to the audio apis.

What is too bad is that android already has an internal midi synthesizer, but apparently lacks a dynamic interface to it, so it can only play midi files.

like image 197
Chris Stratton Avatar answered Nov 14 '22 22:11

Chris Stratton


By far the easiest solution would be to record the sound of each note on the piano and play it back when the key is pressed. Many professional virtual piano instruments work this way, recording every note on the piano being played at multiple velocities. Obviously this can take many gigabytes of disk space, but for a mobile phone app, you might get away with a single MP3 recording of each note in an octave.

Actually algorithmically synthesizing the sound of a piano is very difficult to do, and until fairly recently, very few have done it convincingly (pianoteq is one of the best current implementations).

like image 38
Mark Heath Avatar answered Nov 14 '22 22:11

Mark Heath