2020-05-09 14:45:23 +00:00
|
|
|
const path = require("path");
|
2020-05-19 09:08:28 +00:00
|
|
|
const audiosprite = require("gulp-audiosprite");
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
function gulptasksSounds($, gulp, buildFolder) {
|
|
|
|
// Gather some basic infos
|
2020-06-13 18:21:32 +00:00
|
|
|
const soundsDir = path.join(__dirname, "..", "res_raw", "sounds");
|
|
|
|
const builtSoundsDir = path.join(__dirname, "..", "res_built", "sounds");
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
gulp.task("sounds.clear", () => {
|
2020-06-13 16:46:25 +00:00
|
|
|
return gulp.src(builtSoundsDir, { read: false, allowEmpty: true }).pipe($.clean({ force: true }));
|
2020-05-09 14:45:23 +00:00
|
|
|
});
|
|
|
|
|
2020-05-16 07:49:00 +00:00
|
|
|
const filters = ["volume=0.2"];
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
const fileCache = new $.cache.Cache({
|
|
|
|
cacheDirName: "shapezio-precompiled-sounds",
|
|
|
|
});
|
|
|
|
|
fix size overflow when building sounds (#676)
Building the standalone with V8 v8 (Node v14) fails at step `sounds.musicHQ`, either by running out of memory or with the error "Invalid string length". Building with V8 v7 (Node v12) succeeds.
This is because `gulp-cache` builds a 994-MB string when [stringifying file contents](https://github.com/jgable/gulp-cache/blob/master/src/task-proxy.js#L262) to cache them. V8 v8 [limits string length to around 537 MB](https://github.com/v8/v8/blob/master/src/objects/string.h#L384), and thus cannot represent this string. (V8 v7 allows around 1074 MB, which is why the build passes on Node v12.)
But [`theme-full.mp3`](https://github.com/tobspr/shapez.io/blob/master/res_raw/sounds/music/theme-full.mp3) is only 79 MB: how did we get 1250% overhead?
Unlike plaintext files, binary files are read as buffers, but [by default](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L46) `gulp-cache` stringifies them naively, producing the extremely inefficient representation:
````
[
{
"cwd": "/Users/tobspr/shapez.io/gulp",
"base": "/Users/tobspr/shapez.io/res_raw/sounds/music",
"contents": {
"type": "Buffer",
"data": [
73,
68,
51,
4,
0,
0,
…etc.
````
Fortunately, `gulp-cache` [can read base64-encoded cache files](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L26).
Instead of using the default file transform function, **pass a `value` option to base64-encode the file contents**. This results in only 33% overhead on cache file size (106 MB for the largest file).
This has multiple benefits:
- Fixes the build failure
- Requires less memory (from 6 GB down to < 1 GB on my machine)
- When cache files are found, the `sounds.musicHQ` is much faster (from ~30 s down to ~4 s on my machine)
- Smaller cache files on disk
2020-09-21 06:37:50 +00:00
|
|
|
function getFileCacheValue(file) {
|
|
|
|
const { _isVinyl, base, cwd, contents, history, stat, path } = file;
|
2020-09-24 10:53:40 +00:00
|
|
|
const encodedContents = Buffer.from(contents).toString("base64");
|
fix size overflow when building sounds (#676)
Building the standalone with V8 v8 (Node v14) fails at step `sounds.musicHQ`, either by running out of memory or with the error "Invalid string length". Building with V8 v7 (Node v12) succeeds.
This is because `gulp-cache` builds a 994-MB string when [stringifying file contents](https://github.com/jgable/gulp-cache/blob/master/src/task-proxy.js#L262) to cache them. V8 v8 [limits string length to around 537 MB](https://github.com/v8/v8/blob/master/src/objects/string.h#L384), and thus cannot represent this string. (V8 v7 allows around 1074 MB, which is why the build passes on Node v12.)
But [`theme-full.mp3`](https://github.com/tobspr/shapez.io/blob/master/res_raw/sounds/music/theme-full.mp3) is only 79 MB: how did we get 1250% overhead?
Unlike plaintext files, binary files are read as buffers, but [by default](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L46) `gulp-cache` stringifies them naively, producing the extremely inefficient representation:
````
[
{
"cwd": "/Users/tobspr/shapez.io/gulp",
"base": "/Users/tobspr/shapez.io/res_raw/sounds/music",
"contents": {
"type": "Buffer",
"data": [
73,
68,
51,
4,
0,
0,
…etc.
````
Fortunately, `gulp-cache` [can read base64-encoded cache files](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L26).
Instead of using the default file transform function, **pass a `value` option to base64-encode the file contents**. This results in only 33% overhead on cache file size (106 MB for the largest file).
This has multiple benefits:
- Fixes the build failure
- Requires less memory (from 6 GB down to < 1 GB on my machine)
- When cache files are found, the `sounds.musicHQ` is much faster (from ~30 s down to ~4 s on my machine)
- Smaller cache files on disk
2020-09-21 06:37:50 +00:00
|
|
|
return { _isVinyl, base, cwd, contents: encodedContents, history, stat, path };
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
// Encodes the game music
|
2020-05-19 09:08:28 +00:00
|
|
|
gulp.task("sounds.music", () => {
|
2020-05-09 14:45:23 +00:00
|
|
|
return gulp
|
|
|
|
.src([path.join(soundsDir, "music", "**", "*.wav"), path.join(soundsDir, "music", "**", "*.mp3")])
|
2020-05-14 17:12:58 +00:00
|
|
|
.pipe($.plumber())
|
2020-05-09 14:45:23 +00:00
|
|
|
.pipe(
|
|
|
|
$.cache(
|
|
|
|
$.fluentFfmpeg("mp3", function (cmd) {
|
|
|
|
return cmd
|
|
|
|
.audioBitrate(48)
|
|
|
|
.audioChannels(1)
|
|
|
|
.audioFrequency(22050)
|
2020-05-16 07:49:00 +00:00
|
|
|
.audioCodec("libmp3lame")
|
2020-06-21 20:56:38 +00:00
|
|
|
.audioFilters(["volume=0.15"]);
|
2020-05-09 14:45:23 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "music",
|
|
|
|
fileCache,
|
fix size overflow when building sounds (#676)
Building the standalone with V8 v8 (Node v14) fails at step `sounds.musicHQ`, either by running out of memory or with the error "Invalid string length". Building with V8 v7 (Node v12) succeeds.
This is because `gulp-cache` builds a 994-MB string when [stringifying file contents](https://github.com/jgable/gulp-cache/blob/master/src/task-proxy.js#L262) to cache them. V8 v8 [limits string length to around 537 MB](https://github.com/v8/v8/blob/master/src/objects/string.h#L384), and thus cannot represent this string. (V8 v7 allows around 1074 MB, which is why the build passes on Node v12.)
But [`theme-full.mp3`](https://github.com/tobspr/shapez.io/blob/master/res_raw/sounds/music/theme-full.mp3) is only 79 MB: how did we get 1250% overhead?
Unlike plaintext files, binary files are read as buffers, but [by default](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L46) `gulp-cache` stringifies them naively, producing the extremely inefficient representation:
````
[
{
"cwd": "/Users/tobspr/shapez.io/gulp",
"base": "/Users/tobspr/shapez.io/res_raw/sounds/music",
"contents": {
"type": "Buffer",
"data": [
73,
68,
51,
4,
0,
0,
…etc.
````
Fortunately, `gulp-cache` [can read base64-encoded cache files](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L26).
Instead of using the default file transform function, **pass a `value` option to base64-encode the file contents**. This results in only 33% overhead on cache file size (106 MB for the largest file).
This has multiple benefits:
- Fixes the build failure
- Requires less memory (from 6 GB down to < 1 GB on my machine)
- When cache files are found, the `sounds.musicHQ` is much faster (from ~30 s down to ~4 s on my machine)
- Smaller cache files on disk
2020-09-21 06:37:50 +00:00
|
|
|
value: getFileCacheValue,
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.pipe(gulp.dest(path.join(builtSoundsDir, "music")));
|
|
|
|
});
|
|
|
|
|
2020-06-22 10:09:02 +00:00
|
|
|
// Encodes the game music in high quality for the standalone
|
|
|
|
gulp.task("sounds.musicHQ", () => {
|
|
|
|
return gulp
|
|
|
|
.src([path.join(soundsDir, "music", "**", "*.wav"), path.join(soundsDir, "music", "**", "*.mp3")])
|
|
|
|
.pipe($.plumber())
|
|
|
|
.pipe(
|
|
|
|
$.cache(
|
|
|
|
$.fluentFfmpeg("mp3", function (cmd) {
|
|
|
|
return cmd
|
|
|
|
.audioBitrate(256)
|
|
|
|
.audioChannels(2)
|
|
|
|
.audioFrequency(44100)
|
|
|
|
.audioCodec("libmp3lame")
|
|
|
|
.audioFilters(["volume=0.15"]);
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "music-high-quality",
|
|
|
|
fileCache,
|
fix size overflow when building sounds (#676)
Building the standalone with V8 v8 (Node v14) fails at step `sounds.musicHQ`, either by running out of memory or with the error "Invalid string length". Building with V8 v7 (Node v12) succeeds.
This is because `gulp-cache` builds a 994-MB string when [stringifying file contents](https://github.com/jgable/gulp-cache/blob/master/src/task-proxy.js#L262) to cache them. V8 v8 [limits string length to around 537 MB](https://github.com/v8/v8/blob/master/src/objects/string.h#L384), and thus cannot represent this string. (V8 v7 allows around 1074 MB, which is why the build passes on Node v12.)
But [`theme-full.mp3`](https://github.com/tobspr/shapez.io/blob/master/res_raw/sounds/music/theme-full.mp3) is only 79 MB: how did we get 1250% overhead?
Unlike plaintext files, binary files are read as buffers, but [by default](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L46) `gulp-cache` stringifies them naively, producing the extremely inefficient representation:
````
[
{
"cwd": "/Users/tobspr/shapez.io/gulp",
"base": "/Users/tobspr/shapez.io/res_raw/sounds/music",
"contents": {
"type": "Buffer",
"data": [
73,
68,
51,
4,
0,
0,
…etc.
````
Fortunately, `gulp-cache` [can read base64-encoded cache files](https://github.com/jgable/gulp-cache/blob/master/src/index.js#L26).
Instead of using the default file transform function, **pass a `value` option to base64-encode the file contents**. This results in only 33% overhead on cache file size (106 MB for the largest file).
This has multiple benefits:
- Fixes the build failure
- Requires less memory (from 6 GB down to < 1 GB on my machine)
- When cache files are found, the `sounds.musicHQ` is much faster (from ~30 s down to ~4 s on my machine)
- Smaller cache files on disk
2020-09-21 06:37:50 +00:00
|
|
|
value: getFileCacheValue,
|
2020-06-22 10:09:02 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.pipe(gulp.dest(path.join(builtSoundsDir, "music")));
|
|
|
|
});
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
// Encodes the ui sounds
|
2020-05-19 09:08:28 +00:00
|
|
|
gulp.task("sounds.sfxGenerateSprites", () => {
|
2020-05-09 14:45:23 +00:00
|
|
|
return gulp
|
2020-05-19 09:08:28 +00:00
|
|
|
.src([path.join(soundsDir, "sfx", "**", "*.wav"), path.join(soundsDir, "sfx", "**", "*.mp3")])
|
2020-05-14 17:12:58 +00:00
|
|
|
.pipe($.plumber())
|
2020-05-09 14:45:23 +00:00
|
|
|
.pipe(
|
2020-05-19 09:08:28 +00:00
|
|
|
audiosprite({
|
|
|
|
format: "howler",
|
|
|
|
output: "sfx",
|
|
|
|
gap: 0.1,
|
|
|
|
export: "mp3",
|
|
|
|
})
|
2020-05-09 14:45:23 +00:00
|
|
|
)
|
2020-05-19 09:08:28 +00:00
|
|
|
.pipe(gulp.dest(path.join(builtSoundsDir)));
|
2020-05-09 14:45:23 +00:00
|
|
|
});
|
2020-05-19 09:08:28 +00:00
|
|
|
gulp.task("sounds.sfxOptimize", () => {
|
2020-05-09 14:45:23 +00:00
|
|
|
return gulp
|
2020-05-19 09:08:28 +00:00
|
|
|
.src([path.join(builtSoundsDir, "sfx.mp3")])
|
2020-05-14 17:12:58 +00:00
|
|
|
.pipe($.plumber())
|
2020-05-09 14:45:23 +00:00
|
|
|
.pipe(
|
2020-05-19 09:08:28 +00:00
|
|
|
$.fluentFfmpeg("mp3", function (cmd) {
|
|
|
|
return cmd
|
|
|
|
.audioBitrate(128)
|
|
|
|
.audioChannels(1)
|
|
|
|
.audioFrequency(22050)
|
|
|
|
.audioCodec("libmp3lame")
|
|
|
|
.audioFilters(filters);
|
|
|
|
})
|
2020-05-09 14:45:23 +00:00
|
|
|
)
|
2020-05-19 09:08:28 +00:00
|
|
|
.pipe(gulp.dest(path.join(builtSoundsDir)));
|
|
|
|
});
|
|
|
|
gulp.task("sounds.sfxCopyAtlas", () => {
|
|
|
|
return gulp
|
|
|
|
.src([path.join(builtSoundsDir, "sfx.json")])
|
|
|
|
.pipe(gulp.dest(path.join(__dirname, "..", "src", "js", "built-temp")));
|
2020-05-09 14:45:23 +00:00
|
|
|
});
|
|
|
|
|
2020-05-19 09:11:08 +00:00
|
|
|
gulp.task(
|
|
|
|
"sounds.sfx",
|
2020-06-13 15:59:25 +00:00
|
|
|
gulp.series("sounds.sfxGenerateSprites", "sounds.sfxOptimize", "sounds.sfxCopyAtlas")
|
2020-05-19 09:11:08 +00:00
|
|
|
);
|
2020-05-19 09:08:28 +00:00
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
gulp.task("sounds.copy", () => {
|
|
|
|
return gulp
|
|
|
|
.src(path.join(builtSoundsDir, "**", "*.mp3"))
|
2020-05-14 17:12:58 +00:00
|
|
|
.pipe($.plumber())
|
2020-05-09 14:45:23 +00:00
|
|
|
.pipe(gulp.dest(path.join(buildFolder, "res", "sounds")));
|
|
|
|
});
|
|
|
|
|
2020-06-13 18:44:00 +00:00
|
|
|
gulp.task("sounds.buildall", gulp.parallel("sounds.music", "sounds.sfx"));
|
2020-06-22 10:09:02 +00:00
|
|
|
gulp.task("sounds.buildallHQ", gulp.parallel("sounds.musicHQ", "sounds.sfx"));
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-06-13 15:59:25 +00:00
|
|
|
gulp.task("sounds.fullbuild", gulp.series("sounds.clear", "sounds.buildall", "sounds.copy"));
|
2020-06-22 10:09:02 +00:00
|
|
|
gulp.task("sounds.fullbuildHQ", gulp.series("sounds.clear", "sounds.buildallHQ", "sounds.copy"));
|
2020-06-13 15:59:25 +00:00
|
|
|
gulp.task("sounds.dev", gulp.series("sounds.buildall", "sounds.copy"));
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
gulptasksSounds,
|
|
|
|
};
|