Question

I want to extract 2 parts from 4 voice krn score and save them as a midi file.

I can load the files:

s = converter.parse('/something.krn') 

I can get some basic info like this:

s.metadata.title

In v2, I want to store the part of s that has a label "Cantus". Any idea how to check for a label? They have a label in krn.

Once I have the number of the part, I can get it with

s.parts[i]

The krn file is defined like this:

**kern  **kern  **kern  **kern  **kern
*Ibass  *Itenor *Itenor *Icalto *Icant
!Bassus !Tenor 2    !Tenor 1    !Altus  !Cantus

I am guessing labels is not the correct name, as I can't find this in music21 documentation, perhaps the name of the part?

I can't seem to find the property in the music21 documentation.

Was it helpful?

Solution

I was finally able to do it this way:

import sys
from music21 import *
import os
# input ("Please make sure that you have places all the krn files in a subdirectory called data. Press enter to continue")
for filename in os.listdir('./data'):
 s = converter.parse('./data/' + filename) 
 sys.stdout.write('Processing ' + filename + '... ')
 numcant = -1
 nums = list()
 try:
  length = len(s.parts)
 except: 
  length = 0
 if (length > 0): 
  for num in range(0,length): 
      # sys.stdout.write(s.parts[num].flat.getElementsByClass('SpineComment')[0].comment + ' - ') 
      if (s.parts[num].flat.getElementsByClass('SpineComment')[0].comment == "Cantus"):
        numcant = num
        # print "cant "
        # print numcant
      else:
        # print "nums"
        nums.append(num)
        # print num
 else:
    # sys.stdout.write(' - no parts present.')
    sys.stdout.write('\n')

 try:
  length = len(nums)
 except:
  length = 0
 if (length > 0):
  sys.stdout.write('\n')
  if (numcant != -1):
    for num in nums:
      sys.stdout.write(' - ' + filename[:-4] + '_' + str(num) + '.mid written.\n')  
      # print "cantus present"
      s2 = stream.Stream()
      s2.insert(0, s.parts[num])
      s2.insert(0, s.parts[numcant])
      # write the midi file 
      s2.write('midi', './midi/' + filename[:-4] + '_' + str(num) + '.mid')
      # sys.stdout.write('I')
  else:
     sys.stdout.write(' - no cantus specified for this file.\n')
 else:
  sys.stdout.write(' - not enough parts in this file.\n')
 sys.stdout.write('\n')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top