Hack44.Use Other Portable Audio Players


Hack 44. Use Other Portable Audio Players

Access other USB storage device audio players under Linux and automatically sync them when you plug them in.

It's easy to feel left out if you have a portable audio player that isn't an iPod. It seems that most of the development efforts on all platforms are geared toward iPod compatibility before any other devices. Since most vendors only ship Windows and Mac integration software, that leaves us Linux users to fend for ourselves. Luckily most non-iPod portable audio players can be used not only as a USB storage device, they can also have music files added to them, just by copying the tracks to a particular directory on the drive. In this hack, I cover how to access these devices and how to leverage their USB storage device compatibility with hotplug and rsync to automatically synchronize your files the moment you plug your device in.

To access your media player, plug it into a USB port in your computer. Most modern Linux distributions have all of the USB drivers you need to access USB storage devices, and most are configured to automatically set up new devices you might plug in. After the device is plugged in, check /var/log/messages for information about your device. Here's a section from my file when I plugged in my media player:

 Jul 23 15:29:17 moses kernel: usb 4-1: new high speed USB device using ehci_ hcd and address 9 Jul 23 15:29:17 moses kernel: scsi1 : SCSI emulation for USB Mass Storage  device  s  Jul 23 15:29:18 moses usb.agent[2175]:      usb-storage: already loaded Jul 23 15:29:22 moses kernel:   Vendor: HTC42603  Model: 0G7AT00 Rev:      0 0     Jul 23 15:29:22 moses kernel:   Type:   Direct-Access ANSI SCSI revision: 00 Jul 23 15:29:22 moses kernel: SCSI device sda: 58605120 512-byte hdwr  sectors (3  0006 MB)  Jul 23 15:29:22 moses kernel: SCSI device sda: 58605120 512-byte hdwr  sectors (3  0006 MB)  Jul 23 15:29:22 moses kernel:  /dev/scsi/host1/bus0/target0/lun0: p1  Jul 23 15:29:22 moses kernel: Attached scsi disk sda at scsi1, channel 0, id  0,  lun 0  Jul 23 15:29:23 moses scsi.agent[2214]: sd_mod:loaded sucessfully (for disk) 

As you can see, my computer detected the USB storage device and configured it as /dev/sda:

 Jul 23 15:29:22 moses kernel: SCSI device sda: 58605120 512-byte hdwr  sectors (3  006 MB) 

It then detected a single partition on the drive:

 Jul 23 15:29:22 moses kernel:  /dev/scsi/host1/bus0/target0/lun0: p1 

With many desktop-oriented Linux distributions, this drive would now pop up on my desktop as an icon that I can just click to access the drive. Otherwise, to access it, all I would need to do is mount it as root somewhere on my system:

 # mount /dev/sda1  /mnt/portable  

Most media players have a preconfigured directory to store MP3 files, so you can now add new files to the device through your file manager or the terminal. You could also use the rsync command to synchronize between a directory on your local system and the device. The particular directory you would use varies for each device. On my media player the directory is called Music, so if I had it mounted at /mnt/portable and wanted to synchronize between my local directory at ~/mp3 and it, I would type:

 $ rsync -av --size-only --delete  ~/mp3/ /mnt/portable/Music / 

Use the --delete option with rsync carefully. It will delete all of the files inside the Music directory that aren't on the local system. If you only want to synchronize files between the two, but don't want to delete anything, remove the --delete argument from the command.


Notice that I used the --size-only argument. Because we are synchronizing between a Linux filesystem and a FAT32 filesystem, I've noticed that often the timestamps don't always match up. The --size-only argument tells rsync to base whether a file should be copied by the file size only, not the file's timestamp as well. This also saves on copying the same exact files over just because the timestamps are different.

Once you are finished copying files to and from your media player, you can unmount it from the system and then unplug it.

 # umount  /mnt/portable  

Be sure to remove the device only after it is unmounted to ensure you don't corrupt the filesystem on the device.


2.33.1. Automatically Synchronize Your Media Device

You can also leverage hotplug and autofs to automatically synchronize your computer and your media player just by plugging it in. The steps are almost identical to the steps outlined in "Automatically Synchronize Your Camera and Computer" [Hack #11], so follow those steps, replacing device settings with those corresponding to your media player. Stop once you get to the section called "Make a Synchronization Script," since we are going to create a different customization script here.

First, edit the usb-storagehotplug script mentioned in [Hack #11] so that it calls a new script, /usr/local/bin/portable_sync. Here's a modified version of that hotplug script:

 #!/bin/sh  sleep 3  DEVICE=`grep "kernel: Attached scsi .*disk" /var/log/syslog | tail -n 1 |  cut -f 10 -d " "`  set > /tmp/settings  case "$PRODUCT" in  # Archos PMA430        e79/1106/0) ln -s /var/autofs/usb/$DEVICE /mnt/portable    echo -e '#!/bin/sh\nrm /mnt/portable' > $REMOVER    chmod a+x $REMOVER    /usr/local/bin/portable_sync &     ;; esac 

Be sure to change the USB product ID from e79/1106/0 to match your device. Now create /usr/local/bin/portable_sync:

 #!/bin/sh LOCAL_DIR="/mnt/audio/mp3/" PORTABLE_DIR="/mnt/portable/Music/" RSYNC_OPTIONS="-a -q --size-only --delete" # only sync if the remote directory is available if [ -x $PORTABLE_DIR ]; then rsync $RSYNC_OPTIONS $LOCAL_DIR $PORTABLE_DIR fi 

Change the values of LOCAL_DIR and PORTABLE_DIR to match your system. Also keep in mind that this will remove any files on the portable device that aren't on the system. If that isn't how you want to synchronize, then remove the --delete argument from the RSYNC_OPTIONS variable. Now when you plug in your device, it will automatically run this script. The first time you set it up, monitor the processes on the system to make sure that the rsync has finished before you unplug the device. You can also just type df and check to see if the device is still mounted, since autofs will automatically unmount it for you once the rsync is finished.

Since this script is set to run each time the device is plugged in, you might also want to do other things automatically. Just add the commands you want to run below the rsync command.





Linux Multimedia Hacks
Linux Multimedia Hacks
ISBN: 596100760
EAN: N/A
Year: 2005
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net