-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
145 lines (120 loc) · 6.34 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
from flask import Flask, send_from_directory, render_template, request
import os
app = Flask(__name__)
chromatic_scale = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
major_intervals = [2, 2, 1, 2, 2, 2, 1]
minor_intervals = [2, 1, 2, 2, 1, 2, 2]
major_chord_names = ['Major', 'Minor', 'Minor', 'Major', 'Major', 'Minor', 'Diminished']
minor_chord_names = ['Minor', 'Diminished', 'Major', 'Minor', 'Minor', 'Major', 'Major']
# Default scale type and root note
default_scale_type = 'Major'
default_root_note = 'C'
@app.route('/audio/<path:filename>')
def serve_audio(filename):
root_dir = os.path.dirname(os.path.abspath(__file__))
return send_from_directory(os.path.join(root_dir, 'static', 'audio'), filename)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
scale_type = request.form.get('scale_type')
root_note = request.form.get('root_note')
else:
scale_type = default_scale_type
root_note = default_root_note
# Check if the scale type is valid
if scale_type != 'Major' and scale_type != 'Minor':
scale_type = default_scale_type
# Check if the root note is valid
if root_note not in chromatic_scale:
root_note = default_root_note
# Determine the intervals and chord names based on the scale type
intervals = major_intervals if scale_type == 'Major' else minor_intervals
chord_names = major_chord_names if scale_type == 'Major' else minor_chord_names[:len(intervals)]
# Calculate the scale notes
root_index = chromatic_scale.index(root_note)
scale_notes = [chromatic_scale[root_index]]
for interval in intervals:
root_index = (root_index + interval) % len(chromatic_scale)
note = chromatic_scale[root_index]
scale_notes.append(note)
# Calculate the triads
triads = []
for i in range(len(scale_notes)):
root = scale_notes[i]
third = scale_notes[(i + 2) % 7]
fifth = scale_notes[(i + 4) % 7]
chord_name = chord_names[i % len(chord_names)]
triad_name = f"{root} {third} {fifth} ({root} {chord_name})"
triads.append(triad_name)
# Calculate the mediant triad
mediant_triad = triads[2]
# Calculate the submediant triad
submediant_triad = triads[5]
# Calculate the first chromatic mediant
root, third, fifth, chord_name = mediant_triad.split(maxsplit=3)
if scale_type == 'Major':
raised_third = chromatic_scale[(chromatic_scale.index(third) + 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_mediant_1 = f"{root} {raised_third} {fifth} ({root} {chord_name})"
else:
lowered_third = chromatic_scale[(chromatic_scale.index(third) - 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_mediant_1 = f"{root} {lowered_third} {fifth} ({root} {chord_name})"
# Calculate the second chromatic mediant
root, third, fifth, chord_name = mediant_triad.split(maxsplit=3)
if scale_type == 'Major':
lowered_first = chromatic_scale[(chromatic_scale.index(root) - 1) % len(chromatic_scale)]
lowered_fifth = chromatic_scale[(chromatic_scale.index(fifth) - 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_mediant_2 = f"{lowered_first} {third} {lowered_fifth} ({lowered_first} {chord_name})"
else:
raised_first = chromatic_scale[(chromatic_scale.index(root) + 1) % len(chromatic_scale)]
raised_fifth = chromatic_scale[(chromatic_scale.index(fifth) + 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_mediant_2 = f"{raised_first} {third} {raised_fifth} ({raised_first} {chord_name})"
# Calculate the first chromatic submediant
root, third, fifth, chord_name = submediant_triad.split(maxsplit=3)
if scale_type == 'Major':
raised_third = chromatic_scale[(chromatic_scale.index(third) + 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_submediant_1 = f"{root} {raised_third} {fifth} ({root} {chord_name})"
else:
lowered_third = chromatic_scale[(chromatic_scale.index(third) - 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_submediant_1 = f"{root} {lowered_third} {fifth} ({root} {chord_name})"
# Calculate the second chromatic submediant
root, third, fifth, chord_name = submediant_triad.split(maxsplit=3)
if scale_type == 'Major':
lowered_first = chromatic_scale[(chromatic_scale.index(root) - 1) % len(chromatic_scale)]
lowered_fifth = chromatic_scale[(chromatic_scale.index(fifth) - 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_submediant_2 = f"{lowered_first} {third} {lowered_fifth} ({lowered_first} {chord_name})"
else:
raised_first = chromatic_scale[(chromatic_scale.index(root) + 1) % len(chromatic_scale)]
raised_fifth = chromatic_scale[(chromatic_scale.index(fifth) + 1) % len(chromatic_scale)]
chord_name = chord_names[3]
chromatic_submediant_2 = f"{raised_first} {third} {raised_fifth} ({raised_first} {chord_name})"
return render_template('index.html', scale_type=scale_type, root_note=root_note,
triads=triads, mediant_triad=mediant_triad, submediant_triad=submediant_triad,
chromatic_mediant_1=chromatic_mediant_1, chromatic_mediant_2=chromatic_mediant_2,
chromatic_submediant_1=chromatic_submediant_1, chromatic_submediant_2=chromatic_submediant_2,
scale_types=['Major', 'Minor'], chromatic_scale=chromatic_scale)
if __name__ == '__main__':
# Use a production-ready server like Gunicorn
from gunicorn.app.base import BaseApplication
class FlaskApplication(BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super().__init__()
def load_config(self):
for key, value in self.options.items():
if key in self.cfg.settings and value is not None:
self.cfg.set(key.lower(), value)
def load(self):
return self.application
options = {
'bind': '0.0.0.0', # Set the appropriate host and port
'workers': 4, # Adjust the number of workers based on your needs
}
FlaskApplication(app, options).run()