1 ############################################################################
2 # Copyright (C) 2008 by RafaĆ Rzepecki #
3 # divided.mind@gmail.com #
5 # This program is free software; you can redistribute it and#or modify #
6 # it under the terms of the GNU General Public License as published by #
7 # the Free Software Foundation; either version 2 of the License, or #
8 # (at your option) any later version. #
10 # This program is distributed in the hope that it will be useful, #
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
13 # GNU General Public License for more details. #
15 # You should have received a copy of the GNU General Public License #
16 # along with this program; if not, write to the #
17 # Free Software Foundation, Inc., #
18 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
19 ############################################################################
25 MPEG_VERSION = [:v2_5, :reserved, :v2, :v1]
26 attr_accessor :length, :frames, :tags
27 attr_reader :filename, :slot
28 class FromDevice < AudioFile
31 @length, @frames, @tags, @filename, @slot, @encoding = args
38 def self.open(filename)
39 File.open(filename) { |f|
41 slot = filename.match("1([0-9a-f]{7})\.")[1].hex
42 tag_data = f.read(3072)
44 encoding, _, length, frames = f.read(10).unpack("CCNN")
46 tag_data[0..4] = ["ID3", 4, 0].pack("a3CC")
50 tags.each { |key, tag|
52 tags[key] = tag.recode(0)
54 begin # try harder to recode
56 tag["text"] = tag["text"][0...(tag["text"].length-1)]
57 tags[key] = tag.recode(0)
62 return FromDevice.new(length, frames, tags, filename, slot, encoding)
64 STDERR.puts("Corrupted file #{filename}")
69 def self.openAll(mount_point)
71 Dir[File.join([mount_point, 'omgaudio', '10f*'])].sort.each { |dir|
72 Dir[File.join([dir, "*.oma"])].sort.each do |filename|
73 af = AudioFile.open(filename)
74 files[af.slot-1] = af if af
87 File.open(filename, 'w+') {|outfile|
89 if tags # convert to ID3v2
90 tags.each { |key, value|
92 tag[key] = tag[key].recode(2) if tag[key]["text"]
96 tag[0...10] = ["ea3", 3, 0x1776].pack("a3c@8n")
97 outfile.sysseek(0, IO::SEEK_SET)
98 outfile.syswrite [tag].pack("a3072")
100 outfile.syswrite "EA3"
101 outfile.syswrite [2, 0, 0x60, 0xff, 0xfe, 1, 0x0f, 0x50, 0x00].pack("c5@9c4")
102 outfile.syswrite [0, 4, 0, 0, 0, 1, 2, 3, 0xc8, 0xd8, 0x36, 0xd8, 0x11, 0x22, 0x33, 0x44].pack("c*")
103 outfile.syswrite [0x03, 0x80, encoding, 10, length, frames, 0].pack("c4N3")
104 outfile.syswrite [0,0,0,0].pack("N4")
105 outfile.syswrite [0,0,0,0].pack("N4")
106 outfile.syswrite [0,0,0,0].pack("N4")
107 outfile.sysseek outfile.tell