/
smike.py
executable file
·181 lines (132 loc) · 5.02 KB
/
smike.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time, collections, RPi.GPIO as GPIO, os, sys
from omxplayer import OMXPlayer
if os.path.exists("/home/pi/SMIKE/Smike.pid"):
sys.exit(0)
else:
open("/home/pi/SMIKE/Smike.pid", "w").close()
ReedBeatPin = 4
ReedBrakePin = 17
LEDPin = 18
SongDict = collections.OrderedDict()
def UpdateFrequency(channel):
global Time, ActualFrequency
if time.time() - Time > Bias:
##Frequency Check
#print("CONTACT")
Tmp = time.time()
PeriodTime = Tmp - Time
Time = Tmp
ActualFrequency = 1/PeriodTime*60*2
global logfile
logfile.write(str(ActualFrequency)+"; ")
def UpdateSong(SongId):
global Fading
Fading = True
def OnBrakeRising(channel):
GPIO.output(LEDPin,GPIO.LOW)
GPIO.remove_event_detect(ReedBrakePin)
GPIO.add_event_detect(ReedBrakePin, GPIO.FALLING, callback=OnBrakeFalling)
#print("OFF")
def OnBrakeFalling(channel):
GPIO.output(LEDPin,GPIO.HIGH)
GPIO.remove_event_detect(ReedBrakePin)
GPIO.add_event_detect(ReedBrakePin, GPIO.RISING, callback=OnBrakeRising)
#print("OFF")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(ReedBeatPin, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(ReedBrakePin, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(LEDPin, GPIO.OUT)
GPIO.add_event_detect(ReedBeatPin, GPIO.RISING, callback=UpdateFrequency)
GPIO.add_event_detect(ReedBrakePin, GPIO.FALLING, callback=OnBrakeFalling)
##Frequency
PeriodTime = 0
Time = time.time()
Frequency = 1
ActualFrequency = 1
##User Data
Tolerance = 10
MinSongTime = 10
MinSongTimeRatio = 20
PushFactor = 1.05
Bias = 0.2
BlendSpeed = 5
Volume = CurrentVolume = -1100
##Song Data
CurrentSong = 1
LastSongStart = Time
Fading = False
SongUpdated = False
##SongDict Import
import csv
reader = csv.reader(open('/home/pi/SMIKE/Playlist.csv', 'r'))
Header = []
is_Header = True
for row in reader:
if is_Header:
Header = row
is_Header = False
else:
SongDict[int(row[0])] = {}
for index in range(1, len(Header)):
if Header[index] == "BPM":
SongDict[int(row[0])][Header[index]] = int(row[index])
else:
SongDict[int(row[0])][Header[index]] = row[index]
Song = (SongDict[1]["Title"]+'.mp3')
player = OMXPlayer('/home/pi/Music/' + Song)
logfile = open("/home/pi/SMIKE/Log.txt", "w")
try:
#UpdateSong(1)
player.play()
player.set_volume(CurrentVolume)
##Tick
while True:
##Frequqncy Interpolation
Frequency += (ActualFrequency-Frequency)*0.025
Frequency = min(Frequency, 4000)
#print(Frequency)
##print(int(Frequency), MinSongTimeRatio * (Tolerance/abs(Frequency - SongDict[CurrentSong]["BPM"]))**0.066)
##Song Update
if Fading:
LastSongStart = time.time()
if not SongUpdated:
CurrentVolume = max(-3333, CurrentVolume - 100*BlendSpeed)
if CurrentVolume == -3333:
SongUpdated = True
else:
if CurrentVolume == -3333:
player.quit()
player = OMXPlayer('/home/pi/Music/' + (SongDict[CurrentSong]["Title"]+'.mp3'))
player.play()
CurrentVolume = min(Volume, CurrentVolume + 100*BlendSpeed)
if CurrentVolume == Volume:
SongUpdated = Fading = False
player.set_volume(CurrentVolume)
print(CurrentVolume, CurrentSong)
elif SongDict[CurrentSong]["BPM"] - Tolerance < Frequency < SongDict[CurrentSong]["BPM"] + Tolerance:
pass
else:
CurrentDelta = Frequency - SongDict[CurrentSong]["BPM"]
MinSongTime = MinSongTimeRatio * (Tolerance/abs(CurrentDelta))**0.1
if time.time() - LastSongStart > MinSongTime:
OldSong = CurrentSong
if CurrentDelta > 0:
while CurrentSong < len(SongDict) and abs(Frequency - SongDict[CurrentSong+1]["BPM"]) <= abs(Frequency - SongDict[CurrentSong]["BPM"])*PushFactor:
CurrentSong += 1
if OldSong != CurrentSong:
UpdateSong(CurrentSong)
elif CurrentDelta < 0 and CurrentSong != 1:
while CurrentSong > 1 and abs(Frequency - SongDict[CurrentSong-1]["BPM"])*PushFactor <= abs(Frequency - SongDict[CurrentSong]["BPM"]):
CurrentSong -= 1
if OldSong != CurrentSong:
UpdateSong(CurrentSong)
else:
pass
#print("w8 m8 1337: ", MinSongTime - (time.time() - LastSongStart), MinSongTime, CurrentDelta)
except KeyboardInterrupt:
player.quit()
logfile.close()
GPIO.output(LEDPin,GPIO.LOW)