def loadMemory(self): """Call the sa api to get all names.""" self.keys = [tmp.name for tmp in sa.list()] if len(self.keys) > 0 and isinstance(self.keys[0], bytes): self.keys = [key.decode('utf-8') for key in self.keys] # also re_construct dct_keys slash_finder = re.compile('^(\w+\\\\)') def assign_key_to_dct(dct, key, header): """For some keys, convert to corresponding things such as keys of dict. :param dct: a dictionary to write data into :param key: a key being processed :param header: str, the header, header\\\\ is removed """ if header is not None: use_key = key.replace(header, '') # remove those slash else: use_key = key grp = slash_finder.findall(use_key) if len(grp) == 0: dct[use_key] = None else: if header is None: header = grp[0].replace('\\', '') else: header = header.replace('\\', '') if not header in dct: dct[header] = {} assign_key_to_dct(dct[header], use_key, grp[0]) for key in self.keys: assign_key_to_dct(self.dct_keys, key, None)
async def video_feed(request): if "camera" in [e.name.decode() for e in sa.list()]: return StreamingResponse( generator(request), media_type='multipart/x-mixed-replace; boundary=frame') else: return Response('camera not online', 404)
def get_publisher(channel: str, shape: tuple, dtype) -> np.ndarray: # Create an array in shared memory. short_name = channel.split("://")[-1] mapping = {e.name.decode(): e for e in sa.list()} if short_name in mapping: array = mapping[short_name] if array.dtype == dtype and array.dims == shape: return sa.attach(channel) sa.delete(short_name) return sa.create(channel, shape, dtype)
#!/usr/bin/python # -*- coding: UTF-8 -*- import numpy as np import SharedArray as sa import glob,h5py,os from norm_lib import * print(sa.list()) def load(data_name): try: Xtr = sa.attach('shm://%s_Xtr'%(data_name)) Ytr = sa.attach('shm://%s_Ytr'%(data_name)) Ytr_p = sa.attach('shm://%s_Ytr_pitch'%(data_name)) Ytr_s = sa.attach('shm://%s_Ytr_stream'%(data_name)) except: # load cqt trdata = h5py.File('../ex_data/tr.h5', 'r') Xtr = sa.create('shm://%s_Xtr'%(data_name), (trdata['x'].shape), dtype='float32') Xtr[:] = trdata['x'][:] #load instrument label Ytr = sa.create('shm://%s_Ytr'%(data_name), (trdata['yi'].shape), dtype='float32') Ytr[:] = trdata['yi'][:] #load pitch label Ytr_p = sa.create('shm://%s_Ytr_pitch'%(data_name), (trdata['yp'].shape), dtype='float32') Ytr_p[:] = trdata['yp'][:] #load pianoroll label Ytr_s = sa.create('shm://%s_Ytr_stream'%(data_name), (trdata['ys'].shape), dtype='float32') Ytr_s[:] = trdata['ys'][:] return Xtr, Ytr, Ytr_p, Ytr_s
# pygame code comes here import pygame import pygame.midi from pygame.locals import * import signal import SharedArray as sa import numpy as np import sys from musicai.main.lib.predict import predict from musicai.utils.chords import get_notes, get_chord_mapping if sys.argv[1] == "--test": print(predict([1, 2, 3, 4, 5, 6, 7])) sys.exit() if sa.list(): sa.delete("shm://notes") queue = sa.create("shm://notes", 4) for i in range(0, len(queue)): queue[i] = 0.0 tempo = 80 # int(input("Enter the tempo : ").strip()) input("Enter to start...") bar_length = int((4 / tempo) * 60) # bar length in seconds print(bar_length) chords = get_chord_mapping() def push_notes(signum, frame): # read midi data of global array
def tearDownClass(cls) -> None: assert len( sa.list() ) == 0, f"Not all shared arrays have been freed. Leftover: {sa.list()}"
def setUpClass(cls) -> None: for a in sa.list(): sa.delete(a.name.decode("utf-8"))
def tearDown(self): import SharedArray as sa assert len(sa.list()) == 0
def setUpClass(cls) -> None: import SharedArray as sa for arr in sa.list(): sa.delete(arr.name.decode("utf-8"))
def free_all(): for arr in sa.list(): sa.delete(arr.name.decode("utf-8"))
def ListNames(): ll = list(SharedArray.list()) return [AR.name for AR in ll]
def remove_shared_memory(): for shared in sa.list(): logging.info("Deleting %s" % shared.name.decode("utf-8")) sa.delete(shared.name.decode("utf-8"))
def clearSA(): for key in sa.list(): sa.delete(key.name)
def listing(): for n in sa.list(): print(n) print('%d shared arrays'%len(sa.list()))
def ListNames(): T = ClassTimeIt.ClassTimeIt(" SharedToDico") ll = list(SharedArray.list()) return [(AR.name).decode("ascii") for AR in ll]