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