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 "silence_processors.h"
00029
00030 splt_scan_silence_data *splt_scan_silence_data_new(splt_state *state, short first,
00031 float min, int shots, short set_new_length)
00032 {
00033 splt_scan_silence_data *ssd = malloc(sizeof(splt_scan_silence_data));
00034 if (!ssd)
00035 {
00036 return NULL;
00037 }
00038
00039 ssd->state = state;
00040 ssd->first = first;
00041 ssd->min = min;
00042 ssd->number_of_shots = shots;
00043 ssd->set_new_length = set_new_length;
00044
00045 ssd->flush = SPLT_FALSE;
00046 ssd->silence_begin = 0;
00047 ssd->silence_end = 0;
00048 ssd->len = 0;
00049 ssd->found = 0;
00050 ssd->shot = shots;
00051 ssd->silence_begin_was_found = SPLT_FALSE;
00052 ssd->continue_after_silence = SPLT_FALSE;
00053
00054 return ssd;
00055 }
00056
00057 void splt_free_scan_silence_data(splt_scan_silence_data **ssd)
00058 {
00059 if (!ssd || !*ssd)
00060 {
00061 return;
00062 }
00063
00064 free(*ssd);
00065 *ssd = NULL;
00066 }
00067
00068 short splt_scan_silence_processor(double time, int silence_was_found,
00069 short must_flush, splt_scan_silence_data *ssd, int *found_silence_points, int *error)
00070 {
00071 if (time < 0) { return SPLT_TRUE; }
00072
00073 short stop = SPLT_FALSE;
00074
00075 if (must_flush)
00076 {
00077 ssd->flush = SPLT_TRUE;
00078 stop = SPLT_TRUE;
00079 }
00080
00081 if (!ssd->flush && silence_was_found)
00082 {
00083 if (ssd->len == 0)
00084 {
00085 ssd->silence_begin = time;
00086 }
00087
00088 if (ssd->first == SPLT_FALSE)
00089 {
00090 ssd->len++;
00091 }
00092
00093 if (ssd->shot < ssd->number_of_shots)
00094 {
00095 ssd->shot += 2;
00096 }
00097
00098 ssd->silence_end = time;
00099
00100 *found_silence_points = ssd->found;
00101 return stop;
00102 }
00103
00104 if (ssd->len > SPLT_DEFAULTSILLEN)
00105 {
00106 if (ssd->flush || (ssd->shot <= 0))
00107 {
00108 double begin_position = ssd->silence_begin;
00109 double end_position = ssd->silence_end;
00110
00111 if (ssd->set_new_length)
00112 {
00113 ssd->len = (int) (ssd->silence_end * 100.0 - ssd->silence_begin * 100.0);
00114 }
00115
00116 if ((end_position - begin_position - ssd->min) >= 0.f)
00117 {
00118 if (splt_siu_ssplit_new(&ssd->state->silence_list,
00119 begin_position, end_position, ssd->len, error) == -1)
00120 {
00121 ssd->found = -1;
00122 *found_silence_points = ssd->found;
00123 return SPLT_TRUE;
00124 }
00125
00126 ssd->found++;
00127 }
00128
00129 ssd->len = 0;
00130 ssd->shot = ssd->number_of_shots;
00131 }
00132 }
00133 else
00134 {
00135 ssd->len = 0;
00136 }
00137
00138 if (ssd->flush)
00139 {
00140 return -1;
00141 }
00142
00143 if (ssd->first && (ssd->shot <= 0))
00144 {
00145 ssd->first = SPLT_FALSE;
00146 }
00147
00148 if (ssd->shot > 0)
00149 {
00150 ssd->shot--;
00151 }
00152
00153 if (ssd->found >= SPLT_MAXSILENCE)
00154 {
00155 stop = SPLT_TRUE;
00156 }
00157
00158 *found_silence_points = ssd->found;
00159
00160 return stop;
00161 }
00162
00163 static short splt_detect_where_begin_silence_ends(double time, int silence_was_found,
00164 short must_flush, splt_scan_silence_data *ssd, int *found_silence_points, int *error)
00165 {
00166 if (silence_was_found)
00167 {
00168 if (ssd->shot < ssd->number_of_shots)
00169 {
00170 ssd->shot += 2;
00171 }
00172
00173 ssd->silence_end = time;
00174 }
00175
00176 if (ssd->shot <= 0)
00177 {
00178 if (splt_siu_ssplit_new(&ssd->state->silence_list, ssd->silence_end, ssd->silence_end, 0, error) == -1)
00179 {
00180 return SPLT_TRUE;
00181 }
00182
00183 ssd->found++;
00184 ssd->silence_begin_was_found = SPLT_TRUE;
00185 ssd->shot = ssd->number_of_shots;
00186 }
00187
00188 if (ssd->shot > 0)
00189 {
00190 ssd->shot--;
00191 }
00192
00193 return SPLT_FALSE;
00194 }
00195
00196 static short splt_detect_where_end_silence_begins(double time, int silence_was_found,
00197 short must_flush, splt_scan_silence_data *ssd, int *found_silence_points, int *error)
00198 {
00199 if (time < 0)
00200 {
00201 if (splt_siu_ssplit_new(&ssd->state->silence_list, ssd->silence_begin, ssd->silence_begin, 0, error) == -1)
00202 {
00203 return SPLT_TRUE;
00204 }
00205
00206 ssd->found++;
00207
00208 return SPLT_TRUE;
00209 }
00210
00211 if (silence_was_found)
00212 {
00213 if (ssd->len == 0)
00214 {
00215 ssd->silence_begin = time;
00216 ssd->continue_after_silence = SPLT_FALSE;
00217 }
00218
00219 if (ssd->first == SPLT_FALSE)
00220 {
00221 ssd->len++;
00222 }
00223
00224 if (ssd->shot < ssd->number_of_shots)
00225 {
00226 ssd->shot += 2;
00227 }
00228
00229 return SPLT_FALSE;
00230 }
00231 else if (ssd->continue_after_silence)
00232 {
00233 ssd->silence_begin = time;
00234 }
00235
00236 if (ssd->len > SPLT_DEFAULTSILLEN)
00237 {
00238 if (ssd->shot <= 0)
00239 {
00240 ssd->len = 0;
00241 ssd->shot = ssd->number_of_shots;
00242 ssd->continue_after_silence = SPLT_TRUE;
00243 }
00244 }
00245 else
00246 {
00247 ssd->len = 0;
00248 }
00249
00250 if (ssd->first && (ssd->shot <= 0))
00251 {
00252 ssd->first = SPLT_FALSE;
00253 }
00254
00255 if (ssd->shot > 0)
00256 {
00257 ssd->shot--;
00258 }
00259
00260 return SPLT_FALSE;
00261 }
00262
00263 short splt_trim_silence_processor(double time, int silence_was_found,
00264 short must_flush, splt_scan_silence_data *ssd, int *found_silence_points, int *error)
00265 {
00266 if (!ssd->silence_begin_was_found)
00267 {
00268 splt_detect_where_begin_silence_ends(time, silence_was_found, must_flush, ssd, found_silence_points, error);
00269 }
00270 else
00271 {
00272 splt_detect_where_end_silence_begins(time, silence_was_found, must_flush, ssd, found_silence_points, error);
00273 }
00274
00275 return SPLT_FALSE;
00276 }
00277