Skip to content

Instantly share code, notes, and snippets.

@chadmiller
Created February 8, 2019 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadmiller/3a692e3c7b1ee863c9564d96698fca6b to your computer and use it in GitHub Desktop.
Save chadmiller/3a692e3c7b1ee863c9564d96698fca6b to your computer and use it in GitHub Desktop.
#!/bin/bash
""":"
# Run a pulseaudio wrapper. Fancy ugly hack.
exec padsp -d python3 "$0" "$@"
"""
import struct
import numpy
from scipy import signal
sampling_rate_hz = 8000
freq_hz = 261.63
sample_count = 8000
sample_positions = numpy.arange(sample_count)
sinewave = 100 * numpy.sin(2*numpy.pi * freq_hz * sample_positions / sampling_rate_hz)
# squarewave = 100 * signal.square(2*numpy.pi * freq_hz * sample_positions / sampling_rate_hz) # square wave
# y = 100 * signal.square(2*numpy.pi * freq_hz * sample_positions / sampling_rate_hz, duty = 0.8) # square wave with Duty Cycle
# y = 100 * signal.sawtooth(2*numpy.pi * freq_hz * sample_positions / sampling_rate_hz) # Sawtooth wave
with open('/dev/dsp','wb') as dsp:
while True:
for each_value in sinewave:
dsp.write(struct.pack('b', int(round(each_value))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment