Skip to content

Commit

Permalink
Fix WAVE metadata
Browse files Browse the repository at this point in the history
Add support for sfpk in RMIDIs
  • Loading branch information
spessasus committed Aug 8, 2024
1 parent c9a066c commit 2fc472a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 77 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.15.0",
"version": "3.15.1",
"type": "module",
"scripts": {
"start": "node src/website/server/server.js",
Expand Down
2 changes: 1 addition & 1 deletion src/spessasynth_lib/midi_parser/midi_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class MIDI{
if(currentChunk.header === "RIFF")
{
const type = readBytesAsString(currentChunk.chunkData, 4);
if(type === "sfbk")
if(type === "sfbk" || type === "sfpk")
{
SpessaSynthInfo("%cFound embedded soundfont!", consoleColors.recognized);
this.embeddedSoundFont = binaryData.slice(startIndex, startIndex + currentChunk.size).buffer;
Expand Down
2 changes: 1 addition & 1 deletion src/spessasynth_lib/synthetizer/worklet_processor.min.js

Large diffs are not rendered by default.

80 changes: 42 additions & 38 deletions src/spessasynth_lib/utils/buffer_to_wav.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @property {string|undefined} genre - the song's genre
*/

import { combineArrays } from './indexed_array.js'
import { combineArrays, IndexedByteArray } from './indexed_array.js'
import { getStringBytes } from './byte_functions/string.js'
import { writeRIFFOddSize } from '../soundfont/read/riff_chunk.js'

Expand All @@ -20,17 +20,54 @@ import { writeRIFFOddSize } from '../soundfont/read/riff_chunk.js'
*/
export function audioBufferToWav(audioBuffer, normalizeAudio = true, channelOffset = 0, metadata = {})
{
// this code currently doesn't add any metadata
const channel1Data = audioBuffer.getChannelData(channelOffset);
const channel2Data = audioBuffer.getChannelData(channelOffset + 1);
const length = channel1Data.length;

const bytesPerSample = 2; // 16-bit PCM

// prepare INFO chunk
let infoChunk = new IndexedByteArray(0);
const infoOn = Object.keys(metadata).length > 0;
// INFO chunk
if(infoOn)
{
const encoder = new TextEncoder();
const infoChunks = [
getStringBytes("INFO"),
writeRIFFOddSize("ICMT", encoder.encode("Created with SpessaSynth"))
];
if(metadata.artist)
{
infoChunks.push(
writeRIFFOddSize("IART", encoder.encode(metadata.artist))
);
}
if(metadata.album)
{
infoChunks.push(
writeRIFFOddSize("IPRD", encoder.encode(metadata.album))
);
}
if(metadata.genre)
{
infoChunks.push(
writeRIFFOddSize("IGNR", encoder.encode(metadata.genre))
);
}
if(metadata.title)
{
infoChunks.push(
writeRIFFOddSize("INAM", encoder.encode(metadata.title))
);
}
infoChunk = writeRIFFOddSize("LIST", combineArrays(infoChunks));
}

// Prepare the header
const headerSize = 44;
const dataSize = length * 2 * bytesPerSample; // 2 channels, 16-bit per channel
const fileSize = headerSize + dataSize - 8; // total file size minus the first 8 bytes
const fileSize = headerSize + dataSize + infoChunk.length - 8; // total file size minus the first 8 bytes
const header = new Uint8Array(headerSize);

// 'RIFF'
Expand Down Expand Up @@ -65,46 +102,13 @@ export function audioBufferToWav(audioBuffer, normalizeAudio = true, channelOffs

let wavData;
let offset = headerSize;
let infoChunk = undefined;
// INFO chunk
if(Object.keys(metadata).length > 0)
if(infoOn)
{
const encoder = new TextEncoder();
const infoChunks = [
getStringBytes("INFO"),
writeRIFFOddSize("ICMT", encoder.encode("Created with SpessaSynth"))
];
if(metadata.artist)
{
infoChunks.push(
writeRIFFOddSize("IART", encoder.encode(metadata.artist))
);
}
if(metadata.album)
{
infoChunks.push(
writeRIFFOddSize("IPRD", encoder.encode(metadata.album))
);
}
if(metadata.genre)
{
infoChunks.push(
writeRIFFOddSize("IGNR", encoder.encode(metadata.genre))
);
}
if(metadata.title)
{
infoChunks.push(
writeRIFFOddSize("INAM", encoder.encode(metadata.title))
);
}
infoChunk = writeRIFFOddSize("LIST", combineArrays(infoChunks));
wavData = new Uint8Array(headerSize + dataSize + infoChunk.length);
}
else
{
wavData = new Uint8Array(headerSize + dataSize);

}
wavData.set(header, 0);

Expand Down Expand Up @@ -150,7 +154,7 @@ export function audioBufferToWav(audioBuffer, normalizeAudio = true, channelOffs
wavData[offset++] = (sample2 >> 8) & 0xff;
}

if(infoChunk)
if(infoOn)
{
wavData.set(infoChunk, offset);
}
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 2fc472a

Please sign in to comment.