Beispiel #1
0
void PlayerLoopCommand::execute(const Transport& transport) {
  Player * p = player();
  BeatBuffer * beat_buff = p->beat_buffer();
  AudioBuffer * audio = p->audio_buffer();
  if (!audio)
    return;

  if (p->looping() && mStartFrame < 0 && mEndFrame < 0) {
    switch (mResizePolicy) {
      case RESIZE_FROM_FRONT:
        mStartFrame = p->loop_start_frame();
        break;
      case RESIZE_FROM_BACK:
        mEndFrame = p->loop_end_frame();
        break;
      case RESIZE_AT_POSITION:
      default:
        //do nothing as this is the default when nothing is set
        break;
    }
  }

  int old_loop_frames = 0;
  if (p->looping())
    old_loop_frames = p->loop_end_frame() - p->loop_start_frame();
  const unsigned int beat = beat_buff ? beat_index(beat_buff, p->frame()) : 0;

  if (mEndFrame < 0) {
    if (!beat_buff)
      return;
    unsigned int beat_end = 0;
    if (mStartFrame < 0) {
      //find both start and end frame based on current location
      //we don't use the closest index, we use the last index before our frame so we stay in the current beat
      beat_end = beat + mBeats;
      if (beat_end >= beat_buff->size())
        return;

      mStartFrame = beat_buff->at(beat);
      if (beat == beat_end)
        mEndFrame = mStartFrame;
      else
        mEndFrame = beat_buff->at(beat_end);
    } else {
      beat_end = beat_index(beat_buff, mStartFrame) + mBeats;
      mEndFrame = beat_buff->at(beat_end);
    }

    //deal with fractional part
    double remainder = fmod(mBeats, 1.0);
    if (remainder != 0.0) {
      if (beat_end + 1 < beat_buff->size()) {
        double frames = beat_buff->at(beat_end + 1) - mEndFrame;
        mEndFrame += (frames * remainder);
      } else {
        //XXX what?
      }
    }

  } else if (mStartFrame < 0) {
    if (!beat_buff)
      return;
    unsigned int beat_end = beat_index(beat_buff, mEndFrame);
    if (beat_end < mBeats) {
      mStartFrame = 0;
    } else {
      int beat_start = beat_end - mBeats;
      mStartFrame = beat_buff->at(beat_start);
      if (beat_start >= 1) {
        //deal with fractional part
        double remainder = fmod(mBeats, 1.0);
        if (remainder != 0.0) {
          double frames = mStartFrame - beat_buff->at(beat_start - 1);
          mStartFrame -= (frames * remainder);
          if (mStartFrame < 0)
            mStartFrame = 0;
        }
      }
    }
  }

  if (mStartFrame >= mEndFrame)
    return; //XXX what to do?

  p->loop_start_frame(mStartFrame);
  p->loop_end_frame(mEndFrame);
  if (mStartLooping)
    p->loop(true);
  mLooping = p->looping();

  double loop_frames = static_cast<double>(mEndFrame - mStartFrame);
  //if we are past the end point, fold
  if (p->looping()) {
    if (p->frame() > mEndFrame) {
      int offset = round(loop_frames * floor(static_cast<double>(p->frame() - mStartFrame) / loop_frames));
      p->position_at_frame(p->frame() - offset);
    } else if (old_loop_frames > 0 && loop_frames > old_loop_frames) {
      //XXX untested, do we ever hit this?
      //if the loop grows we might need to offset to before the loop 
      if (p->syncing() && beat_buff && beat + 1 < beat_buff->size()) {
        double beat_frames = beat_buff->at(beat + 1) - beat_buff->at(beat);
        //this is where we should be if we weren't looping
        double target_offset_frames = transport.position().pos_in_beat() * beat_frames;
#if 1
        //XXX hard jump to position we should be at for now
        if (beat_frames > old_loop_frames)
          p->position_at_frame(beat_buff->at(beat) + target_offset_frames);
#else
        int loop_skip_back = target_offset_frames / static_cast<double>(old_loop_frames);
        int offset = loop_skip_back * old_loop_frames;
        p->position_at_frame(p->frame() - offset);
#endif
      }
    }
  }
}