Wednesday, November 11, 2009

Carmichael orrery

It occurs to me that it ought to be possible to demonstrate pseudoprimality with a purely mechanical device.

For instance, we could demonstrate 2^560%561 with 560 2:1 (or equivalently, 35 65536:1) gear amplifications followed by a 1:561 reduction, and rotate the input shaft once to see the output gear rest at 1. Of course, 2^560 ~ 3.8E168, so you'd have to turn the crank fairly hard.

You could also do a single 65536:1:561 train cranked n times for individual 65536n (mod 561) reductions, and repeat 35 iterations for 65536^35%561.

That would provide a (false) Fermat witness of 2. Simile for the other integers < 561. Not very interesting so far, but perhaps there's something more elegant.

Thursday, November 05, 2009

Math and music

If you represent the notes in the musical scale as the numbers 0 to 11 (0=A, 1=A#, 2=B, etc.), then the wheel of fifths is merely the multiples of 7 modulo 12. Likewise, the wheel of fourths is 5 mod 12. For some reason, the internets appear to be silent on this relationship.

Monday, November 02, 2009

Capturing sound in Linux using a USB headset (Plantronics DA40)

I picked up a Plantronics DA40 USB headset today and figured out how to capture .wav files to disk and play them back through the headset.

There are lots of other options, but I like this one because it's USB and I could use it with any Linux system, whereas if I used the native souncard, I'd probably have to do more per-machine troubleshooting. dmesg showed that it was detected okay:

[14563.672694] input: Plantronics DA40 Adapter as /devices/pci0000:00/0000:00:1d.3/usb4/4-2/4-2:1.3/input/input6
[14563.728138] input,hidraw3: USB HID v1.00 Device [Plantronics DA40 Adapter] on usb-0000:00:1d.3-2
[14563.909787] usbcore: registered new interface driver snd-usb-audio

Usually the mic comes muted by default, so I pulled up alsamixer to check. It came up with my native sound card first. So I ran "alsamixer -c 1" to try it with the second card, and that was indeed the DA40. Hitting TAB let met toggle between record and playback settings.

In the playback tab, there's a Mic option. You want to mute that ("m"), since that just loops the mic back through the speakers. On the Capture tab, there's a "Mic" field, and that's the one that actually sets the mic gain.

To record, I ran:

arecord -r 44100 -f S16_LE -D hw:1,0 foo.wav

(Hit control-c to end capture.) hw:1,0 let me record from card 1, the second sound card. It refused to record at its default setting of 8000Hz, so the 44100 and S16_LE were necessary. Then I was able to play back foo.wav with:

mplayer -ao alsa:device=hw=1.0 foo.wav

So far so good. Next, I wanted to know how much deviation I was getting (was the gain turned up enough?) so I downloaded a nifty, very old program called Wavesurfer, available as a statically linked binary:

http://www.speech.kth.se/wavesurfer/

I selected a "waveform" panel and loaded up foo.wav, and could see that I was only getting around +-500 units max (out of +-32767). In File... Preferences... Sound I/O, I set the Input and Output device to /dev/dsp1 (which I believe is the OSS version of the second sound card). Now I can record and playback from wavesurfer. I used alsamixer to tweak the Mic setting (remember to use the Record tab!) until I got +-10000 or so on my recordings. Anything over +-1000 would probably have been fine.

(BTW, wavesurfer will also show you nifty spectrograms, from which I learned a lot about the human voice.)

Here's a trivial script to start arecord recording in the background, and another script to kill it when you've had enough.

$more start-recording.sh
#!/bin/bash

arecord -r 44100 -f S16_LE -D hw:1,0 /tmp/recording-in-progress.wav &
ARECORD_PID="$!"
echo Recording. Wrote arecord pid of $ARECORD_PID to /tmp/arecord.pid
echo $ARECORD_PID > /tmp/arecord.pid

$more stop-recording.sh
#!/bin/bash

kill "`cat /tmp/arecord.pid`"

Tuesday, October 27, 2009

Machine Monastery

I've been reading 365 Tomorrows, and I like the idea of flash fiction. Here's a story that popped into my head while deep cleaning my carpets:

Cynthia was reluctant when it came time to leave the Machine Monastery. Nobody had predicted that machines would be Buddhists. Crazed killers, perhaps. Indifferent to humanity, perhaps. Cold calculators, almost certainly.

She had learned the tactile pleasures of sanding the walnut sides of an imperfect jewelry box she had made herself with hand tools. The visual pleasure of brushing a finish with a wet edge.

The empty contentedness of sweeping a floor. The ragged exhaustion of breaking out old concrete sidewalks with a sledgehammer and hauling them to a skip. The gleam of a toilet scrubbed clean.

The machines had done all these things, mostly better than humans could, and had found the same peace from their lessons. Cynthia would go back to her life in the city, where her finance skills would pay the bills, and where machine and human craftsmen continue to do their jobs with the labor-saving tools that made mass production cheap. But perhaps in the summer she would take another vacation to the mountains east of town, away from the noise, and rejuvenate with the joys of manual labor.

Tuesday, October 13, 2009

Ubuntu 8.04 (Hardy LTS) on HP EliteBook 8530w

Trying to boot the laptop with my 8.04 live CD, it hangs with a message "BUG: soft lockup - CPU#0 stuck for 11s". Hit F6 at the ubuntu boot screen and edit the boot params: remove "splash" and "quiet", and add "acpi=off".

Friday, September 25, 2009

Shopbot joinery

I tried my first joinery on the shopbot today. It's a concept for a sideboard to foot joint for a bed. I recessed the post 1/8" deeper than the plywood to give it a sense of depth when assembled, but I'm still deciding whether I like the effect. Maybe with a very gentle roundover on the post edges.

Here are my Sketchup, PartWorks and Shopbot partfiles. The partfiles are written for a 1/4" bit.

The raised sections of the post need a little rounding off at the tip to accommodate the 1/4" radius in the mating part; I should probably fix that in the sketchup model before I do any more of those. Also, in partworks, I relieved the plywood path by 0.030" so it had enough clearance to fit. I like how the fit came out with that amount of clearance.



Friday, September 04, 2009

squid reverse proxy (aka website accelerator) on ubuntu hardy

We had a bunch of machines all hitting the same URLs on www.example.com, so we put up a squid reverse proxy on ourcache.example.com so that http://www.example.com/foo/bar would get served and cached by http://ourcache.example.com/foo/bar

sudo apt-get install squid squid-cgi # squid-cgi enables the cache manager web interface

Edit /etc/squid/squid.conf. It's very well documented, and we only had to modify a few lines from the default ubuntu hardy config:

Allow other local machines to use our cache:

acl our_networks src 10.42.42.0/24
http_access allow our_networks

instead of the default of:

http_access allow localhost

Have the cache listen on port 80 and forward all requests to www.example.com:

http_port 80 defaultsite=www.example.com
cache_peer www.example.com parent 80 0 no-query originserver

In our case, we wanted to cache pages that included "GET parameters" in the URL, such as http://www.example.com/search?query=foo (which is something you should only do in special cases):

# enable logging of the full URL, so you can see what's going on (though it's a potential privacy risk to your users)
strip_query_terms off

Comment out the lines that exclude cgi-bin and GET parameter URLs from being cached:

#acl QUERY urlpath_regex cgi-bin \?
#cache deny QUERY

Then we went to: http://localhost/cgi-bin/cachemgr.cgi to see how well our cache was working (blank login and password by default).

After doing an "/etc/init.d/squid restart", we found that we could hit http://ourcache.example.com/foo/bar.html and get http://www.example.com/foo/bar.html, as expected.