+ delete[] lsamples;
+ delete[] asamples;
+ }
+}
+
+void SignalBase::convert_single_segment(AnalogSegment *asegment, LogicSegment *lsegment)
+{
+ uint64_t start_sample, end_sample, old_end_sample;
+ start_sample = end_sample = 0;
+ bool complete_state, old_complete_state;
+
+ start_sample = lsegment->get_sample_count();
+ end_sample = asegment->get_sample_count();
+ complete_state = asegment->is_complete();
+
+ // Don't do anything if the segment is still being filled and the sample count is too small
+ if ((!complete_state) && (end_sample - start_sample < ConversionBlockSize))
+ return;
+
+ do {
+ convert_single_segment_range(asegment, lsegment, start_sample, end_sample);
+
+ old_end_sample = end_sample;
+ old_complete_state = complete_state;
+
+ start_sample = lsegment->get_sample_count();
+ end_sample = asegment->get_sample_count();
+ complete_state = asegment->is_complete();
+
+ // If the segment has been incomplete when we were called and has been
+ // completed in the meanwhile, we convert the remaining samples as well.
+ // Also, if a sufficient number of samples was added in the meanwhile,
+ // we do another round of sample conversion.
+ } while ((complete_state != old_complete_state) ||
+ (end_sample - old_end_sample >= ConversionBlockSize));
+}
+
+void SignalBase::conversion_thread_proc()
+{
+ shared_ptr<Analog> analog_data;
+
+ if (conversion_is_a2l()) {
+ analog_data = dynamic_pointer_cast<Analog>(data_);
+
+ if (analog_data->analog_segments().size() == 0) {
+ unique_lock<mutex> input_lock(conversion_input_mutex_);
+ conversion_input_cond_.wait(input_lock);
+ }
+
+ } else
+ // Currently, we only handle A2L conversions
+ return;
+
+ // If we had to wait for input data, we may have been notified to terminate
+ if (conversion_interrupt_)
+ return;
+
+ uint32_t segment_id = 0;
+
+ AnalogSegment *asegment = analog_data->analog_segments().front().get();
+ assert(asegment);
+
+ const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(converted_data_);
+ assert(logic_data);
+
+ // Create the initial logic data segment if needed
+ if (logic_data->logic_segments().size() == 0) {
+ shared_ptr<LogicSegment> new_segment =
+ make_shared<LogicSegment>(*logic_data.get(), 0, 1, asegment->samplerate());
+ logic_data->push_segment(new_segment);
+ }
+
+ LogicSegment *lsegment = logic_data->logic_segments().front().get();
+ assert(lsegment);
+
+ do {
+ convert_single_segment(asegment, lsegment);
+
+ // Only advance to next segment if the current input segment is complete
+ if (asegment->is_complete() &&
+ analog_data->analog_segments().size() > logic_data->logic_segments().size()) {
+ // There are more segments to process
+ segment_id++;
+
+ try {
+ asegment = analog_data->analog_segments().at(segment_id).get();
+ } catch (out_of_range&) {
+ qDebug() << "Conversion error for" << name() << ": no analog segment" \
+ << segment_id << ", segments size is" << analog_data->analog_segments().size();
+ return;