Skip to content

Commit

Permalink
test for clicking sound at the end of signal
Browse files Browse the repository at this point in the history
  • Loading branch information
kbasaran committed Aug 16, 2023
1 parent ee5dd0b commit 5916832
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions beep_buffer_underrun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import numpy as np
import sounddevice as sd
import time
import matplotlib.pyplot as plt
plt.rcParams["figure.dpi"] = 150

class SoundEngine():
def __init__(self):
self.FS = sd.query_devices(
device=sd.default.device,
kind='output',
)["default_samplerate"]
self.start_stream()
self.test_beep()

def start_stream(self):
self.stream = sd.OutputStream(samplerate=self.FS, channels=2, latency='high')
self.stream.start()

def beep(self, T, freq):
t = np.arange(T * self.FS) / self.FS
y = 0.1 * np.sin(t * 2 * np.pi * freq)

# pad = np.zeros(100)
# y = np.concatenate([pad, y, pad])

y = np.tile(y, self.stream.channels)
y = y.reshape((len(y) // self.stream.channels, self.stream.channels), order='F')
y = np.ascontiguousarray(y, self.stream.dtype)
plt.plot(y[-1000:, :]); plt.grid()
self.stream.write(y)

def test_beep(self):
self.beep(1, 200)

sound_engine = SoundEngine()
time.sleep(1)

# %%

def beep(T, freq):
channels = 2

FS = sd.query_devices(
device=sd.default.device,
kind='output',
)["default_samplerate"]

t = np.arange(T * FS) / FS
y = np.tile(0.1 * np.sin(t * 2 * np.pi * freq), channels)
y = y.reshape((len(y) // channels, channels), order='F')
y = y.astype(sd.default.dtype[0])
plt.plot(y[-100:, :]); plt.grid() # what the signal looks like at the end
sd.play(y, latency='high')

# beep(1, 200)

0 comments on commit 5916832

Please sign in to comment.