Generate a tone using ruby?

Discussion in 'Ruby' started by Jeremiah Dodds, Jul 26, 2006.

  1. Hey everyone, I'm looking for a simple way to generate a tone in ruby .
    . any suggestions?

    --
    Posted via http://www.ruby-forum.com/.
     
    Jeremiah Dodds, Jul 26, 2006
    #1
    1. Advertising

  2. Jeremiah Dodds wrote:
    > Hey everyone, I'm looking for a simple way to generate a tone in ruby .
    > . any suggestions?
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    print "\a"
     
    William James, Jul 26, 2006
    #2
    1. Advertising

  3. William James wrote:
    > Jeremiah Dodds wrote:
    >> Hey everyone, I'm looking for a simple way to generate a tone in ruby .
    >> . any suggestions?
    >>
    >> --
    >> Posted via http://www.ruby-forum.com/.

    >
    > print "\a"


    Well, yeah sure. What I'd like to be able to do is generate a tone of a
    specific frequency. Like an A note, or a D sharp.


    --
    Posted via http://www.ruby-forum.com/.
     
    Jeremiah Dodds, Jul 27, 2006
    #3
  4. Jeremiah Dodds wrote:
    > William James wrote:
    > > Jeremiah Dodds wrote:
    > >> Hey everyone, I'm looking for a simple way to generate a tone in ruby .
    > >> . any suggestions?
    > >>
    > >> --
    > >> Posted via http://www.ruby-forum.com/.

    > >
    > > print "\a"

    >
    > Well, yeah sure. What I'd like to be able to do is generate a tone of a
    > specific frequency. Like an A note, or a D sharp.
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    require "Win32API"
    Beep = Win32API.new("kernel32", "Beep", ["I", "I"], 'v')
    def beep freq, duration
    Beep.call(freq, duration)
    end

    beep 600, 400
     
    William James, Jul 27, 2006
    #4
  5. Jeremiah Dodds

    Hans Fugal Guest

    Jeremiah Dodds wrote:
    > William James wrote:
    >> Jeremiah Dodds wrote:
    >>> Hey everyone, I'm looking for a simple way to generate a tone in ruby .
    >>> . any suggestions?
    >>>
    >>> --
    >>> Posted via http://www.ruby-forum.com/.

    >> print "\a"

    >
    > Well, yeah sure. What I'd like to be able to do is generate a tone of a
    > specific frequency. Like an A note, or a D sharp.
    >
    >


    I presume you want to play it out the speaker? I'm not sure if there's a
    portable way to do that, but in most unices it would be a matter of
    sending the audio data for that frequency to a special file, e.g.
    /dev/dsp. Or perhaps you want to save to a file. In either case, esp.
    the latter, ruby/audio would be worth a look.

    http://hans.fugal.net/src/ruby-audio/
     
    Hans Fugal, Jul 27, 2006
    #5
  6. Thanks William and Hans. William, if I was on windows that probably
    would've helped.

    Hans, I don't necessarily need to save to file - it could be handy
    though. I'll check out ruby-audio.

    I really need to learn not to post these help requests when I'm
    sleep-deprived - I leave out vital information like what exactly I want
    to do, and what OS I'm running on.

    I want to generate the sound real-time - so I guess I'll need to figure
    out how to send it to /dev/dsp or /dev/audio or whatnot.

    --
    Posted via http://www.ruby-forum.com/.
     
    Jeremiah Dodds, Jul 27, 2006
    #6
  7. Jeremiah Dodds

    Ico Guest

    Jeremiah Dodds <> wrote:
    > Thanks William and Hans. William, if I was on windows that probably
    > would've helped.
    >
    > Hans, I don't necessarily need to save to file - it could be handy
    > though. I'll check out ruby-audio.
    >
    > I really need to learn not to post these help requests when I'm
    > sleep-deprived - I leave out vital information like what exactly I want
    > to do, and what OS I'm running on.
    >
    > I want to generate the sound real-time - so I guess I'll need to figure
    > out how to send it to /dev/dsp or /dev/audio or whatnot.


    Something like this might work on *unix :

    ------------------------------------------------------------

    Samplerate = 8000

    def beep(frequency, amplitude, duration)

    f = File.open("/dev/dsp", "w")

    wave = ""

    0.step(duration, 1.0/Samplerate) do |t|
    y = Math.sin(t * frequency) * 50 + 127;
    wave << y.to_i.chr
    end

    f.write(wave)
    end

    beep(2000, 100, 1)

    ------------------------------------------------------------

    this code assumes the default settings of your soundcard are 8000 hz, 8
    bits samples, signed, one channel. If you want more control over these
    values, you will have to do fiddling with OSS IOCTL's or alsa libraries.

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    Ico, Jul 27, 2006
    #7
    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. Macky G
    Replies:
    3
    Views:
    1,260
    Macky G
    Jun 30, 2009
  2. Alex Hall
    Replies:
    1
    Views:
    498
    Gregory Ewing
    May 17, 2010
  3. ratullloch_delthis

    Frequency/tone generator in java tia sal22

    ratullloch_delthis, Nov 28, 2010, in forum: Java
    Replies:
    2
    Views:
    4,141
    Roedy Green
    Dec 2, 2010
  4. Jonah Olsson

    How to recognize what ring tone support?

    Jonah Olsson, Jul 26, 2004, in forum: ASP .Net Mobile
    Replies:
    1
    Views:
    132
    Jean-Luc David [MS-MVP]
    Jul 26, 2004
  5. fulio pen

    han yu pin yin's tone marks

    fulio pen, Nov 1, 2012, in forum: HTML
    Replies:
    6
    Views:
    462
    Jukka K. Korpela
    Nov 2, 2012
Loading...

Share This Page