Skip to content

Narmour

NarmourOption

Bases: Enum

The specific principles and/or corrections to calculate for narmour.

The string values for all of these options contain their corresponding note label string for when the call to narmour succeeds in processing a score.

Attributes:

  • REGISTRAL_DIRECTION ((Schellenberg 1997)) –

    Original 'rd' option from midi toolbox

  • REGISTRAL_RETURN ((Schellenberg 1997)) –

    Original 'rr' option from midi toolbox

  • INTERVALLIC_DIFFERENCE (intervallic difference) –

    Original 'id' option from midi toolbox

  • CLOSURE (closure) –

    Original 'cl' option from midi toolbox

  • PROXIMITY ((Schellenberg 1997)) –

    Original 'pr' option from midi toolbox

  • CONSONANCE ((Krumhansl 1995)) –

    Original 'co' option from midi toolbox

narmour

narmour(
    score: Score, principle: NarmourOption
) -> tuple[Score, str] | None

Calculate prediction values for a trait in Narmour's Implication-realization model, and annotates the individual notes with their corresponding predictions. The annotated field can be accessed through the + "_narmour" from a note in the score (e.g. "registral_direction_narmour for the registral_direction trait").

This function implements various principles from Narmour's (1990) model of melodic expectancy, including revisions by Schellenberg (1997) and Krumhansl (1995).

Please see the comments for the internal functions detailing what each flavor of narmour is doing.

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze.

  • principle (NarmourOption) –

    The specific principle options to calculate.

Returns:

  • tuple[Score, str] | None –

    Returns 2 things if successful: (1) Score where each note is annotated with a value calculated according to the desired narmour principle. (2) annotation string used to annotate the values obtained by that particular narmour option. Else: None if score does not satisfy the preconditions of being monophonic or if the score does not satisfy the preconditions necessary to calculate the specified feature.

References
Narmour, E. (1990). The Analysis and cognition of basic melodic
    structures: The Implication-realization model. Chicago: University
    of Chicago Press.
Schellenberg, E. G. (1997). Simplifying the implication-realization
    model of melodic expectancy. Music Perception, 14, 295-318.
Krumhansl, C. L. (1995). Effects of musical context on similarity
    and expectancy. Systematische musikwissenschaft, 3, 211-250.
Source code in amads/melody/narmour.py
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
def narmour(score: Score, principle: NarmourOption) -> tuple[Score, str] | None:
    """
    Calculate prediction values for a trait in Narmour's Implication-realization
    model, and annotates the individual notes with their corresponding
    predictions. The annotated field can be accessed through the
    <principle> + "_narmour" from a note in the score (e.g.
    "registral_direction_narmour for the registral_direction trait").

    This function implements various principles from Narmour's (1990) model
    of melodic expectancy, including revisions by Schellenberg (1997) and
    Krumhansl (1995).

    Please see the comments for the internal functions detailing what each
    flavor of narmour is doing.

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze.
    principle : NarmourOption
        The specific principle options to calculate.

    Returns
    -------
    tuple[Score, str] | None
        Returns 2 things if successful:
        (1) Score where each note is annotated with a value calculated according
        to the desired narmour principle.
        (2) annotation string used to annotate the values obtained by that
        particular narmour option.
        Else:
        None if score does not satisfy the preconditions of being monophonic
        or if the score does not satisfy the preconditions necessary to calculate
        the specified feature.

    References
    ----------
        Narmour, E. (1990). The Analysis and cognition of basic melodic
            structures: The Implication-realization model. Chicago: University
            of Chicago Press.
        Schellenberg, E. G. (1997). Simplifying the implication-realization
            model of melodic expectancy. Music Perception, 14, 295-318.
        Krumhansl, C. L. (1995). Effects of musical context on similarity
            and expectancy. Systematische musikwissenschaft, 3, 211-250.
    """

    # Calculate expectations based on selected principle
    feature_functions = {
        NarmourOption.REGISTRAL_DIRECTION: _registral_direction,
        NarmourOption.REGISTRAL_RETURN: _registral_return,
        NarmourOption.INTERVALLIC_DIFFERENCE: _intervallic_difference,
        NarmourOption.CLOSURE: _closure,
        NarmourOption.PROXIMITY: _proximity,
        NarmourOption.CONSONANCE: _consonance,
    }

    feature_func = feature_functions.get(principle)
    if feature_func is None:
        return None
    annotation_str = principle.value
    result = feature_func(score)
    if result is None:
        return None

    return (result, annotation_str)

_registral_direction

_registral_direction(score: Score) -> Score | None

Calculates registral direction (revised version) as follows: if small pitch interval between the first two neighboring notes is at or below threshold, annotate with 0 else, if followed by change in direction, annotate with 1 and if followed by same direction, annotate with -1 (Thompson p. 248, Schellenberg 1996)

Implements the REGISTRAL_DIRECTION option.

The annotated field within each note of the annotated score if successful is "narmour_registral_direction"

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze.

Returns:

  • Score | None –

    Score where each note is annotated with a value calculated according to the desired narmour principle. None if score does not satisfy the preconditions of having at least 3 notes and is not monophonic

Source code in amads/melody/narmour.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def _registral_direction(score: Score) -> Score | None:
    """
    Calculates registral direction (revised version) as follows:
    if small pitch interval between the first two neighboring notes is at or
    below threshold, annotate with 0
    else, if followed by change in direction, annotate with 1
    and if followed by same direction, annotate with -1
    (Thompson p. 248, Schellenberg 1996)

    Implements the REGISTRAL_DIRECTION option.

    The annotated field within each note of the annotated score if successful
    is "narmour_registral_direction"

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze.

    Returns
    -------
    Score | None
        Score where each note is annotated with a value calculated according
        to the desired narmour principle.
        None if score does not satisfy the preconditions of having at least
        3 notes and is not monophonic
    """
    annotation_str = "narmour_registral_direction"
    small_interval_threshold = 6
    notes_window_iter = _narmour_preprocess_score_internal(
        score, annotation_str
    )
    if notes_window_iter is None:
        return None
    for notes in notes_window_iter:
        current_diff = notes[1].midi_num - notes[0].midi_num
        next_diff = notes[2].midi_num - notes[1].midi_num
        if current_diff <= small_interval_threshold:
            notes[2].set(annotation_str, 0)
        elif (
            current_diff > small_interval_threshold
            and _simple_int_sign_comparison(current_diff, next_diff)
        ):
            notes[2].set(annotation_str, 1)
        elif (
            current_diff > small_interval_threshold
            and not _simple_int_sign_comparison(current_diff, next_diff)
        ):
            notes[2].set(annotation_str, -1)
        else:
            raise ValueError(f"{current_diff}, {next_diff} are invalid")
    return score

_registral_return

_registral_return(score: Score) -> Score | None

Calculates registral return (revised version) as follows: if second realized tone is within 2 semitones of the first tone in the implied interval, 1 (or 1.5), others 0. Modifier of 1 is used in the version revised by Schellenberg (1996) and a better modifier (1.5, which is used) suggested by Schellengerg (1997).

Implements the REGISTRAL_RETURN option.

The annotated field within each note of the annotated score if successful is "narmour_registral_return"

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze.

Returns:

  • Score | None –

    Score where each note is annotated with a value calculated according to the desired narmour principle. None if score does not satisfy the preconditions of having at least 3 notes and is not monophonic

Source code in amads/melody/narmour.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def _registral_return(score: Score) -> Score | None:
    """
    Calculates registral return (revised version) as follows:
    if second realized tone is within 2 semitones of the
    first tone in the implied interval, 1 (or 1.5), others 0.
    Modifier of 1 is used in the version revised by Schellenberg (1996) and
    a better modifier (1.5, which is used) suggested by Schellengerg (1997).

    Implements the REGISTRAL_RETURN option.

    The annotated field within each note of the annotated score if successful
    is "narmour_registral_return"

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze.

    Returns
    -------
    Score | None
        Score where each note is annotated with a value calculated according
        to the desired narmour principle.
        None if score does not satisfy the preconditions of having at least
        3 notes and is not monophonic
    """
    annotation_str = "narmour_registral_return"
    interval_symmetry_check_threshold = 2
    interval_symmetry_modifier = 1.5
    notes_window_iter = _narmour_preprocess_score_internal(
        score, annotation_str
    )
    if notes_window_iter is None:
        return None
    for notes in notes_window_iter:
        current_diff = notes[1].midi_num - notes[0].midi_num
        next_diff = notes[2].midi_num - notes[1].midi_num
        if abs(current_diff + next_diff) <= interval_symmetry_check_threshold:
            notes[2].set(annotation_str, interval_symmetry_modifier)
        else:
            notes[2].set(annotation_str, 0)
    return score

_closure

_closure(score: Score) -> Score | None

Closure occurs when registral direction changes, or when a large interval is followed by a smaller interval (i.e. smaller by more than 3 semitones if registral direction is the same, or smaller by two semitones otherwise). All events that satisfy this condition are assigned a score of 1, and all other events are assigned a score of 0.

Implements the CLOSURE option.

The annotated field within each note of the annotated score if successful is "narmour_closure"

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze.

Returns:

  • Score | None –

    Score where each note is annotated with a value calculated according to the desired narmour principle. None if score does not satisfy the preconditions of having at least 3 notes and is not monophonic

Source code in amads/melody/narmour.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
def _closure(score: Score) -> Score | None:
    """
    Closure occurs when registral direction changes, or when a large interval
    is followed by a smaller interval (i.e. smaller by more than 3 semitones
    if registral direction is the same, or smaller by two semitones otherwise).
    All events that satisfy this condition are assigned a score of 1, and all
    other events are assigned a score of 0.

    Implements the CLOSURE option.

    The annotated field within each note of the annotated score if successful
    is "narmour_closure"

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze.

    Returns
    -------
    Score | None
        Score where each note is annotated with a value calculated according
        to the desired narmour principle.
        None if score does not satisfy the preconditions of having at least
        3 notes and is not monophonic
    """
    annotation_str = "narmour_closure"
    notes_window_iter = _narmour_preprocess_score_internal(
        score, annotation_str
    )
    if notes_window_iter is None:
        return None
    for notes in notes_window_iter:
        current_diff = notes[1].midi_num - notes[0].midi_num
        next_diff = notes[2].midi_num - notes[1].midi_num
        sign_compare_diffs = _simple_int_sign_comparison(
            current_diff, next_diff
        )
        diff_of_diffs = abs(current_diff) - abs(next_diff)
        if not sign_compare_diffs:
            if diff_of_diffs < 3:
                notes[2].set(annotation_str, 1)
            else:
                notes[2].set(annotation_str, 2)
        else:
            if diff_of_diffs > 3:
                notes[2].set(annotation_str, 1)
            else:
                notes[2].set(annotation_str, 0)

    return score

_intervallic_difference

_intervallic_difference(score: Score) -> Score | None

This states that small implicative intervals (i.e. perfect fourth or less) imply similarly-sized realized intervals whereas large implicative intervals imply comparatively smaller intervals. 'Similarly-sized' realized intervals after a small interval are defined as the same size +- three semitones if the realized interval does not change registral direction, otherwise they are defined as the same size +-two semitones. All events fulfilling these conditions are assigned a score of 1 whereas other events are assigned a value of 0. Derived from Schellenberg, 1997, p. 296-297).

Implements the INTERVALLIC_DIFFERENCE option.

The annotated field within each note of the annotated score if successful is "narmour_intervallic_difference"

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze.

Returns:

  • Score | None –

    Score where each note is annotated with a value calculated according to the desired narmour principle. None if score does not satisfy the preconditions of having at least 3 notes and is not monophonic

Source code in amads/melody/narmour.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
def _intervallic_difference(score: Score) -> Score | None:
    """
    This states that small implicative intervals (i.e. perfect fourth or less)
    imply similarly-sized realized intervals whereas large implicative
    intervals imply comparatively smaller intervals.
    'Similarly-sized' realized intervals after a small interval are defined as
    the same size +- three semitones if the realized interval does not change
    registral direction, otherwise they are defined as the same size +-two
    semitones.
    All events fulfilling these conditions are assigned a score of 1 whereas
    other events are assigned a value of 0.
    Derived from Schellenberg, 1997, p. 296-297).

    Implements the INTERVALLIC_DIFFERENCE option.

    The annotated field within each note of the annotated score if successful
    is "narmour_intervallic_difference"

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze.

    Returns
    -------
    Score | None
        Score where each note is annotated with a value calculated according
        to the desired narmour principle.
        None if score does not satisfy the preconditions of having at least
        3 notes and is not monophonic
    """
    annotation_str = "narmour_intervallic_difference"
    small_interval_threshold = 6
    notes_window_iter = _narmour_preprocess_score_internal(
        score, annotation_str
    )
    if notes_window_iter is None:
        return None
    for notes in notes_window_iter:
        current_diff = notes[1].midi_num - notes[0].midi_num
        next_diff = notes[2].midi_num - notes[1].midi_num
        sign_compare_diffs = _simple_int_sign_comparison(
            current_diff, next_diff
        )
        diff_of_diffs = abs(current_diff) - abs(next_diff)
        if current_diff < small_interval_threshold:
            if (not sign_compare_diffs and abs(diff_of_diffs) < 3) or (
                sign_compare_diffs and abs(diff_of_diffs) < 4
            ):
                notes[2].set(annotation_str, 1)
            else:
                notes[2].set(annotation_str, 0)
        elif current_diff > small_interval_threshold:
            if abs(current_diff) >= abs(next_diff):
                notes[2].set(annotation_str, 1)
            else:
                notes[2].set(annotation_str, 0)
        else:
            notes[2].set(annotation_str, 0)
    return score

_proximity

_proximity(score: Score) -> Score | None

Linear coding of pitch proximity.

Implements the PROXIMITY option.

The annotated field within each note of the annotated score if successful is "narmour_proximity"

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze.

Returns:

  • Score | None –

    Score where each note is annotated with a value calculated according to the desired narmour principle. None if score does not satisfy the preconditions of having at least 2 notes and is not monophonic

Source code in amads/melody/narmour.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
def _proximity(score: Score) -> Score | None:
    """
    Linear coding of pitch proximity.

    Implements the PROXIMITY option.

    The annotated field within each note of the annotated score if successful
    is "narmour_proximity"

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze.

    Returns
    -------
    Score | None
        Score where each note is annotated with a value calculated according
        to the desired narmour principle.
        None if score does not satisfy the preconditions of having at least
        2 notes and is not monophonic
    """
    annotation_str = "narmour_proximity"
    if not score.ismonophonic():
        return None

    note_iter = score.find_all(Note)
    check_note_iter, note_iter = tee(note_iter)
    obtained_notes = list(islice(check_note_iter, 2))
    if len(obtained_notes) != 2:
        return None
    obtained_notes[0].set(annotation_str, 0)
    for notes in pairwise(note_iter):
        current_diff = notes[1].midi_num - notes[0].midi_num
        notes[1].set(annotation_str, abs(current_diff))

    return score

_consonance

_consonance(score: Score) -> Score | None

Narmour's model under the consonance principle: if consonant with previous tone (see Krumhansl 1995 "Effects of musical context on similarity and expectancy", Systematische musikwissenschaft or Krumhansl (1990), p. 57.

Implements the CONSONANCE option.

The annotated field within each note of the annotated score if successful is "narmour_consonance"

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze.

Returns:

  • Score | None –

    Score where each note is annotated with a value calculated according to the desired narmour principle. None if score does not satisfy the preconditions of having at least 3 notes and is not monophonic

Source code in amads/melody/narmour.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
def _consonance(score: Score) -> Score | None:
    """
    Narmour's model under the consonance principle:
    if consonant with previous tone (see Krumhansl 1995
    "Effects of musical context on similarity and expectancy",
    Systematische musikwissenschaft or Krumhansl (1990), p. 57.

    Implements the CONSONANCE option.

    The annotated field within each note of the annotated score if successful
    is "narmour_consonance"

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze.

    Returns
    -------
    Score | None
        Score where each note is annotated with a value calculated according
        to the desired narmour principle.
        None if score does not satisfy the preconditions of having at least
        3 notes and is not monophonic
    """
    # Consonance ratings from Krumhansl (1995)
    CONSONANCE_RATINGS = [
        10,
        1,
        4.16,
        6.03,
        7.25,
        8.03,
        5.76,
        9.16,
        6.32,
        7.76,
        5.66,
        3.8,
    ]
    annotation_str = "narmour_consonance"

    note_iter = score.find_all(Note)
    note_iter, check_note_iter = tee(note_iter)
    obtained_notes = list(islice(check_note_iter, 2))
    if len(obtained_notes) != 2:
        return None
    if not score.ismonophonic():
        return None
    obtained_notes[0].set(annotation_str, CONSONANCE_RATINGS[0])

    for notes in pairwise(note_iter):
        rating_idx = abs(notes[1].midi_num - notes[0].midi_num) % 12
        consonance_rating = CONSONANCE_RATINGS[rating_idx]
        notes[1].set(annotation_str, consonance_rating)

    return score