This repository has been archived by the owner on Jul 7, 2019. It is now read-only.
forked from sankalpg/Hodorizor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
karaokeParser.py
110 lines (83 loc) · 2.82 KB
/
karaokeParser.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
#import numpy as np
import sys
tagKeys = ['TITLE', 'ARTIST', 'LANGUAGE', 'MP3', 'COVER', 'BACKGROUND', 'BPM', 'GAP', 'EDITION']
class MyError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def parseKaraokeFile(karaokeTxtFile):
lines = open(karaokeTxtFile,'r').readlines()
fileContent = {}
fileContent['data'] = []
Offset = -1
durPBeat = -1
sumTones=0
cntTones=0
for line in lines:
if line.startswith("E"):
break
#Parsing tags
if line.startswith('#'):
try:
tag, content = parseTagLine(line)
fileContent[tag] = content
if tag == 'GAP':
Offset = content
if tag == 'BPM':
durPBeat = 60.0/(4*float(content))
continue
except MyError as e:
print e.value
continue
#Parse the data
if not fileContent.has_key('BPM'):
print "BPM tag not parsed correctly"
raise MyError('BPM_Not_Parsed')
lineSplt = line.split()
if len(lineSplt) == 5: #normal frame
start_time = Offset + float(lineSplt[1])*durPBeat
end_time = start_time + float(lineSplt[2])*durPBeat
sumTones+=int(lineSplt[3])
cntTones+=1
fileContent['data'].append({'type': lineSplt[0], 'start': start_time, 'end': end_time, 'tone':int(lineSplt[3]), 'syl':lineSplt[4], 'durBeats':int(lineSplt[2])})
else:
if lineSplt[0] == '-':
fileContent['data'].append({'type': lineSplt[0], 'start': -1, 'end': -1, 'tone': None, 'syl':'-'})
else:
print line
print "This is something really wierd happening here X1"
raise MyError('ParsingError')
# in addition filling start and end point of the silence regions as well
for ii, elem in enumerate(fileContent['data']):
if elem['type'] == '-':
fileContent['data'][ii]['start'] = fileContent['data'][ii-1]['end']
fileContent['data'][ii]['end'] = fileContent['data'][ii+1]['start']
#print Offset, durPBeat
#print fileContent
dumpSonicVisualizerAnnotFile('Test.txt', fileContent['data'])
#sometimes tones are in correct midi format and sometimes they have a wierd format of 0 being some C. detecting this automatically
if float(sumTones)/float(cntTones) < 35:
for ii,elem in enumerate(fileContent['data']):
if not elem['tone'] is None:
fileContent['data'][ii]['tone']+=60
return fileContent
def dumpSonicVisualizerAnnotFile(fileOut, data):
fid = open(fileOut, "w")
for elem in data:
fid.write("%f\t%f\t%s\n"%(elem['start'], elem['end'], elem['syl']))
fid.close()
def parseTagLine(tagline):
tagline = tagline.strip('#')
for tag in tagKeys:
if tagline.startswith(tag):
content = tagline.strip(tag).strip(':').strip()
if tag == 'BPM':
content = float(content.replace(',', '.'))
if tag == 'GAP':
content = float(content)/1000.0
return tag, content
raise MyError("InvalidTag")
if __name__ == "__main__":
karaokeTxtFile = sys.argv[1]
parseKaraokeFile(karaokeTxtFile)