-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtextMusicCombine.py
99 lines (80 loc) · 3.42 KB
/
textMusicCombine.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
# -*- coding: utf-8 -*-
# Python script for analyzing phonemic data from IPA-encoded poems
# Copyright (C) 2015 Kris P. Shaffer
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from music21 import *
import codecs
import fnmatch
from os import listdir
musicDirectory = 'music'
textDirectory = 'texts'
destinationDirectory = 'textAndMusic'
def getText(directory, filename):
return [line.rstrip('\n') for line in codecs.open((directory + '/' + filename), encoding='utf-8')]
def wholeSong(content):
song = ''
for line in content:
if line != '':
song += line
song += ' '
return song
def syllabify(songTextAsSingleString):
return songTextAsSingleString.replace('.', ' ').split()
# run
musicCorpus = []
for file in listdir(musicDirectory):
if fnmatch.fnmatch(file, '*Music.xml'):
musicCorpus.append(file)
for musicFilename in musicCorpus:
songTitle = musicFilename.split('Music.')[0]
textFilename = songTitle + 'IPAMusic.txt'
text = syllabify(wholeSong(getText(textDirectory, textFilename)))
s = converter.parse(musicDirectory + '/' + musicFilename)
slurflag = False
for n in s.flat.notes:
if n.isGrace == False:
if n.getSpannerSites() == []:
if text:
if n.tie:
if n.tie.type in ['stop', 'continue']:
if slurflag == True:
n.lyric = text[0]
else:
n.lyric = text.pop(0)
else:
n.lyric = text[0]
else:
if slurflag == True:
n.lyric = text[0]
else:
n.lyric = text.pop(0)
else:
ss = n.getSpannerSites()
for thisSpanner in ss:
if 'Slur' in thisSpanner.classes:
if thisSpanner.isLast(n):
slurflag = False
if text:
if n.tie:
if n.tie.type in ['stop', 'continue']:
n.lyric = text.pop(0)
else:
n.lyric = text[0]
else:
n.lyric = text.pop(0)
else:
if text:
n.lyric = text[0]
slurflag = True
writePath = destinationDirectory + '/' + songTitle + '.xml'
s.write(fp=writePath)
print writePath, "created."