-
Notifications
You must be signed in to change notification settings - Fork 1
/
haptic_pattern_creator.py
83 lines (63 loc) · 2.58 KB
/
haptic_pattern_creator.py
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
import os
import datetime
import json
import numpy as np
import librosa
import librosa.display
from scipy.io import wavfile
from scipy import signal
from matplotlib import pyplot as plt
class AudioHapticPattern:
def __init__(self):
self.pattern_data = {
"Version": 1.0,
"Metadata": {
"Project": "Basis",
"CreationDate": str(datetime.datetime.now()),
"Description": "Generated AHAP file from wav. Github: AHAPGenerator",
"Author": "Ryan Han"
},
"Pattern": []
}
def add_haptic_event(self, event_type, timestamp, params, duration=None, waveform_path=None):
event = {
"Event": {
"Time": timestamp,
"EventType": event_type,
"EventParameters": params
}
}
if duration is not None:
event["Event"]["EventDuration"] = duration
if waveform_path is not None:
event["Event"]["EventWaveformPath"] = waveform_path
self.pattern_data["Pattern"].append(event)
def add_transient_haptic(self, timestamp, intensity=0.5, sharpness=0.5):
params = [
{"ParameterID": "HapticIntensity", "ParameterValue": intensity},
{"ParameterID": "HapticSharpness", "ParameterValue": sharpness}
]
self.add_haptic_event("HapticTransient", timestamp, params)
def add_continuous_haptic(self, timestamp, duration=1, intensity=0.5, sharpness=0.5):
params = [
{"ParameterID": "HapticIntensity", "ParameterValue": intensity},
{"ParameterID": "HapticSharpness", "ParameterValue": sharpness}
]
self.add_haptic_event("HapticContinuous", timestamp, params, duration=duration)
def add_custom_audio_event(self, timestamp, audio_path, volume=0.75):
params = [{"ID": "Volume", "Value": volume}]
self.add_haptic_event("AudioCustom", timestamp, params, waveform_path=audio_path)
def add_control_point_curve(self, param_id, start_time, control_points):
curve = {
"ControlCurve": {
"ParameterID": param_id,
"Time": start_time,
"ParameterCurveControlPoints": control_points
}
}
self.pattern_data["Pattern"].append(curve)
def display_pattern(self):
print(json.dumps(self.pattern_data, indent=4))
def save_pattern(self, file_name, directory):
with open(os.path.join(directory, file_name), 'w') as file:
json.dump(self.pattern_data, file, indent=4)