# Transposing a play pattern to a key

**URL:** <https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497>\
**Category:** Support, Help & Resources\
**Created:** [February 5, 2022, 5:13am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497 "2022-02-05T05:13:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spicy](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@Spicy](https://in-thread.sonic-pi.net/u/Spicy)\
**Post date:** [February 5, 2022, 5:13am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/1 "2022-02-05T05:13:14Z")

</div>

Suppose I have a play pattern [:a, :b, :c3, d, etc.] and I want to transpose it to a key, say Bb. Thus, I need to lower the notes : [b, e, a, b2, ,e2,a2, b3, e3, a3]  
I’m trying to use a conditional statement with use\_transpose:

in\_thread do  
if note(:b, :e, :a, :b3, :b2, :e3, :e2, :a3, :a2)  
use\_transpose -1  
end  
play\_pattern\_timed [:c2, :c3, :b3, :f3, :gb3, :c2, :b2, :b2],[0.5, 0.25, 0.125, 0.125, 0.25, 0.25, 0.25, 0.25]  
sleep 1  
end

The problem is that the code ends up transposing the entire pattern down a semitone (-1)  
instead of just the necessary notes for the key of Bb (b,e,a) . How can i make this happen? Thanks

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [February 5, 2022, 8:47am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/2 "2022-02-05T08:47:59Z")

</div>

hi,

ouch great question. I guess you will have to handle the degrees in the scale more than the real notes.  
maybe this [Using degree and scale\_degree](https://in-thread.sonic-pi.net/t/using-degree-and-scale-degree/912) can help you in your research

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [February 5, 2022, 4:02pm UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/3 "2022-02-05T16:02:21Z")

</div>

Exactly to @nlb’s approach and the linked resource. In other words, you’ll have to abstract your notes from the scale, which may be a little cumbersome. For now however, here’s how I might approach just the problem of transposing a pattern:

```ruby
set "bottom_note", :c2
set "mode", :ionian

# with parallel arrays
degrees = [1, 8, 14, 11, "d12", 1, 7, 7]
times = [0.5, 0.25, 0.125, 0.125, 0.25, 0.25, 0.25, 0.25]

for n in 0...degrees.length
  play (degree degrees[n], get["bottom_note"], get["mode"]), release: times[n]
  sleep times[n]
end
sleep 1

set "mode", :aeolian

# with an array of structures
notes = [[1, 0.5], [8, 0.25], [14, 0.125], [11, 0.125], ["d12", 0.25], [1, 0.25], [7, 0.25], [7, 0.25] ]

for n in 0...notes.length
  play (degree notes[n][0], get["bottom_note"], get["mode"]), release: notes[n][1]
  sleep notes[n][1]
end
sleep 1

```

---

<div class="post-metadata">

**Author:** ![thatWichBenefits](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/thatwichbenefits/32/1962_2.png) [@thatWichBenefits](https://in-thread.sonic-pi.net/u/thatWichBenefits)\
**Post date:** [February 7, 2022, 7:29am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/4 "2022-02-07T07:29:18Z")

</div>

Hi. So I am interested in doing similar manipulations with tones without manually writing the tones. I am new to sonic pi. Is this pure ruby? What I mean is can I study a ruby book and learn like functions that manipulate lists and other functions and then I could use that for sonic pi? Thanks

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [February 7, 2022, 7:45am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/5 "2022-02-07T07:45:10Z")

</div>

Hello @thatWichBenefits 🙂

While Sonic Pi is written on top of Ruby, not all features from the base language are available. (The decision was made to leave out official support for most Object Oriented scripting for example, as well as the use of Ruby gems). Such things _may_ be usable in some form, but they are unsupported, and may stop working at any point in the future, as Sonic Pi evolves.

Many of us have indeed used parts of plain Ruby in our compositions. You’re welcome to research into Ruby a bit more - as long as you understand that successful use of any complex Ruby is not always guaranteed!

Having said that, simple things such as operations on lists and basic math operations are often quite likely to work. For your particular use case, I’m sure there’s a suitable solution - I and many others I’m sure would be happy to give advice and assistance to help you try to achieve your goals 🙂

---

<div class="post-metadata">

**Author:** ![thatWichBenefits](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/thatwichbenefits/32/1962_2.png) [@thatWichBenefits](https://in-thread.sonic-pi.net/u/thatWichBenefits)\
**Post date:** [February 7, 2022, 8:10am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/6 "2022-02-07T08:10:28Z")

</div>

Hey thanks 🙂, yes I am interested in passing functions around, like if I have a list of tones, and then I apply a function, can I then apply another function on top of it and so on. I come from a haskell background and have played with tidalcycles, but I so like the sonic-pi environment and would like to teach music also with it because its all so intuitive, so I keep coming back to it.

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [February 7, 2022, 8:15am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/7 "2022-02-07T08:15:29Z")

</div>

Sure. If we’re talking about passing methods to other methods as parameters, then Ruby methods are unfortunately not [first class functions](https://en.wikipedia.org/wiki/First-class_function), but there may be other ways to do what you want (using Procs rather than methods). If you are interested in going down that route, feel free to open up a new topic 👍

---

<div class="post-metadata">

**Author:** ![shemp](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shemp](https://in-thread.sonic-pi.net/u/shemp)\
**Post date:** [February 11, 2022, 11:38am UTC](https://in-thread.sonic-pi.net/t/transposing-a-play-pattern-to-a-key/6497/8 "2022-02-11T11:38:20Z")

</div>

A pattern, or MIDI notes, are not actually in a key. Some music is not tonal in that sense at all. That is why your `use_transpose` just shifts all the notes. BTW, B-flat major only has two flats. You can in fact automatically generate all those scales using Sonic Pi, though, so for a very simple example you could type

```ruby
thescale = scale :Bb, :major
thenotes = [1, 1, 1, 5, 1, 1, 1, 3].map { |d| thescale[d-1] }
play_pattern_timed thenotes, [0.25]*3 + [1]

```

Obviously you could do nore sophisticated calculations as necessary, along the lines of the examples given previously. There is also a “ziffers” module with macros for that sort of thing.
