Created
June 14, 2019 05:23
-
-
Save FrankBuss/3c2f0d1eaf289ef9f659139b96e7a459 to your computer and use it in GitHub Desktop.
create a sequence of beeps with configurable pauses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/python3 | |
# play a sequence of beeps | |
# based on https://stackoverflow.com/a/27978895/3210924 | |
import pyaudio | |
import numpy as np | |
from numpy import zeros | |
import sys | |
import time | |
if len(sys.argv) != 4: | |
print("usage: python3 beeps.py tone-length pause-length count") | |
print("example: python3 beeps.py 0.1 0.9 5") | |
sys.exit(1) | |
p = pyaudio.PyAudio() | |
volume = 0.5 # range [0.0, 1.0] | |
fs = 44100 # sampling rate, Hz, must be integer | |
duration = float(sys.argv[1]) # in seconds, may be float | |
f = 440.0 # sine frequency, Hz, may be float | |
durationPause = float(sys.argv[2]) | |
count = int(sys.argv[3]) | |
print("tone length: %f, pause length: %f, count: %f" % (duration, durationPause, count)) | |
# generate samples, note conversion to float32 array | |
tone = (np.sin(2*np.pi*np.arange(int(fs*duration))*f/fs)).astype(np.float32) | |
# generate pause | |
pause = zeros(int(fs*durationPause)).astype(np.float32) | |
# add pause to samples | |
samples = np.concatenate((tone, pause)) | |
# repeat samples | |
data = np.array([]).astype(np.float32) | |
for i in range(count): | |
data = np.concatenate((data , samples)) | |
def callback(in_data, frame_count, time_info, status): | |
global data | |
out = data[:frame_count] | |
data = data[frame_count:] | |
return (out*volume, pyaudio.paContinue) | |
# for paFloat32 sample values must be in range [-1.0, 1.0] | |
stream = p.open(format=pyaudio.paFloat32, | |
channels=1, | |
rate=fs, | |
output=True, | |
stream_callback=callback) | |
stream.start_stream() | |
while stream.is_active(): | |
time.sleep(0.1) | |
stream.stop_stream() | |
stream.close() | |
p.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your solution was very helpful. See how you like this version.