Пример #1
0
def GScsv2RDF(infilename, outfilename, format="xml", withdescriptions=False):
	if withdescriptions:
		commonchords = ConjunctiveGraph()
		commonchords.load("CommonChords.rdf")
		extrachords = ConjunctiveGraph()

	lines = open(infilename).readlines()
	
	#
	# Initial model bits
	#
	mi = mopy.MusicInfo()

	homepage = mopy.foaf.Document("http://sourceforge.net/projects/motools")
	mi.add(homepage)
	program = mopy.foaf.Agent()
	program.name = "GScsv2RDF.py"
	program.homepage = homepage
	mi.add(program)

	tl = TimeLine("#tl")
	tl.label = "Timeline derived from "+infilename
	tl.maker = program
	mi.add(tl)

	[artistStr, titleStr] = [f.strip() for f in lines[0].split("\t")]
	# Add artist & title metadata
	signal = Signal()
	signal.time = sig_int = Interval()
	sig_int.label="Whole signal interval";
	sig_int.beginsAtDuration = secondsToXSDDuration(0);
	sig_int.onTimeLine = tl
	signal.published_as = track = Track()
	artist = MusicArtist()
	artist.made = track
	artist.name = artistStr
	track.title = titleStr
	mi.add(sig_int)
	mi.add(signal)
	mi.add(track)
	mi.add(artist)
	
	
	
	
	lineNum = 1
	segmentNum = 0
	thisSegment_i = None
	chordSymbol=''

	t_secs = 0.0
	
	for line in lines[1:]:
#		print "parsing line "+str(lineNum)
		try:
			lastChordSymbol = chordSymbol
			t_secs = getTimestamp(line)
			chordSymbol = getChordSymbol(line)
			if chordSymbol != lastChordSymbol:
#				print " handling new chord symbol"
				segmentNum += 1

				lastSegment_i = thisSegment_i
				thisSegment_i = Interval("#i_"+str(segmentNum))
				thisSegment_i.beginsAtDuration = secondsToXSDDuration(t_secs)
				if lastSegment_i != None:
#					print " terminating last interval"
					lastSegment_i.endsAtDuration = secondsToXSDDuration(t_secs)
					thisSegment_i.intervalAfter = lastSegment_i
					lastSegment_i.intervalBefore = thisSegment_i
				mi.add(thisSegment_i)
				
			
				chordURI = "http://purl.org/ontology/chord/symbol/"+chordSymbol.replace("#","s").replace(",","%2C")

				if withdescriptions and \
				   len(list(commonchords.predicate_objects(URIRef(chordURI)))) == 0 and \
				   len(list(extrachords.predicate_objects(URIRef(chordURI)))) == 0:
					# Deref to grab chord info
					print "loading <"+chordURI+">..."
					extrachords.load(chordURI)

				c = Chord(chordURI)
				c_event = ChordEvent("#ce_"+str(segmentNum))
				c_event.chord = c
				c_event.time = thisSegment_i
				c_event.label = chordSymbol
					
				mi.add(c); mi.add(c_event);
#				print " added new chord event for "+chordURI
							
		except Exception, e:
			print("ERROR : Problem parsing input file at line "+str(lineNum)+" !\n")
			raise
		lineNum+=1
Пример #2
0
def mma2RDF(infilename, outfilename, format="xml", audiofilename=None, withdescriptions=False):
    if withdescriptions:
        commonchords = ConjunctiveGraph()
        commonchords.load("CommonChords.rdf")
        extrachords = ConjunctiveGraph()

        # Compile mma file and grab output
    lines = os.popen(mmabin + ' "' + infilename + '" -nrw').readlines()
    print "\n".join(lines)

    #
    # Initial model bits
    #
    mi = mopy.MusicInfo()

    homepage = mopy.foaf.Document("http://sourceforge.net/projects/motools")
    mi.add(homepage)
    program = mopy.foaf.Agent()
    program.name = "mma2RDF.py"
    program.homepage = homepage
    mi.add(program)

    tl = TimeLine("#tl")
    tl.label = "Timeline derived from " + infilename
    tl.maker = program
    mi.add(tl)

    # extract tempo from mma file
    tempo = 60
    mmafile = open(infilename, "r")
    for line in mmafile:
        if line.startswith("Tempo "):
            tempo = int(line[len("Tempo ") :].strip().split()[0])
            print "Found tempo = " + str(tempo)
            break

    lineNum = 1
    thisBar_i = None
    i = None
    t_secs = 0.0
    for line in lines:
        print "parsing line " + str(lineNum)
        try:
            # i = None
            barNum = getBarNum(line)

            lastBar_i = thisBar_i
            thisBar_i = Interval("#i_" + str(barNum))
            thisBar_i.beginsAtDuration = secondsToXSDDuration(t_secs)
            if lastBar_i != None:
                lastBar_i.endsAtDuration = secondsToXSDDuration(t_secs)
                thisBar_i.intervalAfter = lastBar_i
                lastBar_i.intervalBefore = thisBar_i
            mi.add(thisBar_i)

            chordMMASymbols = getChordSymbols(line)
            beatNum = 1
            for chordMMASymbol in chordMMASymbols:
                if chordMMASymbol != "/":
                    print " handling new chord symbol"
                    if i != None:
                        print " terminating last interval"
                        i.endsAtDuration = secondsToXSDDuration(t_secs)
                        mi.add(i)

                    i = Interval("#i_" + str(barNum) + "_" + str(beatNum))
                    i.onTimeLine = tl
                    i.beginsAtDuration = secondsToXSDDuration(t_secs)

                    chordURI = "http://purl.org/ontology/chord/symbol/" + mmaSymbolToChordSymbol(
                        chordMMASymbol
                    ).replace("#", "s").replace(",", "%2C")

                    if (
                        withdescriptions
                        and len(list(commonchords.predicate_objects(URIRef(chordURI)))) == 0
                        and len(list(extrachords.predicate_objects(URIRef(chordURI)))) == 0
                    ):
                        # Deref to grab chord info
                        print "loading <" + chordURI + ">..."
                        extrachords.load(chordURI)

                    c = Chord(chordURI)
                    c_event = ChordEvent("#ce_" + str(barNum) + "_" + str(beatNum))
                    c_event.chord = c
                    c_event.time = i
                    c_event.label = mmaSymbolToChordSymbol(chordMMASymbol)

                    mi.add(c)
                    mi.add(c_event)
                    mi.add(i)
                    print " added new chord event for " + chordURI
                else:
                    if beatNum == 1:  # Need to continue the last seen chord
                        print " continuing last bar's chord"
                        # i = Interval("i_"+str(barNum)+"_"+str(beatNum))
                        # i.onTimeLine = tl
                        # i.beginsAtDuration = secondsToXSDDuration(t_secs)
                        # c_event = ChordEvent("ce_"+str(barNum)+"_"+str(beatNum))
                        # c_event.chord = c
                        # c_event.time = i
                        # mi.add(c_event); mi.add(i)

                beatNum += 1
                t_secs += 60.0 / tempo
        except Exception, e:
            print ("ERROR : Problem parsing input file at line " + str(lineNum) + " !\n")
            raise
        lineNum += 1