-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test for clicking sound at the end of signal
reported in spatialaudio/python-sounddevice#482
- Loading branch information
kbasaran
committed
Aug 16, 2023
1 parent
ee5dd0b
commit 5916832
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) |