Forum: Ruby sleep 0.2 acts more like sleep 1

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 Richard (Guest)
on 2007-05-13 15:40
(Received via mailing list)
Hi,

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
end

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

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.

TIA,
Richard
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2007-05-13 16: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.
F0afd024e17c0c4753aa8d618ba9bb0f?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2007-05-13 21:06
(Received via mailing list)
On Sunday 13 May 2007 14:40, Gordon Thiesfeld 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
that
delay

easiest (and most ugly) solution would be
3.times do |i|
       Thread.new{system("wv_player.exe", DING)}
        sleep 0.2i
end
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 Richard (Guest)
on 2007-05-15 05: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
culprit.

> easiest (and most ugly) solution would be
> 3.times do |i|
>        Thread.new{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
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 Richard (Guest)
on 2007-05-15 06: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,
Richard
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2007-05-15 16: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.

<code>
require "win32/sound"
include Win32

wav = "c:\\windows\\media\\chimes.wav"

Sound.play(wav,Sound::ASYNC|Sound::LOOP)
sleep 3
Sound.stop

</code>

Hope it helps.

Gordon
0de2a19e1f26e4675d7dd363cc0157e9?d=identicon&s=25 Cliff Rowley (Guest)
on 2007-05-16 23: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
sound
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
something.

Just my 2p.
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 Richard (Guest)
on 2007-05-22 22: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
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 Richard (Guest)
on 2007-05-22 22: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,
Richard
This topic is locked and can not be replied to.