libmp3splt
src/split_points.c
Go to the documentation of this file.
00001 /**********************************************************
00002  *
00003  * libmp3splt -- library based on mp3splt,
00004  *               for mp3/ogg splitting without decoding
00005  *
00006  * Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
00007  * Copyright (c) 2005-2010 Alexandru Munteanu - io_fx@yahoo.fr
00008  *
00009  * http://mp3splt.sourceforge.net
00010  *
00011  *********************************************************/
00012 
00013 /**********************************************************
00014  *
00015  * This program is free software; you can redistribute it and/or
00016  * modify it under the terms of the GNU General Public License
00017  * as published by the Free Software Foundation; either version 2
00018  * of the License, or (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00028  * 02111-1307,
00029  * USA.
00030  *
00031  *********************************************************/
00032 
00038 #include <string.h>
00039 
00040 #include "splt.h"
00041 
00042 int splt_sp_splitpoint_exists(splt_state *state, int index)
00043 {
00044   if ((index >= 0) && (index < state->split.real_splitnumber))
00045   {
00046     return SPLT_TRUE;
00047   }
00048   else
00049   {
00050     return SPLT_FALSE;
00051   }
00052 }
00053 
00054 int splt_sp_append_splitpoint(splt_state *state, long split_value,
00055     const char *name, int type)
00056 {
00057   int error = SPLT_OK;
00058 
00059   splt_struct *split = &state->split;
00060 
00061   splt_d_print_debug(state,"Appending splitpoint _%s_ with value _%ld_\n",
00062       name, split_value);
00063 
00064   if (split_value >= 0)
00065   {
00066     split->real_splitnumber++;
00067 
00068     if (!split->points)
00069     {
00070       if ((split->points = malloc(sizeof(splt_point))) == NULL)
00071       {
00072         return SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00073       }
00074     }
00075     else
00076     {
00077       if ((split->points = realloc(split->points,
00078               split->real_splitnumber * sizeof(splt_point))) == NULL)
00079       {
00080         return SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00081       }
00082     }
00083 
00084     int index = split->real_splitnumber - 1;
00085     split->points[index].name = NULL;
00086 
00087     error = splt_sp_set_splitpoint_value(state, index, split_value);
00088     if (error != SPLT_OK) { return error; }
00089 
00090     error = splt_sp_set_splitpoint_name(state, index, name);
00091     if (error < 0) { return error; }
00092 
00093     splt_sp_set_splitpoint_type(state, index, type);
00094   }
00095   else
00096   {
00097     return SPLT_ERROR_NEGATIVE_SPLITPOINT;
00098   }
00099 
00100   return error;
00101 }
00102 
00103 splt_point *splt_sp_get_splitpoints(splt_state *state, int *splitpoints_number)
00104 {
00105   splt_struct *split = &state->split;
00106   *splitpoints_number = split->real_splitnumber;
00107   return split->points;
00108 }
00109 
00110 void splt_sp_free_splitpoints(splt_state *state)
00111 {
00112   splt_struct *split = &state->split;
00113 
00114   if (split->points)
00115   {
00116     int i = 0;
00117     for (i = 0; i < split->real_splitnumber; i++)
00118     {
00119       if (split->points[i].name)
00120       {
00121         free(split->points[i].name);
00122         split->points[i].name = NULL;
00123       }
00124     }
00125 
00126     free(split->points);
00127     split->points = NULL;
00128   }
00129 
00130   split->splitnumber = 0;
00131   split->real_splitnumber = 0;
00132 }
00133 
00134 int splt_sp_set_splitpoint_value(splt_state *state, int index, long split_value)
00135 {
00136   splt_d_print_debug(state,"Splitpoint at _%d_ is %ld_\n", index, split_value);
00137 
00138   int error = SPLT_OK;
00139 
00140   if ((index >= 0) &&
00141       (index < state->split.real_splitnumber))
00142   {
00143     state->split.points[index].value = split_value;
00144   }
00145   else
00146   {
00147     splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00148   }
00149 
00150   return error;
00151 }
00152 
00153 int splt_sp_set_splitpoint_name(splt_state *state, int index, const char *name)
00154 {
00155   splt_d_print_debug(state,"Splitpoint name at _%d_ is _%s_\n", index, name);
00156 
00157   int error = SPLT_OK;
00158   splt_point *points = state->split.points;
00159 
00160   if ((index >= 0) && (index < state->split.real_splitnumber))
00161   {
00162     error = splt_su_copy(name, &points[index].name);
00163   }
00164   else
00165   {
00166     splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00167   }
00168 
00169   return error;
00170 }
00171 
00172 int splt_sp_set_splitpoint_type(splt_state *state, int index, int type)
00173 {
00174   int error = SPLT_OK;
00175 
00176   if ((index >= 0) && (index < state->split.real_splitnumber))
00177   {
00178     state->split.points[index].type = type;
00179   }
00180   else
00181   {
00182     splt_e_error(SPLT_IERROR_INT, __func__, index, NULL);
00183   }
00184 
00185   return error;
00186 }
00187 
00188 long splt_sp_get_splitpoint_value(splt_state *state, int index, int *error)
00189 {
00190   if ((index >= 0) && (index < state->split.real_splitnumber))
00191   {
00192     return state->split.points[index].value;
00193   }
00194   else
00195   {
00196     splt_e_error(SPLT_IERROR_INT, __func__, index, NULL);
00197     return -1;
00198   }
00199 }
00200 
00201 const char *splt_sp_get_splitpoint_name(splt_state *state, int index, int *error)
00202 {
00203   if ((index >= 0) && (index < state->split.real_splitnumber))
00204   {
00205     return state->split.points[index].name;
00206   }
00207   else
00208   {
00209     //splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00210     return NULL;
00211   }
00212 }
00213 
00214 int splt_sp_get_splitpoint_type(splt_state *state, int index, int *error)
00215 {
00216   if ((index >= 0) && (index < state->split.real_splitnumber))
00217   {
00218     return state->split.points[index].type;
00219   }
00220   else
00221   {
00222     //splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00223     //wtf ?
00224     return 1;
00225   }
00226 }
00227 
00228 void splt_sp_get_mins_secs_hundr_from_splitpoint(long splitpoint,
00229     long *mins, long *secs, long *hundr)
00230 {
00231   if (hundr)
00232   {
00233     *hundr = splitpoint % 100;
00234   }
00235 
00236   splitpoint /= 100;
00237 
00238   if (secs)
00239   {
00240     *secs = splitpoint % 60;
00241   }
00242 
00243   if (mins)
00244   {
00245     *mins = splitpoint / 60;
00246   }
00247 }
00248 
00249 int splt_sp_cut_splitpoint_extension(splt_state *state, int index)
00250 {
00251   int error = SPLT_OK;
00252 
00253   if (splt_sp_splitpoint_exists(state,index))
00254   {
00255     const char *temp_name = splt_sp_get_splitpoint_name(state, index, &error);
00256     if (error < 0) { return error; }
00257 
00258     if (temp_name)
00259     {
00260       char *new_name = NULL;
00261       error = splt_su_copy(temp_name, &new_name);
00262       if (error < 0) { return error; }
00263 
00264       splt_su_cut_extension(new_name);
00265       error = splt_sp_set_splitpoint_name(state, index, new_name);
00266 
00267       free(new_name);
00268       new_name = NULL;
00269     }
00270   }
00271 
00272   return error;
00273 }
00274 
00275 int splt_sp_order_splitpoints(splt_state *state, int len)
00276 {
00277   //TODO: use quicksort ?
00278 
00279   int error = SPLT_OK;
00280 
00281   int i = 0, j = 1;
00282   float key;
00283   for (j = 1; j < len; j++)
00284   {
00285     key = splt_sp_get_splitpoint_value(state, j, &error);
00286 
00287     if (error < 0) { return error; }
00288 
00289     i = j - 1;
00290     while ((i >= 0) && 
00291         (splt_sp_get_splitpoint_value(state,i, &error) > key))
00292     {
00293       if (error < 0) { return error; }
00294 
00295       long temp = splt_sp_get_splitpoint_value(state, i, &error);
00296       if (error < 0) { return error; }
00297 
00298       splt_sp_set_splitpoint_value(state, i+1, temp);
00299       i--;
00300     }
00301 
00302     splt_sp_set_splitpoint_value(state, i+1, key);
00303   }
00304 
00305   return error;
00306 }
00307 
00308 long splt_sp_overlap_time(splt_state *state, int splitpoint_index)
00309 {
00310   int error = SPLT_OK;
00311   long split_value = splt_sp_get_splitpoint_value(state, splitpoint_index, &error);
00312   long overlap_time = splt_o_get_long_option(state, SPLT_OPT_OVERLAP_TIME);
00313   if ((overlap_time > 0) && (split_value != LONG_MAX))
00314   {
00315     long overlapped_split_value = split_value + overlap_time;
00316     long total_time = splt_t_get_total_time(state);
00317     if ((total_time > 0) && (overlapped_split_value > total_time))
00318     {
00319       overlapped_split_value = total_time;
00320     }
00321 
00322     splt_sp_set_splitpoint_value(state, splitpoint_index, overlapped_split_value);
00323 
00324     return overlapped_split_value;
00325   }
00326 
00327   return split_value;
00328 }
00329