Question

I try to synthesize piano sounds using speed of playing in SoundPool. I get sounds but it sounds... so ugly. It sounds near the right sound frequency but not exactly. It sounds sometimes higher than I need (just a little) and sometimes lower - but It sounds so strange :)

float shift;
for ( int i = 0; i < 13; i++ ) {
    shift = ( float ) i / 12f;
    soundPool.play( soundID, 1, 1, 1, 0, ( float ) 1f + shift );
    try {
        Thread.sleep( 1500 );
    } catch (InterruptedException e) {
        e.printStackTrace();
    }   
}

where: soundID - is the loaded piano C4 note.

Maybe someone can tell me how to synthesize piano sounds in Android?

Was it helpful?

Solution

your main problem is using wrong math for changing sound pitch, instead of:

shift = ( float ) i / 12f;

you should use:

shift = pow( pow(2.0, 1/12.0), (float) i )

or something like that.

another problem of yours is the quality of realtime conversion is usually poor, I'd recommend to prescale 12 key tones and then change pitch 2x, 3x and so on, which can be done by simply dropping out every n-th sample. moreover, if I were you, I'd presample the whole keyboard (your application hardly needs more than 20-25 keys, because of limited screen space, etc.), and have all these sounds loaded in the SoundPool and ready to play at any time.

OTHER TIPS

I found a useful link here . However, it is the suggests the same formula as already posted by lenik, except that it has extra info. too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top