Forum: Ruby sleep 0.2 acts more like sleep 1

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Richard (Guest)
on 2007-05-13 17:40
(Received via mailing list)

I wrote my private alarm_clock using repeated .wav files to announce
that (1) the clock started and (2) the specified time had elapsed.

For (1), which is too protracted:
3.times do |i|
  system "wv_player.exe", DING
  sleep 0.2i

For (2), which works fine for my purposes:
while true
  system "wv_player.exe", ALARM_BELL
  sleep 1

The two constants are defined:
ALARM_BELL   = ENV["WINDIR"] + "\\Media\\chimes.wav"
DING       = ENV["WINDIR"] + "\\Media\\ding.wav"

Is there away to play a few DINGs faster?  If not,  I'll post on a
Windows site to get a suggestion for something better to feed to
Windows Wave Player or maybe Windows Media Player.

I'm running WinXP-Pro/SP2, Ruby 1.8.2-15, Rails  1.1.6.

Gordon T. (Guest)
on 2007-05-13 18:40
(Received via mailing list)
Have a look at the win32-sound gem.  It may already be installed, if
you're using the one-click installer.
Marcin R. (Guest)
on 2007-05-13 23:06
(Received via mailing list)
On Sunday 13 May 2007 14:40, Gordon T. wrote:
> Have a look at the win32-sound gem.  It may already be installed, if
> you're using the one-click installer.

first of all running external program (especially so heavy) is couing

easiest (and most ugly) solution would be
3.times do |i|{system("wv_player.exe", DING)}
        sleep 0.2i
Richard (Guest)
on 2007-05-15 07:40
(Received via mailing list)
Hi Marcin,

> first of all running external program (especially so heavy) is couing that
> delay

I agree that making system calls to Windows is probably the key

> easiest (and most ugly) solution would be
> 3.times do |i|
>{system("wv_player.exe", DING)}
>         sleep 0.2i
> end

I agree that threading is a key to getting sub-second responses, if
indeed it's possible with wv_player having to access the file system
three times (I doubt wv-player would cache the selection.)

I tried your introduction of threading,  but it produced the sane
result I've been getting (after removing the "i" at the end of the
sleep statement.)

I outlined in a previous response to you (that was really intended for
Gordon) about what I thought I'd have to do to get sub-second
response.  I also mentioned that I didn't think my application merited
all that work.

Thanks for your response.  I had never looked at threading in Ruby,
so I appreciated giving it a try.

Best wishes,
Richard (Guest)
on 2007-05-15 08:35
(Received via mailing list)
Hi Gordon,

Following is the original text I prepared in response to you.  I made
mistakes in posting this thing, so you might see other versions of it
popping up.  Please ignore them and excuse me for my sloppiness.

> Have a look at the win32-sound gem.

Thanks for that suggestion.

> It may already be installed, if
> you're using the one-click installer.

It was not already installed, although I think I used one-click (a
long time ago).  Downloading/installing it worked fine, however.

Unfortunately, it seemed to offer the same performance as my system
call to "wv_player.exe".  I suspect that win32-sound is coded in terms
of the same system call (but I'm too lazy to look.)

I further suspect that to achieve my performance goal,  I'd have to do
Win32 programming in C++ to create a thread that, with parameters for
the wave-file-name, repetition-count and sleep-time in milliseconds,
calls wm_player in a loop that honors arguments supplied by the Ruby
script.  That's too much work,  so I think I'll live with the
limitation I'm experiencing.

Nevertheless,  I'm grateful for you taking the trouble to respond to
my question (and offering sound advice).

Best wishes,
Gordon T. (Guest)
on 2007-05-15 18:46
(Received via mailing list)
If I understand your question, you want to play a wav file repeatedly,
as quickly as possible.  This code is from the example file in the
win32-sound gem.  It plays chimes.wav 5 times in 3 seconds on both of
my XP machines.

require "win32/sound"
include Win32

wav = "c:\\windows\\media\\chimes.wav",Sound::ASYNC|Sound::LOOP)
sleep 3


Hope it helps.

Cliff R. (Guest)
on 2007-05-17 01:12
(Received via mailing list)
> </code>
> Hope it helps.
> Gordon

It would also make sense to me to use a longer sound file.  A 3 second
file containing 15 beeps, or something..  Seems a little less like brute
force to me, playing the sound only 4 or 5 times in 15 seconds, or

Just my 2p.
Richard (Guest)
on 2007-05-23 00:02
(Received via mailing list)
Hi Gordon,

That's perfect for my purposes:

1. Achieving looping within the playing mechanism is much faster than
using a Ruby loop invoking a Win32 function repeatedly

2. Using an explicit rather than symbolic path is also a speedup, i.e.
"F:\\WINXPPRO\\media\\chimes.wav" instead of ALARM_BELL   =
ENV["WINDIR"] + "\\Media\\chimes.wav"

Thank you very much for the addition post.

Best wishes,
Richard (Guest)
on 2007-05-23 00:05
(Received via mailing list)
Hi Cliff,

I agree completely,  but I didn't know where to get (of how to
construct) a longer file.  And I'd rather learn a better Ruby
technique (as Cliff provided on May 15, for example) than research
multimedia issues.

But thanks for posting a valid idea.

Best wishes,
This topic is locked and can not be replied to.