diff --git a/server/server.py b/server/server.py index fa192f1..6747e22 100644 --- a/server/server.py +++ b/server/server.py @@ -14,7 +14,7 @@ # limitations under the License. # -from predict import generate_midi, predictmood +from predict import generate_midi import os from flask import send_file, request import pretty_midi @@ -25,29 +25,55 @@ else: from io import StringIO import time import json +import mido +import tempfile +from usingMusicNN import predictmood +import numpy as np from flask import Flask app = Flask(__name__, static_url_path='', static_folder=os.path.abspath('../static')) +HappyNote = 0 +SadNote = 1 + +def HappyTrack(): + track = mido.MidiTrack() + track.append(mido.Message('note_on', note=HappyNote, velocity=3, time=6)) + return track + +def SadTrack(): + track = mido.MidiTrack() + track.append(mido.Message('note_on', note=SadNote, velocity=3, time=6)) + return track @app.route('/predict', methods=['POST']) def predict(): now = time.time() values = json.loads(request.data) midi_data = pretty_midi.PrettyMIDI(StringIO(''.join(chr(v) for v in values))) - print values duration = float(request.args.get('duration')) ret_midi = generate_midi(midi_data, duration) - return send_file(ret_midi, attachment_filename='return.mid', - mimetype='audio/midi', as_attachment=True) -@app.route('/getmood', methods=['POST']) -def getmood(): - values = json.loads(request.data) - midi_data = pretty_midi.PrettyMIDI(StringIO(''.join(chr(v) for v in values))) - ret_file = predictmood(midi_data) - print ret_file - return send_file(ret_file, attachment_filename='return.json', as_attachment=True) + # Store the received midi file in a temporary file to be able to use it with mido + mfile = tempfile.NamedTemporaryFile() + midi_data.write(mfile) + mfile.seek(0) + + midofile = mido.MidiFile(mfile.name) + + mood = predictmood(midofile) + print mood + + # Add a new track with the first note indicating the mood + midi_to_mod = mido.MidiFile(ret_midi.name) + midi_to_mod.tracks.append(HappyTrack() if mood == 'happy' else SadTrack()) + + ret_file = tempfile.NamedTemporaryFile() + midi_to_mod.save(ret_file.name) + ret_file.seek(0) + + return send_file(ret_file, attachment_filename='return.mid', + mimetype='audio/midi', as_attachment=True) @app.route('/', methods=['GET', 'POST']) def index(): diff --git a/static/src/ai/AI.js b/static/src/ai/AI.js index 326e121..41fe88e 100644 --- a/static/src/ai/AI.js +++ b/static/src/ai/AI.js @@ -63,6 +63,16 @@ class AI extends events.EventEmitter{ this.emit('keyUp', note.midi, note.noteOff + now) } }) + // We added another track to the midi file that contains data we want to send back to the client + // the first note of this new track is used as a bool value for happy/sad + if (response.tracks[2]['notes'][0]['midi'] === 0) { + console.log('happy') + window.isSad = 0; + } + else if (response.tracks[2]['notes'][0]['midi'] === 1) { + console.log('sad') + window.isSad = 1; + } }) this._lastPhrase = -1 this.emit('sent') diff --git a/static/src/interface/Glow.js b/static/src/interface/Glow.js index 87d850c..dc43a4d 100644 --- a/static/src/interface/Glow.js +++ b/static/src/interface/Glow.js @@ -45,12 +45,30 @@ class Glow { this._aiVisible = false this._aiGlow.classList.remove('visible') this._userGlow.classList.add('visible') + // Update the class of glow to show the correct color + if (window.isSad) { + this._userGlow.classList.add('sad') + this._userGlow.classList.remove('happy') + } + else { + this._userGlow.classList.add('happy') + this._userGlow.classList.remove('sad') + } } } else { if (!this._aiVisible){ this._aiVisible = true this._aiGlow.classList.add('visible') this._userGlow.classList.remove('visible') + // Update the class of glow to show the correct color + if (window.isSad) { + this._userGlow.classList.add('sad') + this._userGlow.classList.remove('happy') + } + else { + this._userGlow.classList.add('happy') + this._userGlow.classList.remove('sad') + } } } } diff --git a/static/style/common.scss b/static/style/common.scss index 8ff849e..4093f07 100644 --- a/static/style/common.scss +++ b/static/style/common.scss @@ -1,4 +1,6 @@ $blue : rgb(30, 183, 235); $orange : rgb(249, 187, 45); +$green : rgb(0, 225, 0); +$red: rgb(255, 0, 0); $font-family: 'Quicksand', sans-serif; \ No newline at end of file diff --git a/static/style/glow.css b/static/style/glow.css index 31b4256..d485a20 100644 --- a/static/style/glow.css +++ b/static/style/glow.css @@ -15,9 +15,10 @@ } } - $glowReach : 60%; + /*$glowReach : 60%;*/ + $glowReach : 80%; - #ai { + /*#ai { opacity: 0; background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $orange 0%, black $glowReach); } @@ -25,5 +26,15 @@ #user { opacity: 0; background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $blue 0%, black $glowReach); + }*/ + + .happy { + opacity: 0; + background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $green 0%, black $glowReach); + } + + .sad { + opacity: 0; + background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $red 0%, black $glowReach); } } \ No newline at end of file