visakh7843 commited on
Commit
3b240b2
1 Parent(s): 1d4f575

Added time_sign and key_sign control

Browse files
markov_chain.pickle ADDED
Binary file (23.1 kB). View file
 
markov_chain.py CHANGED
@@ -252,7 +252,7 @@ if __name__=="__main__":
252
  # !lilypond -fpng "new_song-midi.ly"
253
 
254
  #Converting abc2ly
255
- # abc2ly "new_song.ly"
256
  #Then same command to convert to png which will generate midi as well
257
 
258
  #midi to abc
 
252
  # !lilypond -fpng "new_song-midi.ly"
253
 
254
  #Converting abc2ly
255
+ # abc2ly "new_song.abc"
256
  #Then same command to convert to png which will generate midi as well
257
 
258
  #midi to abc
n-grams/markov_chain.pickle ADDED
Binary file (6 Bytes). View file
 
n-grams/music.py CHANGED
@@ -9,9 +9,14 @@ import pickle
9
  corpus = []
10
  song = []
11
 
12
- "Generate sheet music for a new song from a corpus of existing songs in abc format"
13
-
14
-
 
 
 
 
 
15
  # get the list of filenames (abc files downloaded from http://www.norbeck.nu/abc/)
16
  # getdirs = []
17
  # dirs = ["hn201612/i/*.abc", "hn201612/s/*.abc"]
@@ -21,7 +26,7 @@ song = []
21
  # for filename in glob.iglob(dir1):
22
  # getdirs += [filename]
23
 
24
-
25
  #Finds all absolute paths in directory
26
  #https://stackoverflow.com/questions/9816816/get-absolute-paths-of-all-files-in-a-directory
27
  def abs_paths(dir):
@@ -30,16 +35,23 @@ def abs_paths(dir):
30
  yield os.path.abspath(os.path.join(dir_path, f))
31
  # ex_filename = "hn201612/i/hnsong1.abc"
32
  # parsing on file to extract songs and add them to corpus
33
- for filename in abs_paths("n-grams/data/hard"):
34
  with open(filename) as f:
 
35
  lines = f.readlines()
36
  last = len(lines)
 
37
  for index, line in enumerate(lines):
38
  if (line.find("|") < 0 and index - 1 == last):
39
  # if the next line does not have pipes add song to corpus and then set song variable empty again
40
- corpus.append(song)
 
 
 
 
 
 
41
  song = []
42
-
43
  else:
44
  if line.find("|") > -1:
45
  # a line should be split on "|" and copied to the corpus if it has pipes
@@ -47,7 +59,18 @@ for filename in abs_paths("n-grams/data/hard"):
47
  # add the list of measures to the song
48
  song += [x.strip("\r\n") for x in sline if len(x.strip("\r\n")) > 0]
49
  last = index
50
-
 
 
 
 
 
 
 
 
 
 
 
51
  print("Training on {} songs...".format(len(corpus)))
52
 
53
  # MARKOV PART
@@ -75,8 +98,9 @@ def writesong(songlength, first):
75
  # choose a random song length from list of song lengths in corpus
76
  lengthofsong = random.choice([len(x) for x in corpus if len(x) > 10])
77
  print("Song length will be {}".format(lengthofsong))
78
-
79
-
 
80
  firstnote = markov.generate(model, n, max_iterations=3)[0]
81
  # print "first note: {}".format(firstnote)
82
 
 
9
  corpus = []
10
  song = []
11
 
12
+ #TODO: convert these into inputs
13
+ # lengthofsong = 10 Should we control this? Setting it to random now
14
+ timesignature = ['3/4','4/4','1/8','C'] #Sometimes the letter “C” (meaning common time) will be used in place of 4/4.
15
+ #Both C and 4/4 indicate that there are four quarter note beats in each measure.
16
+ keysignature = "G"
17
+ key_enforced = False
18
+ key_enforced = True #Set to true if user wants in specific key
19
+ print("hello")
20
  # get the list of filenames (abc files downloaded from http://www.norbeck.nu/abc/)
21
  # getdirs = []
22
  # dirs = ["hn201612/i/*.abc", "hn201612/s/*.abc"]
 
26
  # for filename in glob.iglob(dir1):
27
  # getdirs += [filename]
28
 
29
+ selected_timeSign = '3/4'
30
  #Finds all absolute paths in directory
31
  #https://stackoverflow.com/questions/9816816/get-absolute-paths-of-all-files-in-a-directory
32
  def abs_paths(dir):
 
35
  yield os.path.abspath(os.path.join(dir_path, f))
36
  # ex_filename = "hn201612/i/hnsong1.abc"
37
  # parsing on file to extract songs and add them to corpus
38
+ for filename in abs_paths("n-grams/data/beginner"):
39
  with open(filename) as f:
40
+ print(filename)
41
  lines = f.readlines()
42
  last = len(lines)
43
+ accepted = False
44
  for index, line in enumerate(lines):
45
  if (line.find("|") < 0 and index - 1 == last):
46
  # if the next line does not have pipes add song to corpus and then set song variable empty again
47
+ if accepted and key_enforced and key_accepted:
48
+ corpus.append(song)
49
+ accepted = False
50
+ key_accepted = False
51
+ if accepted:
52
+ corpus.append(song)
53
+ accepted = False
54
  song = []
 
55
  else:
56
  if line.find("|") > -1:
57
  # a line should be split on "|" and copied to the corpus if it has pipes
 
59
  # add the list of measures to the song
60
  song += [x.strip("\r\n") for x in sline if len(x.strip("\r\n")) > 0]
61
  last = index
62
+ elif "M:" in line:
63
+ #time signature
64
+ if selected_timeSign == "4/4":
65
+ if "4/4" in line or "C|" in line:
66
+ accepted = True
67
+ elif selected_timeSign in line:
68
+ accepted = True
69
+ elif line.find("K:") and key_enforced:
70
+ #key signature
71
+ if keysignature in line:
72
+ key_accepted = True
73
+
74
  print("Training on {} songs...".format(len(corpus)))
75
 
76
  # MARKOV PART
 
98
  # choose a random song length from list of song lengths in corpus
99
  lengthofsong = random.choice([len(x) for x in corpus if len(x) > 10])
100
  print("Song length will be {}".format(lengthofsong))
101
+ song_len = [len(x) for x in corpus if len(x)>10]
102
+ song_len.sort()
103
+ print("Song lengths",song_len)
104
  firstnote = markov.generate(model, n, max_iterations=3)[0]
105
  # print "first note: {}".format(firstnote)
106
 
new_song.mid CHANGED
Binary files a/new_song.mid and b/new_song.mid differ