sonic-pi icon indicating copy to clipboard operation
sonic-pi copied to clipboard

proposed ring chain: .invert(n)

Open datramt opened this issue 6 years ago • 8 comments

Hi!

There have been a handful of times in which I desired a ring chain that inverts the ring over a specified pitch axis. This would mainly work for rings that are intended as collections of pitches. Would there be interest in adding a feature like this? I have a prototype below, that I'm positive could be more thoughtfully implemented, but for what I typically need, it works well:

class SonicPi::Core::RingVector
  
  def invert(n)
    self.map {|e| n-e + self.min}
  end
  
end

puts (scale :c, :major).invert(60)

this will return: (ring 60, 58, 56, 55, 53, 51, 49, 48)

At any rate, thanks so much for this fantastic environment. Much <3

datramt avatar Jun 12 '19 04:06 datramt

This looks quite cool. I think there's a small bug in it though: should it be self.map {|e| 2*n-e}? Otherwise, as it is, you get things like: (ring 60, 61, 62, 60).invert(60) -> (ring 60, 59, 58, 60), but (ring 60, 61, 62, 58).invert(60) -> (ring 58, 57, 56, 60).

Also, I wonder if a different name might work better (maybe reflect?) as this one might get confused with chord inversions.

emlyn avatar Jun 23 '19 23:06 emlyn

Yeah, conceptually, your solution self.map {|e| 2*n-e} makes more sense according to the way I described it. I like it, thanks :) Regarding the name, ".reflect" is already in use (similar to .mirror), whereas invert is a term that describes this kind transformation in serialism. I agree that people might confuse it with chord invert though. hm.

datramt avatar Jun 24 '19 03:06 datramt

Oh yes, I remembered .mirror, but forgot there's also .reflect. In that case maybe .invert is best (at least I can't think of anything better).

emlyn avatar Jun 24 '19 19:06 emlyn

@xavriley any thoughts on this? :-)

samaaron avatar Jun 24 '19 19:06 samaaron

Looks good to me - in music theory "inversion" has a specific meaning when applied to counterpoint/melodies (sadly the same word is used for something different when talking about chords).

For reference:

  • https://en.wikipedia.org/wiki/Inversion_(music)#Melodies (counterpoint inversion)
  • https://en.wikipedia.org/wiki/Retrograde_(music) Retrograde - equivalent to .reverse in our case
  • https://en.wikipedia.org/wiki/Retrograde_inversion - equivalent to .invert(pitch_axis).reverse

I like the name .invert but it is confusing if people use rings for chords etc. Maybe .invert_around(pitch) instead?

For the implementation @emlyn's implementation looks good. We just need some good tests to make sure it all makes sense.

xavriley avatar Apr 15 '20 11:04 xavriley

@datramt @emlyn are either/both of you happy to continue with this idea? 🙂

ethancrawford avatar Oct 01 '21 05:10 ethancrawford

@datramt do you want to do this, as you came up with the idea? Otherwise I'm happy to put in a PR.

emlyn avatar Oct 01 '21 10:10 emlyn

I decided to give it a go. PR is in #2853

emlyn avatar Oct 04 '21 18:10 emlyn