File size: 2,935 Bytes
6a16e42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from __future__ import annotations

import hashlib
import json
import math
from pathlib import Path

import numpy as np

SR = 16000
STEP = 1600
VERSION = 'dish_dynamic_demo_v1'


def speech_activity(wave, offset):
    """Frozen 10 ms energy activity labels, including natural phonetic pauses."""
    n=len(wave)
    energy=np.mean(np.pad(wave,(0,(-n)%160)).reshape(-1,160).astype(float)**2,axis=1)
    mask=energy>max(float(energy.max())*.001,1e-10)
    spans=[];start=None
    for i,on in enumerate(np.r_[mask,False]):
        if on and start is None:start=i*160
        if not on and start is not None:
            spans.append([offset+start,offset+min(i*160,n)]);start=None
    return spans


def read(path):
    return json.loads(Path(path).read_text())


def write(path, value):
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(value, ensure_ascii=False, indent=2, allow_nan=False) + '\n')


def jsonl(path, values):
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open('w') as f:
        for v in values:
            f.write(json.dumps(v, ensure_ascii=False, allow_nan=False) + '\n')


def sha(path):
    h = hashlib.sha256()
    with Path(path).open('rb') as f:
        for b in iter(lambda: f.read(1024 * 1024), b''):
            h.update(b)
    return h.hexdigest()


def uid(*parts):
    return hashlib.sha256('/'.join(map(str, (VERSION,) + parts)).encode()).hexdigest()[:24]


def position(entity, t):
    """Minimum-jerk horizontal displacement; fixed height/yaw, finite acceleration."""
    u = np.clip(np.asarray(t, dtype=float) / entity['motion_duration_s'], 0., 1.)
    s = 10*u**3 - 15*u**4 + 6*u**5
    return np.asarray(entity['initial_xyz_m']) + np.expand_dims(s, -1)*np.asarray(entity['displacement_m'])


def velocity(entity, t):
    u = np.clip(np.asarray(t, dtype=float) / entity['motion_duration_s'], 0., 1.)
    speed = (30*u**2 - 60*u**3 + 30*u**4) / entity['motion_duration_s']
    return np.expand_dims(speed, -1)*np.asarray(entity['displacement_m'])


def geometry(source, robot, t):
    d = position(source, t) - position(robot, t)
    x, y, z = map(float, d)
    az = (-(math.degrees(math.atan2(x, -z))-robot['yaw_deg'])+180) % 360-180
    return dict(doa_deg=[az, math.degrees(math.atan2(y, math.hypot(x, z)))],
                distance_m=float(np.linalg.norm(d)), xyz_m=position(source, t).tolist())


def active(source, sample):
    return any(a <= sample < b for event in source['events'] for a, b in event['activity_samples'])


def angular(a, b):
    a, b = np.radians(a), np.radians(b)
    return float(np.degrees(np.arccos(np.clip(np.sin(a[1])*np.sin(b[1]) +
        np.cos(a[1])*np.cos(b[1])*np.cos(a[0]-b[0]), -1., 1.))))


def grid(scene):
    return list(range(0, scene['duration_samples'], STEP))


def tokens(text):
    import re
    return re.findall(r"[a-z0-9]+(?:'[a-z0-9]+)*", text.lower())