Mp3 addiction

I’ve recently developed a tool that helps in keeping your music library clean and organized.

image

It allows you to rename and reorganize all your music library in batch using your mp3 tags.

Of course the tool works best only if your mp3 files have tags complete with all the details such as artist, album, year, track number… 

The script is based on the mp3info gem; this gem can read information and manipulate mp3 tags.

My tool examines all mp3 files contained into base_source_dir folder, it moves them into the base_target_dir folder organizing them following the user defined structure according to dir method and rename files according to filename method. Here’s the code:

#!/usr/bin/env ruby

require 'rubygems'
require 'mp3info'
require 'titleize'
require 'fileutils'
require 'ruby-progressbar'


def base_source_dir
  '/Volumes/Shared/Music/Tmp'
end

def base_target_dir
  '/Volumes/Shared/Music/Temporary'
end

def invalid_character
  Regexp.new('[|/\\<>:"?*;,^]')
end

class Mp3
  attr_accessor :source_path, :artist, :album, :title, :year, :track_n, :disc_n

  def initialize(path)
    @source_path = path
    get_metadata
  end

  def self.all
    Dir.glob("#{base_source_dir}/**/*.mp3").map do |path|
      new(path)
    end
  end

  def filename
    "#{escape(artist)} - [#{year}] #{escape(album)} - #{leading_0(disc_n)}.#{leading_0(track_n)} - #{escape(title)}.mp3"
  end

  def dir
    "#{escape(artist)}/[#{year}] #{escape(album)}"
  end

  def target_dir
    "#{base_target_dir}/#{dir}"
  end

  def target_path
    "#{target_dir}/#{filename}"
  end

  private

  def get_metadata
    Mp3Info.open(source_path) do |mp3info|
      @artist  = mp3info.tag.artist
      @album   = mp3info.tag.album.titleize
      @title   = mp3info.tag.title.titleize
      @year    = mp3info.tag.year || mp3info.tag1.year || mp3info.tag2.TDRC
      @track_n = mp3info.tag.tracknum || 1
      @disc_n  = mp3info.tag2.disc_number || 1
    end
  end

  def escape(tag)
    tag.gsub(invalid_character, '_')
  end

  def leading_0(tag)
    sprintf '%02d', tag
  end
end

class Mp3Renamer
  attr_accessor :source_path, :target_path, :target_dir

  def initialize(mp3)
    @source_path = mp3.source_path
    @target_dir = mp3.target_dir
    @target_path = mp3.target_path
  end

  def perform
    prepare_folder
    FileUtils.mv(source_path, target_path)
  end

  private

  def prepare_folder
    FileUtils.mkdir_p target_dir
  end
end

mp3s = Mp3.all
bar = ProgressBar.create total: mp3s.count

FileUtils.mkdir_p base_target_dir
mp3s.each do |mp3|
  Mp3Renamer.new(mp3).perform
  bar.increment
end
puts "#{mp3s.count} mp3 processed!"

Here’s my organization method, enjoy your listening!

image

Arduino Yún with SOUND the supereasy way

At the Codemotion in Milano, I had a chat with Federico Vanzati from Officine Arduino, and he gave me the fantastic idea to try to use a supercheap USB Audio card with Arduino Yún to give to it full audio capabilities with zero effort.

And suddenly I was like…

image

I mean, it’s awesome! It could give me (and hopefully to you) infinite new possibilities! Sound with Arduino with no external libraries or crappy MP3 shields? How great could it be? Now that we have a great Wi-Fi support thanks to the Yún, this was the real missing feature!

I started with buying this sound card (http://www.manhattan-products.com/hi-speed-usb-3-d-sound-adapter)

Then, all I had to do was plug the sound card in, open an ssh session to the yun, and type:

opkg update
opkg install kmod-usb-audio
opkg install madplay

End of the story. Now you have sound support on the Arduino Yún. I only tried MP3 playback, I didn’t try to record some audio yet, but I’ll try soon!

To test it just copy an mp3 audio file (I named it ItCouldWork.mp3) in the root folder, or, even better, in the SD card, then upload this sketch

#include <Process.h>

const int buttonPin = 2;
const int ledPin =  13;

int buttonState = 0;
Process p;

void setup() {
  Bridge.begin();
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
  Serial.begin(57600);
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH);  
    p.runShellCommand("madplay /root/ItCouldWork.mp3");
    while(p.running());  
    Serial.println("it works!");
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
}

It’s based on the button example, so you have to connect a pushbutton to the digital Pin 2.

image

Now, connect the sound card to the speakers and… push the button!

Magic, isn’t it?

It doesn’t sound too bad, actually the sound quality is good! And the delay is very short! I mean, it’s just the first experiment, we can work on it!

I really hope this will amaze you as it amazed me… I’m superhappy!

Many thanks to my friend and coworker Massimo, that knows Linux better than I know myself.

Bye!

Jeko