/*
* ID3.java
*
* Created on 12 de Abril de 2006, 16:02
*
* The ID3 library allow you to read the ID3v1.1 tags from mp3 files.
*
* Author: Jorge Cardoso
* http://jorgecardoso.org
*/
package jorgecardoso.processing.id3;
import processing.core.*;
import java.io.*;
/**
* The ID3 library allow you to read the ID3v1.1 tags from mp3 files.
*
*
* Example:
*
*import jorgecardoso.processing.id3.*; * *void setup() { * ID3 id3 = new ID3(this); * * id3.readID3("alane.mp3"); * * println("Song: " + id3.songTitle); * println("Artist: " + id3.artist); * println("Album: " + id3.album); * println("Comment: " + id3.comment); * println("Year: " + id3.year); * println("Track:" + id3.track); * println("Genre: " + id3.genre); *} **@author Jorge Cardoso http://jorgecardoso.org */ public class ID3 { /** * The list of musical genres */ private static final String GENRES[] = {"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror", "Indie", "BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta", "Heavy Metal", "Black Metal", "Crossover", "Contemporary C", "Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop", "SynthPop"}; /** * The parent PApplet. Needed for access to the loadBytes method. **/ private PApplet parent; /** * The Song Title tag. */ public String songTitle; // 30 characters /** * The Artist name tag. */ public String artist; // 30 characters /** * The Algum name tag. */ public String album; // 30 characters /** * The Year tag. */ public String year; // 4 characters /** * The Comment tag. */ public String comment; // 28/30 characters /** * The Track tag (ID3v1.1). */ public byte track; /** * The Genre code tag. */ public byte genreCode; // 1 byte /** * The Genre name. */ public String genre; private StringBuffer sb; /** Creates a new instance of ID3 */ public ID3(PApplet parent) { this.parent = parent; } /** * Reads the ID3 tags from the specified file. * Tags are read into the tag fields. * * @param filename The name of the mp3 file to read. * The file can be in the 'data' directory or anywhere else * (if you use an absolute path). * @return True if ID3 tags present. False otherwise. */ public boolean readID3(String filename) { byte file[] = parent.loadBytes(filename); int pointer = file.length-128; byte b; b = file[pointer++]; if ( ((char)b) != 'T') { return false; } b = file[pointer++]; if ( ((char)b) != 'A') { return false; } b = file[pointer++]; if ( ((char)b) != 'G') { return false; } // read song title sb = new StringBuffer(); for (int i = 0; i < 30; i++) { b = file[pointer++]; if (b != 0) { sb.append((char)b); } } songTitle = sb.toString(); // read artist sb = new StringBuffer(); for (int i = 0; i < 30; i++) { b = file[pointer++]; if (b != 0) { sb.append((char)b); } } artist = sb.toString(); // read album sb = new StringBuffer(); for (int i = 0; i < 30; i++) { b = file[pointer++]; if (b != 0) { sb.append((char)b); } } album = sb.toString(); // read year sb = new StringBuffer(); for (int i = 0; i < 4; i++) { b = file[pointer++]; if (b != 0) { sb.append((char)b); } } year = sb.toString(); // read comment sb = new StringBuffer(); for (int i = 0; i < 28; i++) { b = file[pointer++]; if (b != 0) { sb.append((char)b); } } b = file[pointer++]; if (b == 0) { track = file[pointer++]; } else { sb.append((char)b); b = file[pointer++]; sb.append((char)b); } comment = sb.toString(); // read genre genreCode = file[pointer++]; if (genreCode > 0 && genreCode < GENRES.length-1) { genre = GENRES[genreCode]; } return true; } /** * Test method */ /* public static void main(String args[]) { ID3 id3 = new ID3(); if (id3.readID3(args[0])) { System.out.println("Song: " + id3.songTitle); System.out.println("Artist: " + id3.artist); System.out.println("Album: " + id3.album); System.out.println("Comment: " + id3.comment); System.out.println("Year: " + id3.year); System.out.println("Track:" + id3.track); System.out.println("Genre: " + id3.genre); } }*/ }