sleep 0.2 acts more like sleep 1

Discussion in 'Ruby' started by Richard, May 13, 2007.

  1. Richard

    Richard Guest

    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
     
    Richard, May 13, 2007
    #1
    1. Advertising

  2. Have a look at the win32-sound gem. It may already be installed, if
    you're using the one-click installer.
     
    Gordon Thiesfeld, May 13, 2007
    #2
    1. Advertising

  3. 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=
    =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
     
    Marcin Raczkowski, May 13, 2007
    #3
  4. Richard

    Richard Guest

    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
     
    Richard, May 15, 2007
    #4
  5. Richard

    Richard Guest

    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


    On May 13, 10:37 am, Gordon Thiesfeld <> wrote:
    > Have a look at the win32-sound gem. It may already be installed, if
    > you're using the one-click installer.
     
    Richard, May 15, 2007
    #5
  6. 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
     
    Gordon Thiesfeld, May 15, 2007
    #6
  7. Richard

    Richard Guest

    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


    On May 15, 10:41 am, Gordon Thiesfeld <> wrote:
    > 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
     
    Richard, May 22, 2007
    #7
  8. Richard

    Richard Guest

    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

    On May 16, 5:11 pm, "Cliff Rowley" <> wrote:
    > > -----Original Message-----
    > > From: Gordon Thiesfeld [mailto:]
    > > 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

    >
    > > </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.
     
    Richard, May 22, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gonzalo Moreno
    Replies:
    2
    Views:
    12,563
  2. Utku Altinkaya

    RPC Client acts like a server

    Utku Altinkaya, Jul 14, 2008, in forum: Python
    Replies:
    0
    Views:
    235
    Utku Altinkaya
    Jul 14, 2008
  3. Rafe
    Replies:
    5
    Views:
    300
    Wolfgang Grafen
    Aug 15, 2008
  4. AAaron123
    Replies:
    11
    Views:
    1,162
    AAaron123
    Nov 27, 2008
  5. Sam Roberts
    Replies:
    16
    Views:
    408
    Daniel Berger
    Feb 18, 2005
Loading...

Share This Page