def handle_post(userid, n_samples, in_data_raw, new_events, query_string, print_status=True) -> Tuple[Any, str]: try: enc, dec = users[userid] except KeyError: enc = opuslib.Encoder(server.SAMPLE_RATE, CHANNELS, opuslib.APPLICATION_AUDIO) dec = opuslib.Decoder(server.SAMPLE_RATE, CHANNELS) users[userid] = enc, dec in_data = np.frombuffer(in_data_raw, dtype=np.uint8) # If the user does not send us any data, we will treat it as silence of length n_samples. This is useful if they are just starting up. if len(in_data) == 0: if n_samples == 0: raise ValueError("Must provide either n_samples or data") in_data = np.zeros(n_samples, np.float32) else: packets = unpack_multi(in_data) decoded = [] for p in packets: d = dec.decode_float(p.tobytes(), OPUS_FRAME_SAMPLES, decode_fec=False) decoded.append(np.frombuffer(d, np.float32)) in_data = np.concatenate(decoded) # Sending n_samples is optional if data is sent, but in case of both they must match if n_samples == 0: n_samples = len(in_data) if n_samples != len(in_data): raise ValueError( "Client is confused about how many samples it sent (got %s expected %s" % (n_samples, len(in_data))) if shared_memory is not None: data, x_audio_metadata = handle_json_post(in_data, new_events, query_string, print_status) else: data, x_audio_metadata = server.handle_post(in_data, new_events, query_string, print_status) packets = data.reshape([-1, OPUS_FRAME_SAMPLES]) encoded = [] for p in packets: e = np.frombuffer(enc.encode_float(p.tobytes(), OPUS_FRAME_SAMPLES), np.uint8) encoded.append(e) data = pack_multi(encoded).tobytes() return data, x_audio_metadata
def test_post(): q = Mock() filename = 'afakefilename' files = {'file': [{'body': b'a-fake-file-body', 'filename': filename}]} hash_object = hashlib.md5(filename.encode()) audio_filename = hash_object.hexdigest() + "-" + filename analysis_filename = audio_filename + '.analysis.json' expected = {'analysis': get_url(analysis_filename), 'audio': get_url(audio_filename)} actual = json.loads(handle_post(q, files, faux_upload, faux_analyze)) q.enqueue.assert_called_with(do_work, (ANY, audio_filename, analysis_filename, faux_upload, faux_analyze)) assert expected == actual
def fake_inner_request(): server.handle_post( data, [], query_string())
def fake_request(): server.handle_post(in_data_raw, query_params={ "read_clock": str(int(time.time()) * server.SAMPLE_RATE) })
def fake_inner_request(): server.handle_post(data, [], query_string(), print_status=False)