1 /**********************************************************
2  *
3  * libmp3splt -- library based on mp3splt,
4  *               for mp3/ogg splitting without decoding
5  *
6  * Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
7  * Copyright (c) 2005-2014 Alexandru Munteanu - m@ioalex.net
8  *
9  * http://mp3splt.sourceforge.net
10  *
11  *********************************************************/
12 
13 /**********************************************************
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28  * USA.
29  *
30  *********************************************************/
31 
32 #ifndef MP3SPLT_SPLT_H
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 
37 //libtool 1.4e is buggy on mingw if we include ltdl.h
38 #ifndef __WIN32__
39 #include <ltdl.h>
40 #endif
41 
42 #include "mp3splt.h"
43 
44 struct _splt_freedb_one_result {
45   /**
46    * @brief Name of the album for this result
47    */
48   char *name;
49   /**
50    * @brief Unique identifier for this result
51    */
52   int id;
53   /**
54    * @brief How many revisions this result has
55    *
56    * If having revisions, the unique identifier for a revision is
57    * #id + revision + 1, with 0 <= revision < #revision_number\n
58    */
59   int revision_number;
60   /**
61    * @brief Contains the revisions of the result
62    *
63    * The #revisions table contains #revision_number elements,
64    * the consecutive numbers starting at 2 (you might not need it)
65    */
66   int *revisions;
67 };
68 
69 struct _splt_freedb_results {
70   /**
71    * @brief All the freedb results
72    */
73   splt_freedb_one_result *results;
74   /**
75    * @brief How many results we have
76    */
77   int number;
78   int iterator_counter;
79 };
80 
81 /**
82  * @brief Maximum results for the freedb search
83  */
84 #define SPLT_MAXCD 512
85 
86 //maximum length of the disc id
87 #define SPLT_DISCIDLEN 8
88 
89 //structure for the freedb search
90 struct splt_cd {
91   char discid[SPLT_DISCIDLEN+1];
92   char category[20];
93 };
94 
95 typedef struct {
96   struct splt_cd discs[SPLT_MAXCD];
97   int foundcd;
98 } splt_cd_state;
99 
100 //structure containing everything used for the
101 //freedb search
102 typedef struct {
103   //we stock here the results of the freedb search
104   struct _splt_freedb_results *search_results;
105   //we stock the state of the CD
106   //(for the freedb search)
107   splt_cd_state *cdstate;
108 } splt_freedb;
109 
110 typedef char _splt_one_wrap;
111 
112 struct _splt_wrap {
113   /**
114    * @brief How many filenames we have
115    */
116   int wrap_files_num;
117   /**
118    * @brief The filenames
119    */
120   char **wrap_files;
121   int iterator_counter;
122 };
123 
124 struct _splt_syncerrors {
125   off_t *serrors_points;
126   /**
127    * @brief How many syncerrors have been found
128    */
129   long int serrors_points_num;
130 };
131 
132 /**
133  * @brief The number of syncerrors
134  *
135  * @see mp3splt_get_syncerrors
136  */
137 typedef struct _splt_syncerrors splt_syncerrors;
138 
139 struct _splt_tags {
140   /**
141    * @brief The title
142    */
143   char *title;
144   /**
145    * @brief The artist
146    */
147   char *artist;
148   /**
149    * @brief The album
150    */
151   char *album;
152   /**
153    * @brief The performer
154    *
155    * The performer is not part of the tags, but may replace the
156    * #artist in some cases and it is useful for the output filenames\n
157    * You can also look at #SPLT_OPT_OUTPUT_DEFAULT
158    */
159   char *performer;
160   /**
161    * @brief The year
162    */
163   char *year;
164   /**
165    * @brief A comment
166    */
167   char *comment;
168   /**
169    * @brief The track number
170    */
171   int track;
172   /**
173    * @brief The genre
174    */
175   char *genre;
176 
177   /*
178    * @brief tags version (for mp3): 1 or 2 or 1 & 2
179    */
180   int tags_version;
181 
182   int set_original_tags;
183   int was_auto_incremented;
184 };
185 
186 struct _splt_original_tags {
187   splt_tags tags;
188   void *all_original_tags;
189   int last_plugin_used;
190 };
191 
192 struct _splt_tags_group {
193   splt_tags *tags;
194   int real_tagsnumber;
195   int iterator_counter;
196 };
197 
198 #define SPLT_MAXOLEN 255
199 #define SPLT_OUTNUM  20
200 
201 //structure defining the output format
202 typedef struct {
203   //format as @n_@t.. as a string
204   char *format_string;
205   //when we have @n option on output format
206   char output_format_digits;
207   int output_alpha_format_digits;
208   //format for the cddb cue output
209   char format[SPLT_OUTNUM+1][SPLT_MAXOLEN];
210 } splt_oformat;
211 
212 struct _splt_point {
213   /**
214    * @brief Value of the splitpoint in hundreths of seconds
215    */
216   long value;
217   /**
218    * @brief Name of the new filename issued from the splitpoint
219    */
220   char *name;
221   /**
222    * @brief Type of the splitpoint
223    * @see splt_type_of_splitpoint
224    */
225   int type;
226 };
227 
228 struct _splt_points {
229   splt_point *points;
230   int real_splitnumber;
231   int iterator_counter;
232 };
233 
234 /**
235  * @brief The data structure for the progress bar
236  */
237 struct splt_progres {
238   /*!maximum number of characters for the filename
239 
240     (without the extension) when displaying the
241     progress warning; default is 40 */
242   int progress_text_max_char;
243   //! Name of the file  we are currently splitting
244   char filename_shorted[512];
245   //!the current percent of the progress
246   float percent_progress;
247   //!the splitpoint we are currently splitting
248   int current_split;
249   //!the maximum number of splits
250   int max_splits;
251   /*!the progress bar type
252 
253     can be:
254     - SPLT_PROGRESS_PREPARE
255     - SPLT_PROGRESS_CREATE
256     - SPLT_PROGRESS_SEARCH_SYNC
257     - SPLT_PROGRESS_SCAN_SILENCE */
258   int progress_type;
259   //!infos for the silence split
260   int silence_found_tracks;
261   //! The silence leven in dB
262   float silence_db_level;
263   //! A pointer to the callback function
264   void (*progress)(struct splt_progres*, void *);
265   void *progress_cb_data;
266 };
267 
268 //! The data libmp3splt keeps all its internal data in
269 typedef struct {
270   //! total time of the song
271   long total_time;
272   //!When splitting: the number of the last splitpoint we arrived at.
273   int current_split;
274   //! for user feedback: the number of the file we are splitting
275   int current_split_file_number;
276   /*! how many split points do we have?
277 
278     this is updated only by
279     check_splitpts_inf_song_length()
280     If this function has not been called
281     look at points->real_splitnumber instead.
282   */
283   int splitnumber;
284   /*! Pointer to the fallback function that is
285       called when the library starts to split
286       a new file.
287 
288       the char* is the filename that is currently
289       being split.
290   */
291   void (*file_split)(const char *, void *);
292   void *file_split_cb_data;
293 
294   void (*write_cb)(const void *ptr, size_t size, size_t nmemb, void *cb_data);
295   void *write_cb_data;
296 
297   //!All infos for the progress bar
298   splt_progress *p_bar;
299   //!callback for sending the silence level to the client
300   void (*get_silence_level)(long time, float level, void *user_data);
301   //!user data set by the client for the 'get_silence_level' function
302   void *silence_level_client_data;
303   //!sends a message to the main program to tell what we are doing
304   void (*put_message)(const char *, splt_message_type, void *);
305   void *put_message_cb_data;
306   //!structure in which we have all the splitpoints
307   splt_points *points;
308 
309   splt_tags tags_like_x;
310 
311   splt_tags_group *tags_group;
312 } splt_struct;
313 
314 //!structure with all the options supplied to split the file
315 typedef struct {
316   /*! this field can take the following values:
317      - SPLT_OPTION_NORMAL_MODE
318      - SPLT_OPTION_WRAP_MODE
319      - SPLT_OPTION_SILENCE_MODE
320      - SPLT_OPTION_TRIM_SILENCE_MODE
321      - SPLT_OPTION_ERROR_MODE
322      - SPLT_OPTION_TIME_MODE
323   */
324   splt_split_mode_options split_mode;
325 
326   /*! might be:
327        - SPLT_TAGS_ORIGINAL_FILE - write tags from original file
328        - SPLT_NO_TAGS - does not write any tags
329        - SPLT_CURRENT_TAGS - tags issued from the cddb or cue for example
330        - or be set manually with the functions
331          splt_tags_options tags;
332   */
333   splt_tags_options tags;
334 
335   int xing;
336 
337   //!defines the output filenames
338   splt_output_filenames_options output_filenames;
339 
340   //!quiet mode: don't perform CRC check or other interaction with the user
341   int quiet_mode;
342 
343   /*!Pretend to split the file, without real split
344 
345     this option works in all modes except error mode and dewrap split.
346   */
347   int pretend_to_split;
348 
349   /*! frame mode (mp3 only).
350 
351     Process all frames, seeking split positions by counting frames
352     instead of bitrate guessing.
353   */
354   int option_frame_mode;
355   //!the time of split when split_mode = OPTION_TIME_SPLIT
356   long split_time;
357   long overlap_time;
358   //!this option uses silence detection to auto-adjust splitpoints.
359   int option_auto_adjust;
360   /*! True means: input not seekable.
361 
362     enabling this allows you to split mp3 and ogg streams
363     which can be read only one time and can’t be seeked.
364     WARNING!
365     if you don't know what this means you know why it is wise
366     to set this to FALSE.
367   */
368   int option_input_not_seekable;
369 
370   /*! SPLT_TRUE= don't patse output filenames for illegal characters.
371 
372     Otherwise, we parse for illegal characters the filenames and replace
373     them with '_'. The tags are always checked for illegal characters when
374     set into filenames.
375   */
376   int create_dirs_from_filenames;
377 
378   //PARAMETERS---------------------------------------
379   //PARAMETERS for option_auto_adjust and option_silence_mode:
380   /*! the sound level to be considered silence
381 
382    (is a float number between -96 and 0. Default is -48 dB)*/
383   float parameter_threshold;
384   /*! the offset of cutpoint in silence
385 
386     Float number between -2 and 2 and allows you to adjust the offset
387     of cutpoint in silence time.0 is the begin of silence, and 1 the
388     end;default is 0.8.
389 
390     Used for option_auto_adjust and option_silence_mode
391   */
392   float parameter_offset;
393 
394   //PARAMETERS for option_silence_mode:
395   /*! the desired number of tracks for option_silence_mode
396 
397   (positive integer number of tracks to be split;by default all
398   tracks are split)
399   */
400   int parameter_number_tracks;
401   /*!
402      this option defines the number of shots to be found after the silence.
403      Decrease this value if you need to split files having closer silence points.
404 
405      (positive integer number; default is #SPLT_DEFAULT_PARAM_SHOTS)
406   */
407   int parameter_shots;
408   /*! The minimum silence length for option_silence_mode [in seconds]
409 
410     A positive float of the minimum number of seconds to be considered
411     a valid splitpoint
412   */
413   float parameter_minimum_length;
414   /*! The minimum track length for option_silence_mode [in seconds]
415 
416     A positive float of the minimum number of seconds to be considered
417     a valid splitpoint
418   */
419   float parameter_min_track_length;
420 
421   /*! The minimum track length for option_silence_mode [in seconds] to be kept as a valid segment.
422 
423     A positive float of the minimum number of seconds to be left as output file. Smaller
424     segments will be joined among others.
425   */
426   float parameter_min_track_join;
427 
428   //!possible values are #splt_str_format
429   int artist_tag_format;
430   //!possible values are #splt_str_format
431   int album_tag_format;
432   //!possible values are #splt_str_format
433   int title_tag_format;
434   //!possible values are #splt_str_format
435   int comment_tag_format;
436 
437   //!true=replace underscores with space
438   int replace_underscores_tag_format;
439 
440   //!SPLT_TRUE=set the current filename to split from FILE tag - if found in the CUE file
441   int set_file_from_cue_if_file_tag_found;
442 
443   //!true=remove the silence between split tracks
444   int parameter_remove_silence;
445 
446   //!when using the 'remove silence' parameter, number of seconds to keep at the beginning
447   float keep_silence_left;
448   //!when using the 'remove silence' parameter, number of seconds to keep at the end
449   float keep_silence_right;
450 
451   //PARAMETERS for option_auto_adjust:
452   /*! Auto-Adjust: the gap value around splitpoint to search for
453     silence
454 
455     (positive integer for the time to decode before and after
456     splitpoint;default gap is 30 seconds)
457   */
458   int parameter_gap;
459 
460   /*! Copy all tags from tag number 'remaining_tags_like_x'
461 
462     set to -1 to disable
463   */
464   int remaining_tags_like_x;
465 
466   //!true=auto-increment the tracknumber of the tags
467   int auto_increment_tracknumber_tags;
468 
469   /**
470    * true=enable the silence points log ('mp3splt.log')
471    */
472   int enable_silence_log;
473 
474   /**
475    * If we force the mp3 tags version to 1 or 2 or 1 & 2;
476    * Set to 0 if force disable;
477    * If disabled, original tags version is set to split files,
478    *  or version 1 if compiled without libid3tag support
479    */
480   int force_tags_version;
481   /**
482    * the number of files to be created when splitting by equal time length
483    */
484   int length_split_file_number;
485   int replace_tags_in_tags;
486 
487   int cue_set_splitpoint_names_from_rem_name;
488   int cue_disable_cue_file_created_message;
489   int cue_cddb_add_tags_with_keep_original_tags;
490   int warn_if_no_auto_adjust_found;
491   int stop_if_no_auto_adjust_found;
492   int decode_and_write_flac_md5sum;
493   int handle_bit_reservoir;
494   int id3v2_encoding;
495   int input_tags_encoding;
496   long time_minimum_length;
497 } splt_options_variables;
498 
499 //internal structures
500 typedef struct {
501   //!Do we have send the message frame mode enabled?
502   int frame_mode_enabled;
503   //!if current_refresh_rate = refresh_rate, we call the progress callback
504   int current_refresh_rate;
505   //! if set to SPLT_TRUE then we don't send messages to clients
506   int messages_locked;
507   //!if we currently use the library, we lock it
508   int library_locked;
509   //!the new filename path (internal)
510   char *new_filename_path;
511 } splt_internal;
512 
513 typedef enum {
514   SPLT_OPT_ALL_REMAINING_TAGS_LIKE_X = 10000,
515   SPLT_OPT_AUTO_INCREMENT_TRACKNUMBER_TAGS,
516 } splt_internal_options;
517 
518 //!structure containing all the data about a plugin
519 typedef struct {
520   splt_plugin_info info;
521   //!complete filename of the plugin shared object
522   char *plugin_filename;
523   //! plugin handle get with lt_dlopen, used later for lt_dlclose
524   void *plugin_handle;
525   //! plugin functions
526   splt_plugin_func *func;
527 } splt_plugin_data;
528 
529 //!internal plugins structure
530 typedef struct {
531   //!directories where we scan for plugins
532   char **plugins_scan_dirs;
533   int number_of_dirs_to_scan;
534   //!the number of plugins found
535   int number_of_plugins_found;
536   //!data structure about all the plugins
537   splt_plugin_data *data;
538 } splt_plugins;
539 
540 //!structure containing error strings for error messages
541 typedef struct {
542   char *error_data;
543   char *strerror_msg;
544 } splt_error;
545 
546 struct splt_ssplit {
547   double begin_position;
548   double end_position;
549   long len;
550   struct splt_ssplit *next;
551 };
552 
553 typedef struct {
554   char *proxy_address;
555   int proxy_port;
556   char *authentification;
557 } splt_proxy;
558 
559 struct _splt_state {
560   //!setting to SPLT_TRUE cancels the split
561   int cancel_split;
562   //!filename to split
563   char *fname_to_split;
564   //!where the split file will be split
565   char *path_of_split;
566 
567   //if this is non null, we write a m3u from the split files
568   char *m3u_filename;
569 
570   //!setting tags from input filename regex
571   char *input_fname_regex;
572 
573   char *default_comment_tag;
574   char *default_genre_tag;
575 
576   //!tags of the original file to split
577   splt_original_tags original_tags;
578 
579   //!options for the split
580   splt_options_variables options;
581   //!split related
582   splt_struct split;
583   //!output format
584   splt_oformat oformat;
585   //!wrap related
586   splt_wrap *wrap;
587   //!syncerror related
588   splt_syncerrors *serrors;
589   /*! counter for the number of sync errors found
590 
591     - the state->serros->serrors_points_num must be used when processing the
592       syncerrors from 'serrors'
593     - this is just a standalone counter
594   */
595   unsigned long syncerrors;
596   //!freedb related
597   splt_freedb fdb;
598 
599   //!internal options
600   splt_internal iopts;
601 
602   //!see the ssplit structure
603   struct splt_ssplit *silence_list;
604 
605   splt_proxy proxy;
606 
607   //!file format states, mp3,ogg..
608   void *codec;
609 
610   //!error strings for error code messages
611   splt_error err;
612 
613   //!plugins structure
614   splt_plugins *plug;
615   int current_plugin;
616 
617   //!filename of the silence log: 'mp3splt.log' in the original mp3splt
618   char *silence_log_fname;
619   char *silence_full_log_fname;
620   FILE *full_log_file_descriptor;
621 };
622 
623 #include "pair.h"
624 #include "types_func.h"
625 #include "cddb.h"
626 #include "utils.h"
627 #include "checks.h"
628 #include "plugins.h"
629 #include "cue.h"
630 #include "cddb.h"
631 #include "freedb.h"
632 #include "audacity.h"
633 #include "splt_array.h"
634 #include "string_utils.h"
635 #include "tags_utils.h"
636 #include "input_output.h"
637 #include "options.h"
638 #include "output_format.h"
639 #include "split_points.h"
640 #include "errors.h"
641 #include "freedb_utils.h"
642 #include "silence_utils.h"
643 #include "sync_errors.h"
644 #include "wrap.h"
645 #include "client.h"
646 #include "conversions.h"
647 #include "tags_parser.h"
648 #include "oformat_parser.h"
649 #include "debug.h"
650 #include "filename_regex.h"
651 #include "win32.h"
652 #include "proxy.h"
653 
654 #ifdef HAVE_CONFIG_H
655 #include "config.h"
656 #endif
657 
658 #include <limits.h>
659 #include <locale.h>
660 
661 #ifdef ENABLE_NLS
662  #include <libintl.h>
663 
664  #ifndef __WIN32
665   #include <langinfo.h>
666  #endif
667 #endif
668 
669 #ifdef __WIN32__
670 
671 #define fseeko fseeko64
672 #define ftello ftello64
673 
674 #else
675 
676 #if !HAVE_FSEEKO
677 #define fseeko fseek
678 #define ftello ftell
679 #endif
680 
681 #endif
682 
683 #define MP3SPLT_LIB_GETTEXT_DOMAIN "libmp3splt"SPLT_SONAME
684 
685 #ifdef ENABLE_NLS
686 #  define _(STR) dgettext(MP3SPLT_LIB_GETTEXT_DOMAIN, STR)
687 #else
688 #  define _(STR) ((const char *)STR)
689 #endif
690 
691 /**********************************/
692 /* splt normal or syncerror split */
693 
694 void splt_s_error_split(splt_state *state, int *error);
695 void splt_s_multiple_split(splt_state *state, int *error);
696 void splt_s_normal_split(splt_state *state, int *error);
697 
698 /************************************/
699 /* splt time and equal length split */
700 
701 void splt_s_time_split(splt_state *state, int *error);
702 void splt_s_equal_length_split(splt_state *state, int *error);
703 
704 /************************************/
705 /* splt silence detection and split */
706 
707 int splt_s_set_silence_splitpoints(splt_state *state, int *error);
708 int splt_s_set_trim_silence_splitpoints(splt_state *state, int *error);
709 void splt_s_silence_split(splt_state *state, int *error);
710 void splt_s_trim_silence_split(splt_state *state, int *error);
711 
712 /****************************/
713 /* splt wrap split */
714 
715 void splt_s_wrap_split(splt_state *state, int *error);
716 
717 /* other stuff:/ */
718 
719 #define SPLT_DEFAULT_PROGRESS_RATE 350
720 #define SPLT_DEFAULT_PROGRESS_RATE2 50
721 
722 #define SPLT_DEFAULTSILLEN 10
723 
724 #define SPLT_VARCHAR '@'
725 
726 //max number of splitpoints
727 //for silence detection and for syncerrors
728 #define SPLT_MAXSYNC INT_MAX
729 #define SPLT_MAXSILENCE INT_MAX
730 
731 /* libmp3splt internals */
732 #define SPLT_IERROR_INT -1
733 #define SPLT_IERROR_SET_ORIGINAL_TAGS -2
734 #define SPLT_IERROR_CHAR -3
735 
736 //following mp3splt.h->#splt_tag_key
737 #define SPLT_TAGS_VERSION 800
738 
739 #define SPLT_ORIGINAL_TAGS_DEFAULT "%[@o,@N=1]"
740 
741 #define SPLT_INTERNAL_PROGRESS_RATE 1
742 #define SPLT_INTERNAL_FRAME_MODE_ENABLED 2
743 
744 //package information constants
745 #ifndef SPLT_PACKAGE_NAME
746 /**
747  * @brief Package name
748  */
749 #define SPLT_PACKAGE_NAME "libmp3splt"
750 #endif
751 
752 #ifndef SPLT_PACKAGE_VERSION
753 /**
754  * @brief Package version
755  */
756 #define SPLT_PACKAGE_VERSION LIBMP3SPLT_VERSION
757 #endif
758 
759 /**
760  * @brief Package authors
761  */
762 #define SPLT_AUTHOR "Matteo Trotta | Munteanu Alexandru"
763 #define SPLT_EMAIL "<mtrotta@users.sourceforge.net> | <m@ioalex.net>"
764 /**
765  * @brief Package website
766  */
767 #define SPLT_WEBSITE "http://mp3splt.sourceforge.net"
768 
769 /**
770  * @brief Freedb search type: NOT IMPLEMENTED yet
771  */
772 #define SPLT_FREEDB_SEARCH_TYPE_CDDB 2
773 
774 #ifndef SPLT_NDIRCHAR
775 #ifdef __WIN32__
776 #define SPLT_NDIRCHAR '/'
777 #else
778 #define SPLT_NDIRCHAR '\\'
779 #endif
780 #endif
781 
782 #define MP3SPLT_SPLT_H
783 
784 #endif
785 
786