Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a series of high- and low-pitch beeps using Ruby or Python? [closed]

Tags:

python

ruby

I want to create a series of a low- and high-pitch beeps at fixed times. For example:

  • High-pitch beep at 150 ms
  • Low-pitch beep at 151 ms
  • Low-pitch beep at 200 ms
  • High-pitch beep at 250 ms

Is there a way to do this in Ruby or Python? I don't really care what the output encoding is (.wav, .mp3, .ogg, whatever), but I do want to create an output file.

like image 759
grautur Avatar asked Nov 17 '10 05:11

grautur


4 Answers

Here's a function in Python that makes a file with a single sine wave:

# based on : www.daniweb.com/code/snippet263775.html
import math
import wave
import struct

def make_sine(freq=440, datasize=10000, fname="test.wav", framerate=44100.00):
    amp=8000.0 # amplitude
    sine_list=[]
    for x in range(datasize):
        sine_list.append(math.sin(2*math.pi * freq * ( x/frate)))
    # Open up a wav file
    wav_file=wave.open(fname,"w")
    # wav params
    nchannels = 1
    sampwidth = 2
    framerate = int(frate)
    nframes=datasize
    comptype= "NONE"
    compname= "not compressed"
    wav_file.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
    #write on file
    for s in sine_list:
        wav_file.writeframes(struct.pack('h', int(s*amp/2)))
    wav_file.close()

frate = 44100.00 #that's the framerate
freq=987.0 #that's the frequency, in hertz
seconds = 3 #seconds of file
data_length = frate*seconds #number of frames
fname = "WaveTest2.wav" #name of file
make_sine(freq, data_length, fname) 

Not the fastest code...But if you don't need speed, it will work fine!

like image 178
Nemeth Avatar answered Nov 20 '22 06:11

Nemeth


Try out the Ruby Audio File Library (RAFL). It supports:

When writing a WAV file:

Write any number of channels at any sample rate Generate white noise, pink noise and sine waves at any amplitude or frequency

Here's the GitHub source for the project.

like image 45
Phrogz Avatar answered Nov 20 '22 05:11

Phrogz


Python:

Looks like you need the winsound module.

Specifically, the function:

winsound.Beep(frequency, duration)

Which does what you want. But this is specific to Windows only.

There is a package called beep that allows you to do the same on Linux. So call beep using subprocess in case you want to have a Linux based solution to this.

like image 31
user225312 Avatar answered Nov 20 '22 07:11

user225312


Here's some help for Ruby: Adding Sound to Your Ruby Apps. For actually recording a beep from the pc's speaker to a wav/mp3 -- I don't know if that's possible.

For a beep from your sound card (not speaker), in case you ever need that, simply use the Win32API module that comes with Ruby :

require 'Win32API'
Beep = Win32API.new('kernel32', 'Beep', ['I', 'I'], 'I')
Beep.call(1200,150)
Beep.call(200,150)
Beep.call(300,150)
Beep.call(1400,150)
like image 1
Zabba Avatar answered Nov 20 '22 06:11

Zabba