libmp3splt
src/audacity.c
Go to the documentation of this file.
00001 /**********************************************************
00002  *
00003  * audacity.c -- Audacity label file parser portion of the Mp3Splt utility
00004  *                    Utility for mp3/ogg splitting without decoding
00005  *
00006  * Copyright (c) 2002-2004 M. Trotta - <matteo.trotta@lib.unimib.it>
00007  * Copyright (c) 2007 Federico Grau - <donfede@casagrau.org>
00008  * Copyright (c) 2010-2011 Alexandru Munteanu <io_fx@yahoo.fr>
00009  *
00010  * http://mp3splt.sourceforge.net
00011  * http://audacity.sourceforge.net/
00012  */
00013 
00014 /**********************************************************
00015  *
00016  * This program is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU General Public License
00018  * as published by the Free Software Foundation; either version 2
00019  * of the License, or (at your option) any later version.
00020  *
00021  * This program is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU General Public License for more details.
00025  *
00026  * You should have received a copy of the GNU General Public License
00027  * along with this program; if not, write to the Free Software
00028  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00029  * 02111-1307,
00030  * USA.
00031  *
00032  *********************************************************/
00033 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <errno.h>
00042 #include <string.h>
00043 #include <ctype.h>
00044 #include <math.h>
00045 
00046 #include "splt.h"
00047 
00048 
00049 static long splt_audacity_get_begin(splt_audacity *sa)
00050 {
00051   return sa->begin;
00052 }
00053 
00054 static long splt_audacity_get_end(splt_audacity *sa)
00055 {
00056   return sa->end;
00057 }
00058 
00059 static const char *splt_audacity_get_name(splt_audacity *sa)
00060 {
00061   return sa->name;
00062 }
00063 
00064 static int splt_audacity_append_splitpoints(splt_state *state,
00065     splt_audacity *previous_aud, splt_audacity *aud, int *append_begin_point)
00066 {
00067   int err = SPLT_OK;
00068 
00069   long previous_begin_point = -1;
00070   long previous_end_point = -1;
00071 
00072   if (previous_aud)
00073   {
00074     previous_begin_point = splt_audacity_get_begin(previous_aud);
00075     previous_end_point = splt_audacity_get_end(previous_aud);
00076   }
00077 
00078   long start_point = splt_audacity_get_begin(aud);
00079 
00080   if (previous_begin_point != -1 && previous_end_point != -1)
00081   {
00082     if (*append_begin_point)
00083     {
00084       err = splt_sp_append_splitpoint(state, previous_begin_point, 
00085           splt_audacity_get_name(previous_aud), SPLT_SPLITPOINT);
00086       if (err < 0) { return err; }
00087     }
00088 
00089     if (start_point == previous_end_point)
00090     {
00091       err = splt_sp_append_splitpoint(state, previous_end_point, 
00092           splt_audacity_get_name(aud), SPLT_SPLITPOINT);
00093       *append_begin_point = SPLT_FALSE;
00094       if (err < 0) { return err; }
00095     }
00096     else
00097     {
00098       err = splt_sp_append_splitpoint(state, previous_end_point, "skip", SPLT_SKIPPOINT);
00099       *append_begin_point = SPLT_TRUE;
00100       if (err < 0) { return err; }
00101     }
00102   }
00103 
00104   return err;
00105 }
00106 
00107 static splt_audacity *splt_audacity_new()
00108 {
00109   splt_audacity *sa = malloc(sizeof(splt_audacity));
00110   if (!sa)
00111   {
00112     return NULL;
00113   }
00114 
00115   sa->begin = -1;
00116   sa->end = -1;
00117   sa->name = NULL;
00118 
00119   return sa;
00120 }
00121 
00122 static void splt_audacity_free(splt_audacity **sa)
00123 {
00124   if (sa)
00125   {
00126     if (*sa)
00127     {
00128       splt_audacity *s = *sa;
00129 
00130       if (s->name)
00131       {
00132         free(s->name);
00133         s->name = NULL;
00134       }
00135 
00136       free(*sa);
00137       *sa = NULL;
00138     }
00139   }
00140 }
00141 
00142 static int splt_audacity_set_name(splt_audacity *sa, const char *name)
00143 {
00144   return splt_su_copy(name, &sa->name);
00145 }
00146 
00147 static void splt_audacity_set_begin(splt_audacity *sa, double begin)
00148 {
00149   sa->begin = (long) (floorf(begin) * 100.0);
00150 }
00151 
00152 static void splt_audacity_set_end(splt_audacity *sa, double end)
00153 {
00154   sa->end = (long) (floorf(end) * 100.0);
00155 }
00156 
00157 static splt_audacity *splt_audacity_process_line(splt_state *state, char *line,
00158     splt_audacity *previous_aud, int *append_begin_point, int *error)
00159 {
00160   splt_audacity *aud = splt_audacity_new();
00161   if (!aud)
00162   {
00163     *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00164     return NULL;
00165   }
00166 
00167   char *ptr = line;
00168 
00169   errno = 0;
00170   splt_audacity_set_begin(aud, strtod(ptr, &ptr));
00171   if (errno != 0) {
00172     *error = SPLT_INVALID_AUDACITY_FILE;
00173     goto error;
00174   }
00175 
00176   errno = 0;
00177   splt_audacity_set_end(aud, strtod(ptr, &ptr));
00178   if (errno != 0)
00179   {
00180     *error = SPLT_INVALID_AUDACITY_FILE;
00181     goto error;
00182   }
00183 
00184   ptr = splt_su_trim_spaces(ptr);
00185 
00186   int err = splt_audacity_set_name(aud, ptr);
00187   if (err < 0) { *error = err; goto error; }
00188 
00189   err = splt_audacity_append_splitpoints(state, previous_aud, aud, append_begin_point);
00190   if (err < 0) { *error = err; goto error; }
00191 
00192   return aud;
00193 
00194 error:
00195   splt_audacity_free(&aud);
00196   return NULL;
00197 }
00198 
00199 int splt_audacity_put_splitpoints(const char *file, splt_state *state, int *error)
00200 {
00201         int tracks = -1;
00202 
00203   if (file == NULL)
00204   {
00205     *error = SPLT_INVALID_AUDACITY_FILE;
00206     return tracks;
00207   }
00208 
00209   splt_t_free_splitpoints_tags(state);
00210 
00211   *error = SPLT_AUDACITY_OK;
00212 
00213   splt_c_put_info_message_to_client(state, 
00214       _(" reading informations from audacity labels file '%s' ...\n"), file);
00215 
00216         FILE *file_input = NULL;
00217 
00218         if (!(file_input = splt_io_fopen(file, "r")))
00219   {
00220     splt_e_set_strerror_msg_with_data(state, file);
00221     *error = SPLT_ERROR_CANNOT_OPEN_FILE;
00222     return tracks;
00223   }
00224 
00225         if (fseek(file_input, 0, SEEK_SET) != 0)
00226   {
00227     splt_e_set_strerror_msg_with_data(state, file);
00228     *error = SPLT_ERROR_SEEKING_FILE;
00229     goto end;
00230   }
00231 
00232   int append_begin_point = SPLT_TRUE;
00233   int err = SPLT_OK;
00234 
00235   splt_audacity *aud = NULL;
00236   splt_audacity *previous_aud = NULL;
00237 
00238   tracks = 0;
00239   char *line = NULL;
00240   while ((line = splt_io_readline(file_input, error)) != NULL)
00241   {
00242     if (*error < 0) { goto end; }
00243 
00244     if (splt_su_is_empty_line(line))
00245     {
00246       free(line);
00247       line = NULL;
00248       continue;
00249     }
00250 
00251     aud = splt_audacity_process_line(state, line, previous_aud,
00252         &append_begin_point, &err);
00253     if (err < 0) { goto end; }
00254 
00255     if (previous_aud)
00256     {
00257       splt_audacity_free(&previous_aud);
00258     }
00259     previous_aud = aud;
00260 
00261     free(line);
00262     line = NULL;
00263 
00264     tracks++;
00265         }
00266 
00267   if (previous_aud)
00268   {
00269     err = splt_audacity_append_splitpoints(state, previous_aud, aud, &append_begin_point);
00270     if (err < 0) { *error = err; }
00271   }
00272 
00273 end:
00274   if (line)
00275   {
00276     free(line);
00277     line = NULL;
00278   }
00279 
00280   if (previous_aud)
00281   {
00282     splt_audacity_free(&previous_aud);
00283   }
00284 
00285   if (fclose(file_input) != 0)
00286   {
00287     splt_e_set_strerror_msg_with_data(state, file);
00288     *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00289   }
00290   file_input = NULL;
00291 
00292         return tracks;
00293 }
00294