Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What Python codes can help us master the basic music theory?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what Python code can help us master the basic music theory, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

We will first look at notes in western music theory, use them to derive chromatic scales in a given key, and then combine them with interval formulas to derive common scales and chords.

Finally, we will study the patterns, which are the entire set of scales derived from the universal scale, which can be used to evoke more subtle emotions and atmosphere than the dichotomy of sadness and joy provided by the major and secondary scales.

Twelve notes

The letters of Western music are made up of the letters A to G, which represent different pitches.

We can use the following Python list to represent music letters:

Alphabet = ['Aids,' bundles, 'canals,' dudes, 'eaters,' Fathers,'G']

However, the frequency distribution of these notes is uneven. To make the pitch more evenly spaced, we have the following twelve notes:

Notes_basic = [['A'], ['Achievement,' Bb'], ['B'], ['C'], ['Clover,' Db'], ['D'], ['During,' Eb'], ['E'], ['F'], ['Flowers,' Gb'], ['G'] ['Gateway,' Ab'],]

There are four points to note here: first, each note is half-step or half-tone apart, and secondly, it is represented by an optional trailing symbol (called accidental symbol) to indicate a half-step rise (sharp,?) Or lower the tone by half a scale (flat,?). Third, the above notes are just a loop and a fresh start, but the scale is higher.

Finally, you will notice that some of these notes are represented by a list of multiple names: these are homophonic equivalents, which is a strange way to say that the same note can have different "spelling". So, for example, a note half a step above An is a note, but it can also be thought of as a note half a step below B, so it can be called B? For historical reasons, there is no sharp or flat part between the notes Bhammer C and Ehammer F.

The important reason we need these equivalents is that when we begin to derive universal scales (major, minor, and patterns), continuous notes must begin with consecutive letters. Homophonic equivalents with different letters enable us to correctly derive these scales.

In fact, the above homophonic notes are not enough for some keys. In order to meet the "different letter rules of consecutive letters", we finally have to use double sharpness and double flattening to raise or lower the whole step of the note. These scales usually have equivalents that do not require these double contingencies, but for completeness, we can include all possible homophonic equivalents by rewriting our comments, as follows:

Notes = [['Barrier,' Dbb', 'Dbb'], [' Barrier, 'Ebb',' Db'], ['Clearing,' Ebb'], ['During,' Eb', 'Fbb'], [' During, 'Fb',' Fb'], ['Ethereal,' Flying, 'Gbb'] ['Epistasis,' Bb', 'Gb'], [' Flemish, 'Globe,' Abb'], ['Globe,' Ab'], ['Globe,' Bbb'], ['Aging,' Bb', 'Cbb'], [' Achievement, 'Bond,' Cb'],] chromatic scale

The chromatic scale is the simplest scale, which consists only of all (twelve) semitones between the octaves of a given tone (the main note in the scale, also known as the tone).

We can easily generate a chromatic scale for any given key: (I) find the index of the note in our note list, (ii) rotate the note list multiple times.

Find the index of a given note

Let's write a simple function to find a specific note in this list:

Def find_note_index (scale, search_note):''Given a scale, find the index of a particular note' for index, note in enumerate (scale): # Deal with situations where we have a list of enharmonic # equivalents, as well as just a single note as and str. If type (note) = = list: if search_note in note: return index elif type (note) = = str: if search_note = = note: return index

The find_note_index () function takes a series of notes (scale) and notes to search (search_note) as arguments and returns the index through a simple linear search. We deal with two cases in the loop: (I) the scale provided consists of a single note (such as the list of letters above), or (ii) consists of a list of scale equivalents (such as the note or notes_basic list above). The following is an example of this function for both cases:

> find_note_index (notes,'A') # notes is a list of lists 9 > find_note_index (alphabet,'A') # alphabet is a list of notes 0 rotate the note to the left

Now we can write a function that rotates a given scale by n steps:

Def rotate (scale, n):''Left-rotate a scale by n positions. '' Return scale [n:] + scale [: n]

We cut the scale list at position n and swap the two halves. This is an example of rotating the alphabet list by three positions (with the note D in front):

> alphabet ['Aging,' baking, 'canals,' Dems, 'Etudes,' Fathers,'G'] > rotate (alphabet, 3) ['DVS,' Fathers, 'Fathers,' Gems, 'Aids,' bands,'C'] generate chromatic scales in a given key

Now we can finally write our colour () function, which generates a chromatic scale for a given key by rotating the notes array:

Def chromatic (key):''Generate a chromatic scale in a given key. '' # Figure out how much to rotate the notes list by and return # the rotated version. Num_rotations = find_note_index (notes, key) return rotate (notes, num_rotations)

The above colour () function finds the index of the supplied key in the list of comments (using our find_note_index () function), and then rotates that amount to move it to the front (using our rotate () function). This is an example of generating a D-chromatic scale:

> > import pprint > pprint.pprint (chromatic ('D')) [['Clearing,' Ebb', 'Ebb'], [' During, 'Eb',' Fbb'], ['During,' Fb'], ['Emission,' Gbb'], ['Emission,' Flying, 'Gb'], [' Fairchild, 'Abb',' Abb'] ['Groubie,' Ab'], ['Gambier,' Bbb'], ['Aban,' Bb', 'Cbb'], [' Atoll, 'Cb'], [' Barrier, 'Dbb',' Dbb'], ['Barrier,' Db']]

For chromatic scales, sharpness is usually used when rising and flatness is used when falling. However, for the time being, we keep the homophonic equivalence unchanged. We will see how to choose the right syllable for later use.

Interval time

The interval specifies the relative distance between notes.

Therefore, it can be named based on the relative distance between the chromatic note and the root note. The following is the standard name of each syllable in the same order as the index in the syllable list:

Intervals = [['P1mom,' d2'], # Perfect unison Diminished second ['M2BZ,' A1'], # Minor second Augmented unison ['M2Qing,' d3'], # Major second Diminished third ['M3Qing,' A2'], # Minor third Augmented second ['M3Qing,' d4'], # Major third Diminished fourth ['P4shipping,' A3'] # Perfect fourth Augmented third ['d5legs,' A4'], # Diminished fifth Augmented fourth ['P5legs,' d6'], # Perfect fifth Diminished sixth ['M6legs,' A5'], # Minor sixth Augmented fifth ['M6legs,' d7'], # Major sixth Diminished seventh ['M7legs,' A6'], # Minor seventh Augmented sixth ['M7legs,' d8'] # Major seventh Diminished octave ['P8, 'A7'], # Perfect octave Augmented seventh]

Similarly, the same note can have different interval names. For example, the root note can be thought of as a perfect unified tone or a weakened second note.

Select notes from homophonic equivalence

Given the chromatic scale in the key and the interval in the above array, we can indicate the exact note to be used (and filter out from a set of homophonic equivalents). Let's take a look at the basic way to do this.

For example, let's look at how to find the notes corresponding to M3 or the main third scale from the D scale.

1. From the interval array, we can see that the index of finding M3 is 4. That is, 'M3' in intervals [4] = = True.

2. Now, we look at the same index in the D-chromatic scale (modeled by its length). We found that colour ('D') [4] is a list of notes ['E # #', 'Flemish grammatical GB'].

3. The number in M3 (that is, 3) represents the letter we need to use, where 1 represents the root letter. So, for example, for the bond of D, 1 = D ~ 2 = E ~ ~ 3 = F ~ 4 = G ~ ~ 5 = A ~ 6 = B ~ 7 = C ~ 8 = D … Wait. Therefore, we need to look for a syllable in the list of syllables that contain the letter F (['E # #', 'Flemish grammatical GB']). This is the syllable F#.

4. Conclusion: relative to 1/3 of D (M3) is F#.

Programmatically marks the interval of a given key

We can write a relatively simple function that programmatically applies this logic for us and provides us with a dictionary that maps all interval names in a given key to the correct note names:

Def make_intervals_standard (key): # Our labeled set of notes mapping interval names to notes labels = {} # Step 1: Generate a chromatic scale in our desired key chromatic_scale = chromatic (key) # The alphabets starting at provided key's alphabet alphabet_key = rotate (alphabet, find_note_index (alphabet, key [0])) # Iterate through all intervals (list of lists) for index Interval_list in enumerate (intervals): # Step 2: Find the notes to search through based on degree notes_to_search = chromatic_ scale [index% len (chromatic_scale)] for interval_name in interval_list: # Get the interval degree degree = int (interval_name [1])-1 # e.g. M3-- > 3 M7-- > 7 # Get the alphabet to look for alphabet_to_search = alphabet_ key [degree% len (alphabet_key)] try: note = [x for x in notes_to_search if x [0] = alphabet_to_search] [0] except: note = notes_ To_search [0] labels [interval _ name] = note return labels

This is the dictionary where we return the C key:

> import pprint > pprint.pprint (make_intervals_standard ('C'), sort_dicts=False) {'P1Christ:' Dbb','d2: 'Dbb',' m2BZ: 'Db',' A1: 'CZBZ,' M2BZ: 'During,' d3mm: 'Ebb',' m3mm: 'Eb',' A2: 'Fb'', 'M3':' eyed, 'd4mm:' Fb' 'P4:' Ab','A3: 'Bbb',' d7: 'Bbb',' m7: 'Bb',' A6: 'Bb',' A6 The interval formula of'M7': 'Bao,' d8: 'Cb',' P8:'C','A7': 'Bobby'}

Now we can use interval names to specify formulas or syllable groups and map them to any key we want:

Def make_formula (formula, labeled):''Given a comma-separated interval formula, and a set of labeled notes in a key, return the notes of the formula. '' Return [labelled [x] for x in formula.split (',')] major scale formula

For example, the formula for a major scale is:

Formula ='P1, M2, M3, P4, P5, M6, M7, P8'

We can use it to easily generate tonic scales for different keys, as follows:

> > for key in alphabet: > > print (key, make_formula (formula, make_intervals_standard (key) C ['eyed,' fused, 'faded,' marked, 'baked,' C'] D ['dashed,' eyed, 'faded,' marked, 'marked,' baked, 'marked,' baked, 'marked', 'marked,'] E ['eyed,' favored, 'Gaged' F ['Fitch,' Globe, 'Agar,' Bb', 'Clearing,' Dache, 'Effie,' F'] G ['Globe,' Agar, 'Bell,' Bell, 'Che,' Dao,'E,'FA,'G'] A ['Aging,' Baking, 'Clearing,' D' [B] beautify the scale of the scale.

We also quickly wrote a better way to print the function of the scale:

Def dump (scale, separator=''):''Pretty-print the notes of a scale. Replaces b and # characters for unicode flat and sharp symbols. '' Return separator.join (['{:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report