/
MakamScore.py
217 lines (141 loc) · 7.08 KB
/
MakamScore.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: utf-8 -*-
'''
contains a class
Created on Mar 3, 2014
@author: joro
'''
import os
import sys
import imp
from Phonetizer import Phonetizer
from Decoder import logger
# trick to make terminal NOT assume ascii
reload(sys).setdefaultencoding("utf-8")
parentDir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0]) ), os.path.pardir))
pathUtils = os.path.join(parentDir, 'utilsLyrics')
sys.path.append(pathUtils )
from Utilz import writeListToTextFile, findFileByExtensions
import codecs
import glob
from SymbTrParser import SymbTrParser
# from utils.Utils import writeListToTextFile
#
# COMPOSITION_NAME = 'muhayyerkurdi--sarki--duyek--ruzgar_soyluyor--sekip_ayhan_ozisik'
# COMPOSITION_NAME = 'huseyni--sarki--turkaksagi--hicran_oku--sevki_bey'
#
# PATH_TEST_DATASET='/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data/'
# PATH_TEST_DATASET = '/Volumes/IZOTOPE/sertan_sarki/'
class MakamScore():
'''
classdocs
'''
##################################################################################
def __init__(self, pathToSymbTrFile, pathToSectionTsvFile):
'''
Constructor
'''
self.compositionName = os.path.splitext(pathToSymbTrFile)[0]
''' lyrics divided into sectons.
# e.g. "nakarat" : [ word1 word2 ] '''
self.sectionToLyricsMap = []
self._loadSectionsAndSyllablesFromSymbTr(pathToSymbTrFile, pathToSectionTsvFile)
# pats to individual ..txt lyrics files.
self.pathsTolyricSectionFiles = []
##################################################################################
'''
parses symbTr file. Reads lyrics,
reads section names
groups together section names and lyrics
'''
def _loadSectionsAndSyllablesFromSymbTr(self, pathToSymbTrFile, pathToSectionTsvFile):
symbTrParser = SymbTrParser(pathToSymbTrFile, pathToSectionTsvFile)
# list of Word object
symbTrParser.syllables2Lyrics()
lyricsAllSections = symbTrParser.sectionLyrics
# for each section part
for currSectionBoundary,currSectionLyrics in zip(symbTrParser.sectionboundaries, lyricsAllSections):
tupleSectionNameAndLyrics = currSectionBoundary[0], currSectionLyrics
self.sectionToLyricsMap.append(tupleSectionNameAndLyrics)
def getLyricsForSection(self,sectionNumber):
'''
convenience getter
'''
if sectionNumber >= len(self.sectionToLyricsMap) or sectionNumber < 0:
sys.exit("section withNumber {} not present in score. Check your .sections.tsv file".format(sectionNumber) )
#python indexing starts from zero
lyrics = self.sectionToLyricsMap[sectionNumber][1]
if not lyrics.listWords:
logger.warn("no lyrics for demanded section {} : {}".format(sectionNumber, self.sectionToLyricsMap[sectionNumber][0] ))
return None
return lyrics
##################################################################################
def printSectionsAndLyrics(self):
'''
utility method to print all lyrics that are read from symbTr
'''
for currSection in self.sectionToLyricsMap:
print '\n' + str(currSection[0]) + ':'
print currSection[1]
# for word in currSection[1]:
# print word.__str__().encode('utf-8','replace')
def serializePhonemesForSection(self, whichSection, outputFileName):
'''
list of all phonemes. print to file @param outputFileName
'''
lyrics = self.getLyricsForSection(whichSection)
if not lyrics:
sys.exit("no lyrics")
writeListToTextFile(lyrics.phonemesNetwork, None, outputFileName )
return lyrics.phonemesNetwork
def printSyllables(self, whichSection):
'''
debug: print syllables
'''
lyrics = self.getLyricsForSection(whichSection)
if not lyrics:
sys.exit("no lyrics")
lyrics.printSyllables
def loadLyrics(pathToComposition, whichSection):
Phonetizer.initLookupTable(False)
os.chdir(pathToComposition)
pathTotxt = os.path.join(pathToComposition, glob.glob("*.txt")[0])
listExtensions = [ "sections.json", "sections.tsv", "sections.txt"]
sectionFiles = findFileByExtensions(pathToComposition, listExtensions)
sectionFile = sectionFiles[0]
pathToSectionTsv = os.path.join(pathToComposition, sectionFile)
makamScore = MakamScore(pathTotxt, pathToSectionTsv )
# phoneme IDs
lyrics = makamScore.getLyricsForSection(whichSection)
return lyrics
def loadMakamScore(pathToComposition):
'''
same as loadLyrics, but return MakamScore, so that all lyrics can be shown if needed
'''
Phonetizer.initLookupTable(False)
os.chdir(pathToComposition)
pathTotxt = os.path.join(pathToComposition, glob.glob("*.txt")[0])
listExtensions = [ "sections.json", "sections.tsv", "sections.txt"]
sectionFiles = findFileByExtensions(pathToComposition, listExtensions)
sectionFile = sectionFiles[0]
pathToSectionTsv = os.path.join(pathToComposition, sectionFile)
makamScore = MakamScore(pathTotxt, pathToSectionTsv )
return makamScore
def testMakamScore(argv):
if len(argv) != 2:
print ("usage: {} <path to symbtTr.txt and symbTr.sections.tsv>".format(argv[0]) )
sys.exit();
pathToComposition = argv[1]
makamScore = loadMakamScore(pathToComposition)
makamScore.printSectionsAndLyrics()
# lyrics = loadLyrics(pathToComposition, whichSection=0)
if __name__ == '__main__':
# only for unit testing purposes
print "in Makam Score"
a = ['dummy', '/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data-synthesis/ussak--sarki--aksak--bu_aksam_gun--tatyos_efendi/']
a = ['dummy', '/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data-synthesis/rast--turku--semai--gul_agaci--necip_mirkelamoglu/']
a = ['dummy', '/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data-synthesis/nihavent--sarki--duyek--bir_ihtimal--osman_nihat_akin/']
a = ['dummy', '/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data-synthesis/segah--sarki--curcuna--olmaz_ilac--haci_arif_bey/']
a = ['dummy', '/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data/rast--sarki--curcuna--nihansin_dideden--haci_faik_bey/']
a = ['dummy', '/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data/rast--sarki--sofyan--gelmez_oldu--dramali_hasan/']
# a = ['dummy', '/Users/joro/Documents/Phd/UPF/turkish-makam-lyrics-2-audio-test-data/rast--turku--semai--gul_agaci--necip_mirkelamoglu/']
testMakamScore(a)