+static double
+swing_position (double pos, double grid, double swing, double offset)
+{
+ /* beats start out numbered at zero.
+ *
+ * every other position on the start-quantize-grid is
+ * optionally swung, meaning that its position is moved
+ * somewhere between its natural position and 2/3 of
+ * the way to the next start-quantize-grid position.
+ *
+ * so, if the _start grid is 0.5, the beat at 0 isn't
+ * swung, but something at 0.5 is, the beat at 1 isn't
+ * swung, but something at 1.5 is.
+ *
+ * if the start grid is 1.0, the beat at 0 isn't swung,
+ * but the beat at 1.0 is. the beat at 2.0 isn't swung,
+ * but the beat at 3.0 is. and so on.
+ *
+ * so the criterion for a position being swung is
+ * whether or not ((possible_grid_position / grid) % 2) != 0
+ */
+
+ const bool swing_quantize_grid_position = pos > 0.0 && fmod ((pos/grid), 2.0) != 0;
+ const bool swing_previous_grid_position = pos > grid && fmod ((pos-grid)/grid, 2.0) != 0;
+
+ /* one of these will not be subject to swing */
+
+ double swung_pos = pos;
+ double swung_previous_grid_position;
+
+ if (pos > grid) {
+ swung_previous_grid_position = pos - grid;
+ } else {
+ swung_previous_grid_position = 0.0;
+ }
+
+ if (swing_previous_grid_position) {
+ swung_previous_grid_position = swung_previous_grid_position + (2.0/3.0 * swing * grid);
+ }
+
+ if (swing_quantize_grid_position) {
+ swung_pos = swung_pos + (2.0/3.0 * swing * grid);
+ }
+
+ /* now correct for start-of-model offset */
+
+ pos += offset;
+
+ if (fabs (pos - swung_pos) > fabs (pos - swung_previous_grid_position)) {
+ pos = swung_previous_grid_position;
+ } else {
+ pos = swung_pos;
+ }
+
+ return pos;
+}
+