libmp3splt
|
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-2011 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 00039 #include <string.h> 00040 00041 #include "splt.h" 00042 00043 static void splt_t_set_default_state_values(splt_state *state, int *error) 00044 { 00045 state->split.tags = NULL; 00046 splt_tu_reset_tags(splt_tu_get_tags_like_x(state)); 00047 state->split.points = NULL; 00048 state->fname_to_split = NULL; 00049 state->path_of_split = NULL; 00050 state->m3u_filename = NULL; 00051 state->input_fname_regex = NULL; 00052 state->default_comment_tag = NULL; 00053 state->default_genre_tag = NULL; 00054 state->silence_log_fname = NULL; 00055 state->split.real_tagsnumber = 0; 00056 state->split.real_splitnumber = 0; 00057 state->split.splitnumber = 0; 00058 state->split.current_split_file_number = 1; 00059 state->split.get_silence_level = NULL; 00060 state->split.put_message = NULL; 00061 state->split.file_split = NULL; 00062 state->split.p_bar->progress_text_max_char = 40; 00063 snprintf(state->split.p_bar->filename_shorted,512, "%s",""); 00064 state->split.p_bar->percent_progress = 0; 00065 state->split.p_bar->current_split = 0; 00066 state->split.p_bar->max_splits = 0; 00067 state->split.p_bar->progress_type = SPLT_PROGRESS_PREPARE; 00068 state->split.p_bar->silence_found_tracks = 0; 00069 state->split.p_bar->silence_db_level = 0; 00070 state->split.p_bar->user_data = 0; 00071 state->split.p_bar->progress = NULL; 00072 state->cancel_split = SPLT_FALSE; 00073 00074 splt_w_set_wrap_default_values(state); 00075 splt_se_set_sync_errors_default_values(state); 00076 if (splt_of_set_default_values(state) < 0) { return; } 00077 splt_e_set_errors_default_values(state); 00078 splt_fu_set_default_values(state); 00079 splt_o_set_options_default_values(state); 00080 splt_o_set_ioptions_default_values(state); 00081 splt_p_set_default_values(state); 00082 } 00083 00084 splt_state *splt_t_new_state(splt_state *state, int *error) 00085 { 00086 if ((state =malloc(sizeof(splt_state))) ==NULL) 00087 { 00088 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY; 00089 return NULL; 00090 } 00091 00092 memset(state, 0x0, sizeof(splt_state)); 00093 if ((state->wrap = malloc(sizeof(splt_wrap))) == NULL) 00094 { 00095 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY; 00096 free(state); 00097 return NULL; 00098 } 00099 memset(state->wrap, 0x0, sizeof(state->wrap)); 00100 00101 if ((state->serrors = malloc(sizeof(splt_syncerrors))) == NULL) 00102 { 00103 free(state->wrap); 00104 free(state); 00105 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY; 00106 return NULL; 00107 } 00108 memset(state->serrors, 0x0, sizeof(state->serrors)); 00109 00110 if ((state->split.p_bar = malloc(sizeof(splt_progress))) == NULL) 00111 { 00112 free(state->wrap); 00113 free(state->serrors); 00114 free(state); 00115 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY; 00116 return NULL; 00117 } 00118 00119 if ((state->plug = malloc(sizeof(splt_plugins))) == NULL) 00120 { 00121 free(state->wrap); 00122 free(state->serrors); 00123 free(state->split.p_bar); 00124 free(state); 00125 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY; 00126 return NULL; 00127 } 00128 00129 state->current_plugin = -1; 00130 00131 splt_t_set_default_state_values(state, error); 00132 00133 return state; 00134 } 00135 00136 static void splt_t_free_state_struct(splt_state *state) 00137 { 00138 if (state) 00139 { 00140 if (state->fname_to_split) 00141 { 00142 free(state->fname_to_split); 00143 state->fname_to_split = NULL; 00144 } 00145 if (state->path_of_split) 00146 { 00147 free(state->path_of_split); 00148 state->path_of_split = NULL; 00149 } 00150 if (state->m3u_filename) 00151 { 00152 free(state->m3u_filename); 00153 state->m3u_filename = NULL; 00154 } 00155 if (state->input_fname_regex) 00156 { 00157 free(state->input_fname_regex); 00158 state->input_fname_regex = NULL; 00159 } 00160 if (state->default_comment_tag) 00161 { 00162 free(state->default_comment_tag); 00163 state->default_comment_tag = NULL; 00164 } 00165 if (state->default_genre_tag) 00166 { 00167 free(state->default_genre_tag); 00168 state->default_genre_tag = NULL; 00169 } 00170 if (state->silence_log_fname) 00171 { 00172 free(state->silence_log_fname); 00173 state->silence_log_fname = NULL; 00174 } 00175 if (state->wrap) 00176 { 00177 free(state->wrap); 00178 state->wrap = NULL; 00179 } 00180 if (state->serrors) 00181 { 00182 free(state->serrors); 00183 state->serrors = NULL; 00184 } 00185 if (state->plug) 00186 { 00187 free(state->plug); 00188 state->plug = NULL; 00189 } 00190 00191 free(state); 00192 state = NULL; 00193 } 00194 } 00195 00196 void splt_t_free_state(splt_state *state) 00197 { 00198 if (state) 00199 { 00200 splt_tu_free_original_tags(state); 00201 splt_of_free_oformat(state); 00202 splt_w_wrap_free(state); 00203 splt_se_serrors_free(state); 00204 splt_fu_freedb_free_search(state); 00205 splt_t_free_splitpoints_tags(state); 00206 splt_o_iopts_free(state); 00207 splt_p_free_plugins(state); 00208 if (state->split.p_bar) 00209 { 00210 free(state->split.p_bar); 00211 state->split.p_bar = NULL; 00212 } 00213 splt_e_free_errors(state); 00214 splt_t_free_state_struct(state); 00215 } 00216 } 00217 00218 void splt_t_set_total_time(splt_state *state, long value) 00219 { 00220 splt_d_print_debug(state,"Setting total time to _%ld_\n", value); 00221 00222 if (value >= 0) 00223 { 00224 state->split.total_time = value; 00225 } 00226 else 00227 { 00228 splt_e_error(SPLT_IERROR_INT,__func__, value, NULL); 00229 } 00230 } 00231 00232 long splt_t_get_total_time(splt_state *state) 00233 { 00234 return state->split.total_time; 00235 } 00236 00237 double splt_t_get_total_time_as_double_secs(splt_state *state) 00238 { 00239 long total_time = splt_t_get_total_time(state); 00240 00241 double total = total_time / 100; 00242 total += ((total_time % 100) / 100.); 00243 00244 return total; 00245 } 00246 00247 void splt_t_set_new_filename_path(splt_state *state, 00248 const char *new_filename_path, int *error) 00249 { 00250 int err = SPLT_OK; 00251 00252 splt_internal *iopts = &state->iopts; 00253 err = splt_su_copy(new_filename_path, &iopts->new_filename_path); 00254 if (err < 0) { *error = err; } 00255 } 00256 00257 char *splt_t_get_new_filename_path(splt_state *state) 00258 { 00259 return state->iopts.new_filename_path; 00260 } 00261 00262 int splt_t_set_path_of_split(splt_state *state, const char *path_of_split) 00263 { 00264 splt_d_print_debug(state,"Setting path of split to _%s_\n", path_of_split); 00265 int err = splt_su_copy(path_of_split, &state->path_of_split); 00266 00267 if (state->path_of_split == NULL) 00268 { 00269 return err; 00270 } 00271 00272 #ifdef __WIN32__ 00273 if (state->path_of_split[strlen(state->path_of_split)-1] == SPLT_DIRCHAR) 00274 { 00275 if (! splt_w32_str_is_drive_root_directory(state->path_of_split)) 00276 { 00277 splt_su_str_cut_last_char(state->path_of_split); 00278 } 00279 } 00280 #endif 00281 00282 return err; 00283 } 00284 00285 char *splt_t_get_path_of_split(splt_state *state) 00286 { 00287 return state->path_of_split; 00288 } 00289 00290 int splt_t_set_m3u_filename(splt_state *state, const char *filename) 00291 { 00292 splt_d_print_debug(state,"Setting m3u filename to _%s_\n", filename); 00293 return splt_su_copy(filename, &state->m3u_filename); 00294 } 00295 00296 char *splt_t_get_m3u_filename(splt_state *state) 00297 { 00298 return state->m3u_filename; 00299 } 00300 00301 int splt_t_set_input_filename_regex(splt_state *state, const char *regex) 00302 { 00303 splt_d_print_debug(state, "Setting input filename regex to _%s_\n", regex); 00304 return splt_su_copy(regex, &state->input_fname_regex); 00305 } 00306 00307 char *splt_t_get_input_filename_regex(splt_state *state) 00308 { 00309 return state->input_fname_regex; 00310 } 00311 00312 int splt_t_set_default_comment_tag(splt_state *state, const char *default_comment) 00313 { 00314 splt_d_print_debug(state,"Setting default comment tag to _%s_\n", default_comment); 00315 return splt_su_copy(default_comment, &state->default_comment_tag); 00316 } 00317 00318 int splt_t_set_default_genre_tag(splt_state *state, const char *default_genre) 00319 { 00320 splt_d_print_debug(state,"Setting default genre tag to _%s_\n", default_genre); 00321 return splt_su_copy(default_genre, &state->default_genre_tag); 00322 } 00323 00324 char *splt_t_get_default_comment_tag(splt_state *state) 00325 { 00326 return state->default_comment_tag; 00327 } 00328 00329 char *splt_t_get_default_genre_tag(splt_state *state) 00330 { 00331 return state->default_genre_tag; 00332 } 00333 00334 char *splt_t_get_m3u_file_with_path(splt_state *state, int *error) 00335 { 00336 char *m3u_file = splt_t_get_m3u_filename(state); 00337 return splt_su_get_file_with_output_path(state, m3u_file, error); 00338 } 00339 00340 int splt_t_set_silence_log_fname(splt_state *state, const char *filename) 00341 { 00342 splt_d_print_debug(state,"Setting silence log fname to _%s_\n", filename); 00343 return splt_su_copy(filename, &state->silence_log_fname); 00344 } 00345 00346 char *splt_t_get_silence_log_fname(splt_state *state) 00347 { 00348 return state->silence_log_fname; 00349 } 00350 00352 int splt_t_set_filename_to_split(splt_state *state, const char *filename) 00353 { 00354 splt_d_print_debug(state,"Setting filename to split to _%s_\n", filename); 00355 return splt_su_copy(filename, &state->fname_to_split); 00356 } 00357 00359 char * splt_t_get_filename_to_split(splt_state *state) 00360 { 00361 return state->fname_to_split; 00362 } 00363 00364 static void splt_t_set_current_split_file_number(splt_state *state, int index) 00365 { 00366 state->split.current_split_file_number = index; 00367 } 00368 00369 static void splt_t_set_current_split_file_number_next(splt_state *state) 00370 { 00371 splt_t_set_current_split_file_number(state, state->split.current_split_file_number+1); 00372 } 00373 00374 void splt_t_set_current_split(splt_state *state, int index) 00375 { 00376 if (index >= 0) 00377 { 00378 if (index == 0) 00379 { 00380 splt_t_set_current_split_file_number(state,1); 00381 } 00382 else 00383 { 00384 if (splt_sp_splitpoint_exists(state, index)) 00385 { 00386 int err = SPLT_OK; 00387 if (splt_sp_get_splitpoint_type(state, index, &err) != SPLT_SKIPPOINT) 00388 { 00389 splt_t_set_current_split_file_number_next(state); 00390 } 00391 } 00392 else 00393 { 00394 splt_t_set_current_split_file_number_next(state); 00395 } 00396 } 00397 00398 state->split.current_split = index; 00399 } 00400 else 00401 { 00402 splt_e_error(SPLT_IERROR_INT, __func__,index, NULL); 00403 } 00404 } 00405 00406 void splt_t_current_split_next(splt_state *state) 00407 { 00408 splt_t_set_current_split(state, splt_t_get_current_split(state) + 1); 00409 } 00410 00411 int splt_t_get_current_split(splt_state *state) 00412 { 00413 return state->split.current_split; 00414 } 00415 00416 int splt_t_get_current_split_file_number(splt_state *state) 00417 { 00418 return state->split.current_split_file_number; 00419 } 00420 00421 void splt_t_set_splitnumber(splt_state *state, int number) 00422 { 00423 if (number >= 0) 00424 { 00425 state->split.splitnumber = number; 00426 } 00427 else 00428 { 00429 splt_e_error(SPLT_IERROR_INT,__func__, number, NULL); 00430 } 00431 } 00432 00433 int splt_t_get_splitnumber(splt_state *state) 00434 { 00435 return state->split.splitnumber; 00436 } 00437 00438 void splt_t_free_splitpoints_tags(splt_state *state) 00439 { 00440 splt_sp_free_splitpoints(state); 00441 splt_tu_free_tags(state); 00442 } 00443 00444 void splt_t_clean_one_split_data(splt_state *state, int num) 00445 { 00446 if (splt_tu_tags_exists(state,num)) 00447 { 00448 splt_tu_set_tags_field(state,num, SPLT_TAGS_YEAR, NULL); 00449 splt_tu_set_tags_field(state,num, SPLT_TAGS_ARTIST, NULL); 00450 splt_tu_set_tags_field(state,num, SPLT_TAGS_ALBUM, NULL); 00451 splt_tu_set_tags_field(state,num, SPLT_TAGS_TITLE, NULL); 00452 splt_tu_set_tags_field(state,num, SPLT_TAGS_COMMENT, NULL); 00453 splt_tu_set_tags_field(state,num, SPLT_TAGS_PERFORMER, NULL); 00454 } 00455 00456 if (splt_sp_splitpoint_exists(state, num)) 00457 { 00458 splt_sp_set_splitpoint_name(state, num, NULL); 00459 } 00460 } 00461 00462 void splt_t_clean_split_data(splt_state *state,int tracks) 00463 { 00464 splt_t_set_current_split(state,0); 00465 do { 00466 splt_t_clean_one_split_data(state,state->split.current_split); 00467 splt_t_current_split_next(state); 00468 } while (splt_t_get_current_split(state) < tracks); 00469 } 00470 00471 int splt_t_split_is_canceled(splt_state *state) 00472 { 00473 return state->cancel_split; 00474 } 00475 00476 void splt_t_set_stop_split(splt_state *state, int bool_value) 00477 { 00478 state->cancel_split = bool_value; 00479 } 00480