def testTrillExtensionA(self): '''Test basic wave line creation and output, as well as passing objects through make measure calls. ''' from music21 import stream from music21 import note from music21 import chord from music21 import expressions from music21.musicxml import m21ToXml s = stream.Stream() s.repeatAppend(note.Note(), 12) n1 = s.notes[0] n2 = s.notes[-1] sp1 = expressions.TrillExtension(n1, n2) s.append(sp1) raw = m21ToXml.GeneralObjectExporter().parse(s) self.assertEqual(raw.count(b'wavy-line'), 2) s = stream.Stream() s.repeatAppend(chord.Chord(['c-3', 'g4']), 12) n1 = s.notes[0] n2 = s.notes[-1] sp1 = expressions.TrillExtension(n1, n2) s.append(sp1) raw = m21ToXml.GeneralObjectExporter().parse(s) # s.show() self.assertEqual(raw.count(b'wavy-line'), 2)
def testBasic(self): from music21 import abcFormat from music21.abcFormat import translate from music21.musicxml import m21ToXml af = abcFormat.ABCFile() GEX = m21ToXml.GeneralObjectExporter() for i, tf in enumerate(ALL): ah = af.readstr(tf) title = ah.getTitle() environLocal.printDebug([title]) s = translate.abcToStreamScore(ah) # run musicxml processing to look for internal errors #print(repr(s.metadata._workIds['localeOfComposition']._data)) #print(s.metadata.all()) try: unused_out = GEX.parse(s) except UnicodeDecodeError as ude: environLocal.warn('About to fail on ABC file #{}'.format(i)) raise ude if title == 'D Fragment': sharps = s.parts[0].keySignature.sharps self.assertEqual( sharps, 7, 'C# key signature should be parsed as 7 sharps')
def testMusicxmlOutput(self): # test direct rendering of musicxml from music21.musicxml import m21ToXml d = Dynamic('p') xmlout = m21ToXml.GeneralObjectExporter().parse(d).decode('utf-8') match = '<p />' self.assertTrue(xmlout.find(match) != -1, xmlout)
def write(self, obj, fmt, fp=None, subformats=None, **keywords): # pragma: no cover from music21.musicxml import m21ToXml ### hack to make musescore excerpts -- fix with a converter class in MusicXML if subformats is not None and 'png' in subformats: # do not print a title or author -- to make the PNG smaller. savedDefaultTitle = defaults.title savedDefaultAuthor = defaults.author defaults.title = '' defaults.author = '' generalExporter = m21ToXml.GeneralObjectExporter(obj) dataBytes = generalExporter.parse() fp = self.writeDataStream(fp, dataBytes) if subformats is not None and 'png' in subformats: defaults.title = savedDefaultTitle defaults.author = savedDefaultAuthor if (subformats is not None and ('png' in subformats or 'pdf' in subformats) and not str(environLocal['musescoreDirectPNGPath']).startswith('/skip')): fp = self.runThroughMusescore(fp, subformats, **keywords) return fp
def noteflightEmbed(outputStream): ''' Takes in a stream outputStream, and a string title. Returns the HTML for a page containing a noteflight flash embed of the stream and the title title TODO: Change javascript and noteflight embed to relate to be server-specific >>> sc = corpus.parse('bwv7.7').measures(0, 2) >>> (output, contentType) = alpha.webapps.templates.noteflightEmbed(sc) >>> contentType 'text/html; charset=utf-8' ''' from music21.musicxml import m21ToXml musicxmlBytes = m21ToXml.GeneralObjectExporter().parse(outputStream) musicxmlString = musicxmlBytes.decode('utf-8') musicxmlString = musicxmlString.replace('\n', '') musicxmlString = musicxmlString.replace('\'', '\\\'') htmlStr = """ <html> <head> <title>Music21 URL App Response</title> <script language="javascript" src="http://web.mit.edu/music21/webapps/client/javascript/music21.js"></script> <script> // Event handling function function noteflightEventHandler(e) { if(e.type == 'scoreDataLoaded') { m21.noteflight.sendMusicXMLToNoteflightEmbed('nfscore', '$musicxml') } } </script> <script language="javascript"> m21 = new Music21interface(); function setup() { m21.noteflight.createNoteflightEmbed('noteflightembed', 'nfscore', 'fc79df30896da6aa03f90ff771015913ca6880be',800, 450, 1.0); } </script> </head> <body onload="setup()"> <h1>Music21 Output</h1> <div id="noteflightembed"> </div> </body> </html> """ htmlData = Template(htmlStr) htmlData = htmlData.safe_substitute(musicxml=musicxmlString) return (htmlData.encode('utf-8'), 'text/html; charset=utf-8')
def testMusicxmlDirectOut(self): # test rendering musicxml directly from meter from music21.musicxml import m21ToXml ts = TimeSignature('3/8') xmlOut = m21ToXml.GeneralObjectExporter().parse(ts).decode('utf-8') match = '<time><beats>3</beats><beat-type>8</beat-type></time>' xmlOut = xmlOut.replace(' ', '') xmlOut = xmlOut.replace('\n', '') self.assertNotEqual(xmlOut.find(match), -1)
def musicxmlText(outputStream): ''' Takes in a stream outputStream and returns its musicxml with content-type 'text/plain' for displaying in a browser >>> sc = corpus.parse('bwv7.7').measures(0, 2) >>> (output, contentType) = alpha.webapps.templates.musicxmlText(sc) >>> contentType 'text/plain; charset=utf-8' >>> b'score-partwise' in output True ''' from music21.musicxml import m21ToXml musicxmlBytes = m21ToXml.GeneralObjectExporter().parse(outputStream) return (musicxmlBytes, 'text/plain; charset=utf-8')
def testBasic(self): from music21 import abcFormat from music21.abcFormat import translate from music21.musicxml import m21ToXml af = abcFormat.ABCFile() GEX = m21ToXml.GeneralObjectExporter() for tf in ALL: ah = af.readstr(tf) environLocal.printDebug([ah.getTitle()]) s = translate.abcToStreamScore(ah) # run musicxml processing to look for internal errors unused_out = GEX.parse(s)
def musicxmlFile(outputStream): ''' Takes in a stream outputStream and returns its musicxml with content-type 'application/vnd.recordare.musicxml+xml' for downloading >>> sc = corpus.parse('bwv7.7').measures(0,2) >>> (output, contentType) = alpha.webapps.templates.musicxmlFile(sc) >>> contentType 'application/vnd.recordare.musicxml+xml; charset=utf-8' >>> b'score-partwise' in output True ''' from music21.musicxml import m21ToXml musicxmlBytes = m21ToXml.GeneralObjectExporter().parse(outputStream) return (musicxmlBytes, 'application/vnd.recordare.musicxml+xml; charset=utf-8')
def testQuarterToneA(self): p1 = Pitch('D#~') # environLocal.printDebug([p1, p1.accidental]) self.assertEqual(str(p1), 'D#~') # test generation of raw musicxml output xmlOut = m21ToXml.GeneralObjectExporter().parse(p1).decode('utf-8') match = '<step>D</step><alter>1.5</alter><octave>4</octave>' xmlOut = xmlOut.replace(' ', '') xmlOut = xmlOut.replace('\n', '') self.assertNotEqual(xmlOut.find(match), -1) s = stream.Stream() for pStr in ['A~', 'A#~', 'A`', 'A-`']: p = Pitch(pStr) self.assertEqual(str(p), pStr) n = note.Note() n.pitch = p s.append(n) self.assertEqual(len(s), 4) match = [e.pitch.ps for e in s] self.assertEqual(match, [69.5, 70.5, 68.5, 67.5]) s = stream.Stream() alterList = [ None, 0.5, 1.5, -1.5, -0.5, 'half-sharp', 'one-and-a-half-sharp', 'half-flat', 'one-and-a-half-flat', '~' ] sc = scale.MajorScale('c4') for x in range(1, 10): n = note.Note(sc.pitchFromDegree(x % sc.getDegreeMaxUnique())) n.quarterLength = 0.5 n.pitch.accidental = Accidental(alterList[x]) s.append(n) match = [str(n.pitch) for n in s.notes] self.assertEqual(match, [ 'C~4', 'D#~4', 'E-`4', 'F`4', 'G~4', 'A#~4', 'B`4', 'C-`4', 'D~4' ]) match = [e.pitch.ps for e in s] self.assertEqual( match, [60.5, 63.5, 62.5, 64.5, 67.5, 70.5, 70.5, 58.5, 62.5])
def write(self, obj, fmt, fp=None, subformats=None, **keywords): from music21.musicxml import m21ToXml ### hack to make musescore excerpts -- fix with a converter class in MusicXML if subformats is not None and 'png' in subformats: savedDefaultTitle = defaults.title savedDefaultAuthor = defaults.author defaults.title = '' defaults.author = '' generalExporter = m21ToXml.GeneralObjectExporter(obj) dataBytes = generalExporter.parse() fp = self.writeDataStream(fp, dataBytes) if subformats is not None and 'png' in subformats: defaults.title = savedDefaultTitle defaults.author = savedDefaultAuthor if subformats is not None and 'png' in subformats: fp = self.runThroughMusescore(fp, **keywords) return fp
def testBasic(self): from music21 import abcFormat from music21.abcFormat import translate from music21.musicxml import m21ToXml af = abcFormat.ABCFile() GEX = m21ToXml.GeneralObjectExporter() for i, tf in enumerate(ALL): ah = af.readstr(tf) environLocal.printDebug([ah.getTitle()]) s = translate.abcToStreamScore(ah) # run musicxml processing to look for internal errors #print(repr(s.metadata._workIds['localeOfComposition']._data)) #print(s.metadata.all()) try: unused_out = GEX.parse(s) except UnicodeDecodeError as ude: environLocal.warn("About to fail on ABC file #{}".format(i)) raise ude
def to_musicxml(self): return m21ToXml.GeneralObjectExporter( self.to_score()).parse().decode('utf-8')