sleep 0.2 acts more like sleep 1

R

Richard

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
 
G

Gordon Thiesfeld

Have a look at the win32-sound gem. It may already be installed, if
you're using the one-click installer.
 
M

Marcin Raczkowski

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=
=20
delay

easiest (and most ugly) solution would be=20
3.times do |i|
=A0=A0=A0=A0=A0=A0=A0Thread.new{system("wv_player.exe", DING)}
=A0=A0=A0=A0=A0=A0=A0=A0sleep 0.2i
end


=2D-=20
Marcin Raczkowski
=2D--
=46riends teach what you should know
Enemies Teach what you have to know
 
R

Richard

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
 
R

Richard

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
 
G

Gordon Thiesfeld

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
 
R

Richard

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
 
R

Richard

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

-----Original Message-----
From: Gordon Thiesfeld [mailto:[email protected]]
Sent: 15 May 2007 15:45
To: ruby-talk ML
Subject: Re: sleep 0.2 acts more like sleep 1
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

Hope it helps.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top