Skip to content

Commit

Permalink
Merge pull request #81 from interactive-sonification/bf/aserver-wrong…
Browse files Browse the repository at this point in the history
…-device-duplicate-code

Bf/aserver wrong device duplicate code
  • Loading branch information
wiccy46 authored Aug 3, 2023
2 parents 5cacc71 + 0f4b13e commit 0531afd
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pya/amfcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(self, x, sr=None, label='', n_per_frame=None,
# First prepare for parameters
# x represent the audio signal, which can be Asig object or np.array.
self.im = None
if type(x) == pya.asig.Asig:
if isinstance(x, pya.asig.Asig):
self.sr = x.sr
self.x = x.sig
self.label = ''.join([x.label, "_mfccs"])
Expand Down
17 changes: 15 additions & 2 deletions pya/arecorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,23 @@ def __init__(self, sr=44100, bs=256, device=None, channels=None, backend=None, *
self.recordings = [] # store recorded Asigs, time stamp in label
self._recording = False
self._record_all = True
self.gains = np.ones(self.channels)
self.tracks = slice(None)
self._device = self.backend.get_default_input_device_info()['index'] if device is None else device
self.channels = channels or self.backend.get_device_info_by_index(self._device)['maxInputChannels']
self._channels = channels or self.max_in_chn
self.gains = np.ones(self._channels)

@property
def channels(self):
return self._channels

@channels.setter
def channels(self, val: int):
"""
Set the number of channels. Aserver needs reboot.
"""
if val > self.max_in_chn:
raise ValueError(f"AServer: channels {val} > max {self.max_in_chn}")
self._channels = val

def set_tracks(self, tracks, gains):
"""Define the number of track to be recorded and their gains.
Expand Down
48 changes: 23 additions & 25 deletions pya/aserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, sr=44100, bs=None, device=None,
The device index based on pya.device_info(), default is None which will set
the default device from PyAudio
channels : int
number of channel (Default value = 2)
number of channel, default is the max output channels of the device
kwargs : backend parameter
Returns
Expand All @@ -88,56 +88,56 @@ def __init__(self, sr=44100, bs=None, device=None,
if int(self.backend.get_device_info_by_index(i)['maxOutputChannels']) > 0:
self.output_devices.append(self.backend.get_device_info_by_index(i))

self._device = self.backend.get_default_input_device_info()['index'] if device is None else device
self.channels = channels or self.backend.get_device_info_by_index(self.device)['maxOutputChannels']
self._device = self.backend.get_default_output_device_info()['index'] if device is None else device
self._channels = channels or self.max_out_chn

self.gain = 1.0
self.srv_onsets = []
self.srv_asigs = []
self.srv_curpos = [] # start of next frame to deliver
self.srv_asigs = []
self.srv_outs = [] # output channel offset for that asig
self.stream = None
self.boot_time = 0 # time.time() when stream starts
self.block_cnt = 0 # nr. of callback invocations
self.block_duration = self.bs / self.sr # nominal time increment per callback
self.block_time = 0 # estimated time stamp for current block
self._stop = True
self.empty_buffer = np.zeros((self.bs, self.channels),
dtype=self.backend.dtype)
self.empty_buffer = np.zeros((self.bs, self.channels), dtype=self.backend.dtype)
self._is_active = False

@property
def device_dict(self):
return self.backend.get_device_info_by_index(self._device)
def channels(self):
return self._channels

@property
def max_out_chn(self):
return int(self.device_dict['maxOutputChannels'])

@property
def max_in_chn(self):
return int(self.device_dict['maxInputChannels'])
@channels.setter
def channels(self, val: int):
"""
Set the number of channels. Aserver needs reboot.
"""
if val > self.max_out_chn:
raise ValueError(f"AServer: channels {val} > max {self.max_out_chn}")
self._channels = val

@property
def device_dict(self):
return self.backend.get_device_info_by_index(self._device)

@property
def max_out_chn(self):
def max_out_chn(self) -> int:
return int(self.device_dict['maxOutputChannels'])

@property
def max_in_chn(self):
def max_in_chn(self) -> int:
return int(self.device_dict['maxInputChannels'])

@property
def device(self):
return self._device

@property
def is_active(self) -> bool:
return self.stream is not None and self.stream.is_active()

@property
def device(self):
return self._device

@device.setter
def device(self, val):
self._device = val if val is not None else self.backend.get_default_output_device_info()['index']
Expand All @@ -146,7 +146,6 @@ def device(self, val):
self.channels = self.max_out_chn

def __repr__(self):
state = False
msg = f"""AServer: sr: {self.sr}, blocksize: {self.bs},
Stream Active: {self.is_active}, Device: {self.device_dict['name']}, Index: {self.device_dict['index']}"""
return msg
Expand All @@ -163,7 +162,8 @@ def get_devices(self, verbose=False):
return self.input_devices, self.output_devices

def set_device(self, idx, reboot=True):
"""Set audio device
"""Set audio device, an alternative way is to direct set the device property, i.e. Aserver.device = 1,
but that will not reboot the server.
Parameters
----------
Expand Down Expand Up @@ -316,5 +316,3 @@ def __del__(self):
self.quit()
self.backend.terminate()

def __enter__(self):
return self.boot()
4 changes: 2 additions & 2 deletions pya/asig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ def window(self, win="triang", **kwargs):
if not win:
return self
winstr = win
if type(winstr) == tuple:
if isinstance(winstr, tuple):
winstr = win[0]
if self.channels == 1:
return Asig(self.sig * scipy.signal.get_window(win, self.samples, **kwargs),
Expand Down Expand Up @@ -1843,7 +1843,7 @@ def add(self, sig, pos=None, amp=1, onset=None):
Asig with the added signal.
"""
if type(sig) == Asig:
if isinstance(sig, Asig):
n = sig.samples
sr = sig.sr
sigar = sig.sig
Expand Down
4 changes: 2 additions & 2 deletions pya/aspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def __init__(self, x, sr=44100, label=None, cn=None):
cn : list or Nonpya.asige
Channel names (Default value = None)
"""
if type(x) == pya.asig.Asig:
if isinstance(x, pya.asig.Asig):
self.sr = x.sr
self.rfftspec = np.fft.rfft(x.sig, axis=0)
self.label = x.label + "_spec"
self.samples = x.samples
self.channels = x.channels
self.cn = cn or x.cn
elif type(x) == list or type(x) == np.ndarray:
elif isinstance(x, np.ndarray):
# TODO. This is in the assumption x is spec. which is wrong. We define x to be the audio signals instead.
self.rfftspec = np.array(x)
self.sr = sr
Expand Down
2 changes: 1 addition & 1 deletion pya/astft.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, x, sr=None, label=None, window='hann', nperseg=256,
# self.cn = cn
self.im = None # buffer for the image

if type(x) == pya.asig.Asig:
if isinstance(x, pya.asig.Asig):
self.sr = x.sr
self.channels = x.channels
self.label = x.label + "_stft"
Expand Down

0 comments on commit 0531afd

Please sign in to comment.