Here's a version of your script that does what you want:
from music21 import *
allBach = corpus.search('bach')
x = allBach[0]
p = x.parse()
partStream = p.parts.stream()
for n in p.flat.notes:
print "Note: %s%d %0.1f" % (n.pitch.name, n.pitch.octave, n.duration.quarterLength)
The main thing you got caught out by was the behaviour of the .notes
property on Stream
objects. music21 implements a hierarchical structure of containers including: Scores
, Parts
, and Measures
. Most of the iterating read-only properties (including .notes
) respect that hierarchy by not arbitrarily descending it. music21 then provides the read-only property .flat
to flatten that hierarchy into just the leaf-type objects (notes and rests).
The object you got back from your call to p.parts.stream()
was a Score
object and so asking directly for its .notes
resulted in an empty iterator as there were no Notes
which were direct children of that Score
. But using .flat.notes
flattens the hierarchy and so gives you direct access to the Notes
.
In the solution, also notice that I've accessed the .pitch.name
, .pitch.octave
and .duration.quarterLength
values directly rather than just asking for the str
representation of a Note
object.
In your question you seem to be conflating the concepts "duration" and "measure". In music theory, duration is the time a note lasts for (often measured in beats) and "measure" is the (US English) name of a metrical division containing some number of beats (made of notes and/or rests) determined by the current time signature in operation. In notation, measures are delimited on the staff by vertical lines.
p.recurse().notes
is preferred since it does not need to create a new stream in order to descend into the tree. – Frausto