0

I have been trying to write a program which generates 'beeps' at a given frequency. I have tried using winsound, but I have been unable to start and stop the sounds on impulse, just play them for a duration. Is there a module which allows generation of beeps at a specific frequency with a start and stop impulse? I am currently running Python 3.8.2 (I have pip, so can install modules that aren't built in).

Daniel
  • 1

1 Answers1

0

This can be done with PyAudio - Portaudio bindings for python.

import pyaudio
from math import pi
import numpy as np

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32, channels=1, rate=44100, output=1,)


def make_sinewave(frequency, length, sample_rate=44100):
    length = int(length * sample_rate)
    factor = float(frequency) * (pi * 2) / sample_rate
    waveform = np.sin(np.arange(length) * factor)

    return waveform

wave = make_sinewave(500, 1)

stream.write(wave.astype(np.float32).tostring())
stream.stop_stream()
stream.close()
Danny Meyer
  • 321
  • 6
  • 11