ai.muna.muna 0.0.45 → 0.0.47

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.
Files changed (33) hide show
  1. package/Editor/Build/AndroidBuildHandler.cs +3 -3
  2. package/Editor/Build/iOSBuildHandler.cs +4 -4
  3. package/Editor/Build/macOSBuildHandler.cs +6 -6
  4. package/Editor/MunaMenu.cs +9 -11
  5. package/Plugins/Android/Muna.aar +0 -0
  6. package/Plugins/macOS/Function.dylib.meta +26 -25
  7. package/README.md +1 -1
  8. package/Runtime/Beta/OpenAI/SpeechService.cs +6 -0
  9. package/Runtime/Beta/OpenAI/Types.cs +0 -1
  10. package/Runtime/Beta/Remote/RemotePredictionService.cs +43 -42
  11. package/Runtime/Beta/{Remote → Types}/Value.cs +2 -2
  12. package/{Unity → Runtime/Beta}/Types.meta +1 -1
  13. package/Runtime/Muna.cs +1 -1
  14. package/Unity/API/PredictionCacheClient.cs +44 -51
  15. package/Unity/Converters/Color.cs +46 -0
  16. package/Unity/Converters/Color.cs.meta +2 -0
  17. package/Unity/Converters/Rect.cs +230 -0
  18. package/Unity/Converters/Rect.cs.meta +2 -0
  19. package/Unity/Converters/Vector2.cs +44 -0
  20. package/Unity/Converters/Vector2.cs.meta +2 -0
  21. package/Unity/Converters/Vector3.cs +45 -0
  22. package/Unity/Converters/Vector3.cs.meta +2 -0
  23. package/Unity/Converters/Vector4.cs +46 -0
  24. package/Unity/Converters/Vector4.cs.meta +2 -0
  25. package/Unity/Converters.meta +8 -0
  26. package/Unity/Internal/MunaSettings.cs +1 -1
  27. package/Unity/Internal/PredictionCache.cs +160 -0
  28. package/Unity/Internal/PredictionCache.cs.meta +2 -0
  29. package/Unity/MunaUnity.cs +68 -20
  30. package/package.json +1 -1
  31. package/Unity/Types/CachedPrediction.cs +0 -36
  32. package/Unity/Types/CachedPrediction.cs.meta +0 -11
  33. /package/Runtime/Beta/{Remote → Types}/Value.cs.meta +0 -0
@@ -20,9 +20,11 @@ namespace Muna {
20
20
 
21
21
  using System;
22
22
  using System.Collections.Generic;
23
+ using System.Text.RegularExpressions;
23
24
  using UnityEngine;
24
25
  using Unity.Collections.LowLevel.Unsafe;
25
26
  using API;
27
+ using Beta.OpenAI;
26
28
  using Internal;
27
29
 
28
30
  /// <summary>
@@ -44,8 +46,7 @@ namespace Muna {
44
46
  var settings = MunaSettings.Instance!;
45
47
  var client = new PredictionCacheClient(
46
48
  url ?? Muna.URL,
47
- accessKey: accessKey ?? settings?.accessKey,
48
- cache: settings?.cache
49
+ accessKey: accessKey ?? settings?.accessKey
49
50
  );
50
51
  return new Muna(client);
51
52
  }
@@ -61,34 +62,34 @@ namespace Muna {
61
62
  this Texture2D texture,
62
63
  byte[]? pixelBuffer = null
63
64
  ) {
65
+ // Check texture
64
66
  if (texture == null)
65
67
  throw new ArgumentNullException(nameof(texture));
68
+ // Check that texture is readable
66
69
  if (!texture.isReadable)
67
70
  throw new InvalidOperationException(@"Texture cannot be converted to a Muna image because it is not readable");
68
- var FormatChannelMap = new Dictionary<TextureFormat, int> {
69
- [TextureFormat.R8] = 1,
70
- [TextureFormat.Alpha8] = 1,
71
- [TextureFormat.RGB24] = 3,
72
- [TextureFormat.RGBA32] = 4,
73
- };
74
- if (!FormatChannelMap.TryGetValue(texture.format, out var channels))
75
- throw new InvalidOperationException($"Texture cannot be converted to a Muna image because it has unsupported format: {texture.format}");
71
+ // Allocate buffer
72
+ var channels = TextureFormatToImageChannels.GetValueOrDefault(texture.format, 4);
76
73
  var rowStride = texture.width * channels;
77
74
  var bufferSize = rowStride * texture.height;
78
75
  pixelBuffer ??= new byte[bufferSize];
79
76
  if (pixelBuffer.Length < bufferSize)
80
77
  throw new InvalidOperationException($"Texture cannot be converted to a Muna image because pixel buffer length was expected to be greater than or equal to {bufferSize} but got {pixelBuffer.Length}");
81
- fixed (void* dst = pixelBuffer)
78
+ // Copy
79
+ var colorData = !TextureFormatToImageChannels.ContainsKey(texture.format) ? texture.GetPixels32() : null;
80
+ fixed (void* dst = pixelBuffer, colors = colorData) {
81
+ var src = colors == null ? texture.GetRawTextureData<byte>().GetUnsafePtr() : colors;
82
82
  UnsafeUtility.MemCpyStride(
83
83
  dst,
84
84
  rowStride,
85
- (byte*)texture.GetRawTextureData<byte>().GetUnsafePtr() + (rowStride * (texture.height - 1)),
85
+ (byte*)src + (rowStride * (texture.height - 1)),
86
86
  -rowStride,
87
87
  rowStride,
88
88
  texture.height
89
89
  );
90
- var image = new Image(pixelBuffer, texture.width, texture.height, channels);
91
- return image;
90
+ }
91
+ // Return
92
+ return new Image(pixelBuffer, texture.width, texture.height, channels);
92
93
  }
93
94
 
94
95
  /// <summary>
@@ -101,12 +102,7 @@ namespace Muna {
101
102
  this Image image,
102
103
  Texture2D? texture = null
103
104
  ) {
104
- var ChannelFormatMap = new Dictionary<int, TextureFormat> {
105
- [1] = TextureFormat.Alpha8,
106
- [3] = TextureFormat.RGB24,
107
- [4] = TextureFormat.RGBA32
108
- };
109
- if (!ChannelFormatMap.TryGetValue(image.channels, out var format))
105
+ if (!ImageChannelsToTextureFormat.TryGetValue(image.channels, out var format))
110
106
  throw new InvalidOperationException($"Image cannot be converted to a Texture2D because it has unsupported channel count: {image.channels}");
111
107
  texture = texture != null ? texture : new Texture2D(image.width, image.height, format, false);
112
108
  if (texture.width != image.width || texture.height != image.height || texture.format != format)
@@ -124,6 +120,58 @@ namespace Muna {
124
120
  texture.Apply();
125
121
  return texture;
126
122
  }
123
+
124
+ /// <summary>
125
+ /// Convert a `BinaryData` containing linear PCM audio into an audio clip.
126
+ /// </summary>
127
+ /// <param name="data">Binary data containing linear PCM audio.</param>
128
+ /// <returns>Audio clip.</returns>
129
+ public static unsafe AudioClip ToAudioClip(this BinaryData data) {
130
+ // Check that this contains LPCM data
131
+ if (string.IsNullOrEmpty(data.MediaType) || !data.MediaType.StartsWith(@"audio/pcm"))
132
+ throw new ArgumentException($"Failed to create audio clip from binary data because media type was expected to be 'audio/pcm' but got: '{data.MediaType}'");
133
+ // Match sample rate and channel count
134
+ var rateMatch = Regex.Match(data.MediaType, @"rate=(\d+)");
135
+ var channelsMatch = Regex.Match(data.MediaType, @"channels=(\d+)");
136
+ if (!rateMatch.Success || !channelsMatch.Success)
137
+ throw new ArgumentException($"Failed to create audio clip from binary data because media type is invalid: '{data.MediaType}'");
138
+ // Parse
139
+ if (!int.TryParse(rateMatch.Groups[1].Value, out var sampleRate))
140
+ throw new ArgumentException($"Failed to create audio clip from binary data because sample rate is invalid: '{rateMatch.Value}'");
141
+ if (!int.TryParse(channelsMatch.Groups[1].Value, out var channelCount))
142
+ throw new ArgumentException($"Failed to create audio clip from binary data because channel count is invalid: '{channelsMatch.Value}'");
143
+ // Create clip
144
+ var sampleCount = data.Length / sizeof(float);
145
+ var frameCount = sampleCount / channelCount;
146
+ var clip = AudioClip.Create(
147
+ "audio",
148
+ lengthSamples: frameCount,
149
+ channels: channelCount,
150
+ frequency: sampleRate,
151
+ stream: false
152
+ );
153
+ // Copy data
154
+ var samples = new float[sampleCount];
155
+ Buffer.BlockCopy(data.ToArray(), 0, samples, 0, data.Length);
156
+ clip.SetData(samples, 0);
157
+ // Return
158
+ return clip;
159
+ }
160
+ #endregion
161
+
162
+
163
+ #region --Operations--
164
+ private static readonly Dictionary<TextureFormat, int> TextureFormatToImageChannels = new() {
165
+ [TextureFormat.R8] = 1,
166
+ [TextureFormat.Alpha8] = 1,
167
+ [TextureFormat.RGB24] = 3,
168
+ [TextureFormat.RGBA32] = 4,
169
+ };
170
+ private static readonly Dictionary<int, TextureFormat> ImageChannelsToTextureFormat = new() {
171
+ [1] = TextureFormat.Alpha8,
172
+ [3] = TextureFormat.RGB24,
173
+ [4] = TextureFormat.RGBA32,
174
+ };
127
175
  #endregion
128
176
  }
129
177
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai.muna.muna",
3
- "version": "0.0.45",
3
+ "version": "0.0.47",
4
4
  "displayName": "Muna",
5
5
  "description": "Run AI inference in Unity Engine.",
6
6
  "unity": "2022.3",
@@ -1,36 +0,0 @@
1
- /*
2
- * Muna
3
- * Copyright © 2025 NatML Inc. All rights reserved.
4
- */
5
-
6
- #nullable enable
7
-
8
- namespace Muna {
9
-
10
- using System;
11
-
12
- /// <summary>
13
- /// Cached prediction.
14
- /// </summary>
15
- [Preserve, Serializable]
16
- internal class CachedPrediction : Prediction {
17
-
18
- public string? clientId;
19
-
20
- [Preserve]
21
- public CachedPrediction() { }
22
-
23
- public CachedPrediction(Prediction prediction, string clientId) {
24
- this.id = prediction.id;
25
- this.tag = prediction.tag;
26
- this.created = prediction.created;
27
- this.results = prediction.results;
28
- this.latency = prediction.latency;
29
- this.error = prediction.error;
30
- this.logs = prediction.logs;
31
- this.resources = prediction.resources;
32
- this.configuration = prediction.configuration;
33
- this.clientId = clientId;
34
- }
35
- }
36
- }
@@ -1,11 +0,0 @@
1
- fileFormatVersion: 2
2
- guid: 7b5e913e3117a442abc1345c156f136d
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
File without changes