Skip to content

Commit

Permalink
Merge pull request #2 from icywind/package_update
Browse files Browse the repository at this point in the history
Package update
  • Loading branch information
icywind authored Nov 15, 2024
2 parents 3c3c028 + 1344d6c commit 3bf676d
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 85 deletions.
19 changes: 19 additions & 0 deletions Assets/TEN/Controllers/AgoraAudioObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Agora.Rtc;

public class AgoraAudioObserver : IAudioFrameObserver
{
ISoundVisualizer _visual;
public AgoraAudioObserver(ISoundVisualizer visualizer)
{
_visual = visualizer;
}

public override bool OnPlaybackAudioFrameBeforeMixing(string channel_id,
uint uid,
AudioFrame audio_frame)
{
var floatArray = UtilFunctions.ConvertByteToFloat16(audio_frame.RawBuffer);
_visual?.UpdateVisualizer(floatArray);
return false;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Assets/TEN/Controllers/IChatTextDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using UnityEngine;
using Newtonsoft.Json;

using Agora.TEN.Server.Models;
namespace Agora.TEN.Client
{
public abstract class IChatTextDisplay : MonoBehaviour
Expand Down
8 changes: 8 additions & 0 deletions Assets/TEN/Controllers/ISoundVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityEngine;
using Agora.Rtc;

public abstract class ISoundVisualizer : MonoBehaviour
{
abstract public void UpdateVisualizer(float[] pcmData);
abstract public void Init(IRtcEngine RtcEngine);
}
11 changes: 11 additions & 0 deletions Assets/TEN/Controllers/ISoundVisualizer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions Assets/TEN/Controllers/SoundVisualizer.cs

This file was deleted.

15 changes: 13 additions & 2 deletions Assets/TEN/Controllers/SphereVisualizer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using Agora.Rtc;

public class SphereVisualizer : MonoBehaviour
public class SphereVisualizer : ISoundVisualizer
{
[SerializeField]
float sizeMultiplier = 10f; // Multiplier for sphere size
Expand All @@ -14,6 +15,9 @@ public class SphereVisualizer : MonoBehaviour
[SerializeField]
bool showRotationMotion;

public int CHANNEL = 1;
public int SAMPLE_RATE = 44100;

float[] _pcmData = new float[0];

private void Update()
Expand All @@ -28,8 +32,15 @@ private void Update()
}
}

public override void Init(IRtcEngine RtcEngine)
{
RtcEngine.SetPlaybackAudioFrameBeforeMixingParameters(SAMPLE_RATE, CHANNEL);
RtcEngine.RegisterAudioFrameObserver(new AgoraAudioObserver(this),
AUDIO_FRAME_POSITION.AUDIO_FRAME_POSITION_BEFORE_MIXING,
OBSERVER_MODE.RAW_DATA);
}

public void UpdateVisualizer(float[] pcmData)
public override void UpdateVisualizer(float[] pcmData)
{
_pcmData = pcmData;
}
Expand Down
34 changes: 7 additions & 27 deletions Assets/TEN/Scenes/TENDemoChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class TENDemoChat : MonoBehaviour
[SerializeField]
Button CamButton;

public int CHANNEL = 1;
public int SAMPLE_RATE = 44100;

void Start()
{
Expand Down Expand Up @@ -107,7 +105,7 @@ void SetUpUI()
#endif

// invisible until Agent joins
Visualizer.gameObject.SetActive(false);
Visualizer?.gameObject.SetActive(false);
}


Expand All @@ -131,11 +129,7 @@ void InitEngine()
RtcEngine.Initialize(context);
RtcEngine.InitEventHandler(handler);

RtcEngine.SetPlaybackAudioFrameBeforeMixingParameters(SAMPLE_RATE, CHANNEL);

RtcEngine.RegisterAudioFrameObserver(new AudioFrameObserver(this),
AUDIO_FRAME_POSITION.AUDIO_FRAME_POSITION_BEFORE_MIXING,
OBSERVER_MODE.RAW_DATA);
Visualizer?.Init(RtcEngine);
}

async void GetTokenAndJoin()
Expand Down Expand Up @@ -209,7 +203,7 @@ public override void OnUserJoined(RtcConnection connection, uint uid, int elapse
Debug.Log(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid,
elapsed));
if (uid == AppConfig.Shared.AgentUid) {
_app.Visualizer.gameObject.SetActive(true);
_app.Visualizer?.gameObject.SetActive(true);
}
}

Expand All @@ -219,10 +213,12 @@ public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFL
(int)reason));
}

public override void OnStreamMessage(RtcConnection connection, uint remoteUid, int streamId, byte[] data, ulong length, ulong sentTs)
/* SDK v4.2.6 */
// public override void OnStreamMessage(RtcConnection connection, uint remoteUid, int streamId, byte[] data, uint length, System.UInt64 sentTs)
/* SDK v4.4.0 or later */
public override void OnStreamMessage(RtcConnection connection, uint remoteUid, int streamId, byte[] data, ulong length, ulong sentTs)
{
string str = System.Text.Encoding.UTF8.GetString(data, 0, (int)length);
// Debug.Log($"StreamMessage from:{remoteUid} ---> " + str);
_app.TextDisplay.ProcessTextData(remoteUid, str);
_app.TextDisplay.DisplayChatMessages(_app.LogText.gameObject);
}
Expand All @@ -235,22 +231,6 @@ public override void OnRemoteAudioStateChanged(RtcConnection connection, uint re

#endregion

internal class AudioFrameObserver : IAudioFrameObserver
{
TENDemoChat _app;
internal AudioFrameObserver(TENDemoChat client)
{
_app = client;
}

public override bool OnPlaybackAudioFrameBeforeMixing(string channel_id,
uint uid,
AudioFrame audio_frame)
{
var floatArray = UtilFunctions.ConvertByteToFloat16(audio_frame.RawBuffer);
_app.Visualizer?.UpdateVisualizer(floatArray);
return false;
}
}

}
4 changes: 2 additions & 2 deletions Assets/TEN/TENConfigInput.asset
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ MonoBehaviour:
AgentUid: 1234
AgoraAsrLanguage: en-US
Graph: camera_va_openai_azure
AzureVoice: 3
AzureVoice: 1
AltVoiceName:
ServerBaseURL: http://localhost:8080
AgentOpeningGreeting: TEN agent connected. Happy to chat with you today.
AgentOpeningGreeting: TEN agent connected on Unity. Happy to chat with you today.
AgentVisionCheckingWords: '["Let me take a look...","Let me check your camera...","Please
wait for a second..."]'
3 changes: 2 additions & 1 deletion Assets/TEN/TENConfigInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class TENConfigInput : ScriptableObject
public AzureVoiceType AzureVoice;

[SerializeField]
/// if it is not the provided Azure choice, enter the actual voice name here
[Tooltip("if the voice is not provided by Azure, enter the vendor's voice name here")]
/// Alternative voice override
public string AltVoiceName;

[SerializeField]
Expand Down
67 changes: 33 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The Plugin is exported as a reusable package that can be imported on any Agora R
## Prerequisites:

- Agora Developer account
- Agora Video SDK for Unity (v4.4.0 or up)
- Agora Video SDK for Unity (v4.2.6 or up)
- [TEN Frameworks Agent](https://github.com/TEN-framework/TEN-Agent)
- Unity 2021 or up

Expand Down Expand Up @@ -77,13 +77,13 @@ void SetConfig()
{
AppConfig.Shared.SetValue(TENConfig);
// obtain channel name before this call
AppConfig.Shared.Channel = _channelName;
AppConfig.Shared.Channel = UtilFunctions.GenRandomString("agora_", 5);
}
```
Note you should provide a channel name in your app logic to use among the participanting users. In this 1-to-1 chat project, we provide an util function to ganerate a string:
```csharp
_channelName = AppConfig.Shared.Channel = UtilFunctions.GenRandomString("agora_", 5);
```
Note you should provide a channel name in your app logic to use among the participanting users. In this 1-to-1 chat project, we provide an util function GenRandomString() to ganerate the channel name.

Don't forget to drop the TENConfigInput Asset into Inspector field for TENConfig. See demo picture in the previous section.


2. Hook the prefabs up in your main logic:

Expand All @@ -105,45 +105,35 @@ async void GetTokenAndJoin()
}
```

4. Setup for Audio display before Mixing in InitEngine()
4. In your initialization step, setup sound visualization, which will automatically configure the Agora RTC engine for audio data capture.

```csharp
int CHANNEL = 1;
int SAMPLE_RATE = 44100;
RtcEngine.SetPlaybackAudioFrameBeforeMixingParameters(SAMPLE_RATE, CHANNEL);
RtcEngine.RegisterAudioFrameObserver(new AudioFrameObserver(this),
AUDIO_FRAME_POSITION.AUDIO_FRAME_POSITION_BEFORE_MIXING,OBSERVER_MODE.RAW_DATA);
Visualizer?.Init(RtcEngine);
```

5. Implement AudioFrameObserver:
5. Start the TEN session on OnJoinChannelSuccess()
```csharp
internal class AudioFrameObserver : IAudioFrameObserver
{
TENDemoChat _app; // Replace TENDemo with your controller class name
internal AudioFrameObserver(TENDemoChat client)
{
_app = client;
}
}

public override bool OnPlaybackAudioFrameBeforeMixing(string channel_id, uint uid,AudioFrame audio_frame)
{
var floatArray = UtilFunctions.ConvertByteToFloat16(audio_frame.RawBuffer);
_app.Visualizer?.UpdateVisualizer(floatArray);
return false;
}
// _app is the instance of your controller class
_app.TENSession.StartSession(connection.localUid);
```




6. Add the following to OnJoinChannelSuccess()
6. Disable/Enable the sound visualizer, a component of a gameobject that represents the AI Agent:
- Disable it during initialization, e.g. SetupUI() or Start()
```csharp
// _app is the instance of your controller class
_app.TENSession.StartSession(connection.localUid);
Visualizer?.gameObject.SetActive(false);
```
- Enable it when the AI Agent user joins the channel:
```csharp
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
{
if (uid == AppConfig.Shared.AgentUid) {
_app.Visualizer?.gameObject.SetActive(true);
}
}
```

7. Register handler OnStreamMessage:
- SDK ver 4.4.0
```csharp
public override void OnStreamMessage(RtcConnection connection, uint remoteUid, int streamId, byte[] data, ulong length, ulong sentTs)
{
Expand All @@ -153,7 +143,16 @@ public override void OnStreamMessage(RtcConnection connection, uint remoteU
}

```
- SDK ver 4.2.6
```csharp
public override void OnStreamMessage(RtcConnection connection, uint remoteUid, int streamId, byte[] data, uint length, System.UInt64 sentTs)
{
string str = System.Text.Encoding.UTF8.GetString(data, 0, (int)length);
_app.TextDisplay.ProcessTextData(remoteUid, str);
_app.TextDisplay.DisplayChatMessages(_app.LogText.gameObject);
}

```
8. OnDestroy() or logic to stop.

```csharp
Expand Down

0 comments on commit 3bf676d

Please sign in to comment.