After completing the last post, I did a bit of thinking. Some of it in the middle of the night. Here’s a summary.
- if I want to use the
wrtMldyStepmethod for other measures, it has some issues- it currently fills a measure’s available space with the series, may not always want to do so
- should a step series be allowed to move into the next measure
- if it repeats the series in the measure, it currently uses exactly the same notes; I figure the second set of notes should move up or down the scale in this case rather than repeat exactly
- the
getStepDirmethod does not currently consider the action (jump, repeat, or step) taken previously- a jump up should likely be followed by a series going downwards in most cases and vice-versa
- the note selection code does not currently account for any incidentals in the scale
- should it, I really don’t know (see the related note in the previous post)
I am sure there were other thoughts I have since forgotten. But I do believe the above needs to be dealt with at some point.
But first, just to keep things going, I want to code the jump method. And, perhaps look at reworking the code that decides what action to take next to consider the previous action.
Jump For It
Jumps are a routine part of a melody. How often they should happen I don’t really know. That is something that can be played with over time. But I reckon the probability of a jump should increase following a sequence (how big?) of a number of step series or, perhaps, after a longer series.
The size of the jump should likely depend on where the previous note lies in the staff. And likely should not be greater than 6 tones. I.E. less than a full octave with respect to the scale. For now I will limit the jump destination to the range C4 to A6. And only consider jumps greater than 3 tones. Good or bad? I sure don’t know.
As soon as I started working on the code for this action, I discovered the need for helper functions.
getDbtwnN()
The first one gets the size of the jump between two notes. Pretty straightforward. But, I first tried using em_notes.get_half_tone(). That didn’t work as I am after the distance between notes of the C Major scale, not a full piano octave. So back to the ord() function. Do note the adjustment for notes that are multiple octaves apart.
def getDbtwnN(self, nt1, nt2) -> int:
ord_A, ord_G = ord("A"), ord("G")
ord_1, ord_2 = ord(nt1[0]), ord(nt2[0])
oct_1, oct_2 = int(nt1[-1]), int(nt2[-1])
n_dist = 0
if oct_1 == oct_2:
n_dist = ord_2 - ord_1
elif oct_1 > oct_2:
# need to account for octave differences greater than 1
n_dist = -((ord_1 - ord_A) + (ord_G - ord_2) + 1 + ((oct_1 - oct_2 - 1) * 8))
else:
n_dist = (ord_2 - ord_A) + (ord_G - ord_1) + 1 + ((oct_2 - oct_1 - 1) * 8)
return n_dist
And a wee test. Note: that True is now False.
# test only
if True:
tst_nts = [("C4", "A5"), ("A5", "D4"), ("C4", "G4"), ("B6", "C4")]
for n1, n2 in tst_nts:
n_dst = mldy.getDbtwnN(n1, n2)
j_dir = "down" if n_dst < 0 else "up"
nw_nt = mldy.getNtSteps(abs(n_dst), j_dir, prv_nt=n1)
print(f"dist btwn {n1} and {n2} is {n_dst} -> {nw_nt}")
exit(0)
And in the terminal I got the following.
PS R:\learn\e_m_311> uv run tst_melody.py
... ...
getNtSteps(5, up, C4) -> prev note: C4 -> nxt note: A5
dist btwn C4 and A5 is 5 -> A5
getNtSteps(4, down, A5) -> prev note: A5 -> nxt note: D4
dist btwn A5 and D4 is -4 -> D4
getNtSteps(4, up, C4) -> prev note: C4 -> nxt note: G4
dist btwn C4 and G4 is 4 -> G4
getNtSteps(14, down, B6) -> prev note: B6 -> nxt note: ;5
dist btwn B6 and C4 is -14 -> ;5
And that appears to work as desired. Or at least close enough. The last result is expected. Though I have not attempted to confirm that “;” would be the correct character in this case.
getJumpDrSzNt()
It was going to be a fair bit of code to get the jump direction and a acceptable jump size. So, another helper method. And once I have the direction and jump size may as well determine the destination note. Will save the caller from having to do so.
def getJumpDrSzNt(self) -> tuple[str, int, str]:
""" Determine the direction and size the current jump should take.
Then determine the destination note.
Returns: direction of jump (down or up), size, destination note
"""
# max/min note destinations
n_mn, n_mx = "C4", "A6"
n_mn3, n_mx3 = "F4", "E5"
j_mx = 6 # less than a full scale jump
# get previous note
if len(self.mldy[self.c_bar]) == 0:
p_nt = self.mldy[self.c_bar - 1][-1][0]
else:
p_nt = self.mldy[self.c_bar][-1][0]
if self.p_dbg:
print(f"\n\tgetJumpDrSz(p_nt={p_nt}):")
dir_jp = ""
sz_jp = 0
# which direction to jump
# if prev note within 4 tones of min or max, jump the other way
p_hgh, p_lw = nt_lessthan(n_mx3, p_nt), nt_lessthan(p_nt, n_mn3)
if p_hgh:
dir_jp = "down"
elif p_lw:
dir_jp = "up"
else:
dir_jp = rng.choice(["down", "up"])
if self.p_dbg:
print(f"\t\tp_hgh: {p_hgh}, p_lw: {p_lw} -> {dir_jp}")
# get jump size within acceptable limits
if dir_jp == "up":
mx_jp = min(self.getDbtwnN(p_nt, n_mx), j_mx)
if self.p_dbg:\
print(f"\t\tmx_jp({p_nt}, {n_mx}) -> {mx_jp}")
else:
mx_jp = min(self.getDbtwnN(n_mn, p_nt), j_mx)
if self.p_dbg:\
print(f"\t\tmx_jp({n_mn}, {p_nt}) -> {mx_jp}")
sz_jp = rng.integers(3, mx_jp, endpoint=True)
if self.p_dbg:
print(f"\t\trng.integers(3, {mx_jp}, endpoint=True) -> {sz_jp}")
j_nt = self.getNtSteps(sz_jp, dir_jp, prv_nt=p_nt)
return dir_jp, sz_jp, j_nt
Another small test.
if __name__ == '__main__':
... ...
# test only
if True:
j_dr, j_sz, j_nt = mldy.getJumpDrSzNt()
print(f"\n\tjump parameters: {j_dr}, {j_sz} -> {j_nt}")
And in the terminal the following output was displayed for a test run.
PS R:\learn\e_m_311> uv run tst_melody.py
... ...
mldy: [[('D4', 'qtr●', 0.75), ('F4', '16th', 0.125), ('A5', '8th', 0.25), ('B5', '16th', 0.125), ('D5', '8th', 0.25), ('E5', '8th', 0.25), ('G5', '8th', 0.25)], [], [], [('?', '?', '?'), ('E4', 'qtr●', 0.75), ('C#5', 'qtr●', 0.75)], [('F#5', 'qtr●', 0.75)], [], [], [('G4', 'hlf', 1.0), ('D4', 'hlf', 1.0)]]
bars: [2.0, 0, 0, 1.5, 0.75, 0, 0, 2.0]
whats: ['first tonic', 'step up']
getJumpDrSz(p_nt=G5):
p_hgh: True, p_lw: False -> down
mx_jp(C4, G5) -> 7
getNtSteps(6, down, G5) -> prev note: G5 -> nxt note: A5
jump parameters: down, 6 -> A5
Complete wrtMldyJump()
I think we are now ready to code/complete the method to determine and add a jump to the melody. Should be really light, only one note afterall. That if True: below is now if False:
def wrtMldyJump(self) -> None:
""" Add a jump, one note, to the melody in the current measure
:returns: nothing
:side-effect: the method modifies the mldy member
"""
# get direction, size of jump, and destination note
j_dir, j_sz, j_nt = self.getJumpDrSzNt()
# get a note duration, not sure about the half note, but...
j_dur = rng.choice(["hlf", "qtr●", "qtr"])
# write note to melody data structure
self.mldy[self.c_bar].append((j_nt, j_dur, self.n_durs.n_dur[j_dur]))
self.m_dur[self.c_bar] += self.n_durs.n_dur[j_dur]
# record process to date, update current bar if appropriate
self.last_what = f"jump {j_dir} {j_sz}"
self.prev_whats.append(self.last_what)
if self.m_dur[self.c_bar] == self.sec_b:
self.c_bar += 1
And add a test to makeMelody()
def makeMelody(self) -> None:
... ...
# get dicts to allow switching between scale notes and their
# roman numeral equivalents
t_n2r, t_r2n = self.rn2nts()
self.sortInitFW(t_r2n)
# now framework in place, finish the melody
# start by finishing the first measure
self.last_what = f'first tonic'
self.prev_whats.append(self.last_what)
self.getNextPattern()
# test only, add a jump to the melody
if True:
self.wrtMldyJump()
In the following output was printed to the terminal (pertinent bits only).
S R:\learn\e_m_311> uv run tst_melody.py
MelodyConfig(pd_len=8, r_oct=4, t_sig=(4, 4), s_bt=0.5, dbg=True)
scale: ['D4', 'E4', 'F#4', 'G4', 'A5', 'B5', 'C#5']
... ...
getNextPattern(): wht_do = step
... ...
wrtMldyStep(c_bar=0):
s_dir: up
... ...
getStpDurs(7): doSRpt = False
... ...
getJumpDrSz(p_nt=B6):
p_hgh: True, p_lw: False -> down
mx_jp(C4, B6) -> 7
... ...
getNtSteps(5, down, B6) -> prev note: B6 -> nxt note: D5
mldy: [[('D4', 'qtr', 0.5), ('F4', '8th', 0.25), ('A5', '8th', 0.25), ('C5', '8th', 0.25), ('E5', '16th', 0.125), ('F5', '8th', 0.25), ('A6', '16th', 0.125), ('B6', '8th', 0.25)], [('D5', 'qtr●', 0.75)], [], [('G4', 'hlf', 1.0), ('A5', 'hlf', 1.0)], [('F#5', 'hlf', 1.0)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('D4', 'qtr●', 0.75)]]
bars: [2.0, 0.75, 0, 2.0, 1.0, 0, 0, 1.5]
whats: ['first tonic', 'step up 7', 'jump down 5']
That does seem to work as intended.
Refactor?
I think I am for now going to leave coding the wrtMldyRpt() method until after I sort out a couple of my concerns with the existing code. In particular the wrtMldyStep() method. I think I will also need to rework getNextPattern() to ensure a jump does not follow a jump. Well, at least not often. Once I get that sorted I will look at how to handle the bits of melody leading to the two phrase ending measures. Then I think we can have look at adding/using repetition of previous musical motifs. Expect I am using the word ‘motifs’ incorrectly.
getNextPattern()
You know, let’s just get this one out of the way. Should be easy enough. Here’s the updated if block; the elif is the new bit. Will see how that works out down the road.
def getNextPattern(self) -> None:
... ...
if self.last_what == 'first tonic':
m_options = ["jump", "step"]
p_options = [0.05, 0.95]
elif self.last_what[:4] == "jump":
m_options = ["jump", "repeat", "step"]
p_options = [0.005, 0.095, 0.9]
else:
m_options = ["jump", "repeat", "step"]
p_options = [0.2, 0.1, 0.7]
wrtMldyStep()
I really believe this one needs some serious work, as noted above. I will start by looking at the repetition situation. The repeated section will be moved up or down a tone or two. The distance between notes and the rhythm will remain the same.
And rather than include the code in wrtMldyStep() I will write another helper function. It will determine the direction and size of the move and return the appropriately adjusted notes.
seqShift()
Not much to say. Think the method definition pretty much covers it.
def seqShift(self, n_seq:list[str], s_dir:str="", s_sz:int=0) -> list[str]:
""" Shift the passed sequence of notes some number of tones.
If s_sz not zero use that as the shift distance. Otherwise,
generate a random value.
:param n_seq: the sequence of notes to be shifted
:param s_dir: direction to shift the sequence, if empty string
randomly select direction
:param s_sz: the number of tones to shift the sequence, if equal
to 0, generate random value
"""
nw_s, u_dir, u_sz = [], s_dir, s_sz
if s_dir == "":
# for now equal probability of either
u_dir = rng.choice(["down", "up"])
if s_sz == 0:
# not very often size of 2
u_sz = rng.choice([1, 2], p=[.8, .2])
for nt in n_seq:
nw_s.append(self.getNtSteps(u_sz, u_dir, prv_nt=nt))
if self.p_dbg:
print(f"\n\tseqShift: {u_dir} {u_sz} -> {nw_s}")
return nw_s
A couple of tests yielded the following (pertinent bits only). I’ll let you sort the test code.
... ...
shifting ['F3', 'A4', 'B4']
... ...
seqShift: down 2 -> ['D3', 'F3', 'G3']
... ...
shifting ['F3', 'A4', 'B4']
... ...
seqShift: up 1 -> ['G3', 'B4', 'C4']
And here’s the updated code in wrtMldyStep(). The commented out line is what got replaced by the following lines of code.
... ...
# if repeat involved adjust notes list accordingly
if len(s_durs) != len(ss_nts):
# ss_nts.extend(ss_nts)
nw_seq = self.seqShift(ss_nts)
ss_nts.extend(nw_seq)
I ran the module and the very first melody involved a repetition of the step sequence in the first measure. Lucky or what?
PS R:\learn\e_m_311> uv run tst_melody.py
MelodyConfig(pd_len=8, r_oct=4, t_sig=(4, 4), s_bt=0.5, dbg=True)
... ...
getNextPattern(): wht_do = step
... ...
wrtMldyStep(c_bar=0):
s_dir: up
... ...
doStpRpt(3)
avl tm = 1.0, n_8 = 4.0, n_16 = 8.0
nbr 8ths per note = 1.3333333333333333, nbr 16ths per note = 2.6666666666666665
getStpDurs(3): doSRpt = True
... ...
seqShift: down 1 -> ['E5', 'F5', 'G5']
... ...
getJumpDrSz(p_nt=G5):
p_hgh: True, p_lw: False -> down
mx_jp(C4, G5) -> 7
getNtSteps(3, down, G5) -> prev note: G5 -> nxt note: D5
mldy: [[('D5', 'hlf', 1.0), ('F5', '16th', 0.125), ('G5', '16th', 0.125), ('A6', '8th', 0.25), ('E5', '16th', 0.125), ('F5', '16th', 0.125), ('G5', '8th', 0.25)], [('D5', 'qtr●', 0.75)], [], [('E4', 'hlf', 1.0), ('A5', 'hlf', 1.0)], [('B5', 'hlf', 1.0)], [], [], [('G4', 'hlf', 1.0), ('D4', 'hlf', 1.0)]]
bars: [2.0, 0.75, 0, 2.0, 1.0, 0, 0, 2.0]
whats: ['first tonic', 'step up 3', 'jump down 3']
Another Refactor
When I started working on the next issue, I ran into a bug (repeated). I had the following line in a few methods to get the previous/last note:
p_nt = self.mldy[self.c_bar][-1][0]
But after the first measure, any subsequent measure may not have any notes at all just yet. So, in a few methods, I replaced that with something like the following. It did vary in one particular method which I am not going to show. I am sure you can sort that one out.
if self.m_dur[self.c_bar] == 0:
p_nt = self.mldy[self.c_bar - 1][-1][0]
else:
p_nt = self.mldy[self.c_bar][-1][0]
Should Sequence Fill the Measure
This is the other potentially key issue I had: should the step sequence completely fill the current measure. As written I believe the code will do exactly that. Well a little testing showed that to be the case.
mldy: [[('D4', 'hlf', 1.0), ('E4', '16th', 0.125), ('G4', '16th', 0.125), ('B5', '16th', 0.125), ('C5', '16th', 0.125), ('E5', '16th', 0.125), ('G5', '16th', 0.125), ('B6', '16th', 0.125), ('C6', '16th', 0.125)], [('B6', '16th', 0.125), ('A6', '16th', 0.125), ('F5', '8th', 0.25), ('E5', '8th', 0.25), ('C5', '8th', 0.25), ('A6', '16th', 0.125), ('G5', '16th', 0.125), ('E5', '8th', 0.25), ('D5', '8th', 0.25), ('B5', '8th', 0.25)], [], [('?', '?', '?'), ('E4', 'qtr●', 0.75), ('C#5', 'qtr●', 0.75)], [('B5', 'qtr●', 0.75)], [], [], [('A5', 'hlf', 1.0), ('D5', 'hlf', 1.0)]]
bars: [2.0, 2.0, 0, 1.5, 0.75, 0, 0, 2.0]
Now, I am thinking that we may not always want to do so. We may only want to fill some portion of it. We may want to do 2, 3 or 4 step sequences in a measure, with none of them repeats of a previous one. Or we may want to do 2 in a portion of a measure with the second being a shifted repeat of the first. Don’t currently have code to cause any of that to happen. But first I think we need to rework wrtMldyStep() so that a generated step sequence only uses a portion of the current measure.
Though I currently have no idea what the smallest portion should be. For a time signature of x/4, I expect a single beat, i.e. 4 16th to 8 32nd notes, for a sequence should be the absolute minimum. However, I am a little concerned that may in fact be too small in some cases. But, got to start somewhere. I am also thinking that each portion should be a whole multiple of a 1/2 beat.
wrtMldyStep()
And, where will the decision, as to what portion of the measure should be used, be made. Since I don’t currently have an answer to that, I think I will add a new parameter to wrtMldyStep(). If the value of that parameter is not equal to the number of beats in the current time signature when the method is called, it will take some appropriate action when generating the requested step sequence. In fact, I think I wll add another parameter as well. It will let the method know if a smaller sequence should be repeated and how many times. Its default will be 1.
But I expect those parameters are going to affect how I currently proceed to sort out the step sequences. As soon as I started looking at the code, it became clear that the helper method getStepDurs() will immediately need refactoring. And at least one new parameter. So, I think we will start with that one. Note, I have a drafted a new version of wrtMldyStep()’s signature.
def wrtMldyStep(self, n_bts:float=0, s_rpt:int=1) -> None:
""" Generate stepwise sequence of notes and durations, add to the
mldy member in the appropriate measure.
:param n_bts: number of beats the step sequence should use in the
current measure. If 0, fill the measure.
:param s_rpt: if not equal to 1, repeat the generated sequence
the specified number of times.
:returns: nothing
:side-effect: the method modifies the mldy member
"""
getStepDurs()
We will need to add at least a parameter to specify the sequence duration. So a tentative new method signature.
def getStepDurs(self, nbr_stp:int, n_bts:float) -> list[str]:
""" Sort out a set of durations for the number of notes in a step
sequence.
:param nbr_stp: the number of steps in the sequence for which
durations are required
:param n_bts: number of beats the step sequence should use in the
current measure. If 0, fill the measure.
:returns: list of durations, as notes, for each step
In the case of a repeated series twice the number of notes is
returned.
"""
The method, at present, gets the available beats/time in the current measure and fills it. So I am hoping I only need to refactor that code to accomplish my current goal. I also reworked some of the debug/dev print statements, but I won’t bother showing those changes.
def getStepDurs(self, nbr_stp:int, n_bts:float) -> list[str]:
... ...
s_durs = []
# time available in current measure
avl_m = (self.sec_b - self.m_dur[self.c_bar])
if n_bts == 0:
doSRpt = self.doStpRpt(nbr_stp)
if doSRpt:
# s_durs = self.getStpDursRpt(nbr_stp)
avl_tm = avl_m / 2
else:
# s_durs = self.getStpDursSgl(nbr_stp)
avl_tm = avl_m
else:
doSRpt = False
avl_tm = n_bts * self.m_spb
# don't go beyond measure
if avl_tm > avl_m:
avl_tm = avl_m
if self.p_dbg:
print(f"\n\tgetStpDurs({nbr_stp}, n_bts={n_bts}): avl_tm: {avl_tm}, doSRpt = {doSRpt}")
Test
And some code to test things out.
def makeMelody(self) -> None:
... ...
# get dicts to allow switching between scale notes and their
# roman numeral equivalents
t_n2r, t_r2n = self.rn2nts()
self.sortInitFW(t_r2n)
# now framework in place, finish the melody
# start by finishing the first measure
self.last_what = f'first tonic'
self.prev_whats.append(self.last_what)
if self.p_dbg:
print(f"\n{self.mldy}\n{self.m_dur}")
self.getNextPattern()
# test only
if True:
if self.p_dbg:
print(f"\ngetNextPattern(c_bar={self.c_bar}): wht_do = step")
mldy.wrtMldyStep(n_bts=2)
return
... ...
def wrtMldyStep(self, n_bts:float=0, s_rpt:int=1) -> None:
... ...
# let's sort some durations for each note
# in general I am thinking shorter rather than longer, but the
# beats available will likely affect that decision
s_durs = self.getStepDurs(s_nbr, n_bts)
And, in the terminal I got the following output displayed. (I have trimmed it to the essentials)
PS R:\learn\e_m_311> uv run tst_melody.py
MelodyConfig(pd_len=8, r_oct=4, t_sig=(4, 4), s_bt=0.5, dbg=True)
scale: ['D4', 'E4', 'F#4', 'G4', 'A5', 'B5', 'C#5']
... ...
[[('D5', 'qtr', 0.5)], [], [], [('?', '?', '?'), ('D4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('F#5', 'qtr●', 0.75)], [], [], [('A5', 'hlf', 1.0), ('D5', 'hlf', 1.0)]]
[0.5, 0, 0, 1.5, 0.75, 0, 0, 2.0]
... ...
getNextPattern(c_bar=0): wht_do = step
wrtMldyStep(c_bar=0, n_bts=0, s_rpt=1):
s_dir: up
... ...
getStpDurs(8, n_bts=0): avl_tm: 1.5, doSRpt = False
... ...
step notes: ['E5', 'G5', 'A6', 'G5', 'B6', 'C6', 'E6', 'G6']
step durations: ['8th', '16th', '8th', '8th', '16th', '8th', '16th', '16th']
updated c_bar: 1
getNextPattern(c_bar=1): wht_do = step
wrtMldyStep(c_bar=1, n_bts=2, s_rpt=1):
s_dir: down
... ...
step notes: ['E6', 'C6', 'A6']
step durations: ['qtr', '8th', '8th']
... ...
mldy: [[('D5', 'qtr', 0.5), ('E5', '8th', 0.25), ('G5', '16th', 0.125), ('A6', '8th', 0.25), ('G5', '8th', 0.25), ('B6', '16th', 0.125), ('C6', '8th', 0.25), ('E6', '16th', 0.125), ('G6', '16th', 0.125)], [('E6', 'qtr', 0.5), ('C6', '8th', 0.25), ('A6', '8th', 0.25)], [], [('?', '?', '?'), ('D4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('F#5', 'qtr●', 0.75)], [], [], [('A5', 'hlf', 1.0), ('D5', 'hlf', 1.0)]]
bars: [2.0, 1.0, 0, 1.5, 0.75, 0, 0, 2.0]
whats: ['first tonic', 'step up 8', 'step down 3']
getNextPattern()
And another update required to the getNextPattern() method. I figured, for now, that is where the decision regarding the length of the sequence duration will be determined. Ditto for repeating the shorter sequence or not. A little messy, but…
Am including the complete code for the method (not my usual habit of just showing new code).
def getNextPattern(self) -> None:
""" Determine the next big step/task/musical pattern to take in
generating the melody.
! this method may be significantly refactored as development
! continues, for now keeping it horribly simple
:side-effect: calls another function, never does anything else
"""
# repeat not a particularly viable option if only have the
# first note in measure 1 (aka 0), and don't really likely
# want a jump right off the bat, but...
if self.last_what == 'first tonic':
m_options = ["jump", "step"]
p_options = [0.05, 0.95]
elif self.last_what[:4] == "jump":
m_options = ["jump", "repeat", "step"]
p_options = [0.005, 0.095, 0.9]
else:
m_options = ["jump", "repeat", "step"]
p_options = [0.2, 0.1, 0.7]
# return rng.choice(m_options, p=p_options)
wht_do = rng.choice(m_options, p=p_options)
# if wht_do == step, decide about sequence duration and possible repeat
if wht_do == "step":
# set default values
s_bts, is_rpt = 0, 1
# no limit on sequence duration for 1st, last precedent, 1st consequent
# and last measure
m_p_end = (self.m_nbr // 2) - 1
m_c_bgn = self.m_nbr // 2
no_dur_lim = (self.c_bar == 0) or (self.c_bar == m_p_end)
no_dur_lim = no_dur_lim or (self.c_bar == m_c_bgn) or (self.c_bar == self.m_nbr - 1)
if not no_dur_lim:
do_lim = rng.choice([False, True],p=[.3, .7])
if do_lim:
avl_bts = [bt / 2 for bt in range(2, self.m_sig[0] * 2)]
s_bts = rng.choice(avl_bts)
do_rpt = rng.choice([False, True],p=[.3, .7]) and (s_bts <= self.m_sig[0] / 2)
if do_rpt:
is_rpt = rng.choice([2,3])
if self.p_dbg:
print(f"\ngetNextPattern(c_bar={self.c_bar}): wht_do = {wht_do}")
if wht_do == "step":
print(f"\tdo_lim: {do_lim}, do_rpt: {do_rpt}, wrtMldyStep(n_bts={s_bts}, s_rpt={is_rpt})")
p_rts = {
"jump": self.wrtMldyJump,
"repeat": self.wrtMldyRpt,
"step": self.wrtMldyStep
}
if wht_do == "step":
self.wrtMldyStep(n_bts=s_bts, s_rpt=is_rpt)
else:
p_rts[wht_do]()
I also refactored makeMelody() to allow for a 2nd call to getNextPattern(). Expect that call will eventually be in a loop, but for testing I want a touch more control. Well I did put things in a bit of a loop:
while self.c_bar <= ((self.m_nbr // 2) - 2):
if self.p_dbg:
print(f"\n{self.mldy}\n{self.m_dur}")
self.getNextPattern()
And here’s some of the pertinent output for a test run.
PS R:\learn\e_m_311> uv run tst_melody.py
MelodyConfig(pd_len=8, r_oct=4, t_sig=(4, 4), s_bt=0.5, dbg=True)
scale: ['D4', 'E4', 'F#4', 'G4', 'A5', 'B5', 'C#5']
... ...
[[('D4', 'qtr●', 0.75)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('E5', 'qtr●', 0.75)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('D4', 'qtr●', 0.75)]]
[0.75, 0, 0, 1.5, 0.75, 0, 0, 1.5]
getNextPattern(c_bar=0): wht_do = step
do_lim: False, do_rpt: False, wrtMldyStep(n_bts=0, s_rpt=1)
wrtMldyStep(c_bar=0, n_bts=0, s_rpt=1):
s_dir: up
... ...
getStpDurs(7, n_bts=0): avl_tm: 1.25, doSRpt = False
... ...
[[('D4', 'qtr●', 0.75), ('E4', '8th', 0.25), ('G4', '8th', 0.25), ('B5', '8th', 0.25), ('C5', '16th', 0.125), ('E5', '16th', 0.125), ('F5', '16th', 0.125), ('G5', '16th', 0.125)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('E5', 'qtr●', 0.75)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('D4', 'qtr●', 0.75)]]
[2.0, 0, 0, 1.5, 0.75, 0, 0, 1.5]
getNextPattern(c_bar=1): wht_do = step
do_lim: True, do_rpt: False, wrtMldyStep(n_bts=2.5, s_rpt=1)
wrtMldyStep(c_bar=1, n_bts=2.5, s_rpt=1):
s_dir: down
... ...
getStpDurs(3, n_bts=2.5): avl_tm: 1.25, doSRpt = False
... ...
[[('D4', 'qtr●', 0.75), ('E4', '8th', 0.25), ('G4', '8th', 0.25), ('B5', '8th', 0.25), ('C5', '16th', 0.125), ('E5', '16th', 0.125), ('F5', '16th', 0.125), ('G5', '16th', 0.125)], [('F5', 'qtr', 0.5), ('D5', 'qtr', 0.5), ('C5', '8th', 0.25)], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('E5', 'qtr●', 0.75)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('D4', 'qtr●', 0.75)]]
[2.0, 1.25, 0, 1.5, 0.75, 0, 0, 1.5]
getNextPattern(c_bar=1): wht_do = step
do_lim: False, do_rpt: True, wrtMldyStep(n_bts=0, s_rpt=2)
wrtMldyStep(c_bar=1, n_bts=0, s_rpt=2):
s_dir: up
... ..,
getStpDurs(4, n_bts=0): avl_tm: 0.75, doSRpt = False
... ...
[[('D4', 'qtr●', 0.75), ('E4', '8th', 0.25), ('G4', '8th', 0.25), ('B5', '8th', 0.25), ('C5', '16th', 0.125), ('E5', '16th', 0.125), ('F5', '16th', 0.125), ('G5', '16th', 0.125)], [('F5', 'qtr', 0.5), ('D5', 'qtr', 0.5), ('C5', '8th', 0.25), ('E5', '8th', 0.25), ('F5', '16th', 0.125), ('G5', '8th', 0.25), ('B6', '16th', 0.125)], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('E5', 'qtr●', 0.75)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('D4', 'qtr●', 0.75)]]
[2.0, 2.0, 0, 1.5, 0.75, 0, 0, 1.5]
getNextPattern(c_bar=2): wht_do = step
do_lim: True, do_rpt: False, wrtMldyStep(n_bts=3.5, s_rpt=1)
wrtMldyStep(c_bar=2, n_bts=3.5, s_rpt=1):
s_dir: down
... ...
getStpDurs(4, n_bts=3.5): avl_tm: 1.75, doSRpt = False
... ...
[[('D4', 'qtr●', 0.75), ('E4', '8th', 0.25), ('G4', '8th', 0.25), ('B5', '8th', 0.25), ('C5', '16th', 0.125), ('E5', '16th', 0.125), ('F5', '16th', 0.125), ('G5', '16th', 0.125)], [('F5', 'qtr', 0.5), ('D5', 'qtr', 0.5), ('C5', '8th', 0.25), ('E5', '8th', 0.25), ('F5', '16th', 0.125), ('G5', '8th', 0.25), ('B6', '16th', 0.125)], [('A6', 'qtr', 0.5), ('F5', '8th', 0.25), ('E5', 'qtr', 0.5), ('D5', 'qtr', 0.5)], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('E5', 'qtr●', 0.75)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('D4', 'qtr●', 0.75)]]
[2.0, 2.0, 1.75, 1.5, 0.75, 0, 0, 1.5]
getNextPattern(c_bar=2): wht_do = repeat
... ...
getNextPattern(c_bar=2): wht_do = step
do_lim: True, do_rpt: False, wrtMldyStep(n_bts=2.5, s_rpt=1)
wrtMldyStep(c_bar=2, n_bts=2.5, s_rpt=1):
s_dir: up
... ...
getStpDurs(3, n_bts=2.5): avl_tm: 0.25, doSRpt = False
... ...
mldy: [[('D4', 'qtr●', 0.75), ('E4', '8th', 0.25), ('G4', '8th', 0.25), ('B5', '8th', 0.25), ('C5', '16th', 0.125), ('E5', '16th', 0.125), ('F5', '16th', 0.125), ('G5', '16th', 0.125)], [('F5', 'qtr', 0.5), ('D5', 'qtr', 0.5), ('C5', '8th', 0.25), ('E5', '8th', 0.25), ('F5', '16th', 0.125), ('G5', '8th', 0.25), ('B6', '16th', 0.125)], [('A6', 'qtr', 0.5), ('F5', '8th', 0.25), ('E5', 'qtr', 0.5), ('D5', 'qtr', 0.5), ('F5', '32nd', 0.0625), ('A6', '16th', 0.125), ('C6', '32nd', 0.0625)], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('A5', 'qtr●', 0.75)], [('E5', 'qtr●', 0.75)], [], [], [('?', '?', '?'), ('G4', 'qtr●', 0.75), ('D4', 'qtr●', 0.75)]]
bars: [2.0, 2.0, 2.0, 1.5, 0.75, 0, 0, 1.5]
whats: ['first tonic', 'step up 7', 'step down 3', 'step up 4', 'step down 4', 'step up 3']
No idea what it sounds like, but we now have three full measures of notes to play.
A Wee Bit of Music?
Okay, I entered the first 3 measures above into a music score in MuseScore. Then I added a quick step sequence at the start of the 4th measure to bring the melody back down to the closing notes in that measure. The score and audio file follow.

And it sounds like this.
Fini, je croix!
I do believe this post is truly plenty long. It’s covered a fair bit of material and consumed a goodly amount of my time so far this week. To me it feels like a little more progress has been made. Even though I can see the possible need for some changes in determining the direction of a step sequence based on the content of future measures. And, I haven’t gotten around to finishing all the planned refactorings. Mais, c’est la vie!
Until next time, may your efforts give you a feeling that progress is being made.