Add music and voice assets
This commit is contained in:
227
scripts/generate-bgm.mjs
Normal file
227
scripts/generate-bgm.mjs
Normal file
@@ -0,0 +1,227 @@
|
||||
import { mkdirSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const sampleRate = 22050;
|
||||
const duration = 32;
|
||||
const outputDir = 'src/assets/audio/bgm';
|
||||
|
||||
mkdirSync(outputDir, { recursive: true });
|
||||
|
||||
const tracks = [
|
||||
{
|
||||
name: 'title-theme',
|
||||
root: 196,
|
||||
tempo: 72,
|
||||
drum: 0.34,
|
||||
melody: [0, 2, 4, 7, 9, 7, 4, 2],
|
||||
bass: [0, 0, -5, -3],
|
||||
color: 'warm'
|
||||
},
|
||||
{
|
||||
name: 'story-dark',
|
||||
root: 146.83,
|
||||
tempo: 58,
|
||||
drum: 0.22,
|
||||
melody: [0, -3, 0, 2, -5, -3, 0, -7],
|
||||
bass: [0, -7, -5, -3],
|
||||
color: 'dark'
|
||||
},
|
||||
{
|
||||
name: 'oath-theme',
|
||||
root: 220,
|
||||
tempo: 66,
|
||||
drum: 0.24,
|
||||
melody: [0, 4, 7, 9, 12, 9, 7, 4],
|
||||
bass: [0, -5, -3, -7],
|
||||
color: 'gold'
|
||||
},
|
||||
{
|
||||
name: 'militia-theme',
|
||||
root: 174.61,
|
||||
tempo: 82,
|
||||
drum: 0.42,
|
||||
melody: [0, 2, 5, 7, 9, 7, 5, 2],
|
||||
bass: [0, -5, 0, -3],
|
||||
color: 'earth'
|
||||
},
|
||||
{
|
||||
name: 'battle-prep',
|
||||
root: 164.81,
|
||||
tempo: 94,
|
||||
drum: 0.5,
|
||||
melody: [0, 3, 5, 7, 10, 7, 5, 3],
|
||||
bass: [0, 0, -2, -5],
|
||||
color: 'steel'
|
||||
}
|
||||
];
|
||||
|
||||
for (const track of tracks) {
|
||||
const buffer = new Float32Array(sampleRate * duration);
|
||||
buildTrack(buffer, track);
|
||||
normalize(buffer, 0.86);
|
||||
writeFileSync(join(outputDir, `${track.name}.wav`), encodeWav(buffer));
|
||||
}
|
||||
|
||||
function buildTrack(buffer, track) {
|
||||
const beat = 60 / track.tempo;
|
||||
const bar = beat * 4;
|
||||
|
||||
for (let t = 0; t < duration; t += bar) {
|
||||
for (let i = 0; i < 4; i += 1) {
|
||||
const bassFreq = note(track.root, track.bass[i % track.bass.length] - 12);
|
||||
addTone(buffer, t + i * beat, beat * 1.9, bassFreq, 0.075, 'soft');
|
||||
addTone(buffer, t + i * beat, beat * 1.9, bassFreq * 2, 0.025, 'soft');
|
||||
}
|
||||
}
|
||||
|
||||
for (let t = 0; t < duration; t += beat * 2) {
|
||||
const step = Math.floor(t / (beat * 2)) % track.melody.length;
|
||||
const freq = note(track.root, track.melody[step]);
|
||||
addPluck(buffer, t + 0.04, beat * 1.65, freq, 0.12, track.color);
|
||||
addPluck(buffer, t + beat * 0.95, beat * 0.9, freq * 1.5, 0.045, track.color);
|
||||
}
|
||||
|
||||
for (let t = 0; t < duration; t += beat) {
|
||||
if (Math.round(t / beat) % 4 === 0) {
|
||||
addDrum(buffer, t, 0.26, track.drum);
|
||||
} else if (track.drum > 0.3 && Math.round(t / beat) % 2 === 0) {
|
||||
addDrum(buffer, t, 0.16, track.drum * 0.45);
|
||||
}
|
||||
}
|
||||
|
||||
for (let t = 0; t < duration; t += bar * 2) {
|
||||
addNoiseWash(buffer, t, bar * 1.8, track.color === 'dark' ? 0.012 : 0.007);
|
||||
}
|
||||
|
||||
for (let i = 0; i < buffer.length; i += 1) {
|
||||
const seconds = i / sampleRate;
|
||||
const edge = Math.min(1, seconds / 0.6, (duration - seconds) / 0.6);
|
||||
buffer[i] *= Math.max(0, edge);
|
||||
}
|
||||
}
|
||||
|
||||
function note(root, semitone) {
|
||||
return root * 2 ** (semitone / 12);
|
||||
}
|
||||
|
||||
function addTone(buffer, start, length, freq, gain, shape) {
|
||||
const startIndex = Math.floor(start * sampleRate);
|
||||
const count = Math.floor(length * sampleRate);
|
||||
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const index = startIndex + i;
|
||||
if (index >= buffer.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
const t = i / sampleRate;
|
||||
const env = Math.sin(Math.PI * Math.min(1, i / count));
|
||||
const wave = Math.sin(2 * Math.PI * freq * t) + Math.sin(2 * Math.PI * freq * 2 * t) * 0.18;
|
||||
buffer[index] += wave * gain * env * (shape === 'soft' ? 0.78 : 1);
|
||||
}
|
||||
}
|
||||
|
||||
function addPluck(buffer, start, length, freq, gain, color) {
|
||||
const startIndex = Math.floor(start * sampleRate);
|
||||
const count = Math.floor(length * sampleRate);
|
||||
const brightness = color === 'gold' ? 0.36 : color === 'steel' ? 0.42 : 0.28;
|
||||
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const index = startIndex + i;
|
||||
if (index >= buffer.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
const t = i / sampleRate;
|
||||
const env = Math.exp(-t * 4.8) * Math.min(1, t / 0.02);
|
||||
const wave =
|
||||
Math.sin(2 * Math.PI * freq * t) +
|
||||
Math.sin(2 * Math.PI * freq * 2 * t) * brightness +
|
||||
Math.sin(2 * Math.PI * freq * 3 * t) * 0.14;
|
||||
buffer[index] += wave * gain * env;
|
||||
}
|
||||
}
|
||||
|
||||
function addDrum(buffer, start, length, gain) {
|
||||
const startIndex = Math.floor(start * sampleRate);
|
||||
const count = Math.floor(length * sampleRate);
|
||||
let seed = 1337 + startIndex;
|
||||
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const index = startIndex + i;
|
||||
if (index >= buffer.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
const t = i / sampleRate;
|
||||
const env = Math.exp(-t * 15);
|
||||
const freq = 96 - t * 120;
|
||||
seed = (seed * 1664525 + 1013904223) >>> 0;
|
||||
const noise = ((seed / 0xffffffff) * 2 - 1) * 0.18;
|
||||
const wave = Math.sin(2 * Math.PI * Math.max(42, freq) * t) + noise;
|
||||
buffer[index] += wave * gain * env;
|
||||
}
|
||||
}
|
||||
|
||||
function addNoiseWash(buffer, start, length, gain) {
|
||||
const startIndex = Math.floor(start * sampleRate);
|
||||
const count = Math.floor(length * sampleRate);
|
||||
let seed = 4219 + startIndex;
|
||||
let previous = 0;
|
||||
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const index = startIndex + i;
|
||||
if (index >= buffer.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
seed = (seed * 1103515245 + 12345) >>> 0;
|
||||
const raw = (seed / 0xffffffff) * 2 - 1;
|
||||
previous = previous * 0.98 + raw * 0.02;
|
||||
const env = Math.sin(Math.PI * i / count);
|
||||
buffer[index] += previous * gain * env;
|
||||
}
|
||||
}
|
||||
|
||||
function normalize(buffer, target) {
|
||||
let peak = 0;
|
||||
for (const value of buffer) {
|
||||
peak = Math.max(peak, Math.abs(value));
|
||||
}
|
||||
|
||||
if (peak === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scale = target / peak;
|
||||
for (let i = 0; i < buffer.length; i += 1) {
|
||||
buffer[i] *= scale;
|
||||
}
|
||||
}
|
||||
|
||||
function encodeWav(buffer) {
|
||||
const bytesPerSample = 2;
|
||||
const dataSize = buffer.length * bytesPerSample;
|
||||
const wav = Buffer.alloc(44 + dataSize);
|
||||
|
||||
wav.write('RIFF', 0);
|
||||
wav.writeUInt32LE(36 + dataSize, 4);
|
||||
wav.write('WAVE', 8);
|
||||
wav.write('fmt ', 12);
|
||||
wav.writeUInt32LE(16, 16);
|
||||
wav.writeUInt16LE(1, 20);
|
||||
wav.writeUInt16LE(1, 22);
|
||||
wav.writeUInt32LE(sampleRate, 24);
|
||||
wav.writeUInt32LE(sampleRate * bytesPerSample, 28);
|
||||
wav.writeUInt16LE(bytesPerSample, 32);
|
||||
wav.writeUInt16LE(16, 34);
|
||||
wav.write('data', 36);
|
||||
wav.writeUInt32LE(dataSize, 40);
|
||||
|
||||
for (let i = 0; i < buffer.length; i += 1) {
|
||||
const value = Math.max(-1, Math.min(1, buffer[i]));
|
||||
wav.writeInt16LE(Math.round(value * 32767), 44 + i * bytesPerSample);
|
||||
}
|
||||
|
||||
return wav;
|
||||
}
|
||||
127
scripts/generate-voice-assets.mjs
Normal file
127
scripts/generate-voice-assets.mjs
Normal file
@@ -0,0 +1,127 @@
|
||||
import { mkdirSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
|
||||
const outputDir = resolve('src/assets/audio/voice/prologue');
|
||||
|
||||
mkdirSync(outputDir, { recursive: true });
|
||||
|
||||
const voiceName = 'Microsoft Heami Desktop';
|
||||
|
||||
const lines = [
|
||||
{
|
||||
name: 'yellow-turban-chaos',
|
||||
role: 'narrator',
|
||||
rate: -2,
|
||||
volume: 86,
|
||||
text: '중평 원년, 황건의 깃발이 각지에서 일어섰다. 관군은 흩어지고, 마을마다 불안한 소문이 번져갔다.'
|
||||
},
|
||||
{
|
||||
name: 'liu-bei-resolve',
|
||||
role: 'liuBei',
|
||||
rate: -1,
|
||||
volume: 92,
|
||||
text: '천실의 백성이 이토록 고통받는데, 어찌 초개처럼 그늘에 숨어 있을 수 있겠는가.'
|
||||
},
|
||||
{
|
||||
name: 'zhang-fei-joins',
|
||||
role: 'zhangFei',
|
||||
rate: 1,
|
||||
volume: 100,
|
||||
text: '좋소! 나라를 바로잡고 백성을 구한다면, 내 재산과 힘을 모두 아끼지 않겠소.'
|
||||
},
|
||||
{
|
||||
name: 'guan-yu-joins',
|
||||
role: 'guanYu',
|
||||
rate: -3,
|
||||
volume: 95,
|
||||
text: '서로의 뜻을 믿고 의로운 길을 함께한다면, 천 리라도 멀다 하지 않겠습니다.'
|
||||
},
|
||||
{
|
||||
name: 'oath-narration',
|
||||
role: 'narrator',
|
||||
rate: -2,
|
||||
volume: 88,
|
||||
text: '복사꽃 향기가 흩날리는 동산에서 세 사람은 하늘과 땅 앞에 고했다.'
|
||||
},
|
||||
{
|
||||
name: 'oath-liu-bei',
|
||||
role: 'liuBei',
|
||||
rate: -1,
|
||||
volume: 94,
|
||||
text: '우리는 태어난 날은 달라도, 오늘부터 뜻을 함께한다. 백성을 구하고 천하를 바로 세우는 길에서 물러서지 않겠다.'
|
||||
},
|
||||
{
|
||||
name: 'oath-brothers',
|
||||
role: 'guanYu',
|
||||
rate: -3,
|
||||
volume: 96,
|
||||
text: '의리를 저버리지 않고, 형제를 저버리지 않겠습니다.'
|
||||
},
|
||||
{
|
||||
name: 'militia-rises',
|
||||
role: 'zhangFei',
|
||||
rate: 1,
|
||||
volume: 100,
|
||||
text: '창을 들 사람은 앞으로 나오시오! 오늘 모인 이름 없는 무리가 내일의 방패가 될 것이오!'
|
||||
},
|
||||
{
|
||||
name: 'first-sortie',
|
||||
role: 'liuBei',
|
||||
rate: -1,
|
||||
volume: 94,
|
||||
text: '우리는 아직 작다. 그러나 오늘 한 마을을 지켜낸다면, 그 뜻은 천하로 번져갈 것이다.'
|
||||
},
|
||||
{
|
||||
name: 'battle-briefing',
|
||||
role: 'narrator',
|
||||
rate: -2,
|
||||
volume: 88,
|
||||
text: '의용군은 마을 입구로 향했다. 첫 전투가 곧 시작된다.'
|
||||
}
|
||||
];
|
||||
|
||||
for (const line of lines) {
|
||||
const output = resolve(outputDir, `${line.name}.wav`);
|
||||
generateVoice({ ...line, output });
|
||||
}
|
||||
|
||||
function generateVoice(item) {
|
||||
const payload = Buffer.from(JSON.stringify({ ...item, voiceName }), 'utf8').toString('base64');
|
||||
const powershell = `
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
Add-Type -AssemblyName System.Speech
|
||||
$payload = '${payload}'
|
||||
$json = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($payload))
|
||||
$item = $json | ConvertFrom-Json
|
||||
$synth = New-Object System.Speech.Synthesis.SpeechSynthesizer
|
||||
try {
|
||||
try {
|
||||
$synth.SelectVoice($item.voiceName)
|
||||
} catch {
|
||||
$installed = $synth.GetInstalledVoices() | ForEach-Object { $_.VoiceInfo.Name }
|
||||
if ($installed.Count -gt 0) {
|
||||
$synth.SelectVoice($installed[0])
|
||||
}
|
||||
}
|
||||
$synth.Rate = [int]$item.rate
|
||||
$synth.Volume = [int]$item.volume
|
||||
$synth.SetOutputToWaveFile($item.output)
|
||||
$synth.Speak($item.text)
|
||||
} finally {
|
||||
$synth.Dispose()
|
||||
}
|
||||
`;
|
||||
|
||||
const encodedCommand = Buffer.from(powershell, 'utf16le').toString('base64');
|
||||
const result = spawnSync(
|
||||
'powershell.exe',
|
||||
['-NoProfile', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', encodedCommand],
|
||||
{ stdio: 'inherit' }
|
||||
);
|
||||
|
||||
if (result.status !== 0) {
|
||||
throw new Error(`Failed to generate voice asset: ${item.name}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user