forked from HurroWorld/text-to-audio2face
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpcClient.js
66 lines (55 loc) · 2.16 KB
/
grpcClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Import required modules and classes
import grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';
import ffmpeg from 'fluent-ffmpeg';
import ffmpegPath from 'ffmpeg-static';
import { Transform } from 'stream';
// Configuration constants
const PROTO_PATH = "C:/Users/Dave/AppData/Local/ov/pkg/deps/b9070d65cb1908ec472cf47bc29f8126/exts/omni.audio2face.player/omni/audio2face/player/scripts/streaming_server/proto/audio2face.proto";
const A2F_INSTANCE_NAME = '/World/audio2face/PlayerStreaming_03';
const A2F_SERVER_ADDRESS = 'localhost:50051';
const audioBitrate = 24000;
// Set FFmpeg path
ffmpeg.setFfmpegPath(ffmpegPath);
// Load gRPC proto file for audio2face
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const audio2faceProto = grpc.loadPackageDefinition(packageDefinition).nvidia.audio2face;
// gRPC client for audio2face
const client = new audio2faceProto.Audio2Face(A2F_SERVER_ADDRESS, grpc.credentials.createInsecure());
// Setup the gRPC call for streaming audio
const call = client.PushAudioStream((error) => {
if (error) {
console.error('Error during PushAudioStream:', error);
}
});
// Write start marker to the gRPC call
call.write({
start_marker: {
instance_name: A2F_INSTANCE_NAME,
samplerate: audioBitrate,
block_until_playback_is_finished: true
}
});
// Function to stream audio to Audio2Face
export async function streamAudioToA2F(ttsStream) {
// Create an instance of AudioBufferToFloat32Transform
// const audioTransform = new AudioBufferToFloat32Transform();
// Handle data events for the audio transform
const int16Array = new Int16Array(ttsStream.buffer, ttsStream.byteOffset, ttsStream.length / Int16Array.BYTES_PER_ELEMENT);
const float32Array = new Float32Array(int16Array.length);
const gain = 2.0; // Adjust this value to change the amplification level
int16Array.forEach((int16, i) => {
float32Array[i] = (int16 / 32768.0) * gain;
});
call.write({ audio_data: Buffer.from(float32Array.buffer) });
// call.end();
}
export async function endCall() {
call.end();
}