For anyone who finds this in the future, I've opened up this thread on stackoverflow, which will hopefully be more informative: https://stackoverflow.com/questions/66129025/generated-sine-wave-sounds-buzzy-or-square-instead-of-sine
I am working on a toolkit to visualize sound, and also to write custom synthesizers. I wrote the following program, which generates a 100Hz sine wave, plays it back to me through python's simpleaudio package, and plots the signal that was played.
import numpy as np import simpleaudio as sa import matplotlib.pyplot as plt def generate_sine_tone(numsamples, sample_time, frequency): t = np.arange(numsamples) * sample_time # Time vector signal = 8388605*np.sin(2*np.pi * frequency*t) return signal if __name__ == "__main__": duration = 1 samprate = 44100 # Sampling rate numsamples = samprate*duration# Sample count st = 1.0 / samprate # Sample time t = np.arange(numsamples) * st # Time vecto nchannels = 2 sampwidth = 3 signal = generate_sine_tone(numsamples, st, 100) signal2 = np.asarray([ int(x) for x in signal ]) play_obj = sa.play_buffer(signal2, nchannels, sampwidth, samprate) print(signal2) plt.figure(0) plt.plot(signal2) plt.show()
However, despite looking correct, the signal that is played back sounds buzzy, like a square wave, instead of a sine wave. At first I thought this was a code problem, but I've spent a lot of time trying to debug, and it seems to be working as anticipated.
So my question is this: Does this signal look correct to produce a smooth sine wave? Is there some kind of configuration error? Do I need to further process the signal to get something like this?Thank you for any contribution.