ai.muna.muna 0.0.44 → 0.0.46

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 (50) hide show
  1. package/Editor/MunaMenu.cs +17 -7
  2. package/Plugins/Android/Muna.aar +0 -0
  3. package/Plugins/macOS/Function.dylib.meta +26 -25
  4. package/README.md +1 -1
  5. package/Runtime/API/DotNetClient.cs +0 -3
  6. package/Runtime/Beta/BetaClient.cs +14 -1
  7. package/Runtime/Beta/OpenAI/AudioService.cs +38 -0
  8. package/Runtime/Beta/OpenAI/AudioService.cs.meta +11 -0
  9. package/Runtime/Beta/OpenAI/ChatService.cs +38 -0
  10. package/Runtime/Beta/OpenAI/ChatService.cs.meta +11 -0
  11. package/Runtime/Beta/OpenAI/CompletionService.cs +117 -0
  12. package/Runtime/Beta/OpenAI/CompletionService.cs.meta +11 -0
  13. package/Runtime/Beta/OpenAI/EmbeddingService.cs +252 -0
  14. package/Runtime/Beta/OpenAI/EmbeddingService.cs.meta +11 -0
  15. package/Runtime/Beta/OpenAI/OpenAIClient.cs +50 -0
  16. package/Runtime/Beta/OpenAI/OpenAIClient.cs.meta +11 -0
  17. package/Runtime/Beta/OpenAI/SpeechService.cs +256 -0
  18. package/Runtime/Beta/OpenAI/SpeechService.cs.meta +11 -0
  19. package/Runtime/Beta/OpenAI/Types.cs +364 -0
  20. package/Runtime/Beta/OpenAI/Types.cs.meta +11 -0
  21. package/Runtime/Beta/OpenAI.meta +8 -0
  22. package/Runtime/Beta/Remote/RemotePredictionService.cs +50 -70
  23. package/Runtime/Beta/{Value.cs → Types/Value.cs} +3 -4
  24. package/Runtime/Beta/Types.meta +8 -0
  25. package/Runtime/C/Configuration.cs +1 -1
  26. package/Runtime/C/Function.cs +1 -1
  27. package/Runtime/C/Prediction.cs +1 -1
  28. package/Runtime/C/PredictionStream.cs +1 -1
  29. package/Runtime/C/Predictor.cs +1 -1
  30. package/Runtime/C/Value.cs +3 -2
  31. package/Runtime/C/ValueMap.cs +1 -1
  32. package/Runtime/Muna.cs +2 -2
  33. package/Runtime/Types/Parameter.cs +75 -0
  34. package/Runtime/Types/Parameter.cs.meta +11 -0
  35. package/Runtime/Types/Predictor.cs +0 -53
  36. package/Unity/API/PredictionCacheClient.cs +1 -1
  37. package/Unity/Converters/Color.cs +46 -0
  38. package/Unity/Converters/Color.cs.meta +2 -0
  39. package/Unity/Converters/Rect.cs +230 -0
  40. package/Unity/Converters/Rect.cs.meta +2 -0
  41. package/Unity/Converters/Vector2.cs +44 -0
  42. package/Unity/Converters/Vector2.cs.meta +2 -0
  43. package/Unity/Converters/Vector3.cs +45 -0
  44. package/Unity/Converters/Vector3.cs.meta +2 -0
  45. package/Unity/Converters/Vector4.cs +46 -0
  46. package/Unity/Converters/Vector4.cs.meta +2 -0
  47. package/Unity/Converters.meta +8 -0
  48. package/Unity/MunaUnity.cs +67 -19
  49. package/package.json +1 -1
  50. /package/Runtime/Beta/{Value.cs.meta → 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>
@@ -36,7 +38,6 @@ namespace Muna {
36
38
  /// </summary>
37
39
  /// <param name="accessKey">Muna access key. This defaults to your access key in Project Settings.</param>
38
40
  /// <param name="url">Muna API URL.</param>
39
- /// <param name="cachePath">Predictor cache path.</param>
40
41
  /// <returns>Muna client.</returns>
41
42
  public static Muna Create(
42
43
  string? accessKey = null,
@@ -62,34 +63,34 @@ namespace Muna {
62
63
  this Texture2D texture,
63
64
  byte[]? pixelBuffer = null
64
65
  ) {
66
+ // Check texture
65
67
  if (texture == null)
66
68
  throw new ArgumentNullException(nameof(texture));
69
+ // Check that texture is readable
67
70
  if (!texture.isReadable)
68
71
  throw new InvalidOperationException(@"Texture cannot be converted to a Muna image because it is not readable");
69
- var FormatChannelMap = new Dictionary<TextureFormat, int> {
70
- [TextureFormat.R8] = 1,
71
- [TextureFormat.Alpha8] = 1,
72
- [TextureFormat.RGB24] = 3,
73
- [TextureFormat.RGBA32] = 4,
74
- };
75
- if (!FormatChannelMap.TryGetValue(texture.format, out var channels))
76
- throw new InvalidOperationException($"Texture cannot be converted to a Muna image because it has unsupported format: {texture.format}");
72
+ // Allocate buffer
73
+ var channels = TextureFormatToImageChannels.GetValueOrDefault(texture.format, 4);
77
74
  var rowStride = texture.width * channels;
78
75
  var bufferSize = rowStride * texture.height;
79
76
  pixelBuffer ??= new byte[bufferSize];
80
77
  if (pixelBuffer.Length < bufferSize)
81
78
  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}");
82
- fixed (void* dst = pixelBuffer)
79
+ // Copy
80
+ var colorData = !TextureFormatToImageChannels.ContainsKey(texture.format) ? texture.GetPixels32() : null;
81
+ fixed (void* dst = pixelBuffer, colors = colorData) {
82
+ var src = colors == null ? texture.GetRawTextureData<byte>().GetUnsafePtr() : colors;
83
83
  UnsafeUtility.MemCpyStride(
84
84
  dst,
85
85
  rowStride,
86
- (byte*)texture.GetRawTextureData<byte>().GetUnsafePtr() + (rowStride * (texture.height - 1)),
86
+ (byte*)src + (rowStride * (texture.height - 1)),
87
87
  -rowStride,
88
88
  rowStride,
89
89
  texture.height
90
90
  );
91
- var image = new Image(pixelBuffer, texture.width, texture.height, channels);
92
- return image;
91
+ }
92
+ // Return
93
+ return new Image(pixelBuffer, texture.width, texture.height, channels);
93
94
  }
94
95
 
95
96
  /// <summary>
@@ -102,12 +103,7 @@ namespace Muna {
102
103
  this Image image,
103
104
  Texture2D? texture = null
104
105
  ) {
105
- var ChannelFormatMap = new Dictionary<int, TextureFormat> {
106
- [1] = TextureFormat.Alpha8,
107
- [3] = TextureFormat.RGB24,
108
- [4] = TextureFormat.RGBA32
109
- };
110
- if (!ChannelFormatMap.TryGetValue(image.channels, out var format))
106
+ if (!ImageChannelsToTextureFormat.TryGetValue(image.channels, out var format))
111
107
  throw new InvalidOperationException($"Image cannot be converted to a Texture2D because it has unsupported channel count: {image.channels}");
112
108
  texture = texture != null ? texture : new Texture2D(image.width, image.height, format, false);
113
109
  if (texture.width != image.width || texture.height != image.height || texture.format != format)
@@ -125,6 +121,58 @@ namespace Muna {
125
121
  texture.Apply();
126
122
  return texture;
127
123
  }
124
+
125
+ /// <summary>
126
+ /// Convert a `BinaryData` containing linear PCM audio into an audio clip.
127
+ /// </summary>
128
+ /// <param name="data">Binary data containing linear PCM audio.</param>
129
+ /// <returns>Audio clip.</returns>
130
+ public static unsafe AudioClip ToAudioClip(this BinaryData data) {
131
+ // Check that this contains LPCM data
132
+ if (string.IsNullOrEmpty(data.MediaType) || !data.MediaType.StartsWith(@"audio/pcm"))
133
+ throw new ArgumentException($"Failed to create audio clip from binary data because media type was expected to be 'audio/pcm' but got: '{data.MediaType}'");
134
+ // Match sample rate and channel count
135
+ var rateMatch = Regex.Match(data.MediaType, @"rate=(\d+)");
136
+ var channelsMatch = Regex.Match(data.MediaType, @"channels=(\d+)");
137
+ if (!rateMatch.Success || !channelsMatch.Success)
138
+ throw new ArgumentException($"Failed to create audio clip from binary data because media type is invalid: '{data.MediaType}'");
139
+ // Parse
140
+ if (!int.TryParse(rateMatch.Groups[1].Value, out var sampleRate))
141
+ throw new ArgumentException($"Failed to create audio clip from binary data because sample rate is invalid: '{rateMatch.Value}'");
142
+ if (!int.TryParse(channelsMatch.Groups[1].Value, out var channelCount))
143
+ throw new ArgumentException($"Failed to create audio clip from binary data because channel count is invalid: '{channelsMatch.Value}'");
144
+ // Create clip
145
+ var sampleCount = data.Length / sizeof(float);
146
+ var frameCount = sampleCount / channelCount;
147
+ var clip = AudioClip.Create(
148
+ "audio",
149
+ lengthSamples: frameCount,
150
+ channels: channelCount,
151
+ frequency: sampleRate,
152
+ stream: false
153
+ );
154
+ // Copy data
155
+ var samples = new float[sampleCount];
156
+ Buffer.BlockCopy(data.ToArray(), 0, samples, 0, data.Length);
157
+ clip.SetData(samples, 0);
158
+ // Return
159
+ return clip;
160
+ }
161
+ #endregion
162
+
163
+
164
+ #region --Operations--
165
+ private static readonly Dictionary<TextureFormat, int> TextureFormatToImageChannels = new() {
166
+ [TextureFormat.R8] = 1,
167
+ [TextureFormat.Alpha8] = 1,
168
+ [TextureFormat.RGB24] = 3,
169
+ [TextureFormat.RGBA32] = 4,
170
+ };
171
+ private static readonly Dictionary<int, TextureFormat> ImageChannelsToTextureFormat = new() {
172
+ [1] = TextureFormat.Alpha8,
173
+ [3] = TextureFormat.RGB24,
174
+ [4] = TextureFormat.RGBA32,
175
+ };
128
176
  #endregion
129
177
  }
130
178
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai.muna.muna",
3
- "version": "0.0.44",
3
+ "version": "0.0.46",
4
4
  "displayName": "Muna",
5
5
  "description": "Run AI inference in Unity Engine.",
6
6
  "unity": "2022.3",