@hanzo/studio 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/kinds/sound.js +11 -108
- package/package.json +1 -1
package/lib/kinds/sound.js
CHANGED
|
@@ -53,94 +53,6 @@ export const voice = {
|
|
|
53
53
|
View: Listen,
|
|
54
54
|
exports: [{ label: 'MP3', run: async (w, m, v) => ({ name: filename(m.title, 'mp3'), blob: await read(w, v.files[0].key) }) }],
|
|
55
55
|
};
|
|
56
|
-
/** Encode stereo float32 PCM samples into a standard 16-bit PCM WAV Blob. */
|
|
57
|
-
function encodeWav(left, right, sampleRate) {
|
|
58
|
-
const frames = left.length;
|
|
59
|
-
const bytesPerSample = 2;
|
|
60
|
-
const channels = 2;
|
|
61
|
-
const dataBytes = frames * channels * bytesPerSample;
|
|
62
|
-
const buf = new ArrayBuffer(44 + dataBytes);
|
|
63
|
-
const view = new DataView(buf);
|
|
64
|
-
const writeStr = (off, s) => {
|
|
65
|
-
for (let i = 0; i < s.length; i++)
|
|
66
|
-
view.setUint8(off + i, s.charCodeAt(i));
|
|
67
|
-
};
|
|
68
|
-
writeStr(0, 'RIFF');
|
|
69
|
-
view.setUint32(4, 36 + dataBytes, true);
|
|
70
|
-
writeStr(8, 'WAVE');
|
|
71
|
-
writeStr(12, 'fmt ');
|
|
72
|
-
view.setUint32(16, 16, true);
|
|
73
|
-
view.setUint16(20, 1, true);
|
|
74
|
-
view.setUint16(22, channels, true);
|
|
75
|
-
view.setUint32(24, sampleRate, true);
|
|
76
|
-
view.setUint32(28, sampleRate * channels * bytesPerSample, true);
|
|
77
|
-
view.setUint16(32, channels * bytesPerSample, true);
|
|
78
|
-
view.setUint16(34, 16, true);
|
|
79
|
-
writeStr(36, 'data');
|
|
80
|
-
view.setUint32(40, dataBytes, true);
|
|
81
|
-
let offset = 44;
|
|
82
|
-
for (let i = 0; i < frames; i++) {
|
|
83
|
-
const l = Math.max(-1, Math.min(1, left[i]));
|
|
84
|
-
const r = Math.max(-1, Math.min(1, right[i]));
|
|
85
|
-
view.setInt16(offset, l < 0 ? l * 0x8000 : l * 0x7fff, true);
|
|
86
|
-
view.setInt16(offset + 2, r < 0 ? r * 0x8000 : r * 0x7fff, true);
|
|
87
|
-
offset += 4;
|
|
88
|
-
}
|
|
89
|
-
return new Blob([buf], { type: 'audio/wav' });
|
|
90
|
-
}
|
|
91
|
-
/** Synthesize a stereo instrumental music track in PCM WAV format tailored to the prompt. */
|
|
92
|
-
function synthesizeMusicTrack(prompt, seconds) {
|
|
93
|
-
const sr = 44100;
|
|
94
|
-
const dur = Math.min(30, Math.max(10, seconds));
|
|
95
|
-
const total = sr * dur;
|
|
96
|
-
const left = new Float32Array(total);
|
|
97
|
-
const right = new Float32Array(total);
|
|
98
|
-
const lower = prompt.toLowerCase();
|
|
99
|
-
const bpmMatch = lower.match(/(\d{2,3})\s*bpm/);
|
|
100
|
-
const bpm = bpmMatch ? Math.min(180, Math.max(70, Number(bpmMatch[1]))) : lower.includes('ambient') || lower.includes('chill') ? 92 : 122;
|
|
101
|
-
const beatSec = 60 / bpm;
|
|
102
|
-
const stepSec = beatSec / 2;
|
|
103
|
-
// Chord progression (Am9 -> Fmaj7 -> Cmaj9 -> G6)
|
|
104
|
-
const chords = [
|
|
105
|
-
[220.0, 261.63, 329.63, 392.0, 493.88],
|
|
106
|
-
[174.61, 220.0, 261.63, 329.63, 392.0],
|
|
107
|
-
[261.63, 329.63, 392.0, 493.88, 587.33],
|
|
108
|
-
[196.0, 246.94, 293.66, 329.63, 392.0],
|
|
109
|
-
];
|
|
110
|
-
for (let i = 0; i < total; i++) {
|
|
111
|
-
const t = i / sr;
|
|
112
|
-
const bar = Math.floor(t / (beatSec * 4)) % chords.length;
|
|
113
|
-
const chord = chords[bar];
|
|
114
|
-
const beatPhase = (t % beatSec) / beatSec;
|
|
115
|
-
const stepIdx = Math.floor(t / stepSec);
|
|
116
|
-
const stepPhase = (t % stepSec) / stepSec;
|
|
117
|
-
// Warm stereo pad
|
|
118
|
-
let padL = 0;
|
|
119
|
-
let padR = 0;
|
|
120
|
-
for (let c = 0; c < chord.length; c++) {
|
|
121
|
-
const f = chord[c];
|
|
122
|
-
padL += Math.sin(2 * Math.PI * (f * 1.001) * t + c * 0.4) * 0.08;
|
|
123
|
-
padR += Math.sin(2 * Math.PI * (f * 0.999) * t + c * 0.7) * 0.08;
|
|
124
|
-
}
|
|
125
|
-
// Sub bass on 8th notes
|
|
126
|
-
const bassFreq = chord[0] * 0.5;
|
|
127
|
-
const bassEnv = Math.exp(-stepPhase * 4.5);
|
|
128
|
-
const bass = Math.sin(2 * Math.PI * bassFreq * t) * 0.24 * bassEnv;
|
|
129
|
-
// Arpeggio synth
|
|
130
|
-
const arpFreq = chord[stepIdx % chord.length] * 2;
|
|
131
|
-
const arpEnv = Math.exp(-stepPhase * 8);
|
|
132
|
-
const arp = Math.sin(2 * Math.PI * arpFreq * t) * 0.1 * arpEnv;
|
|
133
|
-
// Kick drum on every beat
|
|
134
|
-
const kickEnv = Math.exp(-beatPhase * 14);
|
|
135
|
-
const kickFreq = 48 + 110 * Math.exp(-beatPhase * 28);
|
|
136
|
-
const kick = Math.sin(2 * Math.PI * kickFreq * t) * 0.28 * kickEnv;
|
|
137
|
-
// Fade in / out envelope
|
|
138
|
-
const fade = Math.min(1, t / 0.8, (dur - t) / 1.2);
|
|
139
|
-
left[i] = (padL + bass + kick + arp * 0.8) * fade;
|
|
140
|
-
right[i] = (padR + bass + kick + arp * 1.1) * fade;
|
|
141
|
-
}
|
|
142
|
-
return encodeWav(left, right, sr);
|
|
143
|
-
}
|
|
144
56
|
export const music = {
|
|
145
57
|
id: 'music',
|
|
146
58
|
label: 'Music',
|
|
@@ -160,28 +72,19 @@ export const music = {
|
|
|
160
72
|
required: ['title', 'prompt'],
|
|
161
73
|
},
|
|
162
74
|
},
|
|
163
|
-
|
|
75
|
+
// A song is generated on a GPU or not at all (hanzo-inc/cloud#754): a synth loop
|
|
76
|
+
// returned in its place played the same chords for every prompt.
|
|
77
|
+
needs: (a) => (lit(a.fleet, 'music.ace') ? null : 'Music runs on a GPU. Connect one with `hanzo link`, and it appears here when the GPU can run it.'),
|
|
164
78
|
make: async (w, args) => {
|
|
79
|
+
const why = music.needs(w);
|
|
80
|
+
if (why)
|
|
81
|
+
throw new Error(why);
|
|
165
82
|
const body = { prompt: str(args.prompt), lyrics: str(args.lyrics), seconds: num(args.seconds, 30, 10, 240) };
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const file = await w.keep(`music.${ext}`, g.blob);
|
|
172
|
-
return { title: str(args.title, body.prompt.slice(0, 32)), draft: { spec: body, files: [file], note: body.prompt, by: 'ACE-Step 1.5 (your GPU)' } };
|
|
173
|
-
}
|
|
174
|
-
catch {
|
|
175
|
-
// Fall through to Studio WebAudio Synthesizer
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
w.doing('Synthesizing stereo music track');
|
|
179
|
-
const wav = synthesizeMusicTrack(body.prompt, body.seconds);
|
|
180
|
-
const file = await w.keep('music.wav', wav);
|
|
181
|
-
return {
|
|
182
|
-
title: str(args.title, body.prompt.slice(0, 32) || 'Studio Track'),
|
|
183
|
-
draft: { spec: body, files: [file], note: body.prompt, by: 'Studio Audio Synth' },
|
|
184
|
-
};
|
|
83
|
+
const got = await render(w.wire, 'music', body, w.doing, w.signal);
|
|
84
|
+
const g = got[0];
|
|
85
|
+
const ext = g.path.split('.').pop() || 'mp3';
|
|
86
|
+
const file = await w.keep(`music.${ext}`, g.blob);
|
|
87
|
+
return { title: str(args.title, body.prompt.slice(0, 32)), draft: { spec: body, files: [file], note: body.prompt, by: 'ACE-Step 1.5 (your GPU)' } };
|
|
185
88
|
},
|
|
186
89
|
revise: async (w, made, change) => {
|
|
187
90
|
const now = current(made).spec;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hanzo/studio",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"description": "Hanzo Studio: a chat that makes images, posters, documents, slides, type systems, 3D, video and sound, with every result open in a tab beside it. Mounted by the Hanzo app at hanzo.ai/studio and served whole at studio.hanzo.ai.",
|