#!/usr/bin/ruby # # Copyright (C) 2007 - Mike Rowehl - mike@rowehl.com # # Takes a mail message sent by the Ultimate Voice Recorder app for Nokia S60 # devices and convert it into MP3 format. The assumed input format is amr. # The transcoded file is checked into a subversion repository. # # This work is placed in the public domain: # http://creativecommons.org/licenses/publicdomain/ # require 'pathname' require 'rubygems' require 'rmail' require 'base64' module CmdRunner def lsys( cmd ) system( cmd + " > /dev/null 2>&1" ) end end class VoiceRecorderEmail include CmdRunner attr_reader :filename, :body, :date, :time, :mp3 def initialize( text ) @msg = RMail::Parser.read( text ) @msg.each_part { |part| if part.header.include?( 'Content-Disposition' ) cd = part.header['Content-Disposition'] fileinfo = cd.match( /filename="([^"]+)"/ ) if fileinfo @filename = fileinfo[1] @body = part.body matches = @filename.match( /Memo_(........)_(......)/ ) @date = matches[1] @time = matches[2] end end } end def date_time @date + '_' + @time end def raw_amr Base64.decode64( @body ) end def convert_to_mp3 amr_converter = '/home/mike/src/amr/c-code/decoder' wav_tool = '/usr/bin/sox' mp3_tool = '/usr/local/bin/lame' amr_name = "/tmp/#{$$}_#{date_time}.amr" raw_name = "/tmp/#{$$}_#{date_time}.raw" wav_name = "/tmp/#{$$}_#{date_time}.wav" mp3_name = "/tmp/#{$$}_#{date_time}.mp3" decode_cmd = "#{amr_converter} #{amr_name} #{raw_name}" wav_cmd = "#{wav_tool} -r 8000 -w -c 1 -s #{raw_name} -w -c 1 #{wav_name}" mp3_cmd = "#{mp3_tool} #{wav_name} #{mp3_name} --silent" amr_file = File.new( amr_name, 'w+' ) amr_file.write( raw_amr ) amr_file.close # print decode_cmd + "\n" lsys( decode_cmd ) # print wav_cmd + "\n" lsys( wav_cmd ) # print mp3_cmd + "\n" lsys( mp3_cmd ) mp3_file = File.open( mp3_name ) @mp3 = mp3_file.read mp3_file.close File.unlink( amr_name, raw_name, wav_name, mp3_name ) true end end class SubversionSandbox include CmdRunner def initialize( date ) @sandbox_root = '/home/mike/rowehl' @relative_path = 'personal/audio' audio_path = File.join( @sandbox_root, @relative_path ); @date = date @dir = Pathname.new( File.join( audio_path, date ) ) end def add_recording( time, mp3 ) Dir.chdir( @sandbox_root ) day_path = File.join( @relative_path, @date ) day_dir = Pathname.new( day_path ) note_path = File.join( day_path, time + '.mp3' ) note_file = Pathname.new( note_path ) if ! day_dir.exist? day_dir.mkdir svn_dir_add = "svn add #{day_path}" # print svn_dir_add + "\n" lsys( svn_dir_add ) end if note_file.exist? return end file = File.new( note_path, 'w+' ) file.write( mp3 ) file.close svn_add = "svn add #{note_path}" # print svn_add + "\n" lsys( svn_add ) svn_ci = "svn ci -m \"Adding audio note #{@date} #{time}\"" # print svn_ci + "\n" lsys( svn_ci ) end end raw_email = $stdin.read() vre = VoiceRecorderEmail.new( raw_email ) date = vre.date time = vre.time # print "#{date} #{time}\n" if vre.convert_to_mp3 repos = SubversionSandbox.new( date ) repos.add_recording( time, vre.mp3 ) end