Skip to content

Melodic Complexity

complexity_trans

complexity_trans(score: Score) -> float | None

Calculate Simonton's complexity originality score based on 2nd order pitch-class transition probabilities derived from classical themes.

This function implements Simonton's (1984, 1994) measure of melodic originality using transition probabilities derived from analysis of 15,618 classical themes. Higher values indicate more original/unusual melodic transitions.

Parameters:

  • score (Score) –

    A Score object containing the melody to analyze. The score will be flattened and collapsed into a single sequence of notes ordered by onset time.

Returns:

  • float –

    Melodic originality score scaled between 0 and 10. Higher values indicate higher melodic originality. Returns 0 for empty scores or sequences shorter than 3 notes.

References
Simonton, D. K. (1984). Melodic structure and note transition
    probabilities: A content analysis of 15,618 classical themes.
    Psychology of Music, 12, 3-16.
Simonton, D. K. (1994). Computer content analysis of melodic
    structure: Classical composers and their compositions.
    Psychology of Music, 22, 31-43.
Source code in amads/melody/complexity_trans.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def complexity_trans(score: Score) -> float | None:
    """
    Calculate Simonton's complexity originality score based on 2nd order
    pitch-class transition probabilities derived from classical themes.

    This function implements Simonton's (1984, 1994) measure of melodic
    originality using transition probabilities derived from analysis of
    15,618 classical themes. Higher values indicate more original/unusual
    melodic transitions.

    Parameters
    ----------
    score : Score
        A Score object containing the melody to analyze. The score will be
        flattened and collapsed into a single sequence of notes ordered by
        onset time.

    Returns
    -------
    float
        Melodic originality score scaled between 0 and 10.
        Higher values indicate higher melodic originality.
        Returns 0 for empty scores or sequences shorter than 3 notes.

    References
    ----------
        Simonton, D. K. (1984). Melodic structure and note transition
            probabilities: A content analysis of 15,618 classical themes.
            Psychology of Music, 12, 3-16.
        Simonton, D. K. (1994). Computer content analysis of melodic
            structure: Classical composers and their compositions.
            Psychology of Music, 22, 31-43.
    """
    if not score.ismonophonic():
        return None
    num_notes = nnotes(score)
    if num_notes < 2:
        return None

    note_iter = score.find_all(Note)
    note_iter, next_note_iter = tee(note_iter)
    if next(next_note_iter, None) is None:
        return None
    pitch_transition_sums = np.zeros(simonton_transition_dist.dimensions)
    for note, next_note in pairwise(note_iter):
        assert isinstance(note, Note) and isinstance(next_note, Note)
        current_pc = note.pitch_class
        next_pc = next_note.pitch_class
        pitch_transition_sums[current_pc, next_pc] += 1

    simonton_dist_data = np.array(simonton_transition_dist.data)
    assert simonton_dist_data.shape == pitch_transition_sums.shape

    # takes the weighted average of transition probabilities with our data
    # and change the result's sign.
    weighted_transition_avg = np.sum(simonton_dist_data * pitch_transition_sums)
    weighted_transition_avg /= (num_notes - 1) * -1
    # scaled from 0-10 (10=complex)
    # numbers copied directly from the matlab version (in compltrans.m).
    scaled_transition_weight = (weighted_transition_avg + 0.0530) * 188.68

    return scaled_transition_weight

complexity_ebm

complexity_ebm(
    score: Score,
    method: ComplexityEBMOption = ComplexityEBMOption.OPTIMAL_MIX,
) -> float

Calculate the expectancy-based model of melodic complexity.

This function implements the complexity model from Eerola & North (2000), which can analyze either pitch-related components, rhythm-related components, or their optimal combination. The output is calibrated against the Essen collection (mean=5, std=1).

The miditoolbox version contains a bug in the optimal ('o') option, where the entropy of the pitch-class distribution (through pcdist1) was calculated as entropy of the interval distribution (through ivdist1).

Parameters:

  • score (Score) –

    A monophonic Score object containing the melody to analyze.

  • method (ComplEBMOption, default: OPTIMAL_MIX ) –

    The method to use for complexity calculation: - ComplexityEBMOption.PITCH: pitch-related components only - ComplexityEBMOption.RHYTHM: rhythm-related components only - ComplexityEBMOption.OPTIMAL_MIX: linear combination of pitch complexity and rhythm complexity with predetermined weights (default; as to what it's supposed to be optimal in, that beats me...)

Returns:

  • float –

    Complexity value calibrated relative to the Essen Collection. Higher values indicate higher complexity. Returns 0 for empty scores or single notes.

References
Eerola, T. & North, A. C. (2000). Expectancy-Based Model of Melodic
    Complexity. In Proceedings of the Sixth International Conference on
    Music Perception and Cognition.
Schaffrath, H. (1995). The Essen folksong collection in kern format.
Source code in amads/melody/complexity_ebm.py
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
199
200
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
def complexity_ebm(
    score: Score,
    method: ComplexityEBMOption = ComplexityEBMOption.OPTIMAL_MIX,
) -> float:
    """
    Calculate the expectancy-based model of melodic complexity.

    This function implements the complexity model from Eerola & North (2000),
    which can analyze either pitch-related components, rhythm-related components,
    or their optimal combination. The output is calibrated against the Essen
    collection (mean=5, std=1).

    The miditoolbox version contains a bug in the optimal ('o') option, where the
    entropy of the pitch-class distribution (through pcdist1) was calculated
    as entropy of the interval distribution (through ivdist1).

    Parameters
    ----------
    score : Score
        A monophonic Score object containing the melody to analyze.
    method : ComplEBMOption
        The method to use for complexity calculation:
        - ComplexityEBMOption.PITCH: pitch-related components only
        - ComplexityEBMOption.RHYTHM: rhythm-related components only
        - ComplexityEBMOption.OPTIMAL_MIX: linear combination of pitch
        complexity and rhythm complexity with predetermined weights
        (default; as to what it's supposed to be optimal in, that beats me...)

    Returns
    -------
    float
        Complexity value calibrated relative to the Essen Collection.
        Higher values indicate higher complexity.
        Returns 0 for empty scores or single notes.

    References
    ----------
        Eerola, T. & North, A. C. (2000). Expectancy-Based Model of Melodic
            Complexity. In Proceedings of the Sixth International Conference on
            Music Perception and Cognition.
        Schaffrath, H. (1995). The Essen folksong collection in kern format.
    """

    if not score.ismonophonic():
        return None

    # Calculate complexity based on selected method
    match method:
        case ComplexityEBMOption.PITCH:
            # the linear weights assigned to the different pitch components
            # in pitch complexity
            weights = (0.3, 1, 0.8, -1)
            offset = -0.2407
            divisor = 0.9040  # standard deviation of pitches (in Essen)?
            pitch_components = _compute_pitch_components(score)
            if pitch_components is None:
                return None
            complexity = offset + sum(
                weight * component
                for weight, component in zip(weights, pitch_components)
            )
            complexity /= divisor
        case ComplexityEBMOption.RHYTHM:
            # the linear weights assigned to the different pitch components
            # in rhythm complexity
            weights = (0.7, 0.2, 0.5, 0.5)
            offset = -0.7841
            divisor = 0.3637  # standard deviation of durations (in Essen)?
            rhythm_components = _compute_rhythm_components(score)
            if rhythm_components is None:
                return None
            complexity = offset + sum(
                weight * component
                for weight, component in zip(weights, rhythm_components)
            )
            complexity /= divisor
        case ComplexityEBMOption.OPTIMAL_MIX:
            pitch_weights = (0.2, 1.5, 1.3, -1)
            rhythm_weights = (0.5, 0.4, 0.9, 0.8)
            offset = -1.9025
            divisor = 1.5034

            pitch_components = _compute_pitch_components(score)
            rhythm_components = _compute_rhythm_components(score)
            if pitch_components is None or rhythm_components is None:
                return None

            component_values = chain(pitch_components, rhythm_components)
            component_weights = chain(pitch_weights, rhythm_weights)

            complexity = offset + sum(
                weight * component
                for weight, component in zip(
                    component_weights, component_values
                )
            )
            complexity /= divisor
        case _:
            return None

    calibrated_complexity = complexity + _GLOBAL_MEAN_VALUE

    return calibrated_complexity