Sunday, March 16, 2014

Android Audio Stutter -- Fixed!

Motorola DROID RAZR M
Not long ago I finally bit the bullet and replaced my old flip phone with a smart Android phone. I had several reasons: I had an increasing need to respond to people texting me, and the ability to check email and sync up work and personal calendars on one device was attractive too. I also figured I could take advantage of 32GB SD storage to put my entire music collection on the device, and have it with me always. Or so I thought.

Android audio playback was a disappointment. The audio quality was fine, if you can call stuttering, choppy audio fine. It would be playing along perfectly; I'd be enjoying the music, not thinking about the hardware, when all of a sudden it would stop for a half second or so. Very jarring -- disconcerting even! It doesn't take much to pull all of the enjoyment of music out of an experience when you get yanked back into reality every few minutes at random.

This pattern occurred with all three music apps I use: Amazon MP3, Pandora and Play Music. Choppy audio is usually due to multi-tasking other applications. I tried shutting down all the other running apps, trying to find the culprit. Unfortunately, even with all apps disabled, it kept happening. Then I stumbled upon the Power Control Widget. The Power Control Widget allows me to disable the sync feature (it turns off the periodic email, calendar and other data fetching functions). It appears the sync feature is what stutters the audio.

Android Power Control Widget
Left to Right: Wireless, Bluetooth, GPS, Sync, Screen Brightness

The sync feature may be a poorly written app, or maybe it really needs the hardware to run at a higher priority than keeping the audio buffers full, or feeding audio from the buffers into the D/A converters in real time. Whatever the reason, disabling sync seems to be the solution.

So, I set up a home screen with all of my audio apps, and with the Power Control Widget at the bottom. Now, when I go to play audio, I simply turn off sync. I admit, it's a pain to remember to do that (especially turning sync back on when I'm done listening), but it's better than the alternative. Maybe someday, the Android developers will fix this, but in the meantime I'll live with it as-is.

Thursday, February 20, 2014

WxService Update Available

WxService ow4j140220

  • Fixed a glitch in the total rain accumulation calculation. The reset time wasn't being calculated reliably, causing total accumulation to continue into the next day (or other configured recording interval).

Wednesday, February 19, 2014

WxService Update Available

WxService ow4j140219

  • Added support for WeatherBug publishing. To use the WeatherBug formatter, first register your station, and then follow the WxService setup instructions for expanding the zip file into your preferred location, and start WxMonitor. Switch to the Configuration tab and change weatherbug.formatter.password to the value you selected when you registered your Backyard weather station. Set weatherbug.formatter.station.id to the station ID that you received when you registered your station. Set weatherbug.formatter.station.number to the station number you received when you registered your station. Finally, add WeatherBug.formatter to wxservice.formatter.task.names (separated by semicolons), and click "Apply". The WeatherBug formatter transmits weather data to WeatherBug servers every five minutes (300000 msec) by default. You can change this by editing the weatherbug.formatter.interval property. One second is 1000 msec and one minute is 60000 msec.
  • Added code to explicitly set all temperature sensors to maximum resolution. This does not guarantee that any sensor will actually deliver higher resolution, and most 1-Wire devices are supposed to operate at maximum resolution by default. This is just an attempt to ensure the highest resolution.

Saturday, January 18, 2014

Is 16 Bits at 44.1 KHz Enough?

Is 16 bits at 44.1 Khz enough? Yes, if...

Spectral cleanup with dither
If you're just listening, and if the source material is properly dithered, and properly mastered, 16 bits at 44.1 KHz is good enough. Decades of double-blind testing have proved it time and again. But that's a big if.

There are a number of things that will affect the sound quality and the need for more bits and more samples. The modern CD loudness wars, which compress, limit and clip the audio into a quivering mass of Velveeta would get by with 8 bits and practically nobody would notice. A well-mastered and well-engineered CD at 16 bits and 44.1 KHz sample rate can sound fantastic. A poorly-mastered and poorly-engineered one will sound like crap.

Are there ever times when more than 16 bits and 44.1 KHz sampling would be better? Yes. In a recording environment, where the incoming live sound is unpredictable, it's better to leave some headroom for unexpected, accidental peaks. If you're using all 16 bits, and a louder peak comes along, it will overload the A/D converter. Eight additional bits provide 48 dB of additional headroom, which is itself more than the dynamic range of the vast majority of commercial CDs being sold today - especially the loudness war victims.

In addition, when mixing tracks, each additional track adds about 6 dB to the sum (assuming each track is mixed at full scale - a worst-case scenario). Mixing eight tracks will consume all 8 additional bits of headroom. To avoid overload, recording additional tracks means the level of all tracks will have to be attenuated in the mix. If they're 24 bit tracks, they can be turned down without any loss of resolution (they do have to be re-dithered however - in fact, audio should always be re-dithered when it is re-scaled - up or down).

For live recording and mixing, you can do better with 24 or even 32 bits (Audacity uses 32-bit floating point by default). But for commercial distribution via e.g., CD, 16 bits really is enough. Trust me on this.

So, what about sample rate? Well, that's much less important with modern oversampling sigma-delta A/D and D/A converters. Humans can hear at best, out to 20 KHz. Most of us are lucky if we can hear 15 or 16 KHz, although it really depends on how loud the signal is. I suspect more humans could hear 20 KHz if it's loud enough. For music, there just isn't much significant energy above 15 KHz. A sample rate of 44.1 KHz means that the digital recording system can accurately capture up to a maximum of 22.05 KHz: more than most anyone can possibly hear in a musical setting. The Nyquist Shannon sampling theorem tells us that we absolutely, positively need not sample more than twice the highest frequency of interest. This is not speculation; it is one of the most well-validated results in the field of information theory.

Aliasing

In order to prevent ultrasonic frequencies, above 22.05 KHz, from getting into the A/D converter and getting aliased back down into the audio frequencies, the input should ideally be filtered. As a practical matter, the filtering requirement on A/D converters is fairly weak, because most music doesn't contain a lot of energy at or above 20 KHz, and certainly not far enough above to be aliased down to frequencies that we hear readily. Still, most good A/D converters do provide high quality "brick wall" anti-aliasing filters. It is very difficult to build analog brick wall filters, but modern (i.e., since about 1995) oversampling sigma-delta converters can do this using digital filters with mathematical precision. They hypersample internally, but they output e.g., 44.1 KHz or whatever the desired sample rate is.

The filter function is different for D/A converters, but the requirements are about the same. The output filters are used to remove the ultrasonic sidebands that are generated by the sampling process. Although we can't hear them, those ultrasonic frequencies can wreak havoc with amplifiers and tweeters, and they need to be removed. It is probably sufficient to merely attenuate the ultrasonic sidebands with a softer roll-off that won't over stress downstream equipment. But modern (i.e., since about 1995) oversampling sigma-delta converters can provide digital "brick wall" reconstruction filters with mathematical precision.

Back when ADCs and DACs ran at the nominal sample rate, we had to filter using analog elliptical filters, and I could justify a bit of "slack" between the stop band and the sample frequency. It reduced the constraints on the analog filter, which were critical and hard to keep in spec. In those days, sample rates of 48 or 50 KHz were common. But for today's oversampling sigma-delta ADCs and DACs with their built-in digital filters, any sample rate above 44.1 KHz is simply an outdated concept.

Do we need to sample at 96 KHz? Certainly not! It doesn't make the filtering job any easier. All you're adding is more load on the CPU and wasting storage space. If you can't hear 20 KHz, you're never going to hear 48 KHz, or anything in between. And 192 KHz? That's four times higher than 44.1 KHz! If you can't hear 20 KHz, neither you or your dog are going to hear 96 KHz. Why on earth store all that inaudible chaff? It's processor abuse!

Monday, January 13, 2014

WxService Stops Responding After Approx 17 Hours

I noticed recently that WxService would stop responding after a WxMonitor client was connected for about 17 hours (or half that with two WxMonitor clients, etc.). It turns out that StAX XMLInputFactory cannot create more than 64000 readers in JDK 1.7.0_45. In other words, WxService can only respond to 64,000 update requests until the XML parser stops responding. At one request per second, a single WxMonitor makes about that many requests in about 17 hours. Fortunately, there is a work-around that should hold us until Oracle produces a fix for this problem.

Using Notepad or other simple text editor, create a a file containing the following text:
#Hack for JAXP00010001 error.
jdk.xml.entityExpansionLimit=0
and save it as C:Program Files/Java/jre7/lib/jaxp.properties (that is, place the file jaxp.properties in $JAVA_HOME/jre/lib).

I will publish an update when this is no longer a problem.

Update: The latest WxService includes a code change that uses a different thread pool implementation that seems more compatible with JAX-WS. By "more compatible", I mean "the one it uses internally by default". There were some features that I liked with the one I had been using, but locking up wasn't one of them, so there you go. If you're experiencing lockups, download the latest version

Friday, December 6, 2013

Microsoft Ends Windows 7 Retail Sales



Microsoft quietly announced that it officially ceased sales of Windows 7. This is the first time I am actually sad to see a previous version of Windows come to an end. Windows 7 has been my favorite incarnation of the popular OS to date. I have called it "Vista, done right". With Windows 7, Microsoft solved a number of nagging problems from earlier versions, including performance, security, usability, and so on.

Originally, I was anxious to know what Windows 8 would bring. But not anymore. Windows 8 is a huge regression, in my humble opinion. I have been using Windows 8, side-by-side with Windows 7 on various machines. I still stand by my assessment that Windows 8 is a usability disaster; a schizophrenic OS that does a poor job as a tablet interface and as a desktop interface, and forces users to switch between vastly different UIs to accomplish tasks on the same platform. It's just dreadful.

I write software for a living, so I feel that I have some knowledge of which I speak. I'm sorry to see Win7 die, and I hope Microsoft can see fit to give Windows 8 some much needed treatment for its schizophrenia.

Wednesday, November 6, 2013

High Fidelity Hobby Becomes Necessity

I have been an audiophile all my semi-adult life, getting into it in a big way while I was in high school. I just bought the most sophisticated sound system that I have ever owned: a Phonak Audeo Q90-312 system. That's right, they're deaf-aids. I have been struggling with hearing loss for about a decade, and I finally got fed up with not hearing offhand remarks in meetings, and asking people to repeat themselves. My music enjoyment was never affected much, if I turned it up loud enough. But my family objected to that.

I resisted getting hearing aids because I figured the fidelity would be low; optimized for speech intelligibility over fidelity. I had already decided what I wanted: Something like the Dolby S encoder for cassette tape. And it had to be high fidelity, wide band and low distortion.

The Dolby S encoder is essentially a multi-band, equalized compressor, whose job it is to keep low-level signals above the tape hiss, backing off once the input signal gets above the noise level. In my case, the noise is tinnitus - ringing in the ear. For me, it's like narrow band pink noise, centered on about 4 kHz. Sounds need to be louder than that, in that spectral region, in order for me to hear them.

One of the features I like about Dolby S is that the design was based on the principle of least action (Ray Dolby borrowed the term from physics, but it makes sense in a different way, in this context too). As the signal gets above the noise, further expansion isn't necessary, so Dolby S gets out of the way, and lets the system behave normally. Of course, my hearing is compromised, so while the encoder (hearing aid) brings the signal up above the ringing in my ears, there is no corresponding decoder to undo the encoding. Or is there? As a matter of fact, there is: my brain! I'll get back to that.

Hearing Impaired Musician
I started researching hearing aid technology, asking in particular, what do hearing impaired musicians use? Well, they use high-fidelity, WDRC-TILL (wideband dynamic range compression with treble increase at low levels) devices (or they did a decade or so ago). So, armed with this information, I sallied forth to my local audiologist, and told her that I'm her worst nightmare: a well-informed electronics buff, critical listener and opinionated & demanding audiophile. That news didn't seem to upset her in the least.

She took me back to the soundproof room and we did a hearing test. She keyed the data into her desktop computer, which displayed my hearing chart and calculated the necessary correction. We discussed various models of deaf-aids, and I explained that I would require the highest possible fidelity, and that I was very skeptical that anything could meet my requirements. She finally selected a sample and put them in my ears. She then radioed the prescription to the devices, configured them with the necessary gain, equalization and compression (in 20 bands). I could instantly hear better, and they sounded great (w00t)!

These devices have FM transceivers, so they can communicate with each other, and with a Bluetooth ComPilot that I can wear around my neck if I want to use my cell phone hands-free, or listen to a TV or iPod through my deaf-aids. In normal operation, they radio audio information left and right, to continuously triangulate on stereophonic directional cues, just as our brains do. So they can classify noise sources, as well as speech sources, that the digital signal processor on each ear can then sort out in real time. It's quite amazing, and all that in a package that is so small that the casual observer cannot even see I'm wearing unless I point them out.

The electronics (7/8" long) tuck behind each ear, and the tiny loudspeakers, pictured above right, on the other end of the wire, go in each ear canal. They have excellent fidelity - at least for frequencies above 300 Hz or so. Below 300 Hz, where I don't require any assistance anyway, the direct sound just goes right past them. They're driven by a super high-efficiency class-D amplifier, so battery life is about six or seven days (if I turn the devices off at night when I'm not using them). Batteries are mercury free air-zinc type 312.

For the first two weeks, I was delighted that I could understand speech so much better, even in noisy restaurants, and in conferences at work. But I did notice some distracting artifacts: the stereo image seemed to move around, and I noticed a strange "warble", kind of like the sound I would hear when I was a kid, and my brother and I would talk to each other through a rotating fan. Some people describe the sound as being like talking in a corrugated pipe.

The first artifact was undoubtedly due to the devices trying to make voices more intelligible, by changing their directional characteristics. It's great for speech, but for music, not so much. The second artifact, I found after doing a bit of research on the web, is probably caused by the anti-feedback algorithm being set too high. This chipset (code named Spice) has a characteristic sound when feedback cancellation kicks in.

So when I went in for my initial two-week tune-up and oil change, I told the audiologist that I need a "stable platform" for music; I described the warble, and what I had found on the web. She pulled up the configuration software, and I was pleased to see, there was a "music" program right on the menu. So she added it to my configuration options. When I listen to music, I can push the button behind my ear to select the music mode.

The audiologist also noted that anti-feedback was set rather high. She turned it off, and we experimented with various feedback-inducing scenarios, none of which set them off, so we disabled anti-feedback, and the warble is gone. My hearing loss probably doesn't require enough gain to risk feedback. That could change if my hearing continues to decline, but for now it isn't a problem.

When I first started wearing the devices, it was like having new glasses: everything sounded freakishly clear, and I noticed details that I hadn't heard in years. This is like listening to a Dolby S-encoded cassette without the decoder turned on, only more so. But after a while I became accustomed to the sound, until now it just sounds natural - except that I can understand speech and hear soft music. My brain has adapted and provides the necessary decoding.

The fidelity is spectacular - and I'm speaking as an audiophile now. I can listen to music and enjoy it more, and the hearing aids only enhance the experience - they don't get in the way, as I feared they might. Low-volume live music is audible and enjoyable again. I have noticed zero distortion or overload with loud music, and no compression artifacts with sudden dynamic changes. My family appreciates that I don't have to turn up my sound system or the TV to deafening levels. Now I wonder why I waited so long to do this.