Hey iam facing issue while retirving the audio chu...
# ask-ai
v
Hey iam facing issue while retirving the audio chucnks from live kit,could somebody assist me
t
Thanks for reaching out, Akshay. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To retrieve audio chunks from LiveKit, you can use the
ByteStreamReader
class provided by the LiveKit RTC SDK. Here's a step-by-step guide: 1. Use
ByteStreamReader
to Read Chunks:
Copy code
// Create a ByteStreamReader instance
const streamReader = new ByteStreamReader(info, readableStream);

// Use async iteration to process chunks
for await (const chunk of streamReader) {
  processChunk(chunk);
}

// Alternatively, read all chunks at once
const allChunks = await streamReader.readAll();
2. Subscribe to Audio Tracks:
Copy code
// Subscribe to audio tracks in a room
room.on(RoomEvent.TrackSubscribed, async (track, publication, participant) => {
  if (track.kind === TrackKind.KIND_AUDIO) {
    const stream = new AudioStream(track);
    const reader = new ByteStreamReader(info, stream);

    for await (const chunk of reader) {
      processChunk(chunk);
    }
  }
});
3. Handle Different MIME Types:
Copy code
// Check if the stream is Opus encoded
if (stream.info.mime_type && stream.info.mime_type.includes("opus")) {
  const decoder = new AudioStreamDecoder(sampleRate, numChannels);
  for await (const chunk of stream) {
    decoder.push(chunk);
  }
  decoder.end_input();
  for await (const frame of decoder) {
    processFrame(frame);
  }
} else {
  for await (const chunk of stream) {
    processChunk(chunk);
  }
}
Let me know if you need further assistance with implementing these steps! Sources: examples/receive-audio/index.ts
v
i need to save my convesration and tts to .wav and .txt in local
t
Hey there! If you want to save your conversation and TTS output as
.wav
and
.txt
files on your computer, here’s a simple way to do it: 1. Save Audio to a
.wav
File
You can use the
WavWriter
class to save your audio streams like this:
Copy code
// Example in Rust
let header = WavHeader {
    sample_rate: 48000,
    bit_depth: 16,
    num_channels: 2,
};

let mut wav_writer = WavWriter::create("output.wav", header).await?;

// Write audio samples
wav_writer.write_sample(sample).await?;

// Finalize the file
wav_writer.finalize().await?;
This will create a nicely formatted WAV file with your audio data ready to go. 2. Save Conversation to a
.txt
File
To save your conversation transcripts, here’s a handy Python snippet:
Copy code
import json
from datetime import datetime

def save_conversation_to_file(conversation_history):
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    filename = f"conversation_{timestamp}.txt"

    with open(filename, 'w') as f:
        for message in conversation_history:
            f.write(f"{message['timestamp']}: {message['speaker']}: {message['text']}\n")

    print(f"Conversation saved to {filename}")
Just listen for events like
conversation_item_added
to catch the conversation as it happens and save the text on the fly. 3. Putting It All Together • Use
WavWriter
to save your audio streams into a
.wav
file. • Capture the conversation text and save it into a
.txt
file using event listeners. If you want, I’m here to help you step-by-step with setting this up — just let me know! Sources: examples/save_to_disk/src/main.rs | Recording agent sessions | LiveKit Docs
v
yes i need step by step with full agent config in python
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!