What Is the Easiest Way I Can Create a 'Beep' Sound from a Ruby Program

What is the easiest way I can create a 'beep' sound from a Ruby program?

Try printing the audible bell character:

print "\a"

How do I get shell/ruby to make a noise (make my computer beep or play a sound/track) when my script breaks?

Also on mac there is the afplay program, so it would be possible to do something like:

sleep 180 # sleep 3 minutes
`afplay my_song.mp3`

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

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!

Add sound when click on image with Ruby on Rails

Instead of using inline script tags you can create a delegated event handler that will work properly with Turbolinks.

// app/assets/javascripts/beeper.jsconst audio = new Audio('http://soundbible.com/grab.php?id=1815&type=mp3');document.addEventListener('click', (event)=>{  let el = event.target;  if (el.matches('.beeper')) {    console.log("beep!");    audio.play();    el.classList.toggle("disabled");  }});
<button class="beeper">Click me</button>

Play sound on terminal output

This might be a pretty complex solution, but it does what I need.

In my .bashrc file, I added the following:

#ensure that the call is made only once, preventing an infinite loop

if [ $SHLVL == 1 ]
then
script -afq ~/custom/log.txt #log everything that happens in the shell
fi

#call my script only once by checking for another instance of it

if [[ ! $(pidof -x script.sh) ]]
then
~/custom/script.sh&
fi

My script.sh file checks for changes in log.txt and plays a beep sound (you need to download it) when that happens:

#!/bin/bash

$(stat -c %y ~/custom/log.txt > ~/custom/update.txt)

while :
do
now=$(stat -c %y ~/custom/log.txt)
update=$(cat ~/custom/update.txt)
if [ "$now" != "$update" ]
then
$(stat -c %y ~/custom/log.txt > ~/custom/update.txt)
$(play -q ~/custom/beep.ogg vol 0.1) #props to franklin
fi
done

This will make it so that everytime something changes in the shell, including typing, script.sh will run play. Now I get to know whenever there is a request to my WEBrick server without having to look at the terminal.

python - how can I generate a WAV file with beeps?

I've based this on the answer to the previous question and added a lot of comments. Hopefully this makes it clear. You'll probably want to introduce a for loop to control the number of beeps and the increasing volume.

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

# Audio will contain a long list of samples (i.e. floating point numbers describing the
# waveform). If you were working with a very long sound you'd want to stream this to
# disk instead of buffering it all in memory list this. But most sounds will fit in
# memory.
audio = []
sample_rate = 44100.0

def append_silence(duration_milliseconds=500):
"""
Adding silence is easy - we add zeros to the end of our array
"""
num_samples = duration_milliseconds * (sample_rate / 1000.0)

for x in range(int(num_samples)):
audio.append(0.0)

return

def append_sinewave(
freq=440.0,
duration_milliseconds=500,
volume=1.0):
"""
The sine wave generated here is the standard beep. If you want something
more aggresive you could try a square or saw tooth waveform. Though there
are some rather complicated issues with making high quality square and
sawtooth waves... which we won't address here :)
"""

global audio # using global variables isn't cool.

num_samples = duration_milliseconds * (sample_rate / 1000.0)

for x in range(int(num_samples)):
audio.append(volume * math.sin(2 * math.pi * freq * ( x / sample_rate )))

return

def save_wav(file_name):
# Open up a wav file
wav_file=wave.open(file_name,"w")

# wav params
nchannels = 1

sampwidth = 2

# 44100 is the industry standard sample rate - CD quality. If you need to
# save on file size you can adjust it downwards. The stanard for low quality
# is 8000 or 8kHz.
nframes = len(audio)
comptype = "NONE"
compname = "not compressed"
wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))

# WAV files here are using short, 16 bit, signed integers for the
# sample size. So we multiply the floating point data we have by 32767, the
# maximum value for a short integer. NOTE: It is theortically possible to
# use the floating point -1.0 to 1.0 data directly in a WAV file but not
# obvious how to do that using the wave module in python.
for sample in audio:
wav_file.writeframes(struct.pack('h', int( sample * 32767.0 )))

wav_file.close()

return

append_sinewave(volume=0.25)
append_silence()
append_sinewave(volume=0.5)
append_silence()
append_sinewave()
save_wav("output.wav")


Related Topics



Leave a reply



Submit