music-melodicdevice 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ # The root of the entire project
2
+ from music_melodicdevice.music_melodicdevice import Device
3
+
4
+ __version__ = "0.1.0"
@@ -0,0 +1,161 @@
1
+ import sys
2
+ sys.path.append('./src')
3
+ import music_melodicdevice.musical_scales as musical_scales
4
+ from typing import List, Tuple, Optional, Union
5
+
6
+ # TICKS = 96
7
+ # OCTAVES = 10
8
+
9
+ class Device:
10
+ def __init__(self, scale_note='C', scale_name='chromatic', notes=[], verbose=0):
11
+ self.scale_note = scale_note
12
+ self.scale_name = scale_name
13
+ self.notes = notes
14
+ self.verbose = verbose
15
+ self.scale = self.build_scale()
16
+
17
+ def _find_pitch(self, p):
18
+ try:
19
+ i = self.scale.index(p)
20
+ except ValueError:
21
+ i = -1
22
+ return i
23
+
24
+ def build_scale(self, name=None):
25
+ if name:
26
+ self.scale_name = name
27
+ scale = []
28
+ for i in range(-1,10):
29
+ s = musical_scales.scale(self.scale_note, self.scale_name, starting_octave=i)
30
+ scale.append(s[:-1])
31
+ scale = [ f"{x}" for s in scale for x in s ]
32
+ if self.verbose:
33
+ print("Scale:", scale, len(scale))
34
+ return scale
35
+
36
+ def transpose(self, offset, notes=[]):
37
+ if not notes:
38
+ notes = self.notes
39
+ if self.verbose:
40
+ print("Notes:", notes)
41
+ transposed = []
42
+ for n in notes:
43
+ i = self._find_pitch(n)
44
+ if i == -1:
45
+ transposed.append(None)
46
+ else:
47
+ val = self.scale[i + offset]
48
+ transposed.append(val)
49
+ if self.verbose:
50
+ print('Transposed:', transposed)
51
+ return transposed
52
+
53
+ def intervals(self, notes=[]):
54
+ if not notes:
55
+ notes = self.notes
56
+ pitches = []
57
+ for note in notes:
58
+ i = self._find_pitch(note)
59
+ pitches.append(i)
60
+ if self.verbose:
61
+ print(f"Pitch indexes: {pitches}")
62
+ intervals = []
63
+ last = None
64
+ for pitch in pitches:
65
+ if last is not None:
66
+ intervals.append(pitch - last)
67
+ last = pitch
68
+ if self.verbose:
69
+ print(f"Intervals: {intervals}")
70
+ return intervals
71
+
72
+ def invert(self, axis_note, notes=[]):
73
+ if not notes:
74
+ notes = self.notes
75
+ if self.verbose:
76
+ print("Axis, Notes:", axis_note, notes)
77
+ axis = self._find_pitch(axis_note)
78
+ nums = [ self._find_pitch(n) for n in notes ]
79
+ inverted = []
80
+ for n in nums:
81
+ if n == -1:
82
+ inv = None
83
+ else:
84
+ inv = axis - (n - axis)
85
+ inverted.append(inv)
86
+ named = []
87
+ for x in inverted:
88
+ if not x:
89
+ name = None
90
+ else:
91
+ name = self.scale[x]
92
+ named.append(name)
93
+ if self.verbose:
94
+ print("Inverted:", named)
95
+ return named
96
+
97
+ def grace_note(self, duration, pitch, offset=0):
98
+ i = self._find_pitch(pitch)
99
+ grace_note = self.scale[i + offset]
100
+ x = duration
101
+ y = 1/16 # 64th note
102
+ z = x - y
103
+ if self.verbose:
104
+ print(f"Durations: {x} + {y} = {z}")
105
+ return [[y, grace_note], [z, pitch]]
106
+
107
+ def turn(self, duration, pitch, offset=1):
108
+ factor = 4
109
+ i = self._find_pitch(pitch)
110
+ above = self.scale[i + offset]
111
+ below = self.scale[i - offset]
112
+ x = duration
113
+ z = x / factor
114
+ if self.verbose:
115
+ print(f"Durations: {x}, {z}")
116
+ return [[z, above], [z, pitch], [z, below], [z, pitch]]
117
+
118
+ def trill(self, duration, pitch, number=2, offset=1):
119
+ i = self._find_pitch(pitch)
120
+ alt = self.scale[i + offset]
121
+ x = duration
122
+ z = x / number / 2
123
+ if self.verbose:
124
+ print(f"Durations: {x}, {z}")
125
+ trill = []
126
+ for _ in range(number):
127
+ trill.append([z, pitch])
128
+ trill.append([z, alt])
129
+ return trill
130
+
131
+ def mordent(self, duration, pitch, offset=1):
132
+ factor = 4
133
+ i = self._find_pitch(pitch)
134
+ alt = self.scale[i + offset]
135
+ x = duration
136
+ y = x / factor
137
+ z = x - (2 * y)
138
+ if self.verbose:
139
+ print(f"Durations: {x}, {y}, {z}")
140
+ return [[y, pitch], [y, alt], [z, pitch]]
141
+
142
+ def slide(self, duration, from_pitch, to_pitch):
143
+ # Always use the chromatic scale for slide
144
+ scale_name = self.scale_name
145
+ self.scale = self.build_scale('chromatic')
146
+ i = self._find_pitch(from_pitch)
147
+ j = self._find_pitch(to_pitch)
148
+ start, end = (i, j) if i <= j else (j, i)
149
+ x = duration
150
+ y = end - start + 1
151
+ z = x / y
152
+ if self.verbose:
153
+ print(f"Durations: {x}, {y}, {z}")
154
+ notes = []
155
+ for idx in range(start, end + 1):
156
+ n = self.scale[idx]
157
+ notes.append([z, n])
158
+ if j < i:
159
+ notes = list(reversed(notes))
160
+ self.scale = self.build_scale(scale_name)
161
+ return notes
@@ -0,0 +1,217 @@
1
+ """Retrieve a scale based on a given mode and starting note."""
2
+
3
+ import math
4
+
5
+
6
+ class MusicException(Exception):
7
+ """Base exception for the musical_scales module."""
8
+
9
+ pass
10
+
11
+
12
+ class Note:
13
+ """A single note in a given octave, e.g. C#3.
14
+
15
+ Measured as a number of semitones above Middle C:
16
+ * Note(0) # Middle C, i.e. C3
17
+ * Note(2) # D3
18
+ """
19
+
20
+ semitones_above_middle_c: int
21
+ name: str
22
+ octave: int
23
+
24
+ def __init__(self, name: str = None, semitones_above_middle_c: int = None, starting_octave: int = 3):
25
+ """Create a note with a given name or degree.
26
+
27
+ Examples:
28
+ * Note("C#")
29
+ * Note(semitones_above_middle_c = 1)
30
+ """
31
+ self.starting_octave = starting_octave
32
+ if name is not None:
33
+ if name not in interval_from_names:
34
+ raise MusicException(f"No note found with name {name}.")
35
+ self._set_degree(interval_from_names[name])
36
+ elif semitones_above_middle_c is not None:
37
+ self._set_degree(semitones_above_middle_c)
38
+ else:
39
+ self._set_degree(0)
40
+
41
+ def _set_degree(self, semitones_above_middle_c: int):
42
+ """Set the note name and octave.
43
+
44
+ Should only be used during initialisation.
45
+ """
46
+ self.semitones_above_middle_c = semitones_above_middle_c
47
+ self.name = names_from_interval[semitones_above_middle_c % 12]
48
+ self.octave = math.floor(semitones_above_middle_c / 12) + self.starting_octave
49
+
50
+ def __str__(self):
51
+ """MIDI-style string representation e.g. C#3."""
52
+ return self.midi
53
+
54
+ def __repr__(self):
55
+ """MIDI-style string representation e.g. C#3."""
56
+ return self.midi
57
+
58
+ @property
59
+ def midi(self):
60
+ """Note name and octave, e.g. C3."""
61
+ return f"{self.name}{self.octave}"
62
+
63
+ def __add__(self, shift: int):
64
+ """Shifting this note's degree upwards."""
65
+ return Note(semitones_above_middle_c=self.semitones_above_middle_c + shift, starting_octave=self.starting_octave)
66
+
67
+ def __sub__(self, shift: int):
68
+ """Shifting this note's degree downwards."""
69
+ return self + (-shift)
70
+
71
+ def __eq__(self, other):
72
+ """Check equality via .midi."""
73
+ if isinstance(other, Note):
74
+ return self.midi == other.midi
75
+ else:
76
+ return self.midi == other or self.name == other
77
+
78
+
79
+ def scale(starting_note, mode="ionian", octaves=1, starting_octave=3):
80
+ """Return a sequence of Notes starting on the given note in the given mode.
81
+
82
+ Example:
83
+ * scale("C") # C major (ionian)
84
+ * scale(Note(4), "harmonic minor") # E harmonic minor
85
+ """
86
+ if mode not in scale_intervals:
87
+ raise MusicException(f"The mode {mode} is not available.")
88
+ if not isinstance(starting_note, Note):
89
+ starting_note = Note(starting_note, starting_octave=starting_octave)
90
+ notes = [starting_note]
91
+ for octave in range(0, octaves):
92
+ for interval in scale_intervals[mode]:
93
+ notes.append(notes[-1] + interval)
94
+ return notes
95
+
96
+
97
+ # Found at https://en.wikipedia.org/wiki/List_of_musical_scales_and_modes
98
+ # Only scales that include a representation given
99
+ # by semi-tone intervals are included.
100
+ # One alteration is that this repository will use the term "Romani"
101
+ scale_intervals = {
102
+ "acoustic": [2, 2, 2, 1, 2, 1, 2],
103
+ "aeolian": [2, 1, 2, 2, 1, 2, 2],
104
+ "algerian": [2, 1, 3, 1, 1, 3, 1, 2, 1, 2],
105
+ "super locrian": [1, 2, 1, 2, 2, 2, 2],
106
+ "augmented": [3, 1, 3, 1, 3, 1],
107
+ "bebop dominant": [2, 2, 1, 2, 2, 1, 1, 1],
108
+ "blues": [3, 2, 1, 1, 3, 2],
109
+ "chromatic": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
110
+ "dorian": [2, 1, 2, 2, 2, 1, 2],
111
+ "double harmonic": [1, 3, 1, 2, 1, 3, 1],
112
+ "enigmatic": [1, 3, 2, 2, 2, 1, 1],
113
+ "flamenco": [1, 3, 1, 2, 1, 3, 1],
114
+ "romani": [2, 1, 3, 1, 1, 2, 2],
115
+ "half-diminished": [2, 1, 2, 1, 2, 2, 2],
116
+ "harmonic major": [2, 2, 1, 2, 1, 3, 1],
117
+ "harmonic minor": [2, 1, 2, 2, 1, 3, 1],
118
+ "hijaroshi": [4, 2, 1, 4, 1],
119
+ "hungarian minor": [2, 1, 3, 1, 1, 3, 1],
120
+ "hungarian major": [3, 1, 2, 1, 2, 1, 2],
121
+ "in": [1, 4, 2, 1, 4],
122
+ "insen": [1, 4, 2, 3, 2],
123
+ "ionian": [2, 2, 1, 2, 2, 2, 1],
124
+ "iwato": [1, 4, 1, 4, 2],
125
+ "locrian": [1, 2, 2, 1, 2, 2, 2],
126
+ "lydian augmented": [2, 2, 2, 2, 1, 2, 1],
127
+ "lydian": [2, 2, 2, 1, 2, 2, 1],
128
+ "locrian major": [2, 2, 1, 1, 2, 2, 2],
129
+ "pentatonic major": [2, 2, 3, 2, 3],
130
+ "melodic minor ascending": [2, 1, 2, 2, 2, 2, 1],
131
+ "melodic minor descending": [2, 1, 2, 2, 2, 2, 1],
132
+ "pentatonic minor": [3, 2, 2, 3, 2],
133
+ "mixolydian": [2, 2, 1, 2, 2, 1, 2],
134
+ "neapolitan major": [1, 2, 2, 2, 2, 2, 1],
135
+ "neapolitan minor": [1, 2, 2, 2, 1, 3, 1],
136
+ "octatonic c-d": [2, 1, 2, 1, 2, 1, 2, 1],
137
+ "octatonic c-c#": [1, 2, 1, 2, 1, 2, 1],
138
+ "persian": [1, 3, 1, 1, 2, 3, 1],
139
+ "phrygian dominant": [1, 3, 1, 2, 1, 2, 2],
140
+ "phrygian": [1, 2, 2, 2, 1, 2, 2],
141
+ "prometheus": [2, 2, 2, 3, 1, 2],
142
+ "harmonics": [3, 1, 1, 2, 2, 3],
143
+ "tritone": [1, 3, 2, 1, 3, 2],
144
+ "two-semitone tritone": [1, 1, 4, 1, 1, 4],
145
+ "ukranian dorian": [2, 1, 3, 1, 2, 1, 2],
146
+ "whole-tone scale": [2, 2, 2, 2, 2, 2],
147
+ "yo": [3, 2, 2, 3, 2]
148
+ }
149
+
150
+ scale_intervals["major"] = scale_intervals["ionian"]
151
+
152
+ names_from_interval = {
153
+ 0: "C",
154
+ 1: "C#",
155
+ 2: "D",
156
+ 3: "D#",
157
+ 4: "E",
158
+ 5: "F",
159
+ 6: "F#",
160
+ 7: "G",
161
+ 8: "G#",
162
+ 9: "A",
163
+ 10: "A#",
164
+ 11: "B"
165
+ }
166
+ """From an interval give the note name, favouring sharps over flats."""
167
+
168
+ interval_from_names = {
169
+ "C": 0,
170
+ "C#": 1,
171
+ "Db": 1,
172
+ "D": 2,
173
+ "D#": 3,
174
+ "Eb": 3,
175
+ "E": 4,
176
+ "Fb": 4,
177
+ "E#": 5,
178
+ "F": 5,
179
+ "F#": 6,
180
+ "Gb": 6,
181
+ "G": 7,
182
+ "G#": 8,
183
+ "Ab": 8,
184
+ "A": 9,
185
+ "A#": 10,
186
+ "Bb": 10,
187
+ "B": 11,
188
+ "Cb": 11,
189
+ "B#": 0
190
+ }
191
+ """Dictionary from note names to number of semitones above C."""
192
+
193
+ # MIT License
194
+
195
+ """
196
+ The MIT License (MIT)
197
+
198
+ Copyright (c) 2021 Hector Miller-Bakewell
199
+
200
+ Permission is hereby granted, free of charge, to any person obtaining a copy
201
+ of this software and associated documentation files (the "Software"), to deal
202
+ in the Software without restriction, including without limitation the rights
203
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
204
+ copies of the Software, and to permit persons to whom the Software is
205
+ furnished to do so, subject to the following conditions:
206
+
207
+ The above copyright notice and this permission notice shall be included in all
208
+ copies or substantial portions of the Software.
209
+
210
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
211
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
212
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
213
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
214
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
215
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
216
+ SOFTWARE.
217
+ """