Logo Search packages:      
Sourcecode: xcfa version File versions

tags_mp3.c

 /*
 * file    : tags_mp3.c
 * project : xcfa
 * with    : Gtk-2
 *
 * xcfa - GTK+ implementation of the GNU shell command
 * GNU General Public License
 * 2003,2004,2005,2006,2007,2008,2009 Claude Bulin
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * OLD ADRESS:
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * NEW ADRESS:
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */



#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include "support.h"

#include <pthread.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "global.h"
#include "file.h"
#include "tags.h"
#include <taglib/tag_c.h>
#include "get.h"
#include "level.h"
#include "utils.h"


/*
*---------------------------------------------------------------------------
* VARIABLES
*---------------------------------------------------------------------------
*/

typedef struct  {
      gchar magic[ 3 ];
      gchar songname[ 30 ];
      gchar artist[ 30 ];
      gchar album[ 30 ];
      gchar year[ 4 ];
      gchar note[ 28 ];
      unsigned char     nnull;
      unsigned char     track;
      unsigned char     style;
} ID3TAG;

/*
*---------------------------------------------------------------------------
* FILE IS MP3 ?
*---------------------------------------------------------------------------
*/
gboolean tagsmp3_file_is_mp3 (gchar *namefile)
{
      FILE    *fp = NULL;
      gchar   *buf = NULL;
      ID3TAG   begin;
      ID3TAG   end;
      gboolean bool = FALSE;
      gint     i;
      size_t   size;

      /*PRINT_FUNC_LF();*/

      fp = fopen (namefile, "rb");
      if (!fp) return (FALSE);

      fread ( &begin, 128, 1, fp );
      fseek ( fp, 0, SEEK_END );
      fseek ( fp, ftell( fp ) - 128, SEEK_SET );
      fread ( &end, 128, 1, fp );
      fclose (fp);

      if (begin.magic[0] == 'I' && begin.magic[1] == 'D' && begin.magic[2] == '3') return (TRUE);
      if (end.magic[0] == 'T' && end.magic[1] == 'A' && end.magic[2] == 'G') return (TRUE);
      
      size = infosong_get_size_file (namefile);
      buf = g_malloc0 (sizeof (gchar) * (size + 10));
      if ((fp = fopen (namefile, "rb"))) {
            fread (buf, 1, size, fp);
            fclose (fp);
            for (i = 0; i < size; i ++) {
                  if (buf[i+0] == 'L' && buf[i+1] == 'A' && buf[i+2] == 'M' && buf[i+3] == 'E') {
                        bool = TRUE;
                        break;
                  }
            }
      }
      else {
            PRINT_FUNC_LF();
            g_print ("------ERREUR DE LECTURE DE: %s\n", namefile);
      }
      
      g_free (buf);
      buf = NULL;

      return (bool);
}

/*
*---------------------------------------------------------------------------
* GET HEADER
*---------------------------------------------------------------------------
*/
INFO_MP3 *tagsmp3_remove_info (INFO_MP3 *info)
{
      if (info) {
            g_free (info->namefile);
            g_free (info->bitrate);
            g_free (info->time);
            g_free (info->size);

            info->tags = (TAGS *)tags_remove (info->tags);

            g_free (info);
            info = NULL;
      }

      return ((INFO_MP3 *)NULL);
}
/*
MPEG-1   layer III sample frequencies (kHz):  32  48  44.1
bitrates (kbit/s): 32 40 48 56 64 80 96 112 128 160 192 224 256 320

MPEG-2   layer III sample frequencies (kHz):  16  24  22.05
bitrates (kbit/s):  8 16 24 32 40 48 56 64 80 96 112 128 144 160

MPEG-2.5 layer III sample frequencies (kHz):   8  12  11.025
bitrates (kbit/s):  8 16 24 32 40 48 56 64 80 96 112 128 144 160
-1
0
1
2
*/
gint tagsmp3_type_mpeg (gchar *namefile)
{
      gchar        *Lout = NULL;
      gchar        *ptr = NULL;
#define MAX_STR 256
      static gchar  str [ MAX_STR + 4];
      gint          cpt = 0;
      gchar        *term[] = {"MPV_1", "MPV_2", "MPV_25"};
      gint          i;
      GString      *gstr = NULL;
      gint          Ret = -1;

      gstr = get_with_checkmp3 (namefile);
      Lout = gstr->str;

      for (i = 0; i < 3; i ++) {
            if ((ptr = strstr (Lout, term[ i ]))) {
                  /* Init la chaine de stockage */
                  for (cpt=0; cpt < MAX_STR; cpt++) str [ cpt ] = '\0';
                  cpt = 0;

                  /* Passe l'intro */
                  while (*ptr != ' ') ptr ++;
                  while (*ptr == ' ') ptr ++;

                  /* Copie */
                  while (*ptr != '\n') {
                        if (cpt > MAX_STR) break;
                        str [ cpt ++ ] = *ptr ++;
                  }
                  str [ cpt ] = '\0';

                  switch ( i ) {
                  case 0 :
                        /*g_print ("----------------------------MPV_1  str=%s\n", str);*/
                        if (str[0] == '1') Ret = 0;
                        break;
                  case 1 :
                        /*g_print ("----------------------------MPV_2  str=%s\n", str);*/
                        if (str[0] == '1') Ret = 1;
                        break;
                  case 2 :
                        /*g_print ("----------------------------MPV_25 str=%s\n", str);*/
                        if (str[0] == '1') Ret = 2;
                        break;
                  }
                  if (Ret > -1) break;
            }
      }
      g_string_free (gstr, TRUE);
      Lout = NULL;

      return (Ret);
}

INFO_MP3 *tagsmp3_get_info (DETAIL *detail, gchar *namefile)
{
      INFO_MP3     *ptrinfo = NULL;
      TagLib_File  *file;
      TagLib_Tag   *tag;
      const TagLib_AudioProperties *properties;
      gint          seconds;
      gint          minutes;
      
      /* PRINT_FUNC_LF();*/
      
      ptrinfo = (INFO_MP3 *)g_malloc0 (sizeof (INFO_MP3));
      if (ptrinfo == NULL) return (NULL);
      ptrinfo->tags = (TAGS *)tags_alloc (FALSE);
      
      if ((file = taglib_file_new (namefile))) {
      
            taglib_set_strings_unicode(FALSE);
            tag = taglib_file_tag(file);
            properties = taglib_file_audioproperties(file);
            
            /*ptrinfo->tags->Title     = g_strdup (utils_convert_string (taglib_tag_title(tag), TRUE));*/
            ptrinfo->tags->Title     = utils_convert_string (taglib_tag_title(tag), TRUE);
            
            
            /*ptrinfo->tags->Artist    = g_strdup (utils_convert_string (taglib_tag_artist(tag), TRUE));*/
            ptrinfo->tags->Artist    = utils_convert_string (taglib_tag_artist(tag), TRUE);
            
            
            /*ptrinfo->tags->Album     = g_strdup (utils_convert_string (taglib_tag_album(tag), TRUE));*/
            ptrinfo->tags->Album     = utils_convert_string (taglib_tag_album(tag), TRUE);
            
            
            ptrinfo->tags->IntYear   = taglib_tag_year(tag);
            ptrinfo->tags->Year      = g_strdup_printf ("%d", ptrinfo->tags->IntYear);
            /*ptrinfo->tags->Comment   = g_strdup (utils_convert_string (taglib_tag_comment(tag), TRUE));*/
            ptrinfo->tags->Comment   = utils_convert_string (taglib_tag_comment(tag), TRUE);
            
            
            ptrinfo->tags->IntNumber = taglib_tag_track(tag);
            ptrinfo->tags->Number    = g_strdup_printf ("%d", ptrinfo->tags->IntNumber);
            /*ptrinfo->tags->Genre     = g_strdup (utils_convert_string (taglib_tag_genre(tag), TRUE));*/
            ptrinfo->tags->Genre     = utils_convert_string (taglib_tag_genre(tag), TRUE);
            
            
            ptrinfo->tags->IntGenre  = infosong_get_genre_by_value (ptrinfo->tags->Genre);

            seconds = taglib_audioproperties_length(properties) % 60;
            minutes = (taglib_audioproperties_length(properties) - seconds) / 60;
            ptrinfo->SecTime =  taglib_audioproperties_length(properties);

            if (detail != NULL) {
                  detail->ValuePlay.SecTime = ptrinfo->SecTime;
            }

            ptrinfo->ValueMark.SecTime    = taglib_audioproperties_length(properties);
            ptrinfo->ValueMark.ValueMarkA = 0.0;
            ptrinfo->ValueMark.ValueMarkB = 100.0;
            ptrinfo->ValueMark.Value = 0.0;

            ptrinfo->namefile = g_strdup (namefile);
            ptrinfo->time     = g_strdup_printf ("%02d:%02d", minutes, seconds);
            ptrinfo->bitrate  = g_strdup_printf ("%d", taglib_audioproperties_bitrate(properties));
            ptrinfo->mpeg_is  = tagsmp3_type_mpeg (namefile);
            ptrinfo->size     = g_strdup_printf ("%d Ko", (gint)infosong_get_size_file (namefile) / 1024);
                  
            taglib_tag_free_strings();
            taglib_file_free (file);
      }
      /*
      ptrinfo->level = level_get_from (FILE_IS_MP3, namefile);
g_print ("%s\t%d\n", namefile, ptrinfo->level);
      */
      ptrinfo->LevelDbfs.level = -1;
      ptrinfo->LevelDbfs.NewLevel = -1;

      return (ptrinfo);
}







Generated by  Doxygen 1.6.0   Back to index