Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added midi-data rpc #264

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,101 changes: 1,101 additions & 0 deletions src/procedures/midi-data/MidiLibrary/midiLibrary.json

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
772 changes: 772 additions & 0 deletions src/procedures/midi-data/midi-api.js

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions src/procedures/midi-data/midi-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* This service allows users to access midi files of different songs.
* @alpha
* @service
* @category Music
*/

"use strict";

const fsp = require("fs/promises");
const { registerTypes } = require("./types");
const { MidiReader } = require("./midi-api");
const path = require("path");
const MidiData = {};
const midiLibrary = require("./MidiLibrary/midiLibrary.json");

registerTypes();

/**
* Get a song by name
* @param {String=} nameOfSong
* @returns {[Object{name: String, notes: [Note]}]}
*/
MidiData.getSongData = async function (nameOfSong = "") {
const metadata = midiLibrary.netsbloxMidiLibrary.find((obj) =>
obj.Name === nameOfSong
);
if (metadata) {
const midiPath = path.join(__dirname, "./MidiLibrary/", metadata.Path);
const data = await fsp.readFile(midiPath);
const raw = new Uint8Array(data);
const midi = new MidiReader(raw.buffer);
return midi.getNotes();
}
throw new Error("Song not found");
};

/**
* Get songs based on query.
* @param {Composers=} composer
* @param {Styles=} style
* @param {String=} search
* @returns {Array}
*/
MidiData.findSong = async function (composer = "", style = "", search = "") {
var names = [];
let queriedJSON = "";

//Ensure at least one field is selected
if (composer !== "" || search !== "" || style !== "") {
queriedJSON = midiLibrary.netsbloxMidiLibrary.filter(function (obj) { // Check if field value is empty before finding obj with value.
return (composer === "" || obj.Composer === composer) &&
(style === "" || obj.Style === style) &&
(search === "" || obj.Name.indexOf(search) !== -1);
});
} else {
queriedJSON = midiLibrary.netsbloxMidiLibrary.filter(function (obj) {
return true;
});
}

//Convert JSON to array of String names
for (let i = 0; i < queriedJSON.length; i++) {
names.push(queriedJSON[i].Name);
}

return names;
};

module.exports = MidiData;
48 changes: 48 additions & 0 deletions src/procedures/midi-data/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const types = require("../../input-types");

const COMPOSERS = [
"J.S. Bach",
"Beethoven",
"Mozart",
"Haydn",
"Debussy",
"Traditional",
"O. Comeau",
"S. C. Foster",
"C. J. Brown",
"F. Abt",
"E. Grieg",
"E. Satie",
"Spagnoletti",
"Kruetzer",
"Yaniewicz",
];

const STYLES = [
"Classical",
"Baroque",
"Folk",
"Gospel",
"Hymn",
"Modern",
"Romantic",
"Popular / Dance",
];

function registerTypes() {
types.defineType({
name: "Composers",
description: "List of available composers",
baseType: "Enum",
baseParams: COMPOSERS,
});

types.defineType({
name: "Styles",
description: "List of available styles",
baseType: "Enum",
baseParams: STYLES,
});
}

module.exports = { registerTypes };