Thursday, February 26, 2009

iTunes to Shower Speakers

[Update: I've posted a video about installing a bathroom speaker using an Airport Express as part of an Amazon review of a
SpeakerCraft CRS6 One in ceiling speaker
. If you like it, I'd appreciate helpful votes.]

As I’ve mentioned before, the big new thing at the house the last year was a much improved moderately large shower in the basement. It’s become a locus of activity, as the babies have taken to bathing there making ad hoc bath basins out of plastic toy bins.

One thing missing would be a source of music. It occurred to me that if I could get an Airport Express mounted near the ceiling over the shower, I could use its AirTunes capability to stream music while I bathed. This would have the advantage of also allowing my wife to stream music or radio programming from her computer as well. Refurbished Airport Expresses go for $79 at the Apple online store with free shipping. Plus, I’d have another wireless N source.

[Update: Did you know that if you have an iPod Touch or iPhone, and an Apple TV, and an Airport Express, you can configure the Apple Remote for iPhone to tell the Apple TV to send its music to the Airport Express. And yes, I have all those things, and yes, Apple makes more money off me than the State of New Hampshire.]

According to the AVS Forum’s speaker section, a good in-ceiling speaker for the high humidity environment of a shower's ceiling is the Jamo 6.52DVCA2. I have done no comparing, so I cannot personally attest to how it sounds versus other in-ceiling products. It sounds fine to me, and it turns out is easy to install due to its well thought out mounting hardware. This speaker integrates both a left and right channel, so I only needed the one, and made do without much separation. This was found online for $78 shipped. [Update: Be sure to point both tweeters at where your head will be. I made the mistake of pointing one at the tiled wall, resulting in a muffled, echoey sound. Also, you might want to put some silicon sealer between the dry wall and the plastic bevel of the speaker.]

Finally, I needed a small amplifier to bridge the unamplified output of the Express with the 8Ω inputs of the speaker. Again, I am not an audio person, but I found a small 2×15 W stereo amplifier on Amazon, a Pyle Pro PCA1 Mini 2×15W Stereo Power Amplifier for $36 shipped. I didn’t suppose the small enclosed space of the shower would need any more power than that. And I was right. The system is plenty loud with the volume knob set at one quarter maximum. The amp's inputs are male RCA jacks, requiring a female RCA to mini jack adaptor. I wrote a short Amazon review on this amp, and I'd appreciate any helpful votes.

Remember, the Airport Express is not a "line out" analog device. The loudness of the output can be controlled via the volume slider in iTunes or the Apple iPhone Remote app depending on the setup. While this results in somewhat less than optimal sound, it is extremely convenient.

My original thought had been to install an electrical socket in the space above the shower and plug both the amplifier and the Airport Express there. But the only advantages to that plan would be to marginally improve my home’s WiFi coverage, and to limit any noise caused by runs of analog signals in stereo wire. Neither reason was compelling enough to justify the added expense and hassle of dealing with household current and possible building code problems. As it happened, I had a wall socket in my laundry room which was 20 feet of stereo cable from my shower's ceiling, and there is where I placed the amp and WiFi hub.

So the total required hardware:

That does tend to add up quickly, doesn’t it? Well, maybe you needed a new 802.11n wireless hub anyway.

In retrospect, I should have gotten 4 conductor in wall stereo cable instead of 2 conductor. The added complexity of dragging two cables had no benefit.

I went to Lowe's and purchased a drywall saw, safety goggles, and a dust mask; all of which made the task of cutting out the speaker mounting hole painless. It is amazing how easily a hand drywall saw cuts through drywall. The hardest part was finding a place to cut the hole where there was plenty of space in the void above the ceiling. I'd have preferred a more symmetrical arrangement with the ceiling light, but the geometry of the joists was such that my options were limited. I used gluestick to temporarily stick the provided paper template so I could very precisely mark out the hole in pencil then started cutting with the saw; very easy. The JAMO speaker easily screws into place with a hidden mount.

And now I have my shower music. Time will tell if it was worth the expense.

[Update: Energy usage. I attached everything to my Kill-A-Watt, and while playing the Airport Express and the amplifier together draw 7 W, while not playing they draw 5 W, and with the amplifier turned off the Airport Express by itself draws 3 W. So, at 5W idle, it costs much less than a dollar a month to power the system. Also, while playing my unibody MacBook draws around 17 W with its screen asleep, so a system which played music 24/7 would cost on the order of $3/month of electricity.]

Wednesday, February 25, 2009

Mythtv + HDHomerun + SheevaPlug : Connecting the Dots

Marvell just announced a 5 W Linux box with a USB 2.0 port and Gigabit ethernet, the SheevaPlug. This is a full Linux server with a 1.2 GHz processor in a box the size of a large wall wart power supply for a price of $79 US. Presumably any geeky person reading this has a dozen things they could do with such a product, and here is one: MythTV backend.

One of my problems with MythTV has been the old Pentium 4 Dell desktop I use as a backend. The monster draws upwards of 100 W. 24/7. Just to record 5 hours of TV a week and serve out videos.

Well look at what we can build now:
  • SheevaPlug 5W ($79)
  • HDHomerun 5W ($159) Dual HD TV Tuner
  • 1.5TB USB Hard Drive 10 W ($110)
  • 20 W for $348

The hard drive cost and the dual tuner are constants when building a MythTV system. I'm not counting the cost of an ethernet switch, as I assume most of us would have an extra port regardless. The major point is using a SheevaPlug for a MythTV backend would be much cheaper even then using a NetBook $250, and would very rapidly be cheaper than even re-using an old desktop $10/month electricity versus $0.50/month for just the SheevaPlug.

And it should be quiet.

On the down side, it would be useless as a frontend, and probably not up to the task of re-encoding videos.

Tuesday, February 17, 2009

When An App Rejection Improves An iPhone App

[Update: Lullabies is now in the app store. Check it out.]

I recently submitted Lullabies an iPhone App to Apple. They reviewed it for a few days and rejected it. The basic design had been that a user would choose a soothing animation/song and that animation would play to completion, and only at that point the user could choose to replay the song, or go back to pick another. Apple didn't like that. The reviewer wanted a mechanism to go directly back to the root view without waiting. Otherwise I was in violation of interface guidelines.

I did not argue. I gave them what they wanted. Touching the screen brings up a small button which will return to the animation picker.

I had been thinking that if the parent handed a child the iPhone, the child would navigate around like my own children do and not settle into being relaxed. However, it's true that they will probably hit the main button to kill the app if they are inclined, and I don't want the app to be thought of as children only. I find the app relaxing too; my brother has a great voice, and it's calming watching bubbles bounce around. And adults don't need or like boundaries in their iPhone apps. So Apple was right. It improved the app.

The second improvement came from the added time for testing the rejection gave me. Now that it was easier to go back to the root view, I actually tried it and found severe resource management problems. First, I found my animation views were not releasing. Then I was leaking animation objects every time a bubble disappeared. Then I found that animations in the root view were taking a huge amount of time while hidden, to the point where touches became non-responsive. A whole long series of problems when I just wanted to get the app out the door. But, you never get a second chance at a first impression. iPhone apps which are branded as junk rarely get their reputations back. If I put my name on a piece of software, it means I think it is a quality piece of software.

I spent most of my debugging time in the Object Allocations and CPU Sampler Instruments figuring out why my little app was misbehaving. Developers, remember that the Leaks Instrument will not find a Leak if an object is attached to the run loop, as an animation is likely to be, instead look for classes of allocations that keep having net growth over time.

That one little button Apple requested blossomed into a series of improvements, cleanups, and learning experiences. And in the end, I have a much more responsive app, and code which I'll be able to reuse in future projects.

So, thank you anonymous Apple reviewer. You did me a huge favor.

Thursday, February 05, 2009

Writing a Lullaby App for the iPhone

[Update: Lullabies is now in the app store. Check it out.]

I have 2 young children, and the familiarity and dexterity they show in using their parent's iPhones continues to amaze me. They can unlock the phone, find the YouTube app, and press on a video to play. And they are only 3 and 2 years old.

I have a brother, James Howes, who is an amazing singer, trying to make a name for himself singing opera in the Midwest. I would love to do something to advance his career.

One night, I came up with the idea of an app to help make babies sleepy, a lullaby app. Restful animations would play while Jim sang something sleep inducing. Originally, a friend of mine was going to do the animations, but he was too busy and I ended up doing the animations using CALayers and Core Animation, which was a first for me.

So here are some ideas about using CALayers on the iPhone. The first thing I did was open up Photoshop Elements and create a 320x480 document. I like Elements because it has simple vectored tools, its Save For Web... function creates excellent PNG files, and most importantly it is a layered editing program. Thus I can mock up my animation before I ever write a line of code to see what parts I need. So for a toy clock, I can combine, a background, a pendulum, a spinning moon dial, a spinning sun, and some foreground artwork. These are all exported from Elements as PNG files. PNG is the proper choice as it has decent compression while still having a transparency, and full color.

To form a toy clock of the kind one might see strapped to a crib.

If I add a layer of shadow—easily created with Element's color to transparent gradient tool—I am able to animate its opacity over the course of the animation ending with a night time scene. I like this gloaming effect better than simply making the whole scene uniformly dim.

Now that I had my parts together, I loaded each into a CALayer as so:

UIImage* moonsImage = [UIImage imageNamed:kMoonsName];
self.moonsLayer = [CALayer layer];
self.moonsLayer.contents = (id)moonsImage.CGImage; = kMoonsName;
self.moonsLayer.opaque = NO;
[self addSublayer:moonsLayer];
self.moonsLayer.zPosition = 2;
self.moonsLayer.contentsGravity = kCAGravityResizeAspectFill;
[self.moonsLayer addAnimation:[self spinningAnimation] forKey:@"transform"];

Note, I made use of the zPosition to make sure my parts are drawn in the right order, and that I have a method that creates a CABasicAnimation spinningAnimation. I made a variety of CABasicAnimations to rotate the moon layer, spin the sun and swing the pendulum. You might have noticed that the center of the sun is the center of the moon dial is the center of the clock as a whole is the rotation point of the pendulum. This is not a coincidence; it makes figuring things out a whole lot easier. The pendulum graphic I used extends from the rotation point to the tip of the pendulum, although most of it is transparent.

I also made animations of snow falling, and bubbles bouncing. The hard part was simulating a bubble's iridescence (rainbow effect) in the Core Animation on the iPhone, believe me it would have been easier if Apple provided support for a proper masking layer as they do on the desktop.

It was darn hard getting my brother's audio tracks. Singing lullabies is not part of his normal repertoire—Rossini is more his speed—and he had no easily available recording facilities. The first tracks he sent me sounded like they were sung in an oak closet (better known as my cousin's kitchen). But after a very long time, he delivered the tracks I wanted, and I think they sound pretty good:
Down in the Valley. Note that any imperfections are caused by my amateurish editing skills; Jim is a singing machine.

Thankfully, Apple brought out the AVAudioPlayer class in the 2.2 SDK. It makes playing music much easier than the previous method of messing with AudioQueues. So actually playing the music was falling off a log easy.

So that's that. It was great learning about Core Animation, and I've ended up making good use of the skills in a per hour job this last month. And I hope to make more children apps in the future. I just hope this one makes a little money along the way.