# Polyrhythm Trainer (#194)

**URL:** <https://www.ruby-forum.com/t/polyrhythm-trainer-194/161597>\
**Category:** Ruby\
**Created:** [February 27, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/polyrhythm-trainer-194/161597 "2009-02-27T18:33:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [February 27, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/polyrhythm-trainer-194/161597/1 "2009-02-27T18:33:10Z")

</div>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q.:

1. Please do not post any solutions or spoiler discussion for this  
quiz until 48 hours have elapsed from the time this message was  
sent.

2. Support Ruby Q. by submitting ideas and responses  
as often as you can! Visit: [http://rubyquiz.strd6.com](http://rubyquiz.strd6.com)

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem  
helps everyone on Ruby T. follow the discussion. Please reply to  
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Polyrhythm Trainer (#194)

Greetings fellow Rubyists,

This week’s quiz is to create a polyrhythm training program. The user  
will be prompted to tap out a rhythm with two keys: left and right.  
Each time the user presses a key the program will display how close to  
the correct timing the key press occurred. The program will be  
configurable to train the user in any polyrhythm 2:3, 5:4, 11:17, and  
so on. The program will also have a configurable tempo. After a number  
of complete rhythms the program will display the ratio of how many  
notes were successfully struck, success determined by being within a  
threshold around the perfect timing.

The display can be as simple as a console application or use a full  
graphical interface. [Ruby Inside](http://www.rubyinside.com/ruby-gui-programming-survey-results-1552.html) recently had an article about  
graphical toolkits if you are interested.

[Polyrhythm](http://en.wikipedia.org/wiki/Polyrhythm) is the simultaneous sounding of two or more independent  
rhythms.

Have Fun!

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 7, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/polyrhythm-trainer-194/161597/2 "2009-03-07T20:55:19Z")

</div>

This quiz may be worthy of a revisit in the future. Until then, here  
is my solution: the beginnings of an “Annoyance” trainer.

```
# rhythm.rb

class Rhythm
  BPM = 20
  SECONDS_PER_MINUTE = 60.0

  def initialize(n, sym)
    @sym = sym
    @seconds_per_hit = SECONDS_PER_MINUTE/(BPM*n)
    @last_hit = @seconds_per_hit
    @hit_count = 0
    @time = 0

    puts @seconds_per_hit
  end

  def elapse(time_elapsed)
    hit = false
    if @last_hit >= @seconds_per_hit
      @hit_count += 1
      @last_hit -= @seconds_per_hit
      puts "#{"%1.3f" % @time}: #{@sym}"
      hit = true
    end
    @time += time_elapsed
    @last_hit += time_elapsed
    return hit
  end
end

s = '@'

rhythms = ARGV.map{|arg| Rhythm.new(arg.to_i, s = s.succ)}

delta = 0.05
time = 0

while true
  hit = rhythms.map{|r| r.elapse(delta)}.inject(false) {|hit, r| hit 

```

|| r}  
print “\a” if hit  
time += delta  
sleep delta  
end

The program will beep indefinitely, producing a sound every time the  
rhythm should be struck (to the nearest 20th of a second).

```
Usage: ruby rhythm.rb [rhythms]

EX: > ruby rhythm.rb 3 2
1.0
1.5
0.000: A
0.000: B
1.000: A
1.500: B
2.000: A
3.000: A
3.000: B
4.000: A
4.500: B
5.000: A

```

Crank up the volume on your PC speaker and have a blast near your  
co-workers!

[http://rubyquiz.strd6.com/quizzes/194](http://rubyquiz.strd6.com/quizzes/194)

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [March 8, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/polyrhythm-trainer-194/161597/3 "2009-03-08T22:52:31Z")

</div>

> configurable to train the user in any polyrhythm 2:3, 5:4, 11:17

11:17 is simply a modified 2:3, but I challenge anyone to do it with  
any degree of precision at high tempo and only 2 references (2 keys).  
If you can use your whole hand (as in playing a piano), it might be  
easier. Scriabin, for example, would frequently throw his poor  
pianist into 11:3, 9:4, etc. brief, but frequent, frenzies (see piano  
sonata no. 1 in F minor).

Todd
