Sunday, July 12, 2009

Civic EX USB Audio Tools

A few months ago, I got myself a 2009 Civic EX, which I totally love. One of the cool features on it is a USB audio interface. You can plug in a USB stick full of music and have it play back. It works very well, but with a problem- it's pretty impossible to navigate 16GB of music with a one line display. I put together a little PHP script to print out the contents of my stick in the same order as is read by the Civic. It's a dead-simple script but might save someone a few minutes. I've tested it on Linux only, but it should work across platforms (but I believe it will require PHP5) I also came across a useful shell script that forces the directory ordering to be alphanumeric, which you can find at the bottom of this thread. Below is the code to print a directory listing. By default, it only prints folders. Two flags are supported: "-t" will print tracks, and "-n" will hide directory names. One last thing that might save you some hunting- according to the Civic manual, the system is capable of handling up to 700 folders and about 65,000+ tracks. I've only pushed it to 100 folders and about 1100 tracks so far.
#!/usr/bin/php
<?php

$media_types="mp3|m4a|wma";

// check options
$list_tracks=false;
$list_directories=true;

for ($i=1;$i<sizeof($argv)-1;$i++){
 if ($argv[$i][0]=='-') {
  if (false !== stripos($argv[$i],'t')) {
   $list_tracks=true; 
  }
  if (false !== stripos($argv[$i],'n')) {
   $list_directories=false; 
  }
 } else {
  die("Invalid option " + $argv[$i]);
 }
}



$path = realpath($argv[sizeof($argv)-1]);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

$di=0;
$ti=1;
$media_dir=false;
$working=false;
$media_pattern='/\.'.$media_types.'$/i';
$tr_pad = ($list_tracks && $list_directories) ? '  ' : '';
foreach ($objects as $name => $object) {
 if ($object->isFile()) {
  if ($working != $object->getPath()) {
   // start new dir
   if ($media_dir && $list_tracks) {
    echo "\n";
   }
   $media_dir=false;
   $working = $object->getPath();
   $ti=1;
  }

  
  if (!$media_dir) {
   if (preg_match($media_pattern,$name)) {
    $media_dir=true;
    $di++;
    if ($list_directories) {
     $el = str_replace($path.DIRECTORY_SEPARATOR,'',$object->getPath());
     echo substr(str_pad($di,2,'0',STR_PAD_LEFT),-2) . ' ' . str_replace(DIRECTORY_SEPARATOR,' :: ', $el) . "\n";
    }
   }
  }
  

  if ($list_tracks) {
   if (preg_match($media_pattern,$name)) {
    echo $tr_pad . substr(str_pad($di,2,'0',STR_PAD_LEFT),-3) . '-' . str_pad(($ti++),3,'0',STR_PAD_LEFT) . ' ' . $object->getBaseName() . "\n";
   }
  }

 }
}
Search Terms: civic directory order, civic music, USB Audio directory order

No comments:

Post a Comment