DSF2FLAC
tagConversion.cpp
00001 /*
00002  * dsf2flac - http://code.google.com/p/dsf2flac/
00003  *
00004  * A file conversion tool for translating dsf dsd audio files into
00005  * flac pcm audio files.
00006  *
00007  * Copyright (c) 2013 by respective authors.
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  *
00024  * Acknowledgements
00025  *
00026  * Many thanks to the following authors and projects whose work has greatly
00027  * helped the development of this tool.
00028  *
00029  *
00030  * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)
00031  * SACD Ripper (http://code.google.com/p/sacd-ripper/)
00032  * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)
00033  * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)
00034  * Jesus R - www.sonore.us
00035  *
00036  */
00037 
00038 
00039 #include "tagConversion.h"
00040 #include "id3/misc_support.h"
00041 
00042 FLAC__StreamMetadata* id3v2_to_flac(ID3_Tag id3tags) {
00043         
00044         
00045         FLAC__StreamMetadata* flacTags;
00046         flacTags = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
00047         FLAC__StreamMetadata_VorbisComment_Entry entry;
00048         bool err = false;
00049         char* tag;
00050         
00051         // ARTIST
00052         tag = latin1_to_utf8 (ID3_GetArtist ( &id3tags ));
00053         if (tag != NULL)
00054         {
00055                 bool err1 = !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", tag);
00056                 if (!err1)
00057                         err |= !FLAC__metadata_object_vorbiscomment_append_comment(flacTags, entry, /*copy=*/false); // copy=false: let metadata object take control of entry's allocated string
00058                 err |= err1;
00059         }
00060         
00061         // ALBUM
00062         tag = latin1_to_utf8 (ID3_GetAlbum ( &id3tags ));
00063         if (tag != NULL)
00064         {
00065                 bool err1 = !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ALBUM", tag);
00066                 if (!err1)
00067                         err |= !FLAC__metadata_object_vorbiscomment_append_comment(flacTags, entry, /*copy=*/false); // copy=false: let metadata object take control of entry's allocated string
00068                 err |= err1;
00069         }
00070         
00071         // TITLE
00072         tag = latin1_to_utf8 (ID3_GetTitle ( &id3tags ));
00073         if (tag != NULL)
00074         {
00075                 bool err1 = !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", tag);
00076                 if (!err1)
00077                         err |= !FLAC__metadata_object_vorbiscomment_append_comment(flacTags, entry, /*copy=*/false); // copy=false: let metadata object take control of entry's allocated string
00078                 err |= err1;
00079         }
00080         
00081         // TRACK
00082         tag = latin1_to_utf8 (ID3_GetTrack ( &id3tags ));
00083         if (tag != NULL)
00084         {
00085                 bool err1 = !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TRACKNUMBER", tag);
00086                 if (!err1)
00087                         err |= !FLAC__metadata_object_vorbiscomment_append_comment(flacTags, entry, /*copy=*/false); // copy=false: let metadata object take control of entry's allocated string
00088                 err |= err1;
00089         }
00090         
00091         // YEAR
00092         tag = latin1_to_utf8 (ID3_GetYear ( &id3tags ));
00093         if (tag != NULL)
00094         {
00095                 bool err1 = !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "DATE", tag);
00096                 if (!err1)
00097                         err |= !FLAC__metadata_object_vorbiscomment_append_comment(flacTags, entry, /*copy=*/false); // copy=false: let metadata object take control of entry's allocated string
00098                 err |= err1;
00099         }
00100         
00101         
00102         if (err)
00103                 return NULL;
00104         
00105         return flacTags;
00106 }
00107 
00108 char* latin1_to_utf8(char* latin1) {
00109         return reinterpret_cast<char*>(latin1_to_utf8( reinterpret_cast<unsigned char*>(latin1)));
00110 }
00111 unsigned char* latin1_to_utf8(unsigned char* latin1)
00112 {
00113         if (latin1==NULL)
00114                 return NULL;
00115                 
00116         // count latin1
00117         int n=0;
00118         while (latin1[n])
00119                 n++;
00120         // make buffer for converted chars
00121         unsigned char* utf8 = new unsigned char[n*2+1];
00122         unsigned char* utf8_t = utf8;
00123 
00124         while (*latin1) {
00125                 if (*latin1<128)
00126                         *utf8++=*latin1++;
00127                 else
00128                         *utf8++=0xc2+(*latin1>0xbf), *utf8++=(*latin1++&0x3f)+0x80;
00129         }
00130         *utf8='\0';
00131         
00132         return utf8_t;
00133 }
 All Classes Files Functions Variables