Archive for the ‘ruby’ Category

RubyQuiz 136 : ID3 Tags

Tuesday, February 5th, 2008

A while back I decided to take on some of the problems from RubyQuiz.  The first one that I picked was the ID3 tags quiz.  My first attempt read in the entire mp3 to get just the last 128 bytes. 

It worked, but really rubbed me the wrong way.  I spent quite a bit of time trying to figure out how to seek to just the bit of  the file that I wanted to read in.  Unfortunately, I didn’t have much luck because I hadn’t used the more low level file IO functions.  In the end, I had to resort to the RubyQuiz solution to figure that out — but it was truly the last resort.  (So what exactly was my input…)

Here is the core of the code:

  # This is the heart of the code for reading the id3 tags.  id3 tags are stored
  #
in the last 128 bytes of the file.
  def read_id3
   
    # When writing this code, this is what caused me the most trouble. 
    #
I didn’t want to have to read in the entire mp3 to get the last 128 bytes.
    #
I knew what I was tring to accomplish, but just couldn’t nail down the .
    #  seek(offset,IO::SEEK_END) on my own.  I ended up finding
    # that code (more or less) on the ruby quiz page — not from lack of

    # searching though.
    offset = -128
    @mp3_file.seek(offset,IO::SEEK_END)
    unparsed_data = @mp3_file.read

   
    # This code is pretty straight forward, after you look at the info on how id3
    #  tags are stored.
    tag, @title, @artist, @album, @year, @comment, genre_index =
               unparsed_data.unpack(’A3A30A30A30A4A30C’)
    @year = @year.to_i
    @genre = @genres[genre_index]
   
  end

The rest of the code, genres file, test code, and a test mp3 are included in the attached zip file.  Do you use autotest?

ID3 Tags Project

Auto-launching my rails development environment

Tuesday, May 15th, 2007

My morning routine is pretty much always the same. My wife wakes me up, saying something about “late” that I can’t quite comprehend. I writhe about on the bed a bit trying to break free from the remaining tendrils of sleep. Finally, I look at my watch and spend a moment processing the numbers.

Shit. I have to go to work.

A 30 second shower later and I am in my car heading to the office. Luckily, Stephanie understands this process enough to get it started earlier enough for me to get to work on time. I roll into my office at the last possible moment though.

Okay. I have to get to work fast. Unlock my windows box, open my MacBook, launch Outlook on my windows box, launch iTerm on my MacBook. Have you seen the scene in office space where the main character is trying to get out of work before his boss asks him to work on the weekend? That is how I feel at about this point.

iTerm is finally up. Now, I just need to open 3 tabs and start my server, my rails console, and a tail of my development log.

Am I not a programmer? Can’t I make my Mac do all of this work for me? Of course I can.

listing for /usr/local/bin/rdev

#!/bin/zsh -f

if [[  $TERM_PROGRAM != iTerm.app ]]; then
    open -a Terminal
    return 0
fi

# First, get the directory for rails

if [[ $# == 0 ]]; then
      ThisDirectory=$PWD
elif [[ $# == 1 && -d "$1" ]]; then
      ThisDirectory=”$@”
else
      print “usage: rdev [directory]”
      return 1
fi

if [[ -e "$ThisDirectory/script/server" ]]; then
      print “Starting Rails Dev Environment”
else
      print “$ThisDirectory does not look like a rails app!”
      return 1
fi

osascript <<-eof
tell application “iTerm”
	make new terminal
	tell the front terminal
		activate current session
		launch session “Default Session”
		tell the last session
			write text “cd \”$ThisDirectory\”"
		end tell
		tell the last session
			write text “./script/server”
		end tell
	end tell
end tell
tell application “iTerm”
	make new terminal
	tell the front terminal
		activate current session
		launch session “Default Session”
		tell the last session
			write text “cd \”$ThisDirectory\”"
		end tell
		tell the last session
			write text “./script/console”
		end tell
	end tell
end tell
tell application “iTerm”
	make new terminal
	tell the front terminal
		activate current session
		launch session “Default Session”
		tell the last session
			write text “cd \”$ThisDirectory\”"
		end tell
		tell the last session
			write text “tail -f log/development.log”
		end tell
	end tell
end tell
tell application “iTerm”
	activate
end tell
eof

Now, when I get in I use Quicksilver to open an iTerm in my development directory. Then I simply type “rdev” and I am ready to roll.

This code is thanks largely to the scripts available at http://xanana.ucsc.edu/xtal/iterm_tab_customization.html