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.
- package/Editor/MunaMenu.cs +17 -7
- package/Plugins/Android/Muna.aar +0 -0
- package/Plugins/macOS/Function.dylib.meta +26 -25
- package/README.md +1 -1
- package/Runtime/API/DotNetClient.cs +0 -3
- package/Runtime/Beta/BetaClient.cs +14 -1
- package/Runtime/Beta/OpenAI/AudioService.cs +38 -0
- package/Runtime/Beta/OpenAI/AudioService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/ChatService.cs +38 -0
- package/Runtime/Beta/OpenAI/ChatService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/CompletionService.cs +117 -0
- package/Runtime/Beta/OpenAI/CompletionService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/EmbeddingService.cs +252 -0
- package/Runtime/Beta/OpenAI/EmbeddingService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/OpenAIClient.cs +50 -0
- package/Runtime/Beta/OpenAI/OpenAIClient.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/SpeechService.cs +256 -0
- package/Runtime/Beta/OpenAI/SpeechService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/Types.cs +364 -0
- package/Runtime/Beta/OpenAI/Types.cs.meta +11 -0
- package/Runtime/Beta/OpenAI.meta +8 -0
- package/Runtime/Beta/Remote/RemotePredictionService.cs +50 -70
- package/Runtime/Beta/{Value.cs → Types/Value.cs} +3 -4
- package/Runtime/Beta/Types.meta +8 -0
- package/Runtime/C/Configuration.cs +1 -1
- package/Runtime/C/Function.cs +1 -1
- package/Runtime/C/Prediction.cs +1 -1
- package/Runtime/C/PredictionStream.cs +1 -1
- package/Runtime/C/Predictor.cs +1 -1
- package/Runtime/C/Value.cs +3 -2
- package/Runtime/C/ValueMap.cs +1 -1
- package/Runtime/Muna.cs +2 -2
- package/Runtime/Types/Parameter.cs +75 -0
- package/Runtime/Types/Parameter.cs.meta +11 -0
- package/Runtime/Types/Predictor.cs +0 -53
- package/Unity/API/PredictionCacheClient.cs +1 -1
- package/Unity/Converters/Color.cs +46 -0
- package/Unity/Converters/Color.cs.meta +2 -0
- package/Unity/Converters/Rect.cs +230 -0
- package/Unity/Converters/Rect.cs.meta +2 -0
- package/Unity/Converters/Vector2.cs +44 -0
- package/Unity/Converters/Vector2.cs.meta +2 -0
- package/Unity/Converters/Vector3.cs +45 -0
- package/Unity/Converters/Vector3.cs.meta +2 -0
- package/Unity/Converters/Vector4.cs +46 -0
- package/Unity/Converters/Vector4.cs.meta +2 -0
- package/Unity/Converters.meta +8 -0
- package/Unity/MunaUnity.cs +67 -19
- package/package.json +1 -1
- /package/Runtime/Beta/{Value.cs.meta → Types/Value.cs.meta} +0 -0
package/Unity/MunaUnity.cs
CHANGED
|
@@ -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
|
-
|
|
70
|
-
|
|
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
|
-
|
|
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*)
|
|
86
|
+
(byte*)src + (rowStride * (texture.height - 1)),
|
|
87
87
|
-rowStride,
|
|
88
88
|
rowStride,
|
|
89
89
|
texture.height
|
|
90
90
|
);
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
File without changes
|