void Chain::transform (ITransformer::info info, ssi_stream_t &stream_in, ssi_stream_t &stream_out, ssi_size_t xtra_stream_in_num, ssi_stream_t xtra_stream_in[]) { // adjust arrays _stream_tmp[0] = &stream_in; // first is always the input stream ssi_size_t sample_number = stream_in.num; for (ssi_size_t i = 1; i < _stream_tmp_num - 1; i++) { ssi_stream_adjust (*_stream_tmp[i], sample_number); } _stream_tmp[_stream_tmp_num-1] = &stream_out; // last is always the output stream // transform data for (ssi_size_t i = 0; i < _n_filters; i++) { _filters[i]->transform (info, *_stream_tmp[i], *_stream_tmp[i+1]); } if (_n_features > 0) { ssi_byte_t *ptr_tmp = _stream_tmp[_stream_tmp_num-1]->ptr; for (ssi_size_t i = 0; i < _n_features; i++) { _stream_tmp[_stream_tmp_num-1]->dim = _feature_dim_out[i]; _features[i]->transform (info, *_stream_tmp[_stream_tmp_num-2], *_stream_tmp[_stream_tmp_num-1]); _stream_tmp[_stream_tmp_num-1]->ptr += _stream_tmp[_stream_tmp_num-1]->byte * _feature_dim_out[i]; } _stream_tmp[_stream_tmp_num-1]->ptr = ptr_tmp; _stream_tmp[_stream_tmp_num-1]->dim = _feature_dim_out_tot; } }
void SignalTools::Series (ssi_stream_t &series, ssi_time_t duration, ssi_real_t offset) { /* matlab code: t = 0:1/sr:dur-1/sr; */ ssi_real_t delta = ssi_cast (ssi_real_t, 1.0 / series.sr); ssi_size_t number = ssi_cast (ssi_size_t, duration / delta); SSI_ASSERT (number > 0); ssi_stream_adjust (series, number); ssi_real_t *out = ssi_pcast (ssi_real_t, series.ptr); ssi_real_t *outptr_new = out; ssi_real_t *outptr_old = out; for (ssi_size_t i = 0; i < series.dim; ++i) { *outptr_new++ = offset; } for (ssi_size_t i = series.dim; i < series.dim * series.num; ++i) { *outptr_new++ = *outptr_old++ + delta; } }
bool FileReader::prepare_file () { if (_options.offsetInSamples > 0) { _offset_in_samples = _options.offsetInSamples; } else { _offset_in_samples = ssi_cast (ssi_size_t, _stream.sr * _options.offset); } _offset_in_bytes = _offset_in_samples * _stream.dim * _stream.byte; if (_offset_in_samples > _sample_number_total) { ssi_wrn ("offset exceeds stream number"); return false; } if (_options.cutoffInSamples > 0) { _cutoff_in_samples = _options.cutoffInSamples; } else { _cutoff_in_samples = ssi_cast (ssi_size_t, _stream.sr * _options.cutoff); } if (_options.blockInSamples > 0) { _sample_number_per_step = _options.blockInSamples; } else { _sample_number_per_step = ssi_cast (ssi_size_t, _stream.sr * _options.block); } _step_counter = 0; if (_cutoff_in_samples == 0) { _max_steps = (_sample_number_total - _offset_in_samples) / _sample_number_per_step; } else { if (_cutoff_in_samples > _sample_number_total) { ssi_wrn ("cutoff exceeds stream number"); return false; } if (_offset_in_samples > _cutoff_in_samples) { ssi_wrn ("offset exceeds cutoff"); return false; } _max_steps = (_cutoff_in_samples - _offset_in_samples) / _sample_number_per_step; } if (_max_steps == 0) { ssi_wrn ("file too short"); return false; } _options.block = _sample_number_per_step / _stream.sr; ssi_stream_adjust (_stream, _sample_number_per_step); _file_stream_in.getDataFile ()->seek (_offset_in_bytes); return true; }
void Transformer::enter () { if (_frame->IsInIdleMode ()) { _read_pos = 0; } else { _frame->GetCurrentWritePos (_buffer_id_in, _read_pos); } _transformer->transform_enter (_stream_in, _stream_out, _xtra_stream_num, _xtra_streams); ssi_stream_adjust (_stream_in, _sample_number_in); ssi_stream_adjust (_stream_out, _sample_number_out); ssi_time_t buffer_size; _frame->GetCapacity(_buffer_id_out, buffer_size); ssi_msg (SSI_LOG_LEVEL_BASIC, "start '%s:%s'", _transformer->getName (), Factory::GetObjectId(_transformer)); if ( ssi_log_level >= SSI_LOG_LEVEL_BASIC) { ssi_print ("\ frame[s]\t= %.2lf\n\ delta[s]\t= %.2lf\n\ id\t\t= %d -> %d\n\ rate[hz]\t= %.2lf -> %.2lf\n\ dim\t= %u -> %d\n\ bytes\t= %u -> %d\n\ type\t= %s -> %s\n\ buffer[s]\t= %.2lf\n", _frame_size, _delta_size, _buffer_id_in, _buffer_id_out, _stream_in.sr, _stream_out.sr, _stream_in.dim, _stream_out.dim, _stream_in.byte, _stream_out.byte, SSI_TYPE_NAMES[_stream_in.type], SSI_TYPE_NAMES[_stream_out.type], buffer_size); }
void MvgConDiv::transform (ITransformer::info info, ssi_stream_t &stream_in, ssi_stream_t &stream_out, ssi_size_t xtra_stream_in_num, ssi_stream_t xtra_stream_in[]) { /* Matlab code: [avg, vrc, hist] = avgfun (signal, sr, [N_short, N_long], hist); macd = avg(:,1:2:dim*2) - avg(:,2:2:dim*2); */ ssi_size_t sample_dimension = stream_in.dim; ssi_size_t sample_number = stream_in.num; ssi_stream_adjust (_mvgs_tmp, sample_number); ssi_stream_adjust (_mvgl_tmp, sample_number); _mvgs->transform (info, stream_in, _mvgs_tmp); _mvgl->transform (info, stream_in, _mvgl_tmp); ssi_real_t *srcptr = ssi_pcast (ssi_real_t, stream_in.ptr); ssi_real_t *dstptr = ssi_pcast (ssi_real_t, stream_out.ptr); ssi_real_t *mvgsptr = ssi_pcast (ssi_real_t, _mvgs_tmp.ptr); ssi_real_t *mvglptr = ssi_pcast (ssi_real_t, _mvgl_tmp.ptr); ssi_real_t short_value, long_value; for (ssi_size_t i = 0; i < sample_number; ++i) { for (ssi_size_t j = 0; j < sample_dimension; ++j) { short_value = *mvgsptr++; long_value = *mvglptr++; *dstptr++ = short_value - long_value; } } }
ssi_size_t FileStreamIn::read (ssi_stream_t &data, ssi_size_t chunk_id) { if (data.sr != _stream.sr || data.byte != _stream.byte || data.dim != _stream.dim || data.type != _stream.type) { ssi_wrn ("stream is not compatible"); return READ_ERROR; } if (chunk_id != NEXT_CHUNK && chunk_id >= _n_chunks) { ssi_wrn ("requested chunk '%u' exceeds #chunks '%u'", chunk_id, _n_chunks); return READ_ERROR; } else if (chunk_id == NEXT_CHUNK && _next_chunk >= _n_chunks) { return 0; } else if (chunk_id == NEXT_CHUNK) { chunk_id = _next_chunk++; } data.time = _time[chunk_id]; ssi_size_t num = _samples[chunk_id]; if (num == 0) { data.num = 0; data.tot = 0; return 0; } if (data.num_real < num) { ssi_stream_adjust (data, num); } if (_file_data->tell () != _bytes[chunk_id]) { _file_data->seek (_bytes[chunk_id], File::BEGIN); } _file_data->setType (data.type); if (!_file_data->read (data.ptr, data.byte, num * data.dim)) { ssi_wrn ("could not read <data>"); return READ_ERROR; } data.num = num; data.tot = num * data.dim * data.byte; return num; }
void AudioActivity::transform (ITransformer::info info, ssi_stream_t &stream_in, ssi_stream_t &stream_out, ssi_size_t xtra_stream_in_num, ssi_stream_t xtra_stream_in[]) { ssi_size_t sample_dimension = stream_in.dim; ssi_size_t sample_number = stream_in.num; ssi_real_t threshold = _options.threshold; if (_convert_input) { ssi_stream_adjust (_stream_convert, stream_in.num); _convert->transform (info, stream_in, _stream_convert); _feature->transform (info, _stream_convert, _stream_feat); } else { _feature->transform (info, stream_in, _stream_feat); } ssi_real_t *srcptr = ssi_pcast (ssi_real_t, _stream_feat.ptr); ssi_real_t *dstptr = ssi_pcast (ssi_real_t, stream_out.ptr); *dstptr = *srcptr > _options.threshold ? *srcptr : 0.0f; }
SSI_INLINE void ISMergeDim::align () { static ssi_size_t count = 0; _sample_out.class_id = _sample_in.class_id; _sample_out.user_id = _sample_in.user_id; _sample_out.score = _sample_in.score; _sample_out.time = _sample_in.time; if (_samples.hasMissingData ()) { for(ssi_size_t i = 0; i < _n_streams; i++) { if (_sample_in.streams[i]->num == 0) { _stream.num = 0; return; } } } ssi_size_t num = _sample_in.streams[0]->num; for(ssi_size_t i = 1; i < _n_streams; i++) { if (_sample_in.streams[i]->num != num) { ssi_err ("streams differ in #frames differ (%u != %u)", num, _sample_in.streams[i]->num); } } ssi_stream_adjust (_stream, num); ssi_byte_t *ptr = _stream.ptr; for (ssi_size_t j = 0; j < num; j++) { for(ssi_size_t i = 0; i < _n_streams; i++){ ssi_size_t tot = _sample_in.streams[i]->byte * _sample_in.streams[i]->dim; memcpy(ptr, _sample_in.streams[i]->ptr + j*tot, tot); ptr += tot; } } }
void QRSDetection::transform (ITransformer::info info, ssi_stream_t &stream_in, ssi_stream_t &stream_out, ssi_size_t xtra_stream_in_num, ssi_stream_t xtra_stream_in[]) { ssi_size_t n = stream_in.num; ssi_time_t sr = stream_in.sr; ssi_stream_adjust (_bandpass_stream, n); ssi_stream_adjust (_diff_stream, n); ssi_stream_adjust (_pre_stream, n); ssi_stream_adjust (_pre_low_stream, n); _bandpass->transform (info, stream_in, _bandpass_stream); _diff->transform (info, _bandpass_stream, _diff_stream); _pre->transform (info, _diff_stream, _pre_stream); _pre_low->transform (info, _pre_stream, _pre_low_stream); //qrs-detect ssi_size_t sample_dimension = _pre_low_stream.dim; ssi_size_t sample_number = _pre_low_stream.num; SSI_ASSERT(sample_dimension == 1); ssi_real_t *ptr_in = ssi_pcast (ssi_real_t, _pre_low_stream.ptr); ssi_real_t *ptr_out = ssi_pcast (ssi_real_t, stream_out.ptr); if (_first_call) { _frame_count = 0; //init ssi_real_t average = 0.0f; ssi_real_t avg_tmp = 0.0f; ssi_size_t n_avg = sample_number; for (ssi_size_t nsamples = 0; nsamples < sample_number; nsamples++) { if(*ptr_in != 0.0f){ avg_tmp += *ptr_in++; }else{ n_avg--; ptr_in++; } *ptr_out++ = 0; } if(n_avg != 0){ average = avg_tmp / ssi_cast(ssi_real_t, n_avg); _rising = true; _first_call = false; }else{ average = 0.0f; } _noise_level = average - average * 0.1f; _signal_level = average + average * 0.1f; _thres1 = _noise_level + 0.25f*(_signal_level - _noise_level); _thres2 = 0.5f * _thres1; }else{ _frame_count++; for (ssi_size_t nsamples = 0; nsamples < sample_number-1; nsamples++) { if(*ptr_in <= *(ptr_in+1)){ *ptr_out = 0.0f; _rising = true; } //found peak? if(_rising && (*ptr_in > *(ptr_in+1))){ //found R? if(*ptr_in >= _thres1 || _samples_since_last_R > 2*_pre_low_stream.sr){ //signal peak with t1 //skip #_options.depthRR first R's and init history_RR and sum_RR if(_n_R < _options.depthRR){ _history_RR[_n_R] = _samples_since_last_R; _n_R++; _sum_RR += _samples_since_last_R; _samples_since_last_R = 0; }else{ _pulsed = true; } if(_pulsed){ //pulsed _average_RR = _sum_RR / ssi_cast(ssi_real_t, _options.depthRR); _low_limit_RR = _options.lowerLimitRR * _average_RR; _high_limit_RR = _options.upperLimitRR * _average_RR; /*ssi_print ("\nRR-history\n"); for (ssi_size_t k = 0; k < _options.depthRR; k++) { ssi_print ("%d ", _history_RR[k]); }ssi_print ("\n"); ssi_print ("RR-sum\n"); ssi_print ("%d ", _sum_RR); ssi_print("\nAverage over %d R's:\t%.1f\nCurrent:\t\t%d\n", _options.depthRR, _average_RR, _samples_since_last_R); ssi_print("low_limit:\t%.1f\nhigh_limit:\t%.1f\n", _low_limit_RR, _high_limit_RR);*/ if( _low_limit_RR <= ssi_cast(ssi_real_t, _samples_since_last_R)){ //R accepted and published _sum_RR = _sum_RR + _samples_since_last_R - _history_RR[_head_RR]; _history_RR[_head_RR] = _samples_since_last_R; _head_RR = (_head_RR + 1) % _options.depthRR; *ptr_out = 1.0f; _samples_since_last_R = 0; if(_options.sendEvent){ sendEvent_h(info, _pre_low_stream.sr, _frame_count, nsamples); } }else{ //T-wave *ptr_out = 0.0f; _samples_since_last_R++; } }else{ //not pulsed *ptr_out = 0.0f; _samples_since_last_R++; } _signal_level = 0.125f*(*ptr_in) + 0.875f*_signal_level; }else{ if(_pulsed){ //t2 necessary? if(ssi_cast(ssi_real_t, _samples_since_last_R) >= _high_limit_RR){ if(*ptr_in >= _thres2){ //signal peak with t2 _noise_level = 0.25f*(*ptr_in) + 0.75f*_noise_level; *ptr_out = 1.0f; _samples_since_last_R = 0; if(_options.sendEvent){ sendEvent_h(info, _pre_low_stream.sr, _frame_count, nsamples); } }else{ //noise peak _noise_level = 0.125f*(*ptr_in) + 0.875f*_noise_level; *ptr_out = 0.0f; _samples_since_last_R++; } }else{ //noise peak _noise_level = 0.125f*(*ptr_in) + 0.875f*_noise_level; *ptr_out = 0.0f; _samples_since_last_R++; } }else{ //not pulsed //noise peak _noise_level = 0.125f*(*ptr_in) + 0.875f*_noise_level; *ptr_out = 0.0f; _samples_since_last_R++; } } _rising = false; }else{ //found no peak *ptr_out = 0.0f; _samples_since_last_R++; } ptr_in++; ptr_out++; _thres1 = _noise_level + 0.25f*(_signal_level - _noise_level); _thres2 = 0.5f * _thres1; } //last sample *ptr_out = 0.0f; _samples_since_last_R++; } /*ssi_real_t *ptr_result = ssi_pcast (ssi_real_t, _pre_low_stream.ptr); ssi_real_t *ptr_out = ssi_pcast(ssi_real_t, stream_out.ptr); for(ssi_size_t nsamp = 0; nsamp < stream_in.num; nsamp++){ *ptr_out = *ptr_result; ptr_result++; ptr_out++; }*/ }
void SignalTools::Transform (ssi_stream_t &from, ssi_stream_t &to, ITransformer &transformer, ssi_size_t frame_size, ssi_size_t delta_size, bool call_enter, bool call_flush) { if (frame_size <= 0) { ssi_time_t sample_rate_in = from.sr; ssi_size_t sample_number_in = from.num; ssi_size_t sample_dimension_in = from.dim; ssi_size_t sample_bytes_in = from.byte; ssi_type_t sample_type_in = from.type; ssi_size_t sample_number_out = sample_number_in == 0 ? 0 : transformer.getSampleNumberOut (sample_number_in); ssi_size_t sample_dimension_out = transformer.getSampleDimensionOut (sample_dimension_in); ssi_size_t sample_bytes_out = transformer.getSampleBytesOut (sample_bytes_in); ssi_type_t sample_type_out = transformer.getSampleTypeOut (sample_type_in); ssi_time_t sample_rate_out = sample_number_in == 0 ? 0 : (ssi_cast (ssi_time_t, sample_number_out) / ssi_cast (ssi_time_t, sample_number_in)) * sample_rate_in; ssi_stream_init (to, 0, sample_dimension_out, sample_bytes_out, sample_type_out, sample_rate_out); if (sample_number_out > 0) { ssi_stream_adjust (to, sample_number_out); if (call_enter) { transformer.transform_enter (from, to, 0, 0); } ITransformer::info tinfo; tinfo.delta_num = 0; tinfo.frame_num = from.num; tinfo.time = 0; transformer.transform (tinfo, from, to, 0, 0); if (call_flush) { transformer.transform_flush (from, to, 0, 0); } } } else { ssi_time_t sample_rate_in = from.sr; ssi_size_t from_num = from.num; ssi_size_t from_tot = from.tot; SSI_ASSERT (from_num > frame_size + delta_size); ssi_size_t max_shift = (from_num - delta_size) / frame_size; ssi_size_t sample_number_in = frame_size + delta_size; ssi_size_t sample_number_out = transformer.getSampleNumberOut (frame_size); ssi_size_t sample_dimension_in = from.dim; ssi_size_t sample_dimension_out = transformer.getSampleDimensionOut (sample_dimension_in); ssi_size_t sample_bytes_in = from.byte; ssi_size_t sample_bytes_out = transformer.getSampleBytesOut (sample_bytes_in); ssi_type_t sample_type_in = from.type; ssi_type_t sample_type_out = transformer.getSampleTypeOut (sample_type_in); ssi_time_t sample_rate_out = (ssi_cast (ssi_time_t, sample_number_out) / ssi_cast (ssi_time_t, frame_size)) * sample_rate_in; ssi_size_t to_num = max_shift * sample_number_out; ssi_stream_init (to, 0, sample_dimension_out, sample_bytes_out, sample_type_out, sample_rate_out); ssi_stream_adjust (to, to_num); ssi_size_t to_tot = to.tot; ssi_byte_t *from_ptr = from.ptr; ssi_byte_t *to_ptr = to.ptr; from.num = sample_number_in; to.num = sample_number_out; ssi_size_t byte_shift_in = sample_bytes_in * sample_dimension_in * frame_size; from.tot = byte_shift_in; ssi_size_t byte_shift_out = sample_bytes_out * sample_dimension_out * sample_number_out; to.tot = byte_shift_out; if (call_enter) { transformer.transform_enter (from, to, 0, 0); } ITransformer::info tinfo; tinfo.delta_num = delta_size; tinfo.frame_num = frame_size; tinfo.time = 0; for (ssi_size_t i = 0; i < max_shift; i++) { transformer.transform (tinfo, from, to, 0, 0); tinfo.time += frame_size / sample_rate_in; from.ptr += byte_shift_in; to.ptr += byte_shift_out; } if (call_flush) { transformer.transform_flush (from, to, 0, 0); } from.ptr = from_ptr; from.num = from_num; from.tot = from_tot; to.ptr = to_ptr; to.num = to_num; to.tot = to_tot; } }
void SignalTools::Transform_Xtra (ssi_stream_t &from, ssi_stream_t &to, ITransformer &transformer, ssi_size_t frame_size, ssi_size_t delta_size, bool call_enter, bool call_flush, ssi_size_t xtra_stream_in_num, ssi_stream_t xtra_stream_in[]){ if (frame_size <= 0) { ssi_time_t sample_rate_in = from.sr; ssi_size_t sample_number_in = from.num; ssi_size_t sample_dimension_in = from.dim; ssi_size_t sample_bytes_in = from.byte; ssi_type_t sample_type_in = from.type; ssi_size_t sample_number_out = sample_number_in == 0 ? 0 : transformer.getSampleNumberOut (sample_number_in); ssi_size_t sample_dimension_out = transformer.getSampleDimensionOut (sample_dimension_in); ssi_size_t sample_bytes_out = transformer.getSampleBytesOut (sample_bytes_in); ssi_type_t sample_type_out = transformer.getSampleTypeOut (sample_type_in); ssi_time_t sample_rate_out = sample_number_in == 0 ? 0 : (ssi_cast (ssi_time_t, sample_number_out) / ssi_cast (ssi_time_t, sample_number_in)) * sample_rate_in; ssi_stream_init (to, 0, sample_dimension_out, sample_bytes_out, sample_type_out, sample_rate_out); if (sample_number_out > 0) { ssi_stream_adjust (to, sample_number_out); if (call_enter) { transformer.transform_enter (from, to, xtra_stream_in_num, xtra_stream_in); } ITransformer::info tinfo; tinfo.delta_num = 0; tinfo.frame_num = from.num; tinfo.time = 0; transformer.transform (tinfo, from, to, xtra_stream_in_num, xtra_stream_in); if (call_flush) { transformer.transform_flush (from, to, xtra_stream_in_num, xtra_stream_in); } } } else { ssi_time_t sample_rate_in = from.sr; ssi_size_t n_froms = n_froms = 1 + xtra_stream_in_num;; ssi_real_t *from_nums = new ssi_real_t[n_froms]; ssi_size_t *from_tots = new ssi_size_t[n_froms]; ssi_size_t from_num_min = 0; from_nums[0] = (ssi_real_t)from.num; from_tots[0] = from.tot; for(ssi_size_t i = 1; i < n_froms; i++){ from_nums[i] = (ssi_real_t)xtra_stream_in[i-1].num; from_tots[i] = xtra_stream_in[i-1].tot; } if(n_froms > 1){ ssi_real_t maxval = 0; ssi_real_t minval = 0; ssi_size_t maxpos = 0; ssi_size_t minpos = 0; ssi_minmax(n_froms, 1, from_nums, &minval, &minpos, &maxval, &maxpos); from_num_min = (ssi_size_t)minval; } else { from_num_min = (ssi_size_t)from_nums[0]; } SSI_ASSERT (from_num_min > frame_size + delta_size); ssi_size_t max_shift = (from_num_min - delta_size) / frame_size; ssi_size_t sample_number_out = transformer.getSampleNumberOut (frame_size); ssi_size_t sample_dimension_in = from.dim; ssi_size_t sample_dimension_out = transformer.getSampleDimensionOut (sample_dimension_in); ssi_size_t sample_byte_in = from.byte; ssi_size_t sample_bytes_out = transformer.getSampleBytesOut (sample_byte_in); ssi_type_t sample_type_in = from.type; ssi_type_t sample_type_out = transformer.getSampleTypeOut (sample_type_in); ssi_time_t sample_rate_out = (ssi_cast (ssi_time_t, sample_number_out) / ssi_cast (ssi_time_t, frame_size)) * sample_rate_in; ssi_size_t to_num = max_shift * sample_number_out; ssi_stream_init (to, 0, sample_dimension_out, sample_bytes_out, sample_type_out, sample_rate_out); ssi_stream_adjust (to, to_num); ssi_size_t to_tot = to.tot; ssi_byte_t **from_ptrs = new ssi_byte_t*[n_froms]; from_ptrs[0] = from.ptr; for(ssi_size_t i = 1; i < n_froms; i++){ from_ptrs[i] = xtra_stream_in[i-1].ptr; } ssi_byte_t *to_ptr = to.ptr; ssi_size_t byte_shift_out = sample_bytes_out * sample_dimension_out * sample_number_out; to.tot = byte_shift_out; to.num = sample_number_out;//! ssi_size_t sample_number_in = frame_size + delta_size; from.num = sample_number_in; ssi_size_t byte_shift_in = sample_byte_in * sample_dimension_in * frame_size; from.tot = byte_shift_in; ssi_time_t from_secs = 0; ssi_time_t to_secs = 0; if (call_enter) { for(ssi_size_t k = 1; k < n_froms; k++){ xtra_stream_in[k-1].ptr = 0; xtra_stream_in[k-1].num = 0; xtra_stream_in[k-1].tot = 0; } transformer.transform_enter (from, to, xtra_stream_in_num, xtra_stream_in); } for (ssi_size_t i = 0; i < max_shift; i++) { ITransformer::info tinfo; tinfo.delta_num = delta_size; tinfo.frame_num = frame_size; tinfo.time = 0; from_secs = (i * frame_size) / from.sr; to_secs = from_secs + (frame_size + delta_size)/from.sr; ssi_size_t from_num, n_num; for(ssi_size_t k = 1; k < n_froms; k++){ from_num = (ssi_size_t)(from_secs * xtra_stream_in[k-1].sr + 0.5); n_num = (ssi_size_t)((to_secs - from_secs) * xtra_stream_in[k-1].sr + 0.5); xtra_stream_in[k-1].ptr = from_ptrs[k] + from_num * xtra_stream_in[k-1].dim * xtra_stream_in[k-1].byte; xtra_stream_in[k-1].num = n_num; xtra_stream_in[k-1].tot = n_num * xtra_stream_in[k-1].dim * xtra_stream_in[k-1].byte; SSI_ASSERT( (from_num + n_num) <= xtra_stream_in[k-1].num_real); } transformer.transform (tinfo, from, to, xtra_stream_in_num, xtra_stream_in); tinfo.time += frame_size / sample_rate_in; from.ptr += byte_shift_in; to.ptr += byte_shift_out; } if (call_flush) { from_secs = (0 * frame_size) / from.sr; to_secs = from_secs + (frame_size + delta_size)/from.sr; for(ssi_size_t k = 1; k < n_froms; k++){ xtra_stream_in[k-1].ptr = 0; xtra_stream_in[k-1].num = 0; xtra_stream_in[k-1].tot = 0; } transformer.transform_flush (from, to, xtra_stream_in_num, xtra_stream_in); } from.ptr = from_ptrs[0]; from.num = (ssi_size_t)from_nums[0]; from.tot = from_tots[0]; for(ssi_size_t i = 1; i < n_froms; i++){ xtra_stream_in[i-1].ptr = from_ptrs[i]; xtra_stream_in[i-1].num = (ssi_size_t)from_nums[i]; xtra_stream_in[i-1].tot = from_tots[i]; } to.ptr = to_ptr; to.num = to_num; to.tot = to_tot; delete [] from_nums; from_nums = 0; delete [] from_tots; from_tots = 0; delete [] from_ptrs; from_ptrs = 0; } }
ssi_sample_t *FileSamplesIn::next () { if (!_file_data || !_file_info) { ssi_wrn ("files not open"); return 0; } if (_sample_count++ >= _n_samples) { return 0; } switch (_version) { case File::V2: { switch (_file_data->getType ()) { case File::ASCII: { if (!_file_data->readLine (SSI_MAX_CHAR, _string)) { ssi_wrn ("could not read <user_id> <class_id> <time> <prob> <num>"); return 0; } sscanf (_string, "%u %u %lf %f %u", &_sample.user_id, &_sample.class_id, &_sample.time, &_sample.prob, &_sample.num); break; } case File::BINARY: { if (!_file_data->read (&_sample.user_id, sizeof (_sample.user_id), 1)) { ssi_wrn ("could not read <user_id>"); return 0; } if (!_file_data->read (&_sample.class_id, sizeof (_sample.class_id), 1)) { ssi_wrn ("could not read <class_id>"); return 0; } if (!_file_data->read (&_sample.time, sizeof (_sample.time), 1)) { ssi_wrn ("could not read <time>"); return 0; } if (!_file_data->read (&_sample.prob, sizeof (_sample.prob), 1)) { ssi_wrn ("could not read <prob>"); return 0; } if (!_file_data->read (&_sample.num, sizeof (_sample.num), 1)) { ssi_wrn ("could not read <num>"); return 0; } break; } } for (ssi_size_t i = 0; i < _sample.num; i++) { ssi_size_t num = 0; switch (_file_data->getType ()) { case File::ASCII: { if (!_file_data->readLine (SSI_MAX_CHAR, _string)) { ssi_wrn ("could not read <num>"); return 0; } sscanf (_string, "%u", &num); break; } case File::BINARY: { if (!_file_data->read (&num, sizeof (num), 1)) { ssi_wrn ("could not read <num>"); return 0; } break; } } if (num > 0) { ssi_stream_adjust (*_sample.streams[i], num); _file_data->setType (_sample.streams[i]->type); if (num > 0) { if (!_file_data->read (_sample.streams[i]->ptr, _sample.streams[i]->byte, num * _sample.streams[i]->dim)) { ssi_wrn ("could not read <data>"); return 0; } } } else { _sample.streams[i]->num = 0; } } break; } case File::V3: { switch (_file_data->getType ()) { case File::ASCII: { if (!_file_data->readLine (SSI_MAX_CHAR, _string)) { ssi_wrn ("could not read <user_id> <class_id> <prob> <time>"); return 0; } sscanf (_string, "%u %u %f %lf", &_sample.user_id, &_sample.class_id, &_sample.prob, &_sample.time); break; } case File::BINARY: { if (!_file_data->read (&_sample.user_id, sizeof (_sample.user_id), 1)) { ssi_wrn ("could not read <user_id>"); return 0; } if (!_file_data->read (&_sample.class_id, sizeof (_sample.class_id), 1)) { ssi_wrn ("could not read <class_id>"); return 0; } if (!_file_data->read (&_sample.prob, sizeof (_sample.prob), 1)) { ssi_wrn ("could not read <prob>"); return 0; } if (!_file_data->read (&_sample.time, sizeof (_sample.time), 1)) { ssi_wrn ("could not read <time>"); return 0; } break; } } for (ssi_size_t i = 0; i < _sample.num; i++) { _file_streams[i].read (*_sample.streams[i], _sample_count-1); } break; } } return &_sample; }
void PaintData::paint(ICanvas *canvas, ssi_handle_t context, ssi_rect_t area) { Lock lock(_mutex); if (_stream.num == 0) { return; } _painter->begin(context, area); if (_stream_has_changed) { if (_stream.dim != _dim) { ssi_stream_destroy(_resample); ssi_stream_init(_resample, 0, _stream.dim, _stream.byte, _stream.type, _stream.sr, _stream.time); _dim = _stream.dim; delete[] _mins; delete[] _maxs; _mins = new ssi_real_t[_dim]; _maxs = new ssi_real_t[_dim]; } switch (_type) { case TYPE::SIGNAL: case TYPE::AUDIO: case TYPE::IMAGE: { ssi_stream_adjust(_resample, ssi_cast(ssi_size_t, area.width)); ssi_resample(_stream.num, _resample.num, _stream.dim, ssi_pcast(ssi_real_t, _stream.ptr), ssi_pcast(ssi_real_t, _resample.ptr), _type == TYPE::AUDIO ? ssi_max : ssi_mean); break; } case TYPE::PATH: case TYPE::SCATTER: { ssi_stream_adjust(_resample, _stream.num); memcpy(_resample.ptr, _stream.ptr, _stream.tot); break; } } if (!_fix_limits) { if (_reset_limits) { ssi_minmax(_resample.num, _resample.dim, ssi_pcast(ssi_real_t, _resample.ptr), _mins, _maxs); _reset_limits = false; } else { ssi_real_t *mins = new ssi_real_t[_dim]; ssi_real_t *maxs = new ssi_real_t[_dim]; ssi_minmax(_resample.num, _resample.dim, ssi_pcast(ssi_real_t, _resample.ptr), mins, maxs); for (ssi_size_t i = 0; i < _dim; i++) { _mins[i] = min(_mins[i], mins[i]); _maxs[i] = max(_maxs[i], maxs[i]); } delete[] mins; delete[] maxs; } if (_type == TYPE::AUDIO) { for (ssi_size_t i = 0; i < _dim; i++) { _maxs[i] = abs(_maxs[i]); _mins[i] = -_maxs[i]; } } } else { for (ssi_size_t i = 0; i < _dim; i++) { _mins[i] = _fix_min; _maxs[i] = _fix_max; } } ssi_norm(_resample.num, _resample.dim, ssi_pcast(ssi_real_t, _resample.ptr), _mins, _maxs); _stream_has_changed = false; } if (_back_toggle) { _painter->fill(*_back_brush, area); } switch (_type) { case TYPE::SIGNAL: GraphicTools::PaintAsSignal(_resample, *_painter, *_pen); break; case TYPE::AUDIO: GraphicTools::PaintAsAudio(_resample, *_painter, *_pen); break; case TYPE::PATH: GraphicTools::PaintAsPath(_resample, _indx, _indy, *_painter, *_pen); break; case TYPE::SCATTER: GraphicTools::PaintAsScatter(_resample, _indx, _indy, *_painter, *_pen, *_brush, _point_size); break; case TYPE::IMAGE: GraphicTools::PaintAsImage(_resample, *_painter, *_colormap); break; } switch (_type) { case TYPE::SIGNAL: case TYPE::AUDIO: GraphicTools::PaintAxis(_stream.dim, _stream.time, _stream.time + (_stream.sr > 0 ? _stream.num / _stream.sr : 0), _mins, _maxs, *_painter, *_font, *_font_pen, *_font_brush, _precision); break; case TYPE::PATH: case TYPE::SCATTER: case TYPE::IMAGE: ssi_real_t minval, maxval, tmp; ssi_minmax(_dim, 1, _mins, &minval, &tmp); ssi_minmax(_dim, 1, _maxs, &tmp, &maxval); GraphicTools::PaintAxis(1, _stream.time, _stream.time + (_stream.sr > 0 ? _stream.num / _stream.sr : 0), &minval, &maxval, *_painter, *_font, *_font_pen, *_font_brush, _precision); break; } _painter->end(); }