00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029
00030 #include "mp3.h"
00031 #include "mp3_silence.h"
00032 #include "mp3_utils.h"
00033 #include "silence_processors.h"
00034
00035 static void splt_mp3_scan_silence_and_process(splt_state *state, off_t begin_offset,
00036 float max_threshold, unsigned long length,
00037 short process_silence(double time, int silence_was_found, short must_flush,
00038 splt_scan_silence_data *ssd, int *found, int *error),
00039 splt_scan_silence_data *ssd, int *error);
00040 static int splt_mp3_silence(splt_mp3_state *mp3state, int channels, mad_fixed_t threshold);
00041
00052 int splt_mp3_scan_silence(splt_state *state, off_t begin, unsigned long length,
00053 float threshold, float min, int shots, short output, int *error,
00054 short silence_processor(double time, int silence_was_found, short must_flush,
00055 splt_scan_silence_data *ssd, int *found, int *error))
00056 {
00057 splt_scan_silence_data *ssd = splt_scan_silence_data_new(state, output, min, shots, SPLT_TRUE);
00058 if (ssd == NULL)
00059 {
00060 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00061 return -1;
00062 }
00063
00064 splt_mp3_scan_silence_and_process(state, begin, threshold, length, silence_processor, ssd, error);
00065
00066 int found = ssd->found;
00067
00068 splt_free_scan_silence_data(&ssd);
00069
00070 if (*error < 0)
00071 {
00072 found = -1;
00073 }
00074
00075 return found;
00076 }
00077
00078 static void splt_mp3_scan_silence_and_process(splt_state *state, off_t begin_offset,
00079 float max_threshold, unsigned long length,
00080 short process_silence(double time, int silence_was_found, short must_flush,
00081 splt_scan_silence_data *ssd, int *found, int *error),
00082 splt_scan_silence_data *ssd, int *error)
00083 {
00084 int found = 0;
00085 short stop = SPLT_FALSE;
00086
00087 mad_fixed_t threshold = mad_f_tofixed(splt_co_convert_from_dB(max_threshold));
00088 splt_mp3_state *mp3state = state->codec;
00089
00090 splt_c_put_progress_text(state, SPLT_PROGRESS_SCAN_SILENCE);
00091
00092
00093 if (fseeko(mp3state->file_input, begin_offset, SEEK_SET) == -1)
00094 {
00095 splt_e_set_strerror_msg_with_data(state, splt_t_get_filename_to_split(state));
00096 *error = SPLT_ERROR_SEEKING_FILE;
00097 return;
00098 }
00099
00100
00101 splt_mp3_init_stream_frame(mp3state);
00102 mad_synth_init(&mp3state->synth);
00103
00104 mad_timer_reset(&mp3state->timer);
00105
00106 mp3state->temp_level = 0.0;
00107
00108 do {
00109 int mad_err = SPLT_OK;
00110
00111 switch (splt_mp3_get_valid_frame(state, &mad_err))
00112 {
00113 case 1:
00114
00115 mad_timer_add(&mp3state->timer, mp3state->frame.header.duration);
00116 mad_synth_frame(&mp3state->synth, &mp3state->frame);
00117 unsigned long time = (unsigned long) mad_timer_count(mp3state->timer, MAD_UNITS_CENTISECONDS);
00118
00119 int silence_was_found =
00120 splt_mp3_silence(mp3state, MAD_NCHANNELS(&mp3state->frame.header), threshold);
00121
00122 int err = SPLT_OK;
00123 short must_flush = (length > 0 && time >= length);
00124 double time_in_double = (double) time / 100.f;
00125 stop = process_silence(time_in_double, silence_was_found, must_flush, ssd, &found, &err);
00126 if (stop || stop == -1)
00127 {
00128 stop = SPLT_TRUE;
00129 if (err < 0) { *error = err; goto end; }
00130 }
00131
00132 if (mp3state->mp3file.len > 0)
00133 {
00134 off_t pos = ftello(mp3state->file_input);
00135
00136 float level = splt_co_convert_to_dB(mad_f_todouble(mp3state->temp_level));
00137 if (state->split.get_silence_level)
00138 {
00139 state->split.get_silence_level(time, level, state->split.silence_level_client_data);
00140 }
00141 state->split.p_bar->silence_db_level = level;
00142 state->split.p_bar->silence_found_tracks = found;
00143
00144
00145
00146 if ((splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_SILENCE_MODE) &&
00147 (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_TRIM_SILENCE_MODE))
00148 {
00149 splt_c_update_progress(state,(double)(time),
00150 (double)(length), 4,1/(float)4, SPLT_DEFAULT_PROGRESS_RATE);
00151 }
00152 else
00153 {
00154 if (splt_t_split_is_canceled(state))
00155 {
00156
00157 stop = SPLT_TRUE;
00158 }
00159 splt_c_update_progress(state,(double)pos,
00160 (double)(mp3state->mp3file.len), 1,0,SPLT_DEFAULT_PROGRESS_RATE);
00161 }
00162 }
00163 break;
00164 case 0:
00165
00166 break;
00167 case -1:
00168
00169 stop = SPLT_TRUE;
00170 break;
00171 case -3:
00172
00173 stop = SPLT_TRUE;
00174 *error = mad_err;
00175 break;
00176 default:
00177 break;
00178 }
00179
00180 } while (!stop);
00181
00182 int junk;
00183 int err = SPLT_OK;
00184 process_silence(-1, SPLT_FALSE, SPLT_FALSE, ssd, &junk, &err);
00185 if (err < 0) { *error = err; }
00186
00187
00188 if ((splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) == SPLT_OPTION_SILENCE_MODE) ||
00189 (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) == SPLT_OPTION_TRIM_SILENCE_MODE))
00190 {
00191 splt_c_update_progress(state,1.0,1.0,1,1,1);
00192 }
00193
00194 end:
00195
00196 splt_mp3_finish_stream_frame(mp3state);
00197 mad_synth_finish(&mp3state->synth);
00198 }
00199
00210 static int splt_mp3_silence(splt_mp3_state *mp3state, int channels, mad_fixed_t threshold)
00211 {
00212 int i, j;
00213 mad_fixed_t sample;
00214 int silence = 1;
00215
00216 for (j = 0; j < channels; j++)
00217 {
00218 for(i = 0; i < mp3state->synth.pcm.length; i++)
00219 {
00220 sample = mad_f_abs(mp3state->synth.pcm.samples[j][i]);
00221 mp3state->temp_level = mp3state->temp_level * 0.999 + sample * 0.001;
00222
00223 if (sample > threshold)
00224 {
00225 silence = 0;
00226 }
00227 }
00228 }
00229
00230 return silence;
00231 }
00232