Skip to content

Commit

Permalink
Merge pull request #33 from jrg94/play-midi
Browse files Browse the repository at this point in the history
Play midi
  • Loading branch information
wustep authored Mar 21, 2019
2 parents 1b6a6ce + ff79fe9 commit 67e46aa
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
File renamed without changes.
Binary file added midiviz/data/Mario-Shift.mid
Binary file not shown.
File renamed without changes.
2 changes: 2 additions & 0 deletions midiviz/prototype/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<script src="lib/tippy.min.js"></script>
<script src="lib/d3.v5.min.js"></script>
<script src="lib/midi-parser.js"></script>
<script src="lib/midiplayer.min.js"></script>
<script src="lib/soundfont-player.min.js"></script>
<script src="dashboard.js"></script>
</body>
</html>
47 changes: 47 additions & 0 deletions midiviz/prototype/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ function addEntryToFileList(fileList, midiFile, name, toggled) {
<div class="icons">
<div class="icons-left">
<span class="tipped midi-toggle midi-btn" data-toggled="${(toggled) ? "true" : "false"}" data-file="${name}" data-tippy-content="Toggle file"><i class="icon-toggle-${(toggled) ? "on" : "off"}"></i></span>
<span class="tipped midi-play midi-btn" data-file="${name}" data-tippy-content="Play file"><i class="icon-play"></i></span>
</div>
<div class="icons-right">
<span class="tipped midi-rename midi-btn" data-file="${name}" data-tippy-content="Rename file"><i class="icon-pencil"></i></span>
Expand Down Expand Up @@ -433,6 +434,11 @@ function setupMidiButtons() {
var midiFile = d3.select(this).attr("data-file");
deleteMIDIFile(midiFile);
});

d3.selectAll(".midi-play").on("click", function() {
var midiFile = d3.select(this).attr("data-file");
playPauseMIDIFile(midiFile);
});
}

function clearSVGs() {
Expand Down Expand Up @@ -460,6 +466,7 @@ function midiLoadCallback(midiFile) {
} else {
var midiColor = colors.splice(0, 1)[0];
midiFiles[latestFile.name] = midiFile;
midiFiles[latestFile.name].file = latestFile;
midiFiles[latestFile.name].color = midiColor;
usedColors.push(midiColor);
buildFileList();
Expand Down Expand Up @@ -617,6 +624,46 @@ function deleteMIDIFile(midiFile) {
}
}

var Player;

/**
* Given MIDI file, play it.
* TODO: Allow pause, add cursor to graph, and
*/
function playPauseMIDIFile(midiFile) {
var playSpan = d3.select(`span.midi-play[data-file="${midiFile}"]`);
var playSpanIcon = playSpan.select("i");
var pause = playSpanIcon.classed("icon-pause");
if (pause) {
Player.pause();
playSpanIcon.classed("icon-play", true);
playSpanIcon.classed("icon-pause", false)
} else {
var AudioContext = window.AudioContext || window.webkitAudioContext || false;
var ac = new AudioContext || new webkitAudioContext;
// TODO: Replace this link with the local version
Soundfont.instrument(ac, 'https://raw.githubusercontent.com/gleitz/midi-js-soundfonts/gh-pages/MusyngKite/acoustic_grand_piano-mp3.js').then(function(instrument) {
var reader = new FileReader();
var file = (midiFile in midiFiles) ? midiFiles[midiFile].file : hiddenMidiFiles[midiFile].file;
if (file) {
reader.readAsArrayBuffer(file);
reader.addEventListener("load", function () {
Player = new MidiPlayer.Player(function(event) {
if (event.name == 'Note on') {
instrument.play(event.noteName, ac.currentTime, {gain:event.velocity/100});
}
});
Player.loadArrayBuffer(reader.result);
Player.play();
}, false);
}
});
playSpanIcon.classed("icon-play", false);
playSpanIcon.classed("icon-pause", true)
}
}


const PANE_ALL = 0;
const PANE_NOTES = ".master-graph-pane";
const PANE_FREQUENCY = ".note-frequency-graph-pane";
Expand Down
93 changes: 93 additions & 0 deletions midiviz/prototype/lib/midi/acoustic_grand_piano-mp3.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions midiviz/prototype/lib/midiplayer.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions midiviz/prototype/lib/soundfont-player.min.js

Large diffs are not rendered by default.

0 comments on commit 67e46aa

Please sign in to comment.