# Function to clamp note values

**URL:** <https://uzu.lurk.org/t/function-to-clamp-note-values/1280>\
**Category:** TidalCycles\
**Tags:** innards\
**Created:** [31 May 2020 13:20 UTC](https://uzu.lurk.org/t/function-to-clamp-note-values/1280 "2020-05-31T13:20:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crashingbooth](https://uzu.lurk.org/user_avatar/uzu.lurk.org/crashingbooth/32/1619_2.png) [@crashingbooth](https://uzu.lurk.org/u/crashingbooth)\
**Post date:** [31 May 2020 13:20 UTC](https://uzu.lurk.org/t/function-to-clamp-note-values/1280/1 "2020-05-31T13:20:41Z")

</div>

@eris mentions in [this post](https://club.tidalcycles.org/t/week-5-lesson-3-superdirt-synths-part-2/1193/5?u=crashingbooth) using a helper function to clamp note values to within some octave range. This sounded like a really useful idea, and I thought I'd give it a try.

What I imagine would be something like this - It would take a note pattern, say  
`pt = n (scale "dorian" (run 16))`

and a function like  
`clamp numOct = (|% (numOct * 12))`

to constrain its values to `numOct` octaves. But this doesn't work because the `|%` operator expects real numbers. How would I get the output of a note pattern (using floats) to act as valid input for `|%`?

And obviously it would be much cooler if this function were patternable, i.e., could do things like  
`clamp "<1 2>" $ n (scale "dorian" (run 16))`

How much extra leg work would be involved with that?

---

<div class="post-metadata">

**Author:** ![eris](https://uzu.lurk.org/user_avatar/uzu.lurk.org/eris/32/233_2.png) [@eris](https://uzu.lurk.org/u/eris)\
**Post date:** [31 May 2020 18:49 UTC](https://uzu.lurk.org/t/function-to-clamp-note-values/1280/2 "2020-05-31T18:49:25Z")

</div>

Hey @crashingbooth!

The old version I had of this used `withI` to be smart about this and only operate on `note` params, so it could be applied to complex ParamMaps/Patterns rather than just numeric patterns.

I'd really like to figure out how to use something like `contrast` or `fix` to make this possible again, but in the meanwhile here's a little something that will let you do this similarly to using `scale`.

```haskell
do
  let octceil by upper noteval = if noteval>upper then octceil by upper (noteval - by) else noteval
  let octfloor by lower noteval = if noteval<lower then octfloor by lower (noteval + by) else noteval
  let wramp lower upper p = ((octfloor 12 lower).(octceil 12 upper)) <$> p
  d1 $ s "pluck" + note (wramp 10 25 ("[7 0 3 0]*4" + ((* 7) <$> (slow 4 $ (run 8)))))

```

~~I've called it `invert` because what it does is much like the theoretical concept of "inversions" of chords, arpeggios, or other groups of pitches. I used to call it `clampoctaves`, which I think is a bit more intuitive but it's also somewhat verbose. Perhaps clamp is better. Unclear!~~

~~Invert is probably bad though now that I think of it, because it's doing more than that.~~

It's constraining pitches to a range by...wrapping them. Maybe `wramp` because it's wrapping _and_ clamping?

---

<div class="post-metadata">

**Author:** ![crashingbooth](https://uzu.lurk.org/user_avatar/uzu.lurk.org/crashingbooth/32/1619_2.png) [@crashingbooth](https://uzu.lurk.org/u/crashingbooth)\
**Post date:** [1 June 2020 01:12 UTC](https://uzu.lurk.org/t/function-to-clamp-note-values/1280/3 "2020-06-01T01:12:04Z")

</div>

Thanks @eris!

I like that this is more general than what I had in mind. And recursively adding or subtracting sidesteps the type issue.

I've added it as

```haskell
wramp lower upper p = ((octfloor 12 lower).(octceil 12 upper)) <$> p where
    octceil by upper noteval = if noteval>upper then octceil by upper (noteval - by) else noteval
    octfloor by lower noteval = if noteval<lower then octfloor by lower (noteval + by) else noteval

```

to my BootTidal.hs

And I get the struggle to make names that are both accurate and easy to type. I've never had to worry about that before in languages that I wouldn't be live coding in. I generally go for overly verbose (and hopefully clear) names in other situations.
