# Precise timing issues with ruby

**URL:** <https://www.ruby-forum.com/t/precise-timing-issues-with-ruby/125446>\
**Category:** Ruby\
**Created:** [January 9, 2008, 6:43am UTC](https://www.ruby-forum.com/t/precise-timing-issues-with-ruby/125446 "2008-01-09T06:43:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 9, 2008, 6:43am UTC](https://www.ruby-forum.com/t/precise-timing-issues-with-ruby/125446/1 "2008-01-09T06:43:29Z")

</div>

I’ve got some code which needs to operate in a time-sensitive way. It  
makes a simple drumbeat, and needs to hit the notes at the right time.  
Currently this doesn’t happen. Every time you run the code you’ll get  
slightly different timing fluctuations. It’s not subtle, either. The  
effect is obvious and ruins the beat.

Is there a way to solve this problem without abandoning my preferred  
language?

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 9, 2008, 6:48am UTC](https://www.ruby-forum.com/t/precise-timing-issues-with-ruby/125446/2 "2008-01-09T06:48:45Z")

</div>

> Is there a way to solve this problem without abandoning my preferred language?

Never mind, sorry. False alarm! There was a problem in my code.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 9, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/precise-timing-issues-with-ruby/125446/3 "2008-01-09T16:31:01Z")

</div>

Giles B. wrote:

> I’ve got some code which needs to operate in a time-sensitive way. It  
> makes a simple drumbeat, and needs to hit the notes at the right time.  
> Currently this doesn’t happen. Every time you run the code you’ll get  
> slightly different timing fluctuations. It’s not subtle, either. The  
> effect is obvious and ruins the beat.
> 
> Is there a way to solve this problem without abandoning my preferred language?

That I think is platform-dependent, but it could be something as  
“simple” as building an interface to an audio library. I only know  
Linux, so that’s the approach I’d take. There are at least three Linux  
distros – JAD, Studio64 and dyne:bolic – dedicated to real-time audio  
processing. I haven’t done anything with dyne:bolic, but I have used  
both JAD and Studio64.

I can’t really tell which of the two is “better” – both of them seemed  
to be excellent, and neither of them had anything I couldn’t get on  
Gentoo. That may change if I don’t get CSound running on Gentoo soon,  
however. 🙂

Another place you might want to look is Planet CCRMA. This is a  
Fedora-based collection of audio software, some of it coming from the  
Lisp-based CCRMA project at Stanford.

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [January 9, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/precise-timing-issues-with-ruby/125446/4 "2008-01-09T17:42:10Z")

</div>

> > I’ve got some code which needs to operate in a time-sensitive way. It  
> > makes a simple drumbeat, and needs to hit the notes at the right time.  
> > Currently this doesn’t happen. Every time you run the code you’ll get  
> > slightly different timing fluctuations. It’s not subtle, either. The  
> > effect is obvious and ruins the beat.
> > 
> > Is there a way to solve this problem without abandoning my preferred language?
> 
> That I think is platform-dependent, but it could be something as  
> “simple” as building an interface to an audio library.

Actually, no; I’m getting the audio from third-party commercial music  
production software. I’m currently using Propellerhead Reason, which  
is at the consumer end of prosumer, and I may add in Ableton Live  
and/or Apple Logic, which are more at the pro end. There’s a standard  
protocol called MIDI which I’m using, but the timing my code was  
generating was completely unreliable. That was the only problem. But  
the error turned out to be in the code. It came from a book, the book  
wasn’t concerned with precise timing, just a proof of concept, so I  
basically just increased the resolution on the timer and I was good  
(which essentially meant just adding a couple zeros to a division  
statement).

Because I’m using a standard protocol and a Ruby interface to the OS X  
core library which handles this standard protocol, you could  
essentially use my code (merged with the code from the book, which is  
MIT license) with any MIDI sound source, including hardware  
synthesizers.

> Another place you might want to look is Planet CCRMA. This is a  
> Fedora-based collection of audio software, some of it coming from the  
> Lisp-based CCRMA project at Stanford.

I don’t have any Linux boxes right now , but that does sound quite  
interesting. SynthBuilder looks particularly nifty.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)
