> ## Documentation Index
> Fetch the complete documentation index at: https://docs.vmonster.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Instance Methods

The `VmonsterRoom` instance provides a variety of methods to manage and control the interactions and functionalities within the virtual space.

These methods enable developers to automate tasks and enhance user engagement efficiently.

The following are the instance methods.

***

## join()

```ts theme={null}
async join(options: JoinOptions): Promise<void>
```

This method is used to connect to a `VmonsterRoom`.

If the method operates successfully, the `joined` event will be triggered.

#### Parameters

The config parameter accepts an object of type `JoinOptions.`

```typescript theme={null}
interface JoinOptions {
  sessionId: string;
  streamId: string;
  token: string;
  config?: SessionConfig;
}
```

**Error Occurrence**

* An error will be triggered if the method is called after the `joining` or `joined` events have already occurred.

***

## speak()

```ts theme={null}
async speak(
  config?: MessageConfig
): Promise<void>
```

This method requests the AI Avatar to speak based on the provided text or text stream. You can receive the AI Avatar's video stream data based on the input text. You can optionally configure the background, position, and scale of the AI Avatar for each request.
It supports standard text input as well as streaming input(AsyncIterable), allowing for real-time speech generation.

#### Parameters

The config parameter accepts an object of type `MessageConfig`, which is a union of the following: `TextMessageConfig` and `StreamMessageConfig`.

```ts theme={null}
// Standard text utterance
interface TextMessageConfig {
  text: string;
  isStream?: false;
  background?: File | Blob | null;
  positionX?: number | null;
  positionY?: number | null;
  scale?: number | null;
}
```

```ts theme={null}
// Streaming text utterance
interface StreamMessageConfig {
  isStream: true;
  stream: AsyncIterable<string>;
  background?: File | Blob | null;
  positionX?: number | null;
  positionY?: number | null;
  scale?: number | null;
}
```

When speak() is called, the following steps occur sequentially.

1. The `AIAvatarState` transitions to `loading`.

2. When the AIAvatar begins speaking:

   a. The `aiavatar-start-speaking` event is triggered.

   b. The `AIAvatarState` transitions to `speaking`.

3. When the AI Avatar finishes speaking:

   a. The `aiavatar-stop-speaking` event is triggered.

   b. The `AIAvatarState` transitions to `idle`.

4. The AIAvatar's speech text can be verified through the callback argument of the `aiavatar-message` event.

**Error Occurrence**

* The `RoomState` is not `joined` when the method is called.
* The `AIAvatarState` is not `idle` when the method is called.

***

## stopSpeaking()

```ts theme={null}
async stopSpeaking(): Promise<void>
```

This method is requests the AI Avatar to stop speaking

You can request to when the AI Avatar is speaking.

After the request, the speaking stops after 1 \~ 2 seconds.

* the `aiavatar-stop-speaking` event is triggered.
* The `AIAvatarState` transitions to idle.

**Error Occurrence:**

* The `AIAvatarState` is not speaking when the method is called.

***

## leave()

```ts theme={null}
leave(): void
```

This method is used to exit the `VmonsterRoom`.

Upon execution, the `left` event is triggered immediately.

**Error Occurrence**

* An error will be triggered if the method is called before the `joined` event has occurred.

***

## addVideo()

```ts theme={null}
addVideo(style?: Partial<CSSStyleDeclaration>): void;
```

This method adds an AI Avatar Video Element to the DOM.

It checks for the presence of a `<video>` element with the id "aiavatar-video" and adds it if it doesn't already exist.

**Parent Element Determination:**

* If an element with the id "aiavatar-video-parent" is found, the video element is added as a child of this element.
* If no such element exists, the video element is added as a child of document.body.

**Styling:**

* If a style is provided as input, the video element is added with the specified style applied.
* If no style is provided, the element is added with the following default styles applied.

```ts theme={null}
{
  width: "100%",
  height: "100%",
  objectFit: "cover",
  boxSizing: "border-box",
  aspectRatio: "calc(16 / 9)",
}
```

***

## removeVideo()

```ts theme={null}
removeVideo(): void
```

This method removes the AIAvatar Video Element from the DOM.

Note that even after the element is removed, the audio will continue to play.

***

## unmuteUserAudio()

```ts theme={null}
unmuteUserAudio(): void
```

Connects the user’s audio track. STT starts immediately.

## muteUserAudio()

```ts theme={null}
muteUserAudio(): void
```

This method mutes the user’s audio track.

The track remains connected, so STT can resume with minimal delay when the audio is unmuted again.

## unPublishUserAudio()

```ts theme={null}
unmuteUserAudio(): void
```

This method unpublishes the user’s audio track.

The track is disconnected, and STT can be restarted by `unmuteUserAudio()`.

***

## stt()

```ts theme={null}
async stt(audioBlob: Blob, language: LanguageType): Promise<string>
// LanguageType : "ko" | "en" | "zh" | "ja"
```

This method converts a voice Blob into text.

This converts audio to text in a **non-real-time manner**, separately from the user’s audio track connection.

**Error Occurrence**

* An error will be triggered if the method is called before the `joined` event has occurred.

***

## startRecordingAudio()

```ts theme={null}
startRecordingAudio(): void
```

This method initiates audio recording using navigator.mediaDevices.getUserMedia. Use this when calling the non-real-time stt() method.

**Error Occurrence:**

* The browser does not support getUserMedia.
* The browser does not support MediaRecorder.

***

## stopRecordingAudio()

```ts theme={null}
async stopRecordingAudio(): Promise<Blob>
```

This method stops the audio recording and returns the recorded audio as a Blob. Use this when calling the non-real-time stt() method.

**Error Occurrence:**

* An error will be triggered if the method is called when not currently recording.

***
