You may recall that in the last post I expressed some disappointment that for any given step sequence length, the existing code generated pretty much the same set of note durations for any given sequence duration, full or partial measure. Though the notes might be played in different orders generating a slightly different rhythm each time. But over time, those rhythms will routinely repeat.
So, I decided to play around an see if I can get something a little less repetitive. But that said, the number of notes in the sequence and the desired duration for the sequence seriously affects the available options. Given a three note sequence for a full measure only allows so many choices for the notes. So, that is something else that will need to be considered and sorted out down the road.
But let’s start with a revised approach to getting a set of note durations for a given step sequence.
Note Durations Revisited?
Not sure this is going to make a big or any difference in the note durations. But, let’s give it a try.
No explanation, just the code for a couple of functions. And a bit of test code. Well, not quite yet. With some additional testing I found a significant number of bugs/issues. Will work on fixes. Then get back to the post sometime in the future.
Non-termination
One of the problems I discovered, with further testing, was endless looping. This occurred because of how my loop statement was coded.
while (len(ct_nts) < nbr_nts) or (ct_tm < avl_tm):
So, I added some code to track what was going on.
... ...
while (len(ct_nts) < nbr_nts) or (ct_tm < avl_tm):
if (len(ct_nts) == nbr_nts) or (ct_tm == avl_tm):
print(f"\nct_tm: {ct_tm}, ct_nd: {ct_nd}\n\t{ct_nts}")
break
What surprised me is just how often I was getting into that situation. Probably shouldn’t have surprised me, but…
PS R:\learn\e_m_311> uv run tst_melody.py
... ...
duration seq (6 notes over 2.0 secs):
ct_tm: 1.375, ct_nd: 0.625
[0.375, 0.0625, 0.25, 0.125, 0.375, 0.1875]
[0.375, 0.0625, 0.25, 0.125, 0.375, 0.1875] -> 6 notes for 1.375 secs total
took 0.0010865000076591969 secs
duration seq (6 notes over 1.0 secs):
ct_tm: 0.5, ct_nd: 0.5
[0.125, 0.0625, 0.0625, 0.0625, 0.125, 0.0625]
[0.125, 0.0625, 0.0625, 0.0625, 0.125, 0.0625] -> 6 notes for 0.5 secs total
took 0.0006984001956880093 secs
duration seq (6 notes over 0.5 secs):
ct_tm: 0.5, ct_nd: 0.0
[0.5]
[0.5] -> 1 notes for 0.5 secs total
took 0.00034549995325505733 secs
PS R:\learn\e_m_311> uv run tst_melody.py
... ...
duration seq (6 notes over 2.0 secs):
[0.75, 0.125, 0.1875, 0.125, 0.75, 0.0625] -> 6 notes for 2.0 secs total
took 0.0005788998678326607 secs
duration seq (6 notes over 1.0 secs):
ct_tm: 1.0, ct_nd: 0.0
[0.375, 0.25, 0.125, 0.0625, 0.1875]
[0.375, 0.25, 0.125, 0.0625, 0.1875] -> 5 notes for 1.0 secs total
took 0.0008014999330043793 secs
duration seq (6 notes over 0.5 secs):
ct_tm: 0.5, ct_nd: 0.0
[0.25, 0.1875, 0.0625]
[0.25, 0.1875, 0.0625] -> 3 notes for 0.5 secs total
took 0.0004133000038564205 secs
Two runs, three sequences each with differing requested duration periods. Only one sequence duration was correctly generated.
I was ending up in situations where one condition was true but the other was not. That is, I would have enough note durations but their sum did not fill the requested time period. Or, vice versa, requested time period used up but not enough individual note durations. I am not sure I can change that compound condition in any way.
Since I am selecting note durations at random with a strong preference for shorter durations, the former situation appears to occur more often than the latter.
Rather than dumping all the unwanted cases and start over, I decided to see if I could easily fix a few of the cases. Hopefully saving a little time.
Fix Some Cases
I thought, for my test sequence of 6 steps, I might be able to fix the following cases more or less easily:
- 6 notes that did not fill the required sequence duration
- 5 notes that filled the required sequence duration, maybe 4 notes as well
So I decided to start with a function to determine whether I was going to attempt a fix or not. A function in case things change. If we are in a endless loop this function will be called to determine what to do next.
... ...
def doNDFix(nbr_nts, ct_nts):
""" Determine whether or not to try and fix an invalid
note sequence for the specified length of a step sequence
:param nbr_nts: number of note durations required
:param ct_nts: the note durations currently in the list
:returns: True if okay to fix list of note durations,
False otherwise
"""
doFix = False
min_nts = (nbr_nts - 1) if nbr_nts <= 5 else (nbr_nts - 2)
if len(ct_nts) >= 2:
# if have at least two notes try to fill out the list
doFix = True
elif len(ct_nts) == nbr_nts:
# if have a full set of notes fix things, again will see
# if that is viable
doFix = True
return doFix
... ...
So, the adjusted loop test code will look something like this.
... ...
strt_ovr = False
while (len(ct_nts) < nbr_nts) or (ct_tm < avl_tm):
... ...
if (len(ct_nts) == nbr_nts) or (ct_tm == avl_tm):
print(f"ct_tm: {ct_tm}, ct_nd: {ct_nd}\n\t{ct_nts}")
if doNDFix(nbr_nts, ct_nts):
# call fixer-upper function(s)
...
if strt_ovr:
ct_tm = 0
ct_nd = avl_tm
ct_nts = []
Since writing the content above, starting with the “Non-termination” sub-section, I have spent some considerable time working on potential fixes. Some seem to work. Some cases not yet sorted. But it is getting well into the day and I am going to take a break.
For the next couple of days, going around in circles, horribly convoluted code, and still no end in sight.
Today, after a brain-wave while awake in the middle of last night, I sorted out the two fixer-upper functions. And, they seem to work as intended. I really should have taken some time with paper and a pencil before starting to code in endless circles. Truly sad on my part!
Fixer-Upper Functions
... ...
def fixND2Few(nbr_nts, ct_nts, mldy):
""" Fix invalid note sequence if there are too few notes, but
the total sequence duration equals the requested time period
:param nbr_nts: number of note durations required
:param ct_nts: the note durations currently in the list
:param mldy: reference to a Melody class instance
:returns: modified ct_nts list
"""
nt_miss = nbr_nts - len(ct_nts)
if mldy.p_dbg:
print(f"\nfixND2Few({nbr_nts}, {ct_nts}): {nt_miss}")
while nt_miss > 0:
# get longest duration in current sequence
nt_lng = np.where(ct_nts == np.max(ct_nts))[0][-1]
t_dur = ct_nts[nt_lng]
t_nt = mldy.n_durs.dur2nt[t_dur]
if "●" in t_nt:
# dotted note, deal with appropriately
t_ndt = t_nt[:-1]
t_nd_dur = mldy.n_durs.n_dur[t_ndt]
ct_nts.append(t_dur - t_nd_dur)
ct_nts[nt_lng] = t_nd_dur
nt_miss -= 1
else:
# any other note duration should be divisible by 2
nw_dur = t_dur / 2
if nw_dur in mldy.n_durs.dur2nt.keys():
ct_nts[nt_lng] = nw_dur
ct_nts.append(nw_dur)
nt_miss -= 1
return ct_nts
def fixND2Shrt(avl_tm, nbr_nts, ct_nts, ct_nd, mldy):
""" Fix invalid note sequence if there are sufficient notes, but
the total sequence duration is less than the requested time period
:param avl_tm: the total time required for note durations
:param nbr_nts: number of note durations required
:param ct_nts: the note durations currently in the list
:param ct_nd: the total note time currently missing
:param mldy: reference to a Melody class instance
:returns: modified ct_nts list
"""
if mldy.p_dbg:
print(f"fixND2Shrt({avl_tm}, {nbr_nts}, {ct_nts}, {ct_nd}, mldy)")
while ct_nd > 0:
nt_shrt = np.where(ct_nts == np.min(ct_nts))[0][-1]
if ct_nd <= .0625:
ct_nts[nt_shrt] += ct_nd
ct_nd = 0
else:
dur_shrt = ct_nts[nt_shrt]
if dur_shrt < ct_nd:
ct_nts[nt_shrt] += dur_shrt
ct_nd -= dur_shrt
else:
hlf_shrt = dur_shrt / 2
if hlf_shrt in mldy.ok_durs:
ct_nts[nt_shrt] += hlf_shrt
ct_nd -= hlf_shrt
else:
thrd_shrt = dur_shrt / 3
ct_nts[nt_shrt] += thrd_shrt
ct_nd -= thrd_shrt
return ct_nts
... ...
genNtSeq()
Here’s the updated section in the genNtSeq() function; which generates the sequence of note durations.
... ...
while (len(ct_nts) < nbr_nts) or (ct_tm < avl_tm):
if ((len(ct_nts) == nbr_nts) or (ct_tm == avl_tm)):
if mldy.p_dbg:
print(f"ct_tm: {ct_tm}, ct_nd: {ct_nd}\n\t{ct_nts}")
print(f"\tdoNDFix({nbr_nts}, {ct_nts}): {doNDFix(nbr_nts, ct_nts)}")
if doNDFix(nbr_nts, ct_nts):
if mldy.p_dbg:
print(f"\t\tattempt fix")
if len(ct_nts) < nbr_nts:
# if not fixed will be caught below, I hope
ct_nts = fixND2Few(nbr_nts, ct_nts, mldy)
if len(ct_nts) < nbr_nts:
strt_ovr = True
else:
ct_nts = fixND2Shrt(avl_tm, nbr_nts, ct_nts, ct_nd, mldy)
# print(f"\n\t{ct_nts} -> {sum(ct_nts)}")
if sum(ct_nts) < avl_tm:
strt_ovr = True
#
break
... ...
Testing
Because I could, I added note symbols to the em_bars.Note_durations class. After getting the sequence I generate a list of the appropriate symbols for the durations returned by genNtSeq().
Here’s the updated test block.
... ...
if __name__ == '__main__':
... ...
if True:
... ...
avl_tms = [2.0, 1.0, 0.5]
ss_len = 6
# in some cases don't want to deal with dotted notes
mldy.ok_durs = [ntd for ntd in mldy.n_durs.dur2nt.keys() if "●" not in mldy.n_durs.dur2nt[ntd]]
mldy.r_ok_durs = mldy.ok_durs[::-1]
for avl_tm in avl_tms:
# get note sequence
print(f"\nduration seq ({ss_len} notes over {avl_tm} secs):")
s_tm = time.perf_counter()
nt_durs = genNtSeq(avl_tm, ss_len, mldy)
e_tm = time.perf_counter()
print(f"took {e_tm - s_tm} secs")
print(f"\t{nt_durs} -> {len(nt_durs)} notes for {sum(nt_durs)} secs total")
nt_syms = [mldy.n_durs.n_sym_utf[mldy.n_durs.dur2nt[ntd]] for ntd in nt_durs]
print(f"\t{' '.join(nt_syms)}")
exit(0)
And here’s the terminal output for a test run or three. The unnecessary bits removed. You might need to zoom the display to get a good look at the note symbols, especially the dotted ones.
PS R:\learn\e_m_311> uv run tst_melody.py
duration seq (6 notes over 2.0 secs):
took 0.0013704000011784956 secs
[0.375, 0.25, 0.25, 0.125, 0.5, 0.5] -> 6 notes for 2.0 secs total
𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅘𝅥𝅯 𝅘𝅥 𝅘𝅥
duration seq (6 notes over 1.0 secs):
took 0.0011567999972612597 secs
[0.125, 0.25, 0.1875, 0.125, 0.125, 0.1875] -> 6 notes for 1.0 secs total
𝅘𝅥𝅯 𝅘𝅥𝅮 𝅘𝅥𝅯 𝅭 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅭
duration seq (6 notes over 0.5 secs):
took 0.0010097000049427152 secs
[0.125, 0.125, 0.0625, 0.0625, 0.0625, 0.0625] -> 6 notes for 0.5 secs total
𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰
PS R:\learn\e_m_311> uv run tst_melody.py
duration seq (6 notes over 2.0 secs):
took 0.0015544000052614138 secs
[0.25, 0.375, 0.375, 0.375, 0.25, 0.375] -> 6 notes for 2.0 secs total
𝅘𝅥𝅮 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅭
duration seq (6 notes over 1.0 secs):
took 0.0015392000059364364 secs
[0.125, 0.125, 0.125, 0.25, 0.25, 0.125] -> 6 notes for 1.0 secs total
𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅘𝅥𝅯
duration seq (6 notes over 0.5 secs):
took 0.0016593000036664307 secs
[0.0625, 0.125, 0.0625, 0.0625, 0.0625, 0.125] -> 6 notes for 0.5 secs total
𝅘𝅥𝅰 𝅘𝅥𝅯 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅯
Curiosity and Comparison
Does this new function actually generate anything fundamentally different than the one of my first approach: Melody.getStepDurs(). Let’s write some code to check that out. Some repetition from previously shown code in this test block.
Note: I did, at first, try using em_bars.make_bar() as my old approach. But could not find a way to get it to generate sequences of less than 2.0 seconds. And, certainly, at this time, was not interested in refactoring the function.
A bried visual comparison of the sequences of 2 seconds duration produced by the two approaches generated similar rhythms. With a slight preference in some shorter duration notes by the
make_bar()method called bymake_chd_rhythm(). Which is due to the specific selection probabilities used inmake_bar()versus bygenNtSeq(). WellgenNtSeq()callsgetDursPrbs()to get the allow notes and their probabilties.
for fn in ["old method", "new function"]:
print(f"\nsequence note durations using {fn}")
for avl_tm in avl_tms:
# get note sequence
print(f"\n\tduration seq ({ss_len} notes over {avl_tm} secs):")
s_tm = time.perf_counter()
if fn == "new function":
nt_durs = genNtSeq(avl_tm, ss_len, mldy)
else:
n_bts = (mldy.sec_b / mldy.m_spb) * (avl_tm / 2)
d_nts = mldy.getStepDurs(ss_len, n_bts=n_bts)
nt_durs = [mldy.n_durs.n_dur[nt] for nt in d_nts]
print(f"\t {nt_durs}")
e_tm = time.perf_counter()
print(f"took {e_tm - s_tm} secs")
print(f"\t {nt_durs} -> {len(nt_durs)} notes for {sum(nt_durs)} secs total")
nt_syms = [mldy.n_durs.n_sym_utf[mldy.n_durs.dur2nt[ntd]] for ntd in nt_durs]
print(f"\t {' '.join(nt_syms)}")
And a couple test runs. Removed anything I didn’t think was relevant.
(base) PS R:\learn\e_m_311> uv run tst_melody.py
sequence note durations using old method
duration seq (6 notes over 2.0 secs):
[0.25, 0.5, 0.25, 0.5, 0.25, 0.25] -> 6 notes for 2.0 secs total
𝅘𝅥𝅮 𝅘𝅥 𝅘𝅥𝅮 𝅘𝅥 𝅘𝅥𝅮 𝅘𝅥𝅮
duration seq (6 notes over 1.0 secs):
[0.125, 0.125, 0.125, 0.25, 0.125, 0.25] -> 6 notes for 1.0 secs total
𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅮 𝅘𝅥𝅯 𝅘𝅥𝅮
duration seq (6 notes over 0.5 secs):
[0.125, 0.0625, 0.0625, 0.0625, 0.0625, 0.125] -> 6 notes for 0.5 secs total
𝅘𝅥𝅯 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅯
sequence note durations using new function
duration seq (6 notes over 2.0 secs):
[0.375, 0.25, 0.375, 0.375, 0.375, 0.25] -> 6 notes for 2.0 secs total
𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮
duration seq (6 notes over 1.0 secs):
[0.125, 0.1875, 0.25, 0.125, 0.1875, 0.125] -> 6 notes for 1.0 secs total
𝅘𝅥𝅯 𝅘𝅥𝅯 𝅭 𝅘𝅥𝅮 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅭 𝅘𝅥𝅯
duration seq (6 notes over 0.5 secs):
[0.125, 0.0625, 0.125, 0.0625, 0.0625, 0.0625] -> 6 notes for 0.5 secs total
𝅘𝅥𝅯 𝅘𝅥𝅰 𝅘𝅥𝅯 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰
(base) PS R:\learn\e_m_311> uv run tst_melody.py
sequence note durations using old method
duration seq (6 notes over 2.0 secs):
[0.5, 0.5, 0.25, 0.25, 0.25, 0.25] -> 6 notes for 2.0 secs total
𝅘𝅥 𝅘𝅥 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅘𝅥𝅮
duration seq (6 notes over 1.0 secs):
[0.125, 0.125, 0.125, 0.25, 0.25, 0.125] -> 6 notes for 1.0 secs total
𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅘𝅥𝅯
duration seq (6 notes over 0.5 secs):
[0.0625, 0.0625, 0.0625, 0.125, 0.125, 0.0625] -> 6 notes for 0.5 secs total
𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅰
sequence note durations using new function
duration seq (6 notes over 2.0 secs):
[0.375, 0.375, 0.375, 0.25, 0.375, 0.25] -> 6 notes for 2.0 secs total
𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮 𝅘𝅥𝅮 𝅭 𝅘𝅥𝅮
duration seq (6 notes over 1.0 secs):
[0.1875, 0.25, 0.1875, 0.125, 0.125, 0.125] -> 6 notes for 1.0 secs total
𝅘𝅥𝅯 𝅭 𝅘𝅥𝅮 𝅘𝅥𝅯 𝅭 𝅘𝅥𝅯 𝅘𝅥𝅯 𝅘𝅥𝅯
duration seq (6 notes over 0.5 secs):
[0.0625, 0.125, 0.0625, 0.0625, 0.0625, 0.125] -> 6 notes for 0.5 secs total
𝅘𝅥𝅰 𝅘𝅥𝅯 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅰 𝅘𝅥𝅯
You know, I do think there is slightly more variation in the sequences with longer durations using the new function. But that is likely only because, in the new function, I am allowing the use of all available notes in the em_bars.Note_durations class. The Melody class method, getStepDurs(), skips dotted notes. If it used all available notes, I expect there would be little or no difference in the sequence of durations the two approaches would generate. At least once we account for any differences in the probabilities used when choosing notes.
Finished for This One
I don’t know what I am going to look at next. A few to-do list items not yet sorted out. And, as mentioned in the preceding paragraph or three, a new item or three added to the list. But that is for another day and/or time.
Until then, do enjoy your play time.
Resources
- Codes for Musical Note & Instrument Symbols