Skip to content

Commit

Permalink
Add MENC support
Browse files Browse the repository at this point in the history
  • Loading branch information
spessasus committed Aug 8, 2024
1 parent d4489f9 commit 253732d
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SpessaSynth",
"version": "3.14.4",
"version": "3.14.5",
"type": "module",
"scripts": {
"start": "node src/website/server/server.js",
Expand Down
13 changes: 12 additions & 1 deletion src/spessasynth_lib/midi_parser/rmidi_writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const RMIDINFOChunks = {
engineer: "IENG",
software: "ISFT",
encoding: "IENC",
midiEncoding: "MENC",
bankOffset: "DBNK"
}

Expand All @@ -40,6 +41,7 @@ const DEFAULT_COPYRIGHT = "Created using SpessaSynth";
* @property {string|undefined} comment - the coment of the file
* @property {string|undefined} creationDate - the creation date of the file
* @property {string|undefined} copyright - the copyright of the file
* @property {string|unescape} midiEncoding - the encoding of the inner MIDI file
*/

/**
Expand Down Expand Up @@ -444,10 +446,19 @@ export function writeRMIDI(soundfontBinary, mid, soundfont, bankOffset = 0, enco
const DBNK = new IndexedByteArray(2);
writeLittleEndian(DBNK, bankOffset, 2);
infoContent.push(writeRIFFOddSize(RMIDINFOChunks.bankOffset, DBNK));
// midi encoding
if(metadata.midiEncoding !== undefined)
{
infoContent.push(
writeRIFFOddSize(RMIDINFOChunks.midiEncoding, encoder.encode(metadata.midiEncoding))
);
encoding = FORCED_ENCODING;
}
// encoding
infoContent.push(writeRIFFOddSize(RMIDINFOChunks.encoding, getStringBytes(encoding)));
const infodata = combineArrays(infoContent);

// combine and write out
const infodata = combineArrays(infoContent);
const rmiddata = combineArrays([
getStringBytes("RMID"),
writeRIFFOddSize(
Expand Down
2 changes: 1 addition & 1 deletion src/spessasynth_lib/synthetizer/worklet_processor.min.js

Large diffs are not rendered by default.

35 changes: 28 additions & 7 deletions src/website/js/sequencer_ui/sequencer_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class SequencerUI
this.controls = element;
this.encoding = DEFAULT_ENCODING;
this.decoder = new TextDecoder(this.encoding);
this.infoDecoder = new TextDecoder(this.encoding);
this.hasInfoDecoding = false;
// the currently displayed (highlighted) lyrics text
this.text = "";
this.requiresTextUpdate = false;
Expand Down Expand Up @@ -132,15 +134,20 @@ class SequencerUI

/**
* @param text {ArrayBuffer}
* @param useInfoEncoding {boolean}
* @returns {string}
*/
decodeTextFix(text)
decodeTextFix(text, useInfoEncoding = false)
{
let encodingIndex = 0;
while(true)
{
try
{
if(useInfoEncoding)
{

}
return this.decoder.decode(text);
}
catch (e)
Expand Down Expand Up @@ -207,15 +214,24 @@ class SequencerUI
}

// use encoding suggested by the rmidi if available
this.hasInfoDecoding = this.seq.midiData.RMIDInfo?.[RMIDINFOChunks.encoding] !== undefined;
if(data.isEmbedded)
{
if(data.RMIDInfo[RMIDINFOChunks.encoding] === undefined)
{
return;
/**
* @param type {string}
* @param def {string}
* @param decoder {TextDecoder}
* @param prepend {string}
* @return {string}
*/
const verifyDecode = (type, def, decoder, prepend = "") => {
return this.seq.midiData.RMIDInfo?.[type] === undefined ? def : prepend + decoder.decode(this.seq.midiData.RMIDInfo?.[type]).replace(/\0$/, '')
}
const dec = new TextDecoder();
const encoding = dec.decode(data.RMIDInfo[RMIDINFOChunks.encoding].buffer).replace(/\0$/, '');
this.changeEncoding(encoding);
const midiEncoding = verifyDecode(RMIDINFOChunks.midiEncoding, this.encoding, dec);
const infoEncoding = verifyDecode(RMIDINFOChunks.encoding, "utf-8", dec);
this.infoDecoder = new TextDecoder(infoEncoding);
this.changeEncoding(midiEncoding);
}
}, "sequi-song-change");

Expand All @@ -235,10 +251,15 @@ class SequencerUI
{
this.encoding = encoding;
this.decoder = new TextDecoder(encoding);
if(!this.hasInfoDecoding)
{
this.infoDecoder = new TextDecoder(encoding);
}
this.updateOtherTextEvents();
this.text = this.decodeTextFix(new Uint8Array(this.rawLyrics).buffer);
this.lyricsElement.selector.value = encoding;
this.updateTitleAndMediaStatus(false);
this.setLyricsText(this.text);
this.updateTitleAndMediaStatus();
}

createControls()
Expand Down
10 changes: 7 additions & 3 deletions src/website/js/sequencer_ui/title_and_media_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ export function createNavigatorHandler()

/**
* @this {SequencerUI}
* @param cleanOtherTextEvents {boolean}
*/
export function updateTitleAndMediaStatus()
export function updateTitleAndMediaStatus(cleanOtherTextEvents = true)
{
if(this.seq?.hasDummyData === true)
{
this.currentSongTitle = this.locale.getLocaleString("locale.synthInit.genericLoading");
}
else
{
const text = this.decodeTextFix(this.seq.midiData.rawMidiName.buffer);
const text = this.infoDecoder.decode(this.seq.midiData.rawMidiName.buffer);
this.currentSongTitle = formatTitle(text);
}
if(this.seq.midiData)
Expand All @@ -71,7 +72,10 @@ export function updateTitleAndMediaStatus()
}
this.currentLyricsString = this.decodeTextFix(this.currentLyrics.buffer) || this.locale.getLocaleString("locale.sequencerController.lyrics.noLyrics");
this.setLyricsText("");
this.rawOtherTextEvents = [];
if(cleanOtherTextEvents)
{
this.rawOtherTextEvents = [];
}
}
document.getElementById("title").innerText = this.currentSongTitle;
document.title = this.currentSongTitle + " - SpessaSynth";
Expand Down
5 changes: 3 additions & 2 deletions src/website/manager/export_rmidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ export async function _exportRMIDI()
const rmidBinary = writeRMIDI(newFont, mid, font, bankOffset, this.seqUI.encoding, {
name: songTitle,
comment: comment,
engineer: font.soundFontInfo["IENG"],
engineer: font.soundFontInfo["IENG"], // use soundfont egineer
picture: fileBuffer,
album: album.length > 0 ? album : undefined,
artist: artist.length > 0 ? artist : undefined,
genre: genre.length > 0 ? genre : undefined
genre: genre.length > 0 ? genre : undefined,
midiEncoding: this.seqUI.encoding // use the selected encoding
});
const blob = new Blob([rmidBinary.buffer], {type: "audio/rmid"})
this.saveBlob(blob, `${songTitle || "unnamed_song"}.rmi`);
Expand Down
36 changes: 18 additions & 18 deletions src/website/minified/demo_main.min.js

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions src/website/minified/local_main.min.js

Large diffs are not rendered by default.

0 comments on commit 253732d

Please sign in to comment.