@4players/odin-nodejs 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,8 @@ Napi::Object OdinClient::Init(Napi::Env env, Napi::Object exports) {
12
12
  // This method is used to hook the accessor and method callbacks
13
13
  Napi::Function func = DefineClass(env, "OdinClient", {
14
14
  InstanceMethod<&OdinClient::GenerateAccessToken>("generateAccessToken", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
15
- InstanceMethod<&OdinClient::CreateRoom>("createRoom", static_cast<napi_property_attributes>(napi_writable | napi_configurable))
15
+ InstanceMethod<&OdinClient::CreateRoom>("createRoom", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
16
+ InstanceMethod<&OdinClient::CreateRoomWithAccessToken>("createRoomWithAccessToken", static_cast<napi_property_attributes>(napi_writable | napi_configurable))
16
17
  });
17
18
 
18
19
  Napi::FunctionReference* constructor = new Napi::FunctionReference();
@@ -44,27 +45,21 @@ OdinClient::OdinClient(const Napi::CallbackInfo& info) : Napi::ObjectWrap<OdinCl
44
45
  Napi::Env env = info.Env();
45
46
  Napi::HandleScope scope(env);
46
47
 
47
- if (info.Length() < 1 || !info[0].IsString()) {
48
- Napi::TypeError::New(env, "Access Key required as first parameter").ThrowAsJavaScriptException();
49
- return;
50
- }
51
-
52
- _accessKey = info[0].ToString().Utf8Value();
53
48
  _sampleRate = 48000;
54
49
  _numChannels = 1;
55
50
 
56
- if (info.Length() >= 2) {
57
- if (info[1].IsNumber()) {
58
- _sampleRate = info[1].ToNumber();
51
+ if (info.Length() >= 1) {
52
+ if (info[0].IsNumber()) {
53
+ _sampleRate = info[0].ToNumber();
59
54
  } else {
60
55
  Napi::TypeError::New(env, "Sample rate must be a number").ThrowAsJavaScriptException();
61
56
  return;
62
57
  }
63
58
  }
64
59
 
65
- if (info.Length() >= 3) {
66
- if (info[2].IsNumber()) {
67
- _numChannels = info[2].ToNumber();
60
+ if (info.Length() >= 2) {
61
+ if (info[1].IsNumber()) {
62
+ _numChannels = info[1].ToNumber();
68
63
  } else {
69
64
  Napi::TypeError::New(env, "Channels must be a number").ThrowAsJavaScriptException();
70
65
  return;
@@ -87,7 +82,6 @@ OdinClient::OdinClient(const Napi::CallbackInfo& info) : Napi::ObjectWrap<OdinCl
87
82
  * OdinClient destructor
88
83
  */
89
84
  void OdinClient::Finalize(Napi::Env env) {
90
-
91
85
  }
92
86
 
93
87
  /**
@@ -99,15 +93,16 @@ Napi::Value OdinClient::GenerateAccessToken(const Napi::CallbackInfo &info)
99
93
  {
100
94
  Napi::Env env = info.Env();
101
95
 
102
- if (info.Length() != 2 || !info[0].IsString() || !info[1].IsString()) {
103
- Napi::TypeError::New(env, "Room and User Id expected").ThrowAsJavaScriptException();
96
+ if (info.Length() != 3 || !info[0].IsString() || !info[1].IsString() || !info[2].IsString()) {
97
+ Napi::TypeError::New(env, "Access-Key, Room and User Id expected").ThrowAsJavaScriptException();
104
98
  return env.Undefined();
105
99
  }
106
100
 
107
- std::string roomId = info[0].ToString().Utf8Value();
108
- std::string userId = info[1].ToString().Utf8Value();
101
+ std::string accessKey = info[0].ToString().Utf8Value();
102
+ std::string roomId = info[1].ToString().Utf8Value();
103
+ std::string userId = info[2].ToString().Utf8Value();
109
104
 
110
- OdinTokenGenerator *generator = odin_token_generator_create(_accessKey.c_str());
105
+ OdinTokenGenerator *generator = odin_token_generator_create(accessKey.c_str());
111
106
  if (!generator)
112
107
  {
113
108
  Napi::TypeError::New(env, "Failed to initialize token generator, invalid access key").ThrowAsJavaScriptException();
@@ -141,15 +136,16 @@ Napi::Value OdinClient::GenerateAccessToken(const Napi::CallbackInfo &info)
141
136
  Napi::Value OdinClient::CreateRoom(const Napi::CallbackInfo &info) {
142
137
  Napi::Env env = info.Env();
143
138
 
144
- if (info.Length() != 2 || !info[0].IsString() || !info[1].IsString()) {
145
- Napi::TypeError::New(env, "Room and User Id expected").ThrowAsJavaScriptException();
139
+ if (info.Length() != 3 || !info[0].IsString() || !info[1].IsString() || !info[2].IsString()) {
140
+ Napi::TypeError::New(env, "Access-Key, Room and User Id expected").ThrowAsJavaScriptException();
146
141
  return env.Undefined();
147
142
  }
148
143
 
149
- std::string roomId = info[0].ToString().Utf8Value();
150
- std::string userId = info[1].ToString().Utf8Value();
144
+ std::string accessKey = info[0].ToString().Utf8Value();
145
+ std::string roomId = info[1].ToString().Utf8Value();
146
+ std::string userId = info[2].ToString().Utf8Value();
151
147
 
152
- OdinTokenGenerator *generator = odin_token_generator_create(_accessKey.c_str());
148
+ OdinTokenGenerator *generator = odin_token_generator_create(accessKey.c_str());
153
149
  if (!generator)
154
150
  {
155
151
  Napi::TypeError::New(env, "Failed to initialize token generator, invalid access key").ThrowAsJavaScriptException();
@@ -176,3 +172,23 @@ Napi::Value OdinClient::CreateRoom(const Napi::CallbackInfo &info) {
176
172
 
177
173
  return OdinRoom::NewInstance(Napi::String::New(env, room_token));
178
174
  }
175
+
176
+
177
+ /**
178
+ * Create a new room with a given access token
179
+ * @param info
180
+ * @return
181
+ */
182
+ Napi::Value OdinClient::CreateRoomWithAccessToken(const Napi::CallbackInfo &info) {
183
+ Napi::Env env = info.Env();
184
+
185
+ // Check for existence of access token
186
+ if (info.Length() < 1 || !info[0].IsString()) {
187
+ Napi::TypeError::New(env, "Access token expected").ThrowAsJavaScriptException();
188
+ return env.Undefined();
189
+ }
190
+
191
+ std::string token = info[0].ToString().Utf8Value();
192
+
193
+ return OdinRoom::NewInstance(Napi::String::New(env, token));
194
+ }
@@ -9,9 +9,8 @@ class OdinClient : public Napi::ObjectWrap<OdinClient> {
9
9
  private:
10
10
  Napi::Value GenerateAccessToken(const Napi::CallbackInfo& info);
11
11
  Napi::Value CreateRoom(const Napi::CallbackInfo& info);
12
+ Napi::Value CreateRoomWithAccessToken(const Napi::CallbackInfo& info);
12
13
 
13
14
  float _sampleRate;
14
15
  uint32_t _numChannels;
15
-
16
- std::string _accessKey;
17
16
  };
package/odin.client.d.ts CHANGED
@@ -6,17 +6,33 @@ import {OdinRoom} from "./odin.room";
6
6
  */
7
7
  export declare class OdinClient {
8
8
  /**
9
- * Creates a new instance of a client with an access key.
10
- * @param accessKey - The access key to use for this client. You can get one from https://developers.4players.io
11
- * @param sampleRate - The sample rate of the audio stream (between 8000 and 48000)
12
- * @param channelCount - The number of channels of the audio stream (1 or 2)
9
+ * Creates a new instance of a client. Use this to create rooms.
10
+ * @param sampleRate - The sample rate of the audio stream (between 8000 and 48000) - default is 48000
11
+ * @param channelCount - The number of channels of the audio stream (1 or 2) - default is 1
13
12
  */
14
- constructor(accessKey: string, sampleRate?: number, channelCount?: number);
13
+ constructor(sampleRate?: number, channelCount?: number);
14
+
15
+ /**
16
+ * Generates an access token for the given access key, room and user ID. This token can be used to join the room.
17
+ * @param accessKey - The access key to use to generate the token. You can get a free access token in our developer center
18
+ * @param roomId - The ID of the room to generate the token for.
19
+ * @param userId - The ID of the user to generate the token for.
20
+ */
21
+ generateAccessToken(accessKey: string, roomId: string, userId: string): string;
15
22
 
16
23
  /**
17
24
  * Creates a new local room instance with the given ID and user ID. Use join to connect to that room.
25
+ * @param accessKey - The access key to use to generate the token. You can get a free access token in our developer center
18
26
  * @param roomId - The ID of the room to create.
19
27
  * @param userId - The ID of the user to create the room for.
20
28
  */
21
- createRoom(roomId: string, userId: string): OdinRoom;
29
+ createRoom(accessKey: string, roomId: string, userId: string): OdinRoom;
30
+
31
+ /**
32
+ * Creates a new local room instance with the given access token. Use `join` on the returned OdinRoom instance to
33
+ * connect to that room. Use this method if you already have an access token, either created elsewhere or by calling
34
+ * `generateAccessToken`.
35
+ * @param accessToken - The access token to use to join the room.
36
+ */
37
+ createRoomWithAccessToken(accessToken: string): OdinRoom;
22
38
  }
package/odin.media.d.ts CHANGED
@@ -19,6 +19,20 @@ export declare class OdinMedia {
19
19
  */
20
20
  close(): void;
21
21
 
22
+ /**
23
+ * Instructs the server to pause the media object, ceasing the reception of
24
+ * data. This operation essentially communicates a server-side mute request from the client, thus
25
+ * indicating a desire to halt packet reception for this media stream.
26
+ */
27
+ pause(): void;
28
+
29
+ /**
30
+ * Instructs the server to resume the media object, resuming the reception of
31
+ * data. This operation essentially communicates a server-side unmute request from the client, thus
32
+ * indicating a desire to resume packet reception for this media stream.
33
+ */
34
+ resume(): void;
35
+
22
36
  /**
23
37
  * Sends audio data to the room. The data must be in the format specified when creating the media as a 32-bit float array.
24
38
  * Samples need to be between -1 and 1. Audio data needs to be sent in regular intervals, otherwise the audio will be sound
package/odin.room.d.ts CHANGED
@@ -402,16 +402,17 @@ export declare interface OdinAPMSettings {
402
402
  export declare class OdinRoom {
403
403
  /**
404
404
  * Creates a new instance of a room with a token. Use OdinClient if you don't want to manage tokens yourself.
405
+ * Important: Don't use this function directly, use OdinClient instead.
405
406
  * @param token - The token to use for this room.
406
407
  */
407
408
  constructor(token: string);
408
409
 
409
410
  /**
410
411
  * Joins the room with the given gateway URL and optional user data.
411
- * @param gatewayUrl - The gateway URL to connect to. Use gateway.odin.4players.io if you are unsure.
412
+ * @param gatewayUrlOrServer - The gateway URL to connect to or the URL to the ODIN Server instance. Use gateway.odin.4players.io if you are unsure.
412
413
  * @param userData - The user data to send to the room. This can be used to identify the user.
413
414
  */
414
- join(gatewayUrl: string, userData?: Uint8Array): void;
415
+ join(gatewayOrServerUrl: string, userData?: Uint8Array): void;
415
416
 
416
417
  /**
417
418
  * Sends a message to the room.
@@ -471,7 +472,7 @@ export declare class OdinRoom {
471
472
  * Note: It's crucial to maintain consistent scaling across all client applications.
472
473
  * @param scale - The new scaling factor to use.
473
474
  */
474
- setPositionScale(scale: number);
475
+ setPositionScale(scale: number): void;
475
476
 
476
477
  /**
477
478
  * Closes the room and disconnects from the server.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4players/odin-nodejs",
3
- "version": "0.9.1",
3
+ "version": "0.10.0",
4
4
  "description": "NodeJS bindings for the ODIN SDK. Use for AI enhanced human interactions, content moderation and audio processing features in a backend.",
5
5
  "main": "index.cjs",
6
6
  "types": "index.d.ts",
Binary file
@@ -1,4 +1,4 @@
1
- const accessKey = "__YOUR_ACCESS_KEY__";
1
+ const accessKey = "ASBKPRwUpEabybkUJMfdLD2b2J8K2mus1ekxMUK7VPEA";
2
2
  const roomName = "Lobby";
3
3
  const userName = "My Bot";
4
4
 
@@ -21,8 +21,14 @@ const userData = {
21
21
  version: "0.1"
22
22
  }
23
23
  const data = new TextEncoder().encode(JSON.stringify(userData));
24
- const odinClient = new OdinClient(accessKey, 48000, 2);
25
- const room = odinClient.createRoom(roomName, userName);
24
+ const odinClient = new OdinClient(48000, 2);
25
+
26
+ // Create a token then join the room with that token, token may also be created elsewhere
27
+ const token = odinClient.generateAccessToken(accessKey, roomName, userName);
28
+ const room = odinClient.createRoomWithAccessToken(token);
29
+
30
+ // It's also possible to create a room with an access key, room name and username then token is created internally
31
+ //const room = odinClient.createRoom(accessKey, roomName, userName);
26
32
 
27
33
  // Join the room
28
34
  room.join("gateway.odin.4players.io", data);
@@ -73,6 +79,9 @@ const sendMusic = async (media) => {
73
79
  audioBufferStream.write(audioBuffer);
74
80
  }
75
81
 
82
+ room.setPositionScale(25);
83
+ room.updatePosition(0, 0, 0);
84
+
76
85
  // Create a media stream in the room - it will return an OdinMedia instance that we can use to send data to ODIN
77
86
  const media = room.createAudioStream(48000, 2);
78
87
  console.log(media);