Spaces:
Running
Running
fix: single voice line per phrase, flat intonation, longer audio tail, cache-busting
Browse filesFiles changed (9)
- app.js+25-11
- public/voice/im_reachy.wavAdded
- public/voice/reachy.wavAdded
- public/voice/reachy1.wavDeleted
- public/voice/reachy1_im.wavDeleted
- public/voice/reachy2.wavDeleted
- public/voice/reachy2_im.wavDeleted
- public/voice/reachy3.wavDeleted
- public/voice/reachy3_im.wavDeleted
66 unmodified lines676869707172737475767778590 unmodified lines66967067167267367467567623 unmodified lines700701702703704705706707708709710711712601 unmodified lines1314131513161317131813191320132113221323132413256 unmodified lines13321333133413351336133766 unmodified lines};// Spoken lines: pre-rendered 16 kHz mono WAVs in public/voice/, played through Reachy's speaker.// Three voices per line (1 = higher female, 2 = male, 3 = high & fast); trim the lists to the ones you like.const SPEECH = {base: "/public/voice/",lines: {name: ["reachy1.wav", "reachy2.wav", "reachy3.wav"], // "Reachy!"im_name: ["reachy1_im.wav", "reachy2_im.wav", "reachy3_im.wav"], // "I'm Reachy!"},};590 unmodified linesthis.mood = "reacting";setStatus(sayText ?? "", lineKey, "reacting");log(`Speak -> ${lineKey}`);const motion = buildTalkMotion({ yawDeg: this.headYaw, pitchDeg: this.headPitch, bodyYawDeg: this.bodyYaw, durSec: wav.durSec + 0.3 });await this.play(motion, this.muted ? undefined : wav.blob, `speak ${lineKey}`);}// Random happy emotion clip (voice greetings, etc.), subject to the clip budget23 unmodified linesawait this.play(motion, this.muted ? undefined : clip.wav, name);}async play(motion, audioBlob, description) {if (this.busy) return;this.busy = true;try {// The daemon indexes head as a 4x4 (head[i][j]); internally we keep flat 16 for our math.const wire = { ...motion, set_target_data: motion.set_target_data.map((f) => ({ ...f, head: f.head ? toNested4x4(f.head) : f.head })) };const result = await this.reachy.playMove(wire, { audioBlob, description, audioLeadMs: -100 });if (result?.error) {const f0 = motion.set_target_data[0], rp = f0?.head ? matrixToRpyDeg(f0.head) : null;log(`playMove error: ${result.error}` + (rp ? ` (start yaw ${rp.yaw.toFixed(0)} pitch ${rp.pitch.toFixed(0)} body ${((f0.body_yaw ?? 0) / DEG).toFixed(0)})` : ""));601 unmodified linesif (this.map.has(file)) return this.map.get(file);const p = (async () => {try {const res = await fetch(SPEECH.base + file);if (!res.ok) throw new Error(`${res.status}`);const buf = await res.arrayBuffer();const v = new DataView(buf);const rate = v.getUint32(24, true), dataLen = v.getUint32(40, true);return { blob: new Blob([buf], { type: "audio/wav" }), durSec: dataLen / 2 / rate };} catch (e) { log(`Voice line ${file} failed: ${e.message}`); this.map.delete(file); return null; }})();this.map.set(file, p);6 unmodified lines}}// "Talking": hold the head, small quick nods and antenna flicks for the length of the linefunction buildTalkMotion({ yawDeg, pitchDeg, bodyYawDeg, durSec = 1.2 }) {if (![yawDeg, pitchDeg, bodyYawDeg].every(Number.isFinite)) { yawDeg = 0; pitchDeg = TRACK.restPitchDeg; bodyYawDeg = 0; }66 unmodified lines6768697071727374757677787980590 unmodified lines67167267367467567667767823 unmodified lines702703704705706707708709710711712713714601 unmodified lines13161317131813191320132113221323132413256 unmodified lines1332133313341335133613371338133913401341134213431344134513461347134813491350135166 unmodified lines};// Spoken lines: pre-rendered 16 kHz mono WAVs in public/voice/, played through Reachy's speaker.// One consistent voice (espeak en-us+f3, pitch 72, speed 160). A key with several files picks one at random.const SPEECH = {base: "/public/voice/",version: "3", // bump when voice files change so browsers don't serve cached old onestailSec: 1.0, // motion runs this much longer than the audio so the robot's audio path can finishlines: {name: ["reachy.wav"], // "Reachy."im_name: ["im_reachy.wav"], // "I'm Reachy."},};590 unmodified linesthis.mood = "reacting";setStatus(sayText ?? "", lineKey, "reacting");log(`Speak -> ${lineKey}`);const motion = buildTalkMotion({ yawDeg: this.headYaw, pitchDeg: this.headPitch, bodyYawDeg: this.bodyYaw, durSec: wav.durSec + SPEECH.tailSec });await this.play(motion, this.muted ? undefined : wav.blob, `speak ${lineKey}`, 0);}// Random happy emotion clip (voice greetings, etc.), subject to the clip budget23 unmodified linesawait this.play(motion, this.muted ? undefined : clip.wav, name);}async play(motion, audioBlob, description, audioLeadMs = -100) {if (this.busy) return;this.busy = true;try {// The daemon indexes head as a 4x4 (head[i][j]); internally we keep flat 16 for our math.const wire = { ...motion, set_target_data: motion.set_target_data.map((f) => ({ ...f, head: f.head ? toNested4x4(f.head) : f.head })) };const result = await this.reachy.playMove(wire, { audioBlob, description, audioLeadMs });if (result?.error) {const f0 = motion.set_target_data[0], rp = f0?.head ? matrixToRpyDeg(f0.head) : null;log(`playMove error: ${result.error}` + (rp ? ` (start yaw ${rp.yaw.toFixed(0)} pitch ${rp.pitch.toFixed(0)} body ${((f0.body_yaw ?? 0) / DEG).toFixed(0)})` : ""));601 unmodified linesif (this.map.has(file)) return this.map.get(file);const p = (async () => {try {const res = await fetch(SPEECH.base + file + "?v=" + SPEECH.version);if (!res.ok) throw new Error(`${res.status}`);const buf = await res.arrayBuffer();return { blob: new Blob([buf], { type: "audio/wav" }), durSec: wavDuration(buf) };} catch (e) { log(`Voice line ${file} failed: ${e.message}`); this.map.delete(file); return null; }})();this.map.set(file, p);6 unmodified lines}}// Walk the RIFF chunks to find the real data length (ffmpeg and others add LIST chunks before it)function wavDuration(buf) {const v = new DataView(buf);let rate = 16000, channels = 1, bits = 16, pos = 12;while (pos + 8 <= buf.byteLength) {const id = String.fromCharCode(v.getUint8(pos), v.getUint8(pos + 1), v.getUint8(pos + 2), v.getUint8(pos + 3));const size = v.getUint32(pos + 4, true);if (id === "fmt ") { channels = v.getUint16(pos + 10, true); rate = v.getUint32(pos + 12, true); bits = v.getUint16(pos + 22, true); }if (id === "data") return size / (rate * channels * (bits / 8));pos += 8 + size + (size & 1);}return 1.0;}// "Talking": hold the head, small quick nods and antenna flicks for the length of the linefunction buildTalkMotion({ yawDeg, pitchDeg, bodyYawDeg, durSec = 1.2 }) {if (![yawDeg, pitchDeg, bodyYawDeg].every(Number.isFinite)) { yawDeg = 0; pitchDeg = TRACK.restPitchDeg; bodyYawDeg = 0; }
app.js+25-11
public/voice/im_reachy.wavAdded
public/voice/reachy.wavAdded
public/voice/reachy1.wavDeleted
public/voice/reachy1_im.wavDeleted
public/voice/reachy2.wavDeleted
public/voice/reachy2_im.wavDeleted
public/voice/reachy3.wavDeleted
public/voice/reachy3_im.wavDeleted