-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrpi_dmxrecord.py
executable file
·159 lines (122 loc) · 4.39 KB
/
lrpi_dmxrecord.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
HOST = "127.0.0.1"
PORT = 4223
DEBUG = True
VERBOSE = True
# reading_interval = 1.0
from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_dmx import BrickletDMX
from time import sleep, perf_counter
from os.path import join, splitext
from os import listdir
import math, time, datetime
from numpy import array, zeros, array_equal
# import pysrt
import signal
import sys
from pysrt import SubRipFile, SubRipItem, SubRipTime
from tf_device_ids import deviceIdentifiersList
import argparse
SRT_FILENAME = "output_dmx.srt"
AUDIO_FILENAME = "input.mp4"
MAX_BRIGHTNESS = 254
TICK_TIME = 0.05 # seconds
srtFile = SubRipFile()
tfIDs = []
tfConnect = True
prevFrame = zeros(512)
prevTime = 0
subs = []
sub_incr = 1
ipcon = IPConnection()
# if tfConnect:
# tfIDs = []
deviceIDs = [i[0] for i in deviceIdentifiersList]
if DEBUG:
print(deviceIDs)
for i in range(len(deviceIDs)):
print(deviceIdentifiersList[i])
def getIdentifier(ID):
deviceType = ""
for t in range(len(deviceIDs)):
if ID[1]==deviceIdentifiersList[t][0]:
deviceType = deviceIdentifiersList[t][1]
return(deviceType)
# Tinkerforge bricklets enumeration
def cb_enumerate(uid, connected_uid, position, hardware_version, firmware_version,
device_identifier, enumeration_type):
tfIDs.append([uid, device_identifier])
def dmxread_callback(frame, frame_no):
global prevFrame, prevTime, subs, srtFile
# if prevFrame. == 0:
# prevFrame = array(frame)
frameArray = array(frame)
if not array_equal(prevFrame,frameArray):
if frame != None:
item = SubRipItem(1, text="DMX1"+str(frame))
item.shift(seconds=prevTime)
item.end.shift(seconds=perf_counter()-prevTime)
if VERBOSE:
print(item)
subs.append(item)
srtFile.append(item)
prevTime = perf_counter()
prevFrame = array(frame)
# if prevframe-frame:
# print(frame, frame_no)
# prev
# print("callback called")
def signal_handler(sig, frame):
global subs, tfConnect, ipcon, srtFile, SRT_FILENAME
if VERBOSE:
print(subs, len(subs))
encoding="utf_8"
srtFile.save(SRT_FILENAME, encoding=encoding)
if tfConnect:
ipcon.disconnect()
sys.exit(0)
def main():
global SRT_FILENAME, AUDIO_FILENAME, MAX_BRIGHTNESS, TICK_TIME, DEBUG, VERBOSE
parser = argparse.ArgumentParser(description="LushRoom sound and light command-line player")
# group = parser.add_mutually_exclusive_group()
# group.add_argument("-v", "--verbose", action="store_true")
# group.add_argument("-q", "--quiet", action="store_true")
parser.add_argument("-s","--srt", default=SRT_FILENAME, help=".srt file name for lighting events")
parser.add_argument("-a","--audio", default=AUDIO_FILENAME, help="audio file for sound stream")
parser.add_argument("-b","--brightness", default=MAX_BRIGHTNESS, help="maximum brightness")
parser.add_argument("-t","--time", default=TICK_TIME, help="time between events")
args = parser.parse_args()
print('args: ', args)
print('args.srt: ', args.srt)
SRT_FILENAME = args.srt
# global ipcon
ipcon.connect(HOST, PORT)
# Register Enumerate Callback
ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, cb_enumerate)
# Trigger Enumerate
ipcon.enumerate()
sleep(2)
if VERBOSE:
print("tfIds: ", tfIDs)
dmxcount = 0
for tf in tfIDs:
# try:
if True:
# print(len(tf[0]))
if len(tf[0])<=3: # if the device UID is 3 characters it is a bricklet
if tf[1] in deviceIDs:
print('printed tfIds: ', tf[0],tf[1], getIdentifier(tf))
if tf[1] == 285: # DMX Bricklet
if dmxcount == 0:
print("Registering %s as slave DMX device for capturing DMX frames" % tf[0])
dmx = BrickletDMX(tf[0], ipcon)
dmx.set_dmx_mode(BrickletDMX.DMX_MODE_SLAVE)
dmx.register_callback(BrickletDMX.CALLBACK_FRAME, dmxread_callback)
dmx.set_frame_callback_config(False, False, True, False)
signal.signal(signal.SIGINT, signal_handler)
dmxcount += 1
while True:
pass
if __name__ == "__main__":
main()