-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
153 lines (125 loc) · 4.64 KB
/
Copy pathapp.js
File metadata and controls
153 lines (125 loc) · 4.64 KB
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
145
146
147
148
149
150
151
152
153
// ---------------------------------------------------------------
// Web server module for the piano (upload, propogation, selection)
// ---------------------------------------------------------------
var express = require('express');
var fileUpload = require('express-fileupload');
var mustacheExpress = require('mustache-express');
var bodyParser = require('body-parser');
var fs = require('fs');
// Only need to export the app itself, everything should be done when executed
// @param - Piano. Piano module exported from pianoserial.js
// @param - midiProcessor. Midi processing module exported from midiprocessor.js
module.exports = function(piano, midiProcessor) {
// Initialize express
var app = express();
// Set use statements for configuration and static file directories
app.use(bodyParser.json());
app.use(fileUpload());
app.use('/js', express.static('js'));
app.use('/bootstrap', express.static('bootstrap'));
app.use('/css', express.static('css'));
// Register and configure templating engine for front end (mustache)
app.engine('html', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
// Home directory - list out all possible files
app.get('/', function(req, res) {
// Get list of file names and pass to front end
fs.readdir('midifiles/', function(err, files) {
res.render('list.html', {files: files});
});
});
// Upload a song
app.post('/upload', function(req, res) {
var newFile;
// TODO: conduct prelim. error checking (filetype)
newFile = req.files.midiFile;
newFile.mv('midifiles/' + newFile.name, function(err) {
if (err) {
res.status(500).send(err);
} else {
// Successful upload: redirect to play page
res.redirect('/song?fn=' + newFile.name);
}
});
});
// Creating songs from MIDI (for admin usage)
app.get('/create', function(req, res) {
res.render('create.html');
});
app.post('/create', function(req, res) {
// Retreive the notes array from the request
var notes = JSON.parse(req.body.song);
var fileName = req.body.fileName;
var encodedMidi = midiProcessor.createMidi(notes);
fs.writeFileSync('midifiles/' + fileName + '.mid', encodedMidi, "binary");
res.redirect('/song?fn=' + fileName + '.mid');
});
// View a song's raw output from processing (for admin usage)
app.get('/view', function(req, res) {
var filePath = 'midifiles/' + req.query.fn;
// Read the specified file from the file directory
fs.readFile(filePath, 'binary', function(err, midiStr) {
if (!err) {
// convert midi to json
var midiJSON = midiProcessor.convertToJSON(midiStr);
console.log('midi json:');
console.log(JSON.stringify(midiJSON));
var processorResult = midiProcessor.parseMidiJSON(midiJSON);
var song = processorResult.simpleArray;
res.send(JSON.stringify(song));
} else {
// Failed to read the .midi file, throw error
res.status(500).send(err);
}
});
});
// Get a specified song, display UI controls for song after loading it in to piano
app.get('/song', function(req, res) {
var filePath = 'midifiles/' + req.query.fn;
// Read the specified file from the file directory
fs.readFile(filePath, 'binary', function(err, midiStr) {
if (!err) {
// convert midi to json
var midiJSON = midiProcessor.convertToJSON(midiStr);
var processorResult = midiProcessor.parseMidiJSON(midiJSON);
var song = processorResult.simpleArray;
console.log('json processed:');
console.log(JSON.stringify(midiJSON));
console.log('song processed:');
console.log(JSON.stringify(song));
// load the song on to the piano
piano.loadSong(song, function() {
// loading song was successful!
res.render('playsong.html', {fn: req.query.fn});
});
} else {
// Failed to read the .midi file, throw error
res.status(500).send(err);
}
});
});
// Playback controls for song (buttons) via post
app.post('/start', function(req, res) {
// String.fromCharCode(10)
var tempo = req.body.tempo;
if (!tempo) {
tempo = 10;
}
var tempoChar = String.fromCharCode(32 + Number(tempo));
piano.play(tempoChar, function() {
res.send('success');
});
});
app.post('/pause', function(req, res) {
piano.pause(function() {
res.send('success');
});
});
app.post('/stop', function(req, res) {
piano.stop(function() {
res.send('success');
});
});
return app;
};