Wednesday, May 25, 2011

PlayList format and translation

Sandisk Sansa mp3 player may have playlists of mp3 files.  These files are stored in \PLAYLIST.
The format of playlist is actullay in UTF-16LE.  In order to translate it to an ASCII (UTF-8) format, we can use a command line tool in Unix/Linux (available in Cygwin for Windows too).

Here's an example how to convert a playlist to a text format:


bash-3.2$ iconv -f UTF-16LE -t UTF-8 playlist.pla
PLP PLAYLIST
VERSION 1.20


HARP, MUSIC\James Blunt\All The Lost Souls\10_-_i_can't_hear_the_music_-_all_the_lost_souls.mp3
HARP, MUSIC\James Blunt\All The Lost Souls\09_-_annie_-_all_the_lost_souls.mp3
HARP, MUSIC\James Blunt\All The Lost Souls\04_-_same_mistake_-_all_the_lost_souls.mp3
HARP, MUSIC\James Blunt\All The Lost Souls\01_-_1973_-_all_the_lost_souls.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd1\01_-_bohemian_rhapsody_-_queen_-_greatest_hits_cd1.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd1\05_-_bicycle_race_-_queen_-_greatest_hits_cd1.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd1\16_-_we_will_rock_you_-_queen_-_greatest_hits_cd1.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd1\17_-_we_are_the_champions_-_queen_-_greatest_hits_cd1.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd2\07_-_it's_a_hard_life_-_queen_-_greatest_hits_cd2.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd2\11_-_the_miracle_-_queen_-_greatest_hits_cd2.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd2\15_-_friends_will_be_friends_-_queen_-_greatest_hits_cd2.mp3
HARP, MUSIC\Queen\Queen - Greatest Hits Cd2\16_-_the_show_must_go_on_-_queen_-_greatest_hits_cd2.mp3
HARP, MUSIC\Eric Clapton\Unplugged\07_-_layla_-_unplugged.mp3
HARP, MUSIC\Rihanna\Good Girl Gone Bad\{1]03_-_don't_stop_the_music_-_good_girl_gone_bad.mp3
HARP, MUSIC\OneRepublic\Dreaming Out Loud\{1]03_-_stop_and_stare_-_dreaming_out_loud.mp3
HARP, MUSIC\Colbie Caillat\Coco\{1]07_-_realize_-_coco.mp3
HARP, MUSIC\Timbaland\Shock Value\{1]16_-_apologize_(feat._one_republic)_-_shock_value.mp3
HARP, MUSIC\Ennio Morricone\Kill Bill Vol.2\03_-_il_tramanto_-_kill_bill_vol.2.mp3
HARP, MUSIC\Charlie Feathers\Kill Bill Vol.2\04_-_cant_hardly_stand_it_-_kill_bill_vol.2.mp3
HARP, MUSIC\Lole Y Manuel\Kill Bill Vol.2\05_-_tu_mira_(edit)_-_kill_bill_vol.2.mp3
HARP, MUSIC\Luis Bacalov\Kill Bill Vol.2\06_-_summertime_killer_-_kill_bill_vol.2.mp3
HARP, MUSIC\Alan Reeves Phil Steele And P\Kill Bill Vol.2\07_-_the_chase_-_kill_bill_vol.2.mp3
HARP, MUSIC\Ennio Morricone\Kill Bill Vol.2\09_-_l_arena_-_kill_bill_vol.2.mp3
HARP, MUSIC\Malcolm Mclaren\Kill Bill Vol.2\12_-_about_her_-_kill_bill_vol.2.mp3
HARP, MUSIC\Chingon\Kill Bill Vol.2\14_-_malaguena_salerosa_-_kill_bill_vol.2.mp3
HARP, MUSIC\Meiko Kaji\Kill Bill Vol.2\15_-_urami_bushi_-_kill_bill_vol.2.mp3
bash-3.2$


To create a playlist, we just reverse the format and redirect the output to a file (with extension pla) and then copy the generated file to Sansa's PLAYLIST folder.  Just to remember, the path of each file is relative to MUSIC folder.

Tuesday, May 24, 2011

Script to find latitude and longitude

The following script utilize Yahoo geocode API to find longitude and langitude of any location.


#!/bin/sh


converter="http://api.maps.yahoo.com/ajax/geocode?appid=onestep&qt=1&id=m&qs="


addr="$(echo $1 | sed 's/ /+/g')"
values="$(curl -s $converter$addr | cut -d\" -f13,15 |sed 's/[^0-9\.\,\-]//g; s/,$//')"


lat1=$(echo $values | cut -d, -f1)
long1=$(echo $values | cut -d, -f2)


echo "Lat=$lat1"
echo "Long=$long1"




(Save the above script to file and chmod to be executable).
For example:



>latlong 1465 mcdowell blvd, petaluma ca 94954
Lat=-33.869629
Long=151.206955

>latlong jakarta, indonesia
Lat=-6.17144
Long=106.82782

Tuesday, May 3, 2011

iMac v.s. AsusTek All-in-One PC

AsusTek All-in-One PC:
 
Price: $1,719.26 at 2020pc.com (free shipping + no tax)

General
Type Personal computer
Product Form Factor All-in-one
Built-in Devices Touch screen
Width 22.9 in
Depth 2 in
Height 19.5 in
Weight 28.7 lbs
Color Black
Bundled with 3D glasses
Processor
Type Intel Core i7 740QM / 1.73 GHz
Multi-Core Technology Quad-Core
64-bit Computing Yes
Installed Qty 1
Max Supported Qty 1
Mainboard
Chipset Type Mobile Intel HM55 Express
RAM
Installed Size 8 GB / 8 GB (max)
Technology DDR3 SDRAM
Memory Speed 1333 MHz
Form Factor SO DIMM 204-pin
Configuration Features 4 x 2 GB
Storage Controller
Type 1 x Serial ATA - integrated
Controller Interface Type Serial ATA-300
Storage
Hard Drive 1 x 1 TB - standard - Serial ATA-300 - 7200 rpm
Optical Storage
Type DVD-Writer / BD-ROM
Card Reader
Type Card reader
Supported Flash Memory Cards SD Memory Card, SDXC Memory Card
Monitor
Monitor Type LCD display - 3D Ready - TFT active matrix - Multi-Touch
Diagonal Size 23.6"
Max Resolution 1920 x 1080 ( Full HD )
Widescreen Display Yes
Image Aspect Ratio 16:9
Graphics Controller
Type Plug-in card
Graphics Processor / Vendor NVIDIA GeForce GTX 460M
Video Memory 1.5 GB
Digital Video Standard High-Definition Multimedia Interface (HDMI)
Multimedia Functionality
TV Tuner Type Digital TV
Digital TV Reception ATSC
Audio Output
Type Sound card
Sound Output Mode Stereo
Camera
Form Factor Integrated
Sensor Resolution 1.3 Megapixel
Input Device
Type Mouse, keyboard
Keyboard
Connectivity Wireless
Mouse
Connectivity Wireless
Audio Input
Type Microphone
Networking
Networking Network adapter
Wireless LAN Supported Yes
Data Link Protocol Ethernet, Fast Ethernet, Gigabit Ethernet, IEEE 802.11b, IEEE 802.11g, IEEE 802.11n, Bluetooth 3.0
Compliant Standards IEEE 802.11b, IEEE 802.11g, IEEE 802.11n, Bluetooth 3.0
Expansion / Connectivity
Expansion Bays Total (Free) Internal - 3.5"
Expansion Slots Total (Free) 1 ( 0 ) x processor 4 ( 0 ) x memory - SO DIMM 204-pin
Interfaces 4 x Hi-Speed USB - 4 pin USB Type A 1 x network - Ethernet 10Base-T/100Base-TX/1000Base-T - RJ-45 2 x SuperSpeed USB - 9 pin USB Type A 1 x microphone - input - mini-phone 3.5 mm 1 x headphones - output - mini-phone stereo 3.5 mm 1 x display / video - VGA input - 15 pin HD D-Sub (HD-15) 1 x audio / video - HDMI - 19 pin HDMI Type A 1 x display / video - TV-in
Miscellaneous
Included Accessories Remote control
Features ASUS Super Hybrid Engine, ASUS SonicMaster
Power
Device Type Power adapter
Power Provided 230 Watt
Operating System / Software
OS Provided Microsoft Windows 7 Home Premium 64-bit Edition
Microsoft Office Preloaded Includes a pre-loaded image of select Microsoft Office 2010 suites. Purchase an Office 2010 Product Key Card or disc to activate preloaded software on this PC.
Environmental Standards
ENERGY STAR Qualified Yes
Manufacturer Warranty
Service & Support 1 year warranty
Service & Support Details Limited warranty - 1 year
Universal Product Identifiers
Brand ASUSTeK COMPUTER
Part Number ET2400XVT-B011E
GTIN 00610839324255

Advantage: USB-3

21.5-inch iMac

  • $1,899.00 at Apple Store/online (free shipping, but there's sales tax)
  • $100 discount for students

21.5-inch iMac

  • Ships: 1-3 business days
  • Part number: Z0M5
Configuration
  • 2.8GHz Quad-Core Intel Core i7
  • 8GB 1333MHz DDR3 SDRAM - 2x4GB
  • 1TB Serial ATA Drive
  • AMD Radeon HD 6770M 512MB GDDR5
  • Apple Magic Mouse
  • Apple Wireless Keyboard (English) & User's Guide
  • Accessory Kit
Advantage: Thunderbolt connection (10 Gbps)

At the end, almost-apple-to-apple comparison (for students) is: 1799+167.11% (CA sales tax) - 1719.25 = 1966.11 - about $250 difference (that's more than the cost for 2 TB USB-3.0 external Hard Drive )

Saturday, March 26, 2011

Binary-tree test

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>

/* 
 Helper function that allocates a new node with the given data and NULL left and right pointers. 
*/ 


typedef struct node {
 int data;
 struct node *left;
 struct node *right;
} NodeT;


NodeT* NewNode(int data);
void BTreeLog(const char *fmt, ...);

typedef int (*BTreeCompFunc)(NodeT* node, void *data);

NodeT* BTreeNewNode(int data) 
{ 
 NodeT* node = (NodeT*)malloc(sizeof(NodeT));    // "new" is like "malloc" 
 node->data = data; 
 node->left = NULL; 
 node->right = NULL;
 BTreeLog("BTreeNewNode: new node=0x%X, data=%d\n", node, data);
    return(node); 
} 
     

void BTreeLog(const char *fmt, ...)
{
#ifdef __ENABLE_LOG__  
 char buf[256];
 va_list ap;
 int n;
 
 va_start(ap, fmt);
 n = vsnprintf(buf, sizeof(buf), fmt, ap);
 printf("%s", buf);
 va_end(ap);
#endif 
}


int BTreeCompare(NodeT* node, void *data)
{
 if (*((int*)data) < node->data)
  return -1;
 if (*((int*)data) > node->data)
  return 1;
 return 0;
}  
/* 
  Give a binary search tree and a number, BTreeInserts a new node 
  with the given number in the correct place in the tree. 
  Returns the new root pointer which the caller should 
  then use (the standard trick to avoid using reference 
  parameters). 
*/ 

NodeT* BTreeInsert(NodeT* node, int data, BTreeCompFunc comp)
{ 
 // 1. If the tree is empty, return a new, single node 
 if (NULL == node) 
 { 
  BTreeLog("New Entry: %d\n", data);
  return BTreeNewNode(data); 
    } 
 else { 
  // 2. Otherwise, recur down the tree 
     if (comp(node, &data) == -1) {
   /*BTreeLog("BTreeInsert: left; data=%d, node=0x%X, left=0x%X, right=0x%X\n", 
       data, node, node->left, node->right);
   */    
   node->left = BTreeInsert(node->left, data, comp); 
  } 
  else {
   /* 
   BTreeLog("BTreeInsert: right; data=%d, node=0x%X, left=0x%X, right=0x%X\n", 
       data, node, node->left, node->right);
   */    
   node->right = BTreeInsert(node->right, data, comp);
  } 
 }
 return(node); // return the (unchanged) node pointer 
}


void BTreeDeleteAll(NodeT* pNode)
{
 if (pNode)
 {
  if (pNode->left)
  {
   BTreeDeleteAll(pNode->left);
  } 
  if (pNode->right)
  {
   BTreeDeleteAll(pNode->right);
  } 
  BTreeLog("BTreeDeleteAll: pNode=0x%X, data=%d\n", pNode, pNode->data);
  free(pNode);
  //pNode->left = NULL;
  //pNode->right = NULL;
 }
}

NodeT *BTreeSearch(NodeT* pNode, int data, BTreeCompFunc comp)
{
 int cmp; 
 if (pNode) {
  cmp = comp(pNode, &data); 
  if (cmp == 0)
   return pNode;
  else 
  if (cmp == -1)
  {
   /* data < pnode->data */
   pNode = BTreeSearch(pNode->left, data, comp); 
  }
  else {
   pNode = BTreeSearch(pNode->right, data, comp);
  } 
 }
 //BTreeLog("Comp...\n");
 return pNode;
}


int main(int argc, char *argv[])
{
 NodeT* root = NULL;
 NodeT* node;
 unsigned int n,i;
 int data1, data2;
 unsigned int count;

 srand(time(NULL));
 BTreeLog("BTree demo\n");
 n = rand() % 100;
 root = BTreeInsert(root, n, BTreeCompare);
 data2 = 4750;
 n = 10000000;
 for (i=0, count=0; i<n; i++)
 {
  data1 = rand()%n;
  /* insert unique entry */
  if (BTreeSearch(root, data1, BTreeCompare) == NULL) {
   BTreeInsert(root, data1, BTreeCompare);
   count++;
  } 
 }
 BTreeInsert(root, data2, BTreeCompare);
 printf("Just inserted %d entries into Binary-tree\n", count+1);
 node = BTreeSearch(root, data2, BTreeCompare);
 printf("\n\n\n\n");
 if (node) {
  printf("!!!!!!!!!!!!!!!!!!!!!!!!Found node=0x%X for data=%d\n", node, data2);
 }
 printf("\n\n\n\n");
 BTreeDeleteAll(root);
}

Wednesday, March 23, 2011

Linux Init

LINUX INIT
--------------
This is the mother of all other userspace's processes.

Where to find it?
In the util-linux package (to be found at ftp.*.kernel.org) for simpleinit or in the sysvinit package for SysV init.

Probably also in [sunsite|metalab].unc.edu or ftp.debian.org


Title: sysvinit and utilities
Version: 2.78
Entered-Date: 11FEB2000
Description: This is the Linux System V init.
                The source package has the debian build files included.
                This version can be compiled with glibc 2.0.6 and up.
Author: miquels@cistron.nl (Miquel van Smoorenburg)
Primary-Site: sunsite.unc.edu /pub/Linux/system/daemons/init
                109K sysvinit-2.78.tar.gz
Alternate-Site: ftp.cistron.nl /pub/people/miquels/software
                109K sysvinit-2.78.tar.gz
Alternate-Site: ftp.debian.org /debian/dists/potato/main/source/base
                108K sysvinit_2.78-X.tar.gz
Copying-Policy: GPL
Keywords: init shutdown halt reboot
End


ftp.debian.org:/debian/dists/potato/main/source/base/sysvinit_2.78-2.tar.gz

https://build.opensuse.org/package/files?package=sysvinit&project=YaST%3AWeb

Some properties:

#define VT_MASTER "/dev/tty0"         /* Virtual console master */
#define CONSOLE "/dev/console"     /* Logical system console */
#define SECURETTY "/etc/securetty"     /* List of root terminals */
#define SDALLOW "/etc/shutdown.allow" /* Users allowed to shutdown */
#define INITTAB "/etc/inittab"     /* Location of inittab */
#define INIT "/sbin/init"     /* Location of init itself. */
#define NOLOGIN "/etc/nologin"     /* Stop user logging in. */
#define FASTBOOT "/fastboot"         /* Enable fast boot. */
#define FORCEFSCK "/forcefsck"     /* Force fsck on boot */
#define SDPID "/var/run/shutdown.pid" /* PID of shutdown program */
#define SHELL "/bin/sh"         /* Default shell */
#define SULOGIN "/sbin/sulogin"     /* Sulogin */
#define INITSCRIPT "/etc/initscript"     /* Initscript. */
#define PWRSTAT_OLD "/etc/powerstatus"     /* COMPAT: SIGPWR reason (OK/BAD) */
#define PWRSTAT "/var/run/powerstatus" /* COMPAT: SIGPWR reason (OK/BAD) */

#if 0
#define INITLVL "/etc/initrunlvl"     /* COMPAT: New runlevel */
#define INITLVL2 "/var/log/initrunlvl" /* COMPAT: New runlevel */
                            /* Note: INITLVL2 definition needs INITLVL */
#define HALTSCRIPT1 "/etc/init.d/halt"     /* Called by "fast" shutdown */
#define HALTSCRIPT2 "/etc/rc.d/rc.0"     /* Called by "fast" shutdown */
#define REBOOTSCRIPT1 "/etc/init.d/reboot" /* Ditto. */
#define REBOOTSCRIPT2 "/etc/rc.d/rc.6" /* Ditto. */



- It exits if UID is != 0 (root)
- It exits if PID != 1 (the first process in Kernel's userspace)
- It check command-line args:
    "single", "-s"      : dfl_level is set to 'S'
    "-a", "auto"        : set environment "AUTOBOOT=YES"
    "-b", "emergency"   : emerg_shell = 1
    "-z"                : ignore -z xxxx
    any of [0-9],[sS]   : dfl_level is set accordingly to that level

- set default environment PATH to "/sbin:/usr/sbin:/bin:/usr/bin"
- say "@(#) init version 2.89  DATE 26-Mar-2010  miquels@cistron.nl booting" into syslog
- spawn/fork to emergency shell if emerg_shell == 1
- read inittab and configure setting based on the settings stored in INITTAB

init.c: main() ---> setproctitle()
    |
    |
    V
init_main() ----------------------------> read_inittab()
 |  |   |
 |  |   |
 |  |   V
 |  |   init_reboot(BMAGIC_SOFT)
 |  |
 |  |
 |  V
 | ioctl(f, KDSIGACCEPT, SIGWINCH): tell kernel SIGACCEPT
 |
 +---> set a bunch of signals' flags
 |
 +----> console_init(), open /dev/CONSOLE (or otherwise if specified in env "CONSOLE") for O_RDONLY
 |
 +----> console_stty() : Set terminal settings to reasonable defaults
 |
 +----> set default environment PATH to "/sbin:/usr/sbin:/bin:/usr/bin"
 |
 loop forever by doing this stuf:
    1) boot transitions
    2) Read from the init FIFO by calling check_init_fifo():
        2.1) try to create /dev/initctl if not present.
        2.2) If /dev/initctl is open, stat the file to see if it is still the _same_ inode
        2.3) try to open /dev/initctl
        2.4) Read data from the pipe, return on EINTR
        2.5) console_init
        2.6) process requests (e.g., runlevel change request, power fail request, set env)

    3) check the 'failing' flags
    4) process any signal:
        4.1) SIGPWR event/signal
        4.2) SIGINT
        4.3) SIGWINCH
        4.4) SIGALRM
        4.5) SIGCHLD
        4.6) SIGHUP
    5) See whether we need to start up (again)




BOOT TRANSITION FSM
--------------------



        '#' (SYSINIT) --+-----> '*' (BOOT) ------> (NORMAL)
                        |              ^
                        |              |
           newlevel=='S'|              | !did_boot && newlevel != 'S'
                        |              |
                        +-----> 'S' (INIT)
                                       ^
                                       |
                                       |
                                   start here