Esempio n. 1
0
  @Override
  protected void uGenerate(float[] channels) {
    // start with our base amplitude
    float outAmp = amplitude.getLastValue();

    // temporary step location with phase offset.
    float tmpStep = step + phase.getLastValue();
    // don't be less than zero
    if (tmpStep < 0.f) {
      tmpStep -= (int) tmpStep - 1f;
    }
    // don't exceed 1.
    // we don't use Math.floor because that involves casting up
    // to a double and then back to a float.
    if (tmpStep > 1.0f) {
      tmpStep -= (int) tmpStep;
    }

    // calculate the sample value
    float sample = outAmp * wave.value(tmpStep) + offset.getLastValue();

    Arrays.fill(channels, sample);

    // update our step size.
    // this will check to make sure the frequency has changed.
    updateStepSize();

    // increase time
    // NOT THIS FROM BEFORE: step += stepSize + fPhase;
    step += stepSize;

    // don't be less than zero
    if (step < 0.f) {
      step -= (int) step - 1f;
    }

    // don't exceed 1.
    // we don't use Math.floor because that involves casting up
    // to a double and then back to a float.
    if (step > 1.0f) {
      step -= (int) step;
    }
  }
  /**
   * Main processing thread
   *
   * <p>General functionality: This function is running as a separate thread from the component's
   * main thread.
   *
   * @generated
   */
  public void run() {
    // begin-user-code
    double[] data = new double[this.xfer_len.getValue()];
    double phase = 0;
    double chirp = 0;
    double sample_time_delta;
    double delta_phase;
    double delta_phase_offset;

    sri = new StreamSRI();
    sri.hversion = 1;
    sri.mode = 0;
    sri.xdelta = 0.0;
    sri.ydelta = 1.0;
    sri.subsize = 0;
    sri.xunits = 1; // TIME_S
    sri.streamID = (this.stream_id.getValue() != null) ? this.stream_id.getValue() : "";
    sriUpdate = true;
    // end-user-code

    while (this.started()) {
      // begin-user-code
      try {
        /// If the transfer length has changed, reallocate the buffer
        if (this.xfer_len.getValue() != data.length) {
          data = new double[this.xfer_len.getValue()];
          sriUpdate = true;
        }

        sample_time_delta = (1.0 / this.sample_rate.getValue());
        if (sample_time_delta != sri.xdelta) {
          sri.xdelta = sample_time_delta;
          sriUpdate = true;
        }

        if (sriUpdate || (!this.port_out.hasSri(sri.streamID))) {
          this.port_out.pushSRI(sri);
        }
        sriUpdate = false;

        delta_phase = this.frequency.getValue() * sample_time_delta;
        delta_phase_offset = chirp * sample_time_delta * sample_time_delta;
        if ((delta_phase < 0) && (!this.shape.getValue().equals("sine"))) {
          delta_phase = -delta_phase;
        }

        // Generate the Waveform
        if (this.shape.getValue().equals("sine")) {
          Waveform.sincos(
              data, this.magnitude.getValue(), phase, delta_phase, this.xfer_len.getValue(), 1);
        } else if (this.shape.getValue().equals("square")) {
          Waveform.square(
              data, this.magnitude.getValue(), phase, delta_phase, this.xfer_len.getValue(), 1);
        } else if (this.shape.getValue().equals("triangle")) {
          Waveform.triangle(
              data, this.magnitude.getValue(), phase, delta_phase, this.xfer_len.getValue(), 1);
        } else if (this.shape.getValue().equals("sawtooth")) {
          Waveform.sawtooth(
              data, this.magnitude.getValue(), phase, delta_phase, this.xfer_len.getValue(), 1);
        } else if (this.shape.getValue().equals("pulse")) {
          Waveform.pulse(
              data, this.magnitude.getValue(), phase, delta_phase, this.xfer_len.getValue(), 1);
        } else if (this.shape.getValue().equals("constant")) {
          Waveform.constant(data, this.magnitude.getValue(), this.xfer_len.getValue(), 1);
        } else if (this.shape.getValue().equals("whitenoise")) {
          Waveform.whitenoise(data, this.magnitude.getValue(), this.xfer_len.getValue(), 1);
        } else if (this.shape.getValue().equals("lrs")) {
          Waveform.lrs(data, this.magnitude.getValue(), this.xfer_len.getValue(), 1, 1);
        }

        phase += delta_phase * this.xfer_len.getValue(); // increment phase
        phase -= Math.floor(phase); // modulo 1.0

        // Create a CPU time-stamp
        long tmp_time = System.currentTimeMillis();
        double wsec = tmp_time / 1000;
        double fsec = (tmp_time % 1000) / 1000.;
        PrecisionUTCTime tstamp =
            new PrecisionUTCTime(BULKIO.TCM_CPU.value, (short) 1, (short) 0, wsec, fsec);

        // Push the data
        this.port_out.pushPacket(
            data,
            tstamp,
            false,
            (this.stream_id.getValue() != null) ? this.stream_id.getValue() : "");

        // If we are throttling, wait...otherwise run at full speed
        if (this.throttle.getValue() == true) {
          long wait_amt = (long) (this.xfer_len.getValue() * sample_time_delta * 1000);
          try {
            Thread.sleep(wait_amt);
          } catch (InterruptedException e) {
            break;
          }
        }
      } catch (Throwable t) {
        logger.error("Error in processing loop", t);
      }
      // end-user-code
    }

    // begin-user-code
    // end-user-code
  }