Monday, March 23, 2009

iPod lap timer parser

The iPod Nano is a beautiful thing.  That is unless you want to do something custom.  So it is with the lap timer in it.  There is no obvious way of synchronising the timer logs with the computer.  Even if there is a way in ITunes (I’m pretty sure there isn’t), the point is moot, as I don’t use ITunes (instead I’m using the wonderful ml_ipod plugin for Winamp).  This is “suboptimal” if you want to, for example, use the lap timer to chart your jogging progress over the course of a year.  You want to crunch those stats in Excel or some such, you certainly aren’t going to copy them from the screen.

The information on the web is a little sparse on this matter, but there are a number of applications that will let you do this.  The one that caught my eye was http://wafflesoftware.net/ipodtimer/ which luckily provided source code (Mac OS X only – aren’t we feeling exclusive).  A relatively trivial exercise in parsing.  The timer format was loosely described in the TimerFormat.txt in the zipped source.  At least detailed enough for me to whip up an implementation in C#.  Hence this post.

The underlying format is a binary file.  Time to dust off .NET’s BinaryReader.  The below method ReadTimerEntries(string filepath) will read in an iPod timer file (on my Nano it’s /IPod_Control/device/timer) and produce a list containing the date of the recording, and the associated laps.  From there you have all the info you need.  Anyway, it works for me, on an iPod Nano (3rd gen).  I don’t imagine that the file structure changes much though.

  1. public partial class Form1 : Form 
  2.     public Form1() 
  3.     { 
  4.         InitializeComponent(); 
  5.     } 
  6.  
  7.     private void button1_Click(object sender, EventArgs e) 
  8.     { 
  9.         OpenFileDialog ofd = new OpenFileDialog(); 
  10.         ofd.FileName = Application.StartupPath; 
  11.         DialogResult dr =ofd.ShowDialog(); 
  12.         if (dr == DialogResult.OK) 
  13.         { 
  14.             List<TimeEntry> entries = ReadTimerEntries(ofd.FileName); 
  15.             foreach (TimeEntry entry in entries) 
  16.             { 
  17.                 System.Diagnostics.Debug.WriteLine(entry.EntryDate.ToString()); 
  18.                 foreach (TimeSpan ts in entry.Laps) 
  19.                 { 
  20.                     System.Diagnostics.Debug.WriteLine(ts.ToString()); 
  21.                 } 
  22.             } 
  23.         } 
  24.     } 
  25.  
  26.     public List<TimeEntry> ReadTimerEntries(string filePath) 
  27.     { 
  28.         List<TimeEntry> results = new List<TimeEntry>(); 
  29.         //open the file 
  30.         var stream = File.OpenRead(filePath); 
  31.         BinaryReader br = new BinaryReader(stream); 
  32.         br.ReadBytes(4); //start file 
  33.         while (!br.ReadBytes(4).SequenceEqual(new byte[] { 0x13, 0, 0, 0xC0 })) 
  34.         { 
  35.             //first 4 bytes are already consumed 
  36.             ReadRound(br, results); 
  37.         } 
  38.         return results; 
  39.     } 
  40.  
  41.  
  42.     private void ReadRound(BinaryReader br, List<TimeEntry> itemList) 
  43.     {             
  44.         br.ReadBytes((16 * 5));// guff 
  45.          
  46.         br.ReadBytes(8);// start date 
  47.         byte seconds = br.ReadByte(); 
  48.         byte minutes = br.ReadByte(); 
  49.         byte hour = br.ReadByte(); 
  50.         byte day = br.ReadByte(); 
  51.         byte month = br.ReadByte(); 
  52.         int year = br.ReadInt16(); 
  53.         br.ReadByte(); //unknown use 
  54.         TimeEntry entry = new TimeEntry(); 
  55.         entry.EntryDate = new DateTime(year, month, day, hour, minutes, seconds); 
  56.         itemList.Add(entry); 
  57.         br.ReadBytes(4); //end date 
  58.  
  59.         br.ReadBytes(4); //start laps 
  60.         while (!br.ReadBytes(4).SequenceEqual(new byte[] { 0x10, 0, 0, 0xC0  })) 
  61.         { 
  62.             br.ReadBytes(4); //start lap 
  63.             int lapMilliseconds = br.ReadInt32(); //4 bytes 
  64.             TimeSpan lapTime = TimeSpan.FromMilliseconds(lapMilliseconds); 
  65.             entry.Laps.Add(new TimeSpan(0, 0, 0, 0, lapMilliseconds)); 
  66.             br.ReadBytes(4); //end lap 
  67.         } 
  68.         br.ReadBytes(4); //end round 
  69.     } 
  70.  
  71.  
  72. public class TimeEntry 
  73.     public DateTime EntryDate; 
  74.     public List<TimeSpan> Laps = new List<TimeSpan>(); 

No comments: