From bc9350dbb5bf140e246faa74553a551f2447dbfc Mon Sep 17 00:00:00 2001 From: manuroe Date: Sun, 19 Oct 2014 08:44:03 +0200 Subject: [PATCH] Go on with hacking midi: load all messages for all rooms. So that, we have the full midi partition with the notes for computing the tempo. --- .../matrix/event-handler-service.js | 5 +- .../components/matrix/event-stream-service.js | 2 +- .../components/matrix/midi-event-handler.js | 50 ++++++++++++------- webclient/room/room-controller.js | 1 + 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/webclient/components/matrix/event-handler-service.js b/webclient/components/matrix/event-handler-service.js index 8ecfca21cc..01653eb10d 100644 --- a/webclient/components/matrix/event-handler-service.js +++ b/webclient/components/matrix/event-handler-service.js @@ -483,10 +483,7 @@ function(matrixService, $rootScope, $q, $timeout, mPresence) { break; case 'org.matrix.midi': - //if (isLiveEvent) - { - MidiEventHandler.handleEvent(event); - } + MidiEventHandler.handleEvent(event, isLiveEvent); break; diff --git a/webclient/components/matrix/event-stream-service.js b/webclient/components/matrix/event-stream-service.js index 05469a3ded..aa58750d0b 100644 --- a/webclient/components/matrix/event-stream-service.js +++ b/webclient/components/matrix/event-stream-service.js @@ -107,7 +107,7 @@ angular.module('eventStreamService', []) // Initial sync: get all information and the last 30 messages of all rooms of the user // 30 messages should be enough to display a full page of messages in a room // without requiring to make an additional request - matrixService.initialSync(30, false).then( + matrixService.initialSync(10000, false).then( function(response) { var rooms = response.data.rooms; for (var i = 0; i < rooms.length; ++i) { diff --git a/webclient/components/matrix/midi-event-handler.js b/webclient/components/matrix/midi-event-handler.js index 23bf88284a..2c6b654765 100644 --- a/webclient/components/matrix/midi-event-handler.js +++ b/webclient/components/matrix/midi-event-handler.js @@ -28,22 +28,20 @@ var MidiEventHandler = { currentMeasureTime: 0, perLine: -1, - init: function () { - MIDI.loadPlugin({ - soundfontUrl: "./soundfont/", - instrument: "acoustic_grand_piano", - callback: function () { - /* - var delay = 0; // play one note every quarter second - var note = 50; // the MIDI note - var velocity = 127; // how hard the note hits - // play the note - MIDI.setVolume(0, 127); - MIDI.noteOn(0, note, velocity, delay); - MIDI.noteOff(0, note, delay + 0.75); - */ + pastMidiEventsInWrongOrder: [], + + init: function (eventHandlerService) { + + // During initialSync, handleEvent is called for each event from latest events to the past. + // Need to reorder them. + var self = this; + eventHandlerService.waitForInitialSyncCompletion().then( + function() { + for (var i = self.pastMidiEventsInWrongOrder.length - 1; i >= 0; i--) { + self.handleEvent(self.pastMidiEventsInWrongOrder[i], true); + } } - }); + ); }, reset: function() { @@ -81,7 +79,12 @@ notes C-D-E/4 #0# =:: C-D-E-F/4 =|="); this.render(); }, - handleEvent: function(event) { + handleEvent: function(event, isLiveEvent) { + + if(!isLiveEvent) { + this.pastMidiEventsInWrongOrder.push(event); + return; + } if (0 === this.beat) { @@ -120,8 +123,10 @@ notes C-D-E/4 #0# =:: C-D-E-F/4 =|="); var musicFraction; + // Flag to ignore artefact(???) + var trashIt = false; - var duration = Math.floor(Math.log2(1 / fraction)) - 1; + var duration = Math.floor(Math.log2(1 / fraction)) - 1; switch (duration) { case 4: musicFraction = "w"; @@ -141,8 +146,19 @@ notes C-D-E/4 #0# =:: C-D-E-F/4 =|="); case -2: musicFraction = "32"; break; + + default : + + console.log("## Ignored note"); + // Too short, ignore it + trashIt = true; + break; } + + // Matthew is about to fix it + if (trashIt) return; + this.currentMeasureTime += duration; diff --git a/webclient/room/room-controller.js b/webclient/room/room-controller.js index d796fd4a3f..41ae8a1c0f 100644 --- a/webclient/room/room-controller.js +++ b/webclient/room/room-controller.js @@ -650,6 +650,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput']) $scope.onInit = function() { console.log("onInit"); + MidiEventHandler.init(eventHandlerService); MidiEventHandler.reset();