Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
tcm390 committed Feb 12, 2025
1 parent d5af5a4 commit 6a9c76a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
53 changes: 1 addition & 52 deletions packages/plugin-elevenlabs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
import { IAgentRuntime, Plugin, logger } from "@elizaos/core";
import { Readable } from "node:stream";
import { ModelClass } from "@elizaos/core";
import { PassThrough } from "stream";

export function getWavHeader(
audioLength: number,
sampleRate: number,
channelCount = 1,
bitsPerSample = 16
): Buffer {
const wavHeader = Buffer.alloc(44);
wavHeader.write("RIFF", 0);
wavHeader.writeUInt32LE(36 + audioLength, 4); // Length of entire file in bytes minus 8
wavHeader.write("WAVE", 8);
wavHeader.write("fmt ", 12);
wavHeader.writeUInt32LE(16, 16); // Length of format data
wavHeader.writeUInt16LE(1, 20); // Type of format (1 is PCM)
wavHeader.writeUInt16LE(channelCount, 22); // Number of channels
wavHeader.writeUInt32LE(sampleRate, 24); // Sample rate
wavHeader.writeUInt32LE((sampleRate * bitsPerSample * channelCount) / 8, 28); // Byte rate
wavHeader.writeUInt16LE((bitsPerSample * channelCount) / 8, 32); // Block align ((BitsPerSample * Channels) / 8)
wavHeader.writeUInt16LE(bitsPerSample, 34); // Bits per sample
wavHeader.write("data", 36); // Data chunk header
wavHeader.writeUInt32LE(audioLength, 40); // Data chunk size
return wavHeader;
}

function prependWavHeader(
readable: Readable,
audioLength: number,
sampleRate: number,
channelCount = 1,
bitsPerSample = 16
): Readable {
const wavHeader = getWavHeader(
audioLength,
sampleRate,
channelCount,
bitsPerSample
);
let pushedHeader = false;
const passThrough = new PassThrough();
readable.on("data", (data) => {
if (!pushedHeader) {
passThrough.push(wavHeader);
pushedHeader = true;
}
passThrough.push(data);
});
readable.on("end", () => {
passThrough.end();
});
return passThrough;
}
import { prependWavHeader } from "./utils.ts";

function getVoiceSettings(runtime: IAgentRuntime) {
return {
Expand Down
53 changes: 53 additions & 0 deletions packages/plugin-elevenlabs/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { PassThrough } from "stream";
import { Readable } from "node:stream";

export function getWavHeader(
audioLength: number,
sampleRate: number,
channelCount = 1,
bitsPerSample = 16
): Buffer {
const wavHeader = Buffer.alloc(44);
wavHeader.write("RIFF", 0);
wavHeader.writeUInt32LE(36 + audioLength, 4); // Length of entire file in bytes minus 8
wavHeader.write("WAVE", 8);
wavHeader.write("fmt ", 12);
wavHeader.writeUInt32LE(16, 16); // Length of format data
wavHeader.writeUInt16LE(1, 20); // Type of format (1 is PCM)
wavHeader.writeUInt16LE(channelCount, 22); // Number of channels
wavHeader.writeUInt32LE(sampleRate, 24); // Sample rate
wavHeader.writeUInt32LE((sampleRate * bitsPerSample * channelCount) / 8, 28); // Byte rate
wavHeader.writeUInt16LE((bitsPerSample * channelCount) / 8, 32); // Block align ((BitsPerSample * Channels) / 8)
wavHeader.writeUInt16LE(bitsPerSample, 34); // Bits per sample
wavHeader.write("data", 36); // Data chunk header
wavHeader.writeUInt32LE(audioLength, 40); // Data chunk size
return wavHeader;
}

export function prependWavHeader(
readable: Readable,
audioLength: number,
sampleRate: number,
channelCount = 1,
bitsPerSample = 16
): Readable {
const wavHeader = getWavHeader(
audioLength,
sampleRate,
channelCount,
bitsPerSample
);
let pushedHeader = false;
const passThrough = new PassThrough();
readable.on("data", (data) => {
if (!pushedHeader) {
passThrough.push(wavHeader);
pushedHeader = true;
}
passThrough.push(data);
});
readable.on("end", () => {
passThrough.end();
});
return passThrough;
}

0 comments on commit 6a9c76a

Please sign in to comment.