Created
June 18, 2019 08:32
-
-
Save FrankBuss/5ed8c46a4233957f6595a0a56632d2ae to your computer and use it in GitHub Desktop.
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 | |
import pyaudio | |
import numpy as np | |
import sys | |
import time | |
import math | |
p = pyaudio.PyAudio() | |
# samplerate | |
fs = 44100 | |
# frequencies | |
f = 440.0 | |
f2 = 44.0 | |
# current angles | |
angle = 0 | |
increment = 2 * math.pi / fs * f | |
angle2 = 0 | |
increment2 = 2 * math.pi / fs * f2 | |
# sample buffer | |
data = [] | |
# called for each sample, creates a FM test signal | |
def sample(): | |
global angle, increment | |
global angle2, increment2 | |
s = math.sin(angle) | |
s2 = math.sin(angle2) | |
angle = angle + increment | |
angle2 = angle2 + increment2 | |
if angle > 2 * math.pi: | |
angle = angle - 2 * math.pi | |
if angle2 > 2 * math.pi: | |
angle2 = angle2 - 2 * math.pi | |
return s * s2 * 0.4 | |
# called for each audio block, e.g. 1024 samples | |
def callback(in_data, frame_count, time_info, status): | |
global data | |
if len(data) == 0: | |
data = np.zeros(frame_count).astype(np.float32) | |
for i in range(frame_count): | |
data[i] = sample() | |
return (data, 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