Skip to content
Snippets Groups Projects
Commit 2474490b authored by jda92's avatar jda92
Browse files

Implement HTML files into Django and with midiMaker.py

parent f7869b6a
No related branches found
No related tags found
No related merge requests found
Showing
with 249 additions and 43 deletions
No preview for this file type
File added
No preview for this file type
......@@ -12,7 +12,7 @@ urlpatterns = [
url(r'^home/generate/$', views.generate, name='generate'),
url(r'^signin/$', views.signIn, name='signIn'),
url(r'^login/$', views.login, name='login'),
url(r'^postsign/$', views.postsign, name='postsing'),
url(r'^postsign/$', views.postsign, name='postsign'),
url(r'admin/', admin.site.urls),
url(r'^home/generateMusic/$', views.generateMusic, name='generateMusic'),
]
File added
File added
File added
import random
from midiutil import MIDIFile
random.seed()
class midi():
def __init__(self, tempo, scale, intrument, length, name):
self.track = 0
self.channel = 0
self.time = 0
self.duration = 1
self.name = name + ".mid"
if intrument == "piano":
self.program = 1
elif intrument == "guitar":
self.program = 28
elif intrument == "bass":
self.program = 33
elif intrument == "violin":
self.program = 41
elif intrument == "synth":
self.program = 89
elif intrument == "banjo":
self.program = 106
if tempo == "slow":
self.tempo = 40
elif tempo == "mid":
self.tempo = 60
elif tempo == "fast":
self.tempo = 80
elif tempo == "faster":
self.tempo = 120
if scale == "major":
self.scale = [48, 50, 52, 53, 55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72, 74, 76, 77, 79, 81, 83]
elif scale == "minor":
self.scale = [48, 50, 51, 53, 55, 56, 58, 60, 62, 63, 65, 67, 68, 70, 72, 74, 75, 77, 79, 80, 82]
self.length = length
self.volume = 100
def createFile(self):
MyMIDI = MIDIFile(1)
MyMIDI.addProgramChange(self.track, self.channel, self.time, self.program)
MyMIDI.addTempo(self.track, self.time, self.tempo)
return MyMIDI
def createMotif(self):
length = random.randint(5, 10)
motifPitch = []
motifTime = []
previous = 60
upPercent = 50
consecutive = 1
time = 0
scale = self.scale
previousJump = 0
previousTimeInterval = 0
for i in range(0, length):
intervalType = random.randint(0, 100)
upDown = random.randint(0, 100)
y = scale.index(previous)
if upDown > upPercent:
upDown = -1
upPercent += 2 * consecutive
consecutive += 1
else:
upDown = 1
upPercent += -2 * consecutive
consecutive += 1
y = scale.index(previous)
try:
if intervalType <= 25:
pitch = previous
elif intervalType <= 27:
upDown = -1 * previousJump
pitch = scale[y + 7*upDown]
elif intervalType <= 75:
pitch = scale[y + 1*upDown]
else:
skipInterval = random.randint(0, 100)
if intervalType <= 25:
pitch = scale[y + 4*upDown]
elif intervalType <= 27:
pitch = scale[y + 3*upDown]
elif intervalType <= 75:
pitch = scale[y + 2*upDown]
else:
pitch = scale[y + 5*upDown]
except IndexError:
upDown *= -1
if intervalType <= 25:
pitch = previous
elif intervalType <= 27:
pitch = scale[y + 7*upDown]
elif intervalType <= 75:
pitch = scale[y + 1*upDown]
else:
skipInterval = random.randint(0, 100)
if intervalType <= 25:
pitch = scale[y + 4*upDown]
elif intervalType <= 27:
pitch = scale[y + 3*upDown]
elif intervalType <= 75:
pitch = scale[y + 2*upDown]
else:
pitch = scale[y + 5*upDown]
timeInterval = random.randint(0, 100)
if timeInterval <= 10:
if previousTimeInterval == 1:
time = time + 1/4
previousTimeInterval = 1/4
else:
time = time + 1/16
previousTimeInterval = 1/16
elif timeInterval <= 41:
if previousTimeInterval == 1:
time = time + 1/4
previousTimeInterval = 1/4
else:
time = time + 1/8
previousTimeInterval = 1/8
elif timeInterval <= 81:
time = time + 1/4
previousTimeInterval = 1/4
elif timeInterval <= 88:
time = time + 1/4 + 1/8
previousTimeInterval = 1/8 + 1/4
elif timeInterval <= 97:
if previousTimeInterval == 1/16:
time = time + 1/4
previousTimeInterval = 1/4
else:
time = time + 1/2
previousTimeInterval = 1/2
elif timeInterval <= 100:
if previousTimeInterval == 1/16:
time = time + 1/2
previousTimeInterval = 1/2
else:
time = time + 1
previousTimeInterval = 1
motifPitch.append(pitch)
motifTime.append(time)
previous = pitch
previousJump = upDown
return motifPitch, motifTime
def addMotif(self, pitch, time, trackTime, MyMIDI):
for i in range(0, len(pitch)):
MyMIDI.addNote(self.track, self.channel, pitch[i], trackTime + time[i], self.duration, self.volume)
return trackTime + time[len(time)-1]
def runProgram(self):
MyMIDI = self.createFile()
trackTime = 0
if self.length == "short":
pitch_A, time_A = self.createMotif()
pitch_B, time_B = self.createMotif()
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
elif self.length == "mid":
pitch_A, time_A = self.createMotif()
pitch_B, time_B = self.createMotif()
pitch_C, time_C = self.createMotif()
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_C, time_C, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
elif self.length == "long":
pitch_A, time_A = self.createMotif()
pitch_B, time_B = self.createMotif()
pitch_C, time_C = self.createMotif()
pitch_D, time_D = self.createMotif()
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_C, time_C, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_C, time_C, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_A, time_A, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_C, time_C, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_D, time_D, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
trackTime = self.addMotif(pitch_B, time_B, trackTime, MyMIDI)
with open(self.name, "wb") as output_file:
MyMIDI.writeFile(output_file)
"""
test = midi("mid", "major", "guitar", "mid", "guitar")
test.runProgram()
test2 = midi("mid", "minor", "piano", "long", "does it work")
test2.runProgram()
"""
No preview for this file type
File added
File added
No preview for this file type
......@@ -95,14 +95,15 @@ body {
<br>
<h3>first choose a tempo!</h3>
<p>Tempo is defined as the pace or speed at which music is played. Tempo is also referred to as BPM (beats per minute). This setting will influence the pacing of the composition you are generating. This is a major component to making music with Fantasmix.</p>
<div class="form-check-inline form-">
<input type="radio" class="form-check-input" name="tempo" id="slow">Slow
<input type="radio" class="form-check-input" name="" id="slow">Slow
<br>
<input type="radio" class="form-check-input" name="tempo" id="mid">Mid
<input type="radio" class="form-check-input" name="" id="mid">Mid
<br>
<input type="radio" class="form-check-input" name="tempo" id="fast">Fast
<input type="radio" class="form-check-input" name="" id="fast">Fast
<br>
<input type="radio" class="form-check-input" name="tempo" id="faster">Faster
<input type="radio" class="form-check-input" name="" id="faster">Faster
</div>
<br><br>
<h3>next, decide on a length</h3>
......@@ -123,27 +124,17 @@ body {
<option value="violin">Violin</option>
<option value="bass">Bass</option>
</select>
<br>
<label for="secondaryInstrument">Secondary Instrument:</label>
<select class="form-control"id="secondaryInstrument">
<option value="random">Random</option>
<option value="piano">Piano</option>
<option value="guitar">Guitar</option>
<option value="synth">Synth</option>
<option value="violin">Violin</option>
<option value="bass">Bass</option>
</select>
<br><br>
<!-- Django Python added code-->
<form action="/home/generateMusic/" method="createMusic">
{% csrf_token %}
<button class="btn btn-outline-success btn-block">Generate!</button>
<input type="tempo" name="tempo" value="mid"/>
<input type="length" name="length" value="short"/>
<input type="intrument" name="intrument" value="Random"/>
<button type="submit" class="btn btn-outline-success btn-block">Generate!</button>
</form>
{% if done %} <p> {{done}} </p> {% endif %}
</div>
<br><br>
<div class="jumbotron text-center" style="margin-bottom:0">
......@@ -175,18 +166,6 @@ body {
slideroutput.innerHTML = this.value;
}
// Submit post on submit
$('#post-form').on('submit', function(event){
event.preventDefault();
console.log("form submitted!") // sanity check
create_music();
});
// AJAX for posting
function create_music() {
console.log("create music is working!") // sanity check
};
// Radio Button Variables
/*
var tempoRDB = document.getElementsByName("tempo");
......
......@@ -3,7 +3,10 @@
from django.shortcuts import render
import pyrebase
from django.http import HttpResponse
from scripts.midiClass import midi
# firebase authentication
config = {
'apiKey': "AIzaSyARx8UiFTfkS2jp7cROOIDhBilvO-EVPm0",
'authDomain': "fantasmix-cf5c2.firebaseapp.com",
......@@ -46,11 +49,12 @@ def login(request):
def postsign(request):
email = request.POST.get('email')
email = request.POST.get("email")
passw = request.POST.get("pass")
try:
user = auth.sign_in_with_email_and_password(email, passw)
except:
# if invalid, throw an alert message in login.html
message = "invalid cerediantials"
return render(request, "login.html", {"msg": message})
print(user)
......@@ -58,6 +62,20 @@ def postsign(request):
def generateMusic(request):
message = "done and a success"
# if request.method == 'post':
# submitbutton = request.POST.get("submit")
instrument = ''
length = ''
tempo = ''
# if request.method == 'createMusic':
instrument = request.POST.get("intrument")
length = request.POST.get("length")
tempo = request.POST.get("tempo")
# runs midiClass.py file and outputs file into django_project folder
# with the manage.py file
test = midi("mid", "major", "guitar", "mid", "Successfor_midiMaker!")
test.runProgram()
message = "program ran! the .midi is in the first django_project folder"
return render(request, "generate.html", {"done": message})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment