I want to create a series of a low- and high-pitch beeps at fixed times. For example:
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.
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!
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With