3

So I'm trying to generate a controllable tone with python. I'm using PyAudio and the simple code is like this (source):

import pyaudio
import numpy as np

p = pyaudio.PyAudio()

fs = 44100       # sampling rate, Hz, must be integer
duration = 1.0   # in seconds, may be float
f = 440.0        # sine frequency, Hz, may be float

# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)

# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=fs,
                output=True)

# play. May repeat with different volume values (if done interactively) 
stream.write(samples.tobytes())

stream.stop_stream()
stream.close()

p.terminate()

the problem is when I played 2 different frequencies in sequence, it didn't play smoothly. I think it is because of the distortion between end of first frequency and beginning of second frequency. I'm trying to give a smooth transition with this code:

fstart = 440.0    # first frequency
fstop = 523.6     # second frequency
transition = 0.5  # transition time
finest = 10       # finest of transition
duration = 2.0    # duration of first and second frequency
samples = []
samples = np.asarray(samples)
# print(duration)
# print(fs*duration)
samples = np.append(samples, (np.sin(2*np.pi*np.arange(fs*duration)*fstart/fs)).astype(np.float32)).astype(np.float32)
for x in range(0, finest):
    freq = fstart + (fstop-fstart)*x/finest
    time = transition/finest
#     print(time)
#     print(fs*time)
    samples = np.append(samples, (np.sin(2*np.pi*np.arange(fs*time)*freq/fs)).astype(np.float32)).astype(np.float32)
samples = np.append(samples, (np.sin(2*np.pi*np.arange(fs*duration)*fstop/fs)).astype(np.float32)).astype(np.float32)

but as expected, it also didn't go well. And when the finest is set to high, the sound was very distorted. Any idea to make this smooth like frequency sweep sound?

Thank you.

juliussin
  • 295
  • 3
  • 20
  • it is probably caused by sample rate of reproduction and frequency you generate to be asynchronous, or aliasing sound problem. What is the sample rate you are using to play the sound? – Guinther Kovalski Dec 30 '19 at 17:50
  • 1
    @GuintherKovalski yes I believe it is, but I still haven't found a way to synchronize it so the-something like crossover-distortion doesn't occur. I'm using standart 44100 sampling frequency. – juliussin Dec 30 '19 at 18:41

0 Answers0