Android Sound Synthesis and Latency

January 27th, 2012
music, phone, tech
Playing with people's iPhones I really enjoy music apps. Even though there's no real tactile feedback they're still really fun to play. When I got a computer with a responsive and accurate multitouch trackpad I wrote one for it. Now that I have a smartphone I wanted to write something similar: map regions of the screen into notes.

I started with libpd for Android, which is a port of libpd to Android, which is a library version of the audio engine from Pure Data. Learning enough Android programming to get this loading on my phone was a little annoying, as was figuring out how to modify the CircleOfFifths example app to play single notes, but I got something working. Unfortunately, the latency is terrible. I put down my finger, and there is a significant delay before sound comes out of the device. This isn't just my experience; round trip generate-speaker-mic-record latency with libpd on Android tends to be around half a second. Measuring mine with their tester I got 558ms.

While this is way high, I thought at first the problem might be with libpd and that by using the lowest level API directly I might get good performance. Unfortunately, it sounds like this is not the case: the best you can do on the best devices is about 50ms, which is still too slow. If you read the Android Audio mailing list, this is nearly all they talk about back to the third message on the list. There's a bug registered, and it's in the top 10.

It looks like the main problem is that most devices are not designed for it. To get 5ms latency at 44KHz you need to be able to get the buffer down to 256 [1] samples and keep it from emptying. Most Android devices have a minimum buffer size over ten times that. I don't think Google's going to be fixing this soon. This is sad. I want to make music live with my phone.


[1] Really 220 samples, but we like powers of two.

Comment via: google plus, facebook, r/androiddev

Recent posts on blogs I like:

Development RCTs Are Good Actually

In defense of trying things out

via Thing of Things March 25, 2024

Clarendon Postmortem

I posted a postmortem of a community I worked to help build, Clarendon, in Cambridge MA, over at Supernuclear.

via Home March 19, 2024

How web bloat impacts users with slow devices

In 2017, we looked at how web bloat affects users with slow connections. Even in the U.S., many users didn't have broadband speeds, making much of the web difficult to use. It's still the case that many users don't have broadband speeds, both …

via Posts on March 16, 2024

more     (via openring)