# Transpose a measure in an array

**URL:** <https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815>\
**Category:** Support, Help & Resources\
**Created:** [December 18, 2020, 2:16am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815 "2020-12-18T02:16:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinodv](https://avatars.discourse-cdn.com/v4/letter/v/97f17d/32.png) [@vinodv](https://in-thread.sonic-pi.net/u/vinodv)\
**Post date:** [December 18, 2020, 2:16am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/1 "2020-12-18T02:16:39Z")

</div>

n = [:d, :g,:g,:a,:g,:f, :e,:e,:e, :g,:g,:a,:g,:f] #notes  
d = [1, 1,0.5,0.5,0.5,0.5, 1,1,1, 1,0.5,0.5,0.5,0.5] #duration

now I want to transpose this part [:e,:e,:e] to [:f,:f:f] inside the array n.

This is what I mean:  
n = [:d, :g,:g,:a,:g,:f, :e,:e,:e, :g,:g,:a,:g,:f , (:e,:e,:e) + 1] #notes  
d = [1, 1,0.5,0.5,0.5,0.5, 1,1,1, 1,0.5,0.5,0.5,0.5, 1,1,1] #duration

What is the correct syntax for this.

---

<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:** [December 18, 2020, 2:56am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/2 "2020-12-18T02:56:19Z")

</div>

Hi @vinodv,

I’m not quite sure what you’re trying to do here still.  
In what situation are you trying to use this code?

- Is it just a static composition, where you just hit run and leave it at that? (eg you are _not_ trying to manipulate the code on the fly once it is running?)
- What other kind of code do you plan on using to play this sequence of notes and note lengths? does this ‘other code’ need to also _manipulate_ the pattern, or just play it exactly as is?

(Ie, I guess what I’m also asking is - what is it about your code that you might need something more than just this):

```ruby
n = [:d, :g,:g,:a,:g,:f, :e,:e,:e, :g,:g,:a,:g,:f, :f, :f, :f] #notes
d = [1, 1,0.5,0.5,0.5,0.5, 1,1,1, 1,0.5,0.5,0.5,0.5, 1,1,1] #duration

```

?

---

<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:** [December 18, 2020, 6:13am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/3 "2020-12-18T06:13:21Z")

</div>

> [@vinodv](#):
>
> What is the correct syntax for this.

You can test for the note is a E and the add 1 before playing it. But it is quite not clear what is your goals 😀

---

<div class="post-metadata">

**Author:** ![vinodv](https://avatars.discourse-cdn.com/v4/letter/v/97f17d/32.png) [@vinodv](https://in-thread.sonic-pi.net/u/vinodv)\
**Post date:** [December 18, 2020, 7:07am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/4 "2020-12-18T07:07:46Z")

</div>

Suppose that some four continuous measures of a melody is identical except that they are transpose to each other. I need not type the second set of continuous measures again right. This is my goal.

---

<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:** [December 18, 2020, 7:43am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/5 "2020-12-18T07:43:29Z")

</div>

One of the simplest ways might be to use one of the built-in commands that do what you are interested in: `use_transpose`/`with_transpose`.

- `use_transpose X` tells Sonic Pi that all following code in the same thread/live\_loop is transposed by X amount.
- `with_transpose X do... end` tells Sonic Pi that all code inside the with\_transpose block is transposed by X amount.

(There are examples in the app Help window (in the Lang section) for those functions) 🙂

---

<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:** [December 18, 2020, 8:15am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/6 "2020-12-18T08:15:58Z")

</div>

Another alternative: if you store the notes and durations as `ring`s, once you have split the notes and durations into groups (the bits that repeat/are the same, and the bits that don’t repeat/aren’t the same) - you can just do things like add rings together, and use the ring helper functions (such as `.repeat`) to chain other bits and pieces of the melody together. The helper functions are useful for exactly that, creating new rings from existing ones.  
See:

> **[Sonic Pi - Tutorial](https://sonic-pi.net/tutorial#section-8-4)**

> **[Sonic Pi - Tutorial](https://sonic-pi.net/tutorial#section-8-5)**

---

<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:** [December 18, 2020, 8:53am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/7 "2020-12-18T08:53:36Z")

</div>

@vinodv try this

```ruby
use_synth :piano
use_bpm 120

live_loop :foo do

  use_transpose [0, 12, 0, 5].tick("transpose")

##| x = (ring 0,1,2,3,4,5,6,7,8,9,10,11,12).tick("transpose") # if you want to stay on ring
##| use_transpose x.to_i # need to be an integer

  sample :drum_bass_hard
  n = ( ring :d, :g,:g,:a,:g,:f, :e,:e,:e, :g,:g,:a,:g,:f, :f, :f, :f) #notes
  d = ( ring 1, 1,0.5,0.5,0.5,0.5, 1,1,1, 1,0.5,0.5,0.5,0.5, 1,1,1) #duration
  
  n = ( ring :c, :g, :a) #notes
  d = ( ring 2, 1, 1) #duration
  play_pattern_timed n, d
  
end

```

So first play, 0 transpose, next +12 so one octave upper , next 0, next 5.

Of course you have to comment my n and d lines to recover your melody

and tell us if it’s ok for you.

Edit : by the way what melody do you code ? is it a well known christmas tune ? sounds like “happy new year” but a bit weird 🙂 Tell us !

---

<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:** [December 18, 2020, 9:15am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/8 "2020-12-18T09:15:03Z")

</div>

Given that it appears that vinodv wants to recreate the ‘we wish you a merry Christmas’ melody, there’d probably be more to the melody than just what they have printed above, so no doubt if using with/use\_transpose there could be a bit more to it than just using them once for the entire loop 🙂

---

<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:** [December 18, 2020, 9:21am UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/9 "2020-12-18T09:21:46Z")

</div>

Btw,

> [@nlb](#):
>
> `##| use_transpose x.to_i # need to be an integer`

I think if you make the ring that `x` comes from a ring of integers, then `x` will already be an integer?

---

<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:** [December 18, 2020, 1:47pm UTC](https://in-thread.sonic-pi.net/t/transpose-a-measure-in-an-array/4815/10 "2020-12-18T13:47:02Z")

</div>

Oupss yep no need for `to_i` here 🙂
