001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.sequencer.mp3;
025    
026    import java.io.File;
027    import java.io.FileOutputStream;
028    import java.io.InputStream;
029    import java.util.logging.Level;
030    import org.jaudiotagger.audio.AudioFile;
031    import org.jaudiotagger.audio.AudioFileIO;
032    import org.jaudiotagger.tag.Tag;
033    
034    /**
035     * Utility for extracting metadata from MP3 files.
036     * 
037     * @author Stefano Maestri
038     */
039    public class Mp3Metadata {
040    
041        private String title;
042        private String author;
043        private String album;
044        private String year;
045        private String comment;
046    
047        private Mp3Metadata() {
048    
049        }
050    
051        public static Mp3Metadata instance( InputStream stream ) {
052    
053            Mp3Metadata me = null;
054            File tmpFile = null;
055            try {
056                tmpFile = File.createTempFile("dna-sequencer-mp3", ".mp3");
057                FileOutputStream writer = new FileOutputStream(tmpFile);
058                byte[] b = new byte[128];
059                while (stream.read(b) != -1) {
060                    writer.write(b);
061                }
062                writer.close();
063                AudioFileIO.logger.getParent().setLevel(Level.OFF);
064                AudioFile f = AudioFileIO.read(tmpFile);
065                Tag tag = f.getTag();
066    
067                me = new Mp3Metadata();
068    
069                me.author = tag.getFirstArtist();
070                me.album = tag.getFirstAlbum();
071                me.title = tag.getFirstTitle();
072                me.comment = tag.getFirstComment();
073                me.year = tag.getFirstYear();
074    
075            } catch (Exception e) {
076                e.printStackTrace();
077            } finally {
078                if (tmpFile != null) {
079                    tmpFile.delete();
080                }
081            }
082            return me;
083    
084        }
085    
086        public String getTitle() {
087            return title;
088        }
089    
090        public String getAuthor() {
091            return author;
092        }
093    
094        public String getAlbum() {
095            return album;
096        }
097    
098        public String getYear() {
099            return year;
100        }
101    
102        public String getComment() {
103            return comment;
104        }
105    
106    }