开发者

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

开发者 https://www.devze.com 2023-01-26 00:42 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. 开发者_JAVA技巧 Want to improve this question? Update the question so it can be answered with facts and citati
Closed. This question is opinion-based. It is not currently accepting answers.
开发者_JAVA技巧

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 4 years ago.

Improve this question

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.


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)


For windows users: use the gem win32-sound

# gem install win32-sound

require 'win32/sound'
include Win32

Sound.play("SystemAsterisk", Sound::ALIAS) # play system asterisk sound
Sound.beep(600,200) # play a beep 600 hertz for 200 milliseconds
Sound.play("trompet.wav") # play a file from disk
0

精彩评论

暂无评论...
验证码 换一张
取 消