22
juillet
2012
Lecture & écriture des tags mp3 en Vala (suite)(2)
juillet
2012
Un article de Rachel
Pas de commentaires
suite de l’écriture de la librairie des tags multimédia. maintenant il est possible de lire les mp3 et les ogg. Cependant, on ne peut plus les modifier pour l’instant. mais j’y travaille
La librairie :
using System;
namespace TagLib
{
public abstract class Frame
{
public Frame(Bytes bytes){}
public abstract Bytes Data {get;}
public abstract Bytes NextData {get;}
public abstract string Type {get;}
}
public abstract class Picture
{
public Picture(Bytes bytes){}
public abstract Bytes Buffer{get;}
public abstract string MimeType {get;}
public abstract Bytes Description {get;}
}
public class File
{
uint8[] data;
Bytes bytes;
string identifier;
TagLib.Tag tag;
File (string filename)
{
FileUtils.get_data(filename, out data);
bytes = new Bytes(data);
Encoding enc = new Encoding();
identifier = enc.GetString(bytes.slice(0,3));
}
public static File Create (string filename)
{
File file = new File(filename);
switch(file.identifier)
{
case "Ogg":
TagLibOgg.File f = new TagLibOgg.File(filename);
file.tag = f.Tag;
break;
case "ID3":
TagLibId3v2.File f = new TagLibId3v2.File(filename);
file.tag = f.Tag;
break;
}
return file;
}
public virtual TagLib.Tag Tag {get{return tag;}}
}
public class Tag
{
string title;
string encoder;
string genre;
string artist;
string album;
TagLib.Picture picture;
public Tag(List<Frame> frames){}
public virtual string Album {get{return album;} set{album = value;}}
public virtual string Artist {get{return artist;} set{artist = value;}}
public virtual string Genre {get{return genre;} set{genre = value;}}
public virtual string Encoder {get{return encoder;} set{encoder = value;}}
public virtual string Title {get{return title;} set{title = value;}}
public virtual TagLib.Picture Picture {get{return picture;} set{picture = value;}}
}
}
namespace TagLibOgg
{
public class File : TagLib.File
{
TagLib.Tag tag;
Bytes bytes;
List<TagLib.Frame> frames;
Encoding enc;
Frame frame;
public File(string filename)
{
base(filename);
uint8[] data;
FileUtils.get_data (filename,out data);
bytes = new Bytes(data);
enc = new Encoding();
string s = "(Schaufenugget)";
int index = Encoding.IndexOf(bytes,s)+s.length;
int not = (int)bytes[index];
frame = new Frame(bytes.slice(index+4,bytes.length-1));
frames = new List<TagLib.Frame>();
for(int i=0; i<not; i++){
frames.append(frame);
frame = new Frame(frame.NextData);
if(frame.Type == "COVERART")break;
}
tag = new TagLibOgg.Tag(frames);
}
public override TagLib.Tag Tag {get{return tag;}}
}
public class Tag : TagLib.Tag
{
string album;
string artist;
string genre;
string encoder;
string title;
public override string Album {get{return album;} set{album = value;}}
public override string Artist {get{return artist;} set{artist = value;}}
public override string Genre {get{return genre;} set{genre = value;}}
public override string Encoder {get{return encoder;} set{encoder = value;}}
public override string Title {get{return title;} set{title = value;}}
public Tag(List<TagLib.Frame> frames){
base(frames);
Encoding enc = new Encoding();
foreach(TagLib.Frame frame in frames){
switch(frame.Type)
{
case "TITLE":
title = enc.GetString(frame.Data);
break;
case "ARTIST":
artist = enc.GetString(frame.Data);
break;
case "ENCODER":
encoder = enc.GetString(frame.Data);
break;
case "GENRE":
genre = enc.GetString(frame.Data);
break;
case "ALBUM":
album = enc.GetString(frame.Data);
break;
}
}
}
}
public class Frame : TagLib.Frame
{
Encoding enc;
int size;
string type;
Bytes data;
Bytes next_data;
public Frame(Bytes bytes)
{
base(bytes);
enc = new Encoding();
size = (int)bytes[0]+256*(int)bytes[1]+256*256*(int)bytes[2]+256*256*256*(int)bytes[3];
data = bytes.slice(4,4+size);
int i = Encoding.IndexOf(data,"=");
type = enc.GetString(data.slice(0,i));
data = data.slice(i+1,data.length);
next_data = bytes.slice(4+size,bytes.length-1);
}
public override Bytes Data {get{return data;}}
public override Bytes NextData {get{return next_data;}}
public override string Type {get{return type;}}
}
}
namespace TagLibId3v2
{
public class File : TagLib.File
{
TagLib.Tag tag;
Bytes bytes;
List<TagLib.Frame> frames;
Encoding enc;
Frame frame;
public File(string filename)
{
base(filename);
uint8[] data;
FileUtils.get_data(filename,out data);
bytes = new Bytes(data);
enc = new Encoding();
frame = new Frame(bytes.slice(10,bytes.length-1));
frames = new List<TagLib.Frame>();
while(frame.Type!="APIC")
{
frames.append(frame);
frame = new Frame(frame.NextData);
}
frames.append(frame);
tag = new TagLibId3v2.Tag(frames);
}
public override TagLib.Tag Tag {get{return tag;}}
}
public class Tag : TagLib.Tag
{
string album;
string artist;
string genre;
string encoder;
string title;
TagLib.Picture picture;
public override string Album {get{return album;} set{album = value;}}
public override string Artist {get{return artist;} set{artist = value;}}
public override string Genre {get{return genre;} set{genre = value;}}
public override string Encoder {get{return encoder;} set{encoder = value;}}
public override string Title {get{return title;} set{title = value;}}
public override TagLib.Picture Picture {get{return picture;} set{picture = value;}}
public Tag(List<TagLib.Frame> frames)
{
base(frames);
Encoding enc = new Encoding();
foreach(TagLib.Frame frame in frames)
{
switch(frame.Type){
case "TIT2":
title = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TENC":
encoder = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TCON":
genre = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TPE1":
artist = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TALB":
album = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "APIC":
picture = new TagLibId3v2.Picture(frame.Data);
break;
}
}
}
}
public class Frame : TagLib.Frame
{
Encoding enc;
string type;
int size;
Bytes data;
Bytes next_data;
public Frame(Bytes bytes)
{
base(bytes);
enc = new Encoding();
type = enc.GetString(bytes.slice(0,4));
stdout.printf(type+"\n");
size = (int)bytes[7]+256*(int)bytes[6]+256*256*(int)bytes[5]+256*256*256*(int)bytes[4];
data = bytes.slice(10,10+size);
next_data = bytes.slice(10+size,bytes.length-1);
}
public override Bytes Data {get{return data;}}
public override Bytes NextData {get{return next_data;}}
public override string Type {get{return type;}}
}
public class Picture : TagLib.Picture
{
int index;
Bytes description;
Bytes buffer;
string mime_type;
public Picture(Bytes bytes)
{
base(bytes);
for(int i=0; i<bytes.length-4;i++)
{
if(bytes[i]==255&&bytes[i+1]==216&&bytes[i+2]==255&&bytes[i+3]==224)
{
mime_type = "image/jpeg";
description = bytes.slice(0,i-1);
buffer = bytes.slice(i,bytes.length-1);
break;
}
}
}
public override Bytes Description {get{return description;}}
public override Bytes Buffer {get{return buffer;}}
public override string MimeType {get{return mime_type;}}
}
}
namespace TagLib
{
public abstract class Frame
{
public Frame(Bytes bytes){}
public abstract Bytes Data {get;}
public abstract Bytes NextData {get;}
public abstract string Type {get;}
}
public abstract class Picture
{
public Picture(Bytes bytes){}
public abstract Bytes Buffer{get;}
public abstract string MimeType {get;}
public abstract Bytes Description {get;}
}
public class File
{
uint8[] data;
Bytes bytes;
string identifier;
TagLib.Tag tag;
File (string filename)
{
FileUtils.get_data(filename, out data);
bytes = new Bytes(data);
Encoding enc = new Encoding();
identifier = enc.GetString(bytes.slice(0,3));
}
public static File Create (string filename)
{
File file = new File(filename);
switch(file.identifier)
{
case "Ogg":
TagLibOgg.File f = new TagLibOgg.File(filename);
file.tag = f.Tag;
break;
case "ID3":
TagLibId3v2.File f = new TagLibId3v2.File(filename);
file.tag = f.Tag;
break;
}
return file;
}
public virtual TagLib.Tag Tag {get{return tag;}}
}
public class Tag
{
string title;
string encoder;
string genre;
string artist;
string album;
TagLib.Picture picture;
public Tag(List<Frame> frames){}
public virtual string Album {get{return album;} set{album = value;}}
public virtual string Artist {get{return artist;} set{artist = value;}}
public virtual string Genre {get{return genre;} set{genre = value;}}
public virtual string Encoder {get{return encoder;} set{encoder = value;}}
public virtual string Title {get{return title;} set{title = value;}}
public virtual TagLib.Picture Picture {get{return picture;} set{picture = value;}}
}
}
namespace TagLibOgg
{
public class File : TagLib.File
{
TagLib.Tag tag;
Bytes bytes;
List<TagLib.Frame> frames;
Encoding enc;
Frame frame;
public File(string filename)
{
base(filename);
uint8[] data;
FileUtils.get_data (filename,out data);
bytes = new Bytes(data);
enc = new Encoding();
string s = "(Schaufenugget)";
int index = Encoding.IndexOf(bytes,s)+s.length;
int not = (int)bytes[index];
frame = new Frame(bytes.slice(index+4,bytes.length-1));
frames = new List<TagLib.Frame>();
for(int i=0; i<not; i++){
frames.append(frame);
frame = new Frame(frame.NextData);
if(frame.Type == "COVERART")break;
}
tag = new TagLibOgg.Tag(frames);
}
public override TagLib.Tag Tag {get{return tag;}}
}
public class Tag : TagLib.Tag
{
string album;
string artist;
string genre;
string encoder;
string title;
public override string Album {get{return album;} set{album = value;}}
public override string Artist {get{return artist;} set{artist = value;}}
public override string Genre {get{return genre;} set{genre = value;}}
public override string Encoder {get{return encoder;} set{encoder = value;}}
public override string Title {get{return title;} set{title = value;}}
public Tag(List<TagLib.Frame> frames){
base(frames);
Encoding enc = new Encoding();
foreach(TagLib.Frame frame in frames){
switch(frame.Type)
{
case "TITLE":
title = enc.GetString(frame.Data);
break;
case "ARTIST":
artist = enc.GetString(frame.Data);
break;
case "ENCODER":
encoder = enc.GetString(frame.Data);
break;
case "GENRE":
genre = enc.GetString(frame.Data);
break;
case "ALBUM":
album = enc.GetString(frame.Data);
break;
}
}
}
}
public class Frame : TagLib.Frame
{
Encoding enc;
int size;
string type;
Bytes data;
Bytes next_data;
public Frame(Bytes bytes)
{
base(bytes);
enc = new Encoding();
size = (int)bytes[0]+256*(int)bytes[1]+256*256*(int)bytes[2]+256*256*256*(int)bytes[3];
data = bytes.slice(4,4+size);
int i = Encoding.IndexOf(data,"=");
type = enc.GetString(data.slice(0,i));
data = data.slice(i+1,data.length);
next_data = bytes.slice(4+size,bytes.length-1);
}
public override Bytes Data {get{return data;}}
public override Bytes NextData {get{return next_data;}}
public override string Type {get{return type;}}
}
}
namespace TagLibId3v2
{
public class File : TagLib.File
{
TagLib.Tag tag;
Bytes bytes;
List<TagLib.Frame> frames;
Encoding enc;
Frame frame;
public File(string filename)
{
base(filename);
uint8[] data;
FileUtils.get_data(filename,out data);
bytes = new Bytes(data);
enc = new Encoding();
frame = new Frame(bytes.slice(10,bytes.length-1));
frames = new List<TagLib.Frame>();
while(frame.Type!="APIC")
{
frames.append(frame);
frame = new Frame(frame.NextData);
}
frames.append(frame);
tag = new TagLibId3v2.Tag(frames);
}
public override TagLib.Tag Tag {get{return tag;}}
}
public class Tag : TagLib.Tag
{
string album;
string artist;
string genre;
string encoder;
string title;
TagLib.Picture picture;
public override string Album {get{return album;} set{album = value;}}
public override string Artist {get{return artist;} set{artist = value;}}
public override string Genre {get{return genre;} set{genre = value;}}
public override string Encoder {get{return encoder;} set{encoder = value;}}
public override string Title {get{return title;} set{title = value;}}
public override TagLib.Picture Picture {get{return picture;} set{picture = value;}}
public Tag(List<TagLib.Frame> frames)
{
base(frames);
Encoding enc = new Encoding();
foreach(TagLib.Frame frame in frames)
{
switch(frame.Type){
case "TIT2":
title = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TENC":
encoder = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TCON":
genre = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TPE1":
artist = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "TALB":
album = enc.GetString(frame.Data.slice(3,frame.Data.length));
break;
case "APIC":
picture = new TagLibId3v2.Picture(frame.Data);
break;
}
}
}
}
public class Frame : TagLib.Frame
{
Encoding enc;
string type;
int size;
Bytes data;
Bytes next_data;
public Frame(Bytes bytes)
{
base(bytes);
enc = new Encoding();
type = enc.GetString(bytes.slice(0,4));
stdout.printf(type+"\n");
size = (int)bytes[7]+256*(int)bytes[6]+256*256*(int)bytes[5]+256*256*256*(int)bytes[4];
data = bytes.slice(10,10+size);
next_data = bytes.slice(10+size,bytes.length-1);
}
public override Bytes Data {get{return data;}}
public override Bytes NextData {get{return next_data;}}
public override string Type {get{return type;}}
}
public class Picture : TagLib.Picture
{
int index;
Bytes description;
Bytes buffer;
string mime_type;
public Picture(Bytes bytes)
{
base(bytes);
for(int i=0; i<bytes.length-4;i++)
{
if(bytes[i]==255&&bytes[i+1]==216&&bytes[i+2]==255&&bytes[i+3]==224)
{
mime_type = "image/jpeg";
description = bytes.slice(0,i-1);
buffer = bytes.slice(i,bytes.length-1);
break;
}
}
}
public override Bytes Description {get{return description;}}
public override Bytes Buffer {get{return buffer;}}
public override string MimeType {get{return mime_type;}}
}
}
et les classes d’utilité
namespace System
{
public class Convert
{
public class ToInt32
{
public static int from_bytes(Bytes bytes)
{
if(bytes.length>4)return -1;
int a = 256*256*256*(int)bytes[0];
int b = 256*256*(int)bytes[1];
int c = 256*(int)bytes[2];
int d = (int)bytes[3];
int result = a+b+c+d;
return result;
}
public static int from_list_bytes(GLib.List<uint8> list)
{
if(list.length()>4)return -1;
int a = 256*256*256*(int)list.nth_data(0);
int b = 256*256*(int)list.nth_data(1);
int c = 256*(int)list.nth_data(2);
int d = (int)list.nth_data(3);
int result = a+b+c+d;
return result;
}
}
public class ToBytes
{
public static uint8[] from_integer(int number)
{
int a = number / (256*256*256);
number -= a*256*256*256;
int b = number / (256*256);
number -= b*256*256;
int c = number / 256;
number -= c*256;
uint8[] u = new uint8[4];
u[0] = (uint8)a;
u[1] = (uint8)b;
u[2] = (uint8)c;
u[3] = (uint8)number;
return u;
}
}
}
public class Encoding
{
public Encoding(){}
public string GetString(Bytes bytes)
{
string s = "";
foreach(uint8 u in bytes.get_data())
if(u!=0)s += ((char)u).to_string();
return s;
}
public Bytes GetBytes(string str)
{
uint8[] u = new uint8[str.length];
for(int i=0; i<str.length; i++)
u[i] = (uint8)str.get_char(i);
Bytes bytes = new Bytes(u);
return bytes;
}
public static int IndexOf(Bytes bytes, string str)
{
Encoding enc = new Encoding();
Bytes b = enc.GetBytes(str);
for(int i = 0; i < bytes.length-1-b.length; i++){
if(enc.GetString(bytes.slice(i,i+b.length))==str){
return i;
}
}
return -1;
}
}
}
{
public class Convert
{
public class ToInt32
{
public static int from_bytes(Bytes bytes)
{
if(bytes.length>4)return -1;
int a = 256*256*256*(int)bytes[0];
int b = 256*256*(int)bytes[1];
int c = 256*(int)bytes[2];
int d = (int)bytes[3];
int result = a+b+c+d;
return result;
}
public static int from_list_bytes(GLib.List<uint8> list)
{
if(list.length()>4)return -1;
int a = 256*256*256*(int)list.nth_data(0);
int b = 256*256*(int)list.nth_data(1);
int c = 256*(int)list.nth_data(2);
int d = (int)list.nth_data(3);
int result = a+b+c+d;
return result;
}
}
public class ToBytes
{
public static uint8[] from_integer(int number)
{
int a = number / (256*256*256);
number -= a*256*256*256;
int b = number / (256*256);
number -= b*256*256;
int c = number / 256;
number -= c*256;
uint8[] u = new uint8[4];
u[0] = (uint8)a;
u[1] = (uint8)b;
u[2] = (uint8)c;
u[3] = (uint8)number;
return u;
}
}
}
public class Encoding
{
public Encoding(){}
public string GetString(Bytes bytes)
{
string s = "";
foreach(uint8 u in bytes.get_data())
if(u!=0)s += ((char)u).to_string();
return s;
}
public Bytes GetBytes(string str)
{
uint8[] u = new uint8[str.length];
for(int i=0; i<str.length; i++)
u[i] = (uint8)str.get_char(i);
Bytes bytes = new Bytes(u);
return bytes;
}
public static int IndexOf(Bytes bytes, string str)
{
Encoding enc = new Encoding();
Bytes b = enc.GetBytes(str);
for(int i = 0; i < bytes.length-1-b.length; i++){
if(enc.GetString(bytes.slice(i,i+b.length))==str){
return i;
}
}
return -1;
}
}
}