UnityPaul commited on
Commit
a9fdeb0
1 Parent(s): 3008652

Upload 5 files

Browse files
AudioResample.cs CHANGED
@@ -21,17 +21,13 @@ public class AudioResample : MonoBehaviour
21
  public bool playFinalAudio = true;
22
 
23
  IWorker engine;
 
24
 
25
- BackendType backend = BackendType.GPUCompute;
26
-
27
- Ops ops;
28
- ITensorAllocator allocator;
29
 
30
  void Start()
31
  {
32
- allocator = new TensorCachingAllocator();
33
- ops = WorkerFactory.CreateOps(backend, allocator);
34
-
35
  ConvertAudio();
36
  }
37
 
@@ -62,7 +58,7 @@ public class AudioResample : MonoBehaviour
62
  return;
63
  }
64
 
65
- engine = WorkerFactory.CreateWorker(backend, model);
66
 
67
  int channels = inputAudio.channels;
68
  int size = inputAudio.samples * channels;
@@ -76,15 +72,23 @@ public class AudioResample : MonoBehaviour
76
 
77
  var output = engine.PeekOutput() as TensorFloat;
78
  if (inputAudio.frequency == 44100)
79
- {
80
- using var A = output.ShallowReshape(new TensorShape( output.shape[1] / 2 , 2)) as TensorFloat;
81
- using var B = ops.Slice(A, new[] { 0 }, new[] { 1 }, new[] { 1 }, new[] { 1 });
82
- B.MakeReadable();
83
- outData = B.ToReadOnlyArray();
 
 
 
 
 
 
 
 
84
  }
85
  else
86
  {
87
- output.MakeReadable();
88
  outData = output.ToReadOnlyArray();
89
  }
90
 
@@ -103,7 +107,7 @@ public class AudioResample : MonoBehaviour
103
 
104
  private void OnDestroy()
105
  {
106
- ops?.Dispose();
107
- allocator?.Dispose();
108
  }
109
- }
 
21
  public bool playFinalAudio = true;
22
 
23
  IWorker engine;
24
+ IBackend backend;
25
 
26
+ BackendType backendType = BackendType.GPUCompute;
 
 
 
27
 
28
  void Start()
29
  {
30
+ backend = WorkerFactory.CreateBackend(backendType);
 
 
31
  ConvertAudio();
32
  }
33
 
 
58
  return;
59
  }
60
 
61
+ engine = WorkerFactory.CreateWorker(backendType, model);
62
 
63
  int channels = inputAudio.channels;
64
  int size = inputAudio.samples * channels;
 
72
 
73
  var output = engine.PeekOutput() as TensorFloat;
74
  if (inputAudio.frequency == 44100)
75
+ {
76
+ //The model gives 2x as many samples as we would like so we fix it:
77
+ //We need to pad it if it has odd number of samples
78
+ int n = output.shape[1] % 2;
79
+ using var output2 = TensorFloat.AllocNoData(new TensorShape(1, output.shape[1] + n));
80
+ backend.Pad(output, output2, new int[] { 0, n }, Unity.Sentis.Layers.PadMode.Constant, 0);
81
+
82
+ //Now we take every second sample:
83
+ output2.Reshape(new TensorShape(output2.shape[1] / 2, 2));
84
+ using var output3 = TensorFloat.AllocNoData(new TensorShape(output2.shape[0], 1));
85
+ backend.Slice(output2, output3, new[] { 0 }, new[] { 1 }, new[] { 1 });
86
+ output3.CompleteOperationsAndDownload();
87
+ outData = output3.ToReadOnlyArray();
88
  }
89
  else
90
  {
91
+ output.CompleteOperationsAndDownload();
92
  outData = output.ToReadOnlyArray();
93
  }
94
 
 
107
 
108
  private void OnDestroy()
109
  {
110
+ engine?.Dispose();
111
+ backend?.Dispose();
112
  }
113
+ }
README.md CHANGED
@@ -4,9 +4,7 @@ library_name: unity-sentis
4
  pipeline_tag: audio-to-audio
5
  ---
6
 
7
- # Model to turn 44kHz and 22kHz audio to 16kHz for Sentis (Version 1.3.0-pre.3*)
8
- *Version 1.3.0 Sentis files are not compatible with Sentis version 1.4.0 and need to be recreated/downloaded
9
-
10
  This contains models in Sentis format to convert 44kHz and 22kHz audioclips to 16kHz. Useful for models such as [Whisper-Tiny](https://huggingface.co/unity/sentis-whisper-tiny)
11
 
12
  ## How to Use
 
4
  pipeline_tag: audio-to-audio
5
  ---
6
 
7
+ # Model to turn 44kHz and 22kHz audio to 16kHz (Sentis version 1.4.0)
 
 
8
  This contains models in Sentis format to convert 44kHz and 22kHz audioclips to 16kHz. Useful for models such as [Whisper-Tiny](https://huggingface.co/unity/sentis-whisper-tiny)
9
 
10
  ## How to Use
audio_resample_22050_16000.sentis CHANGED
Binary files a/audio_resample_22050_16000.sentis and b/audio_resample_22050_16000.sentis differ
 
audio_resample_44100_16000.sentis CHANGED
Binary files a/audio_resample_44100_16000.sentis and b/audio_resample_44100_16000.sentis differ
 
info.json CHANGED
@@ -6,7 +6,7 @@
6
  "audio_resample_22050_16000.sentis",
7
  "audio_resample_44100_16000.sentis"
8
  ],
9
- "version": [
10
- "1.3.0-pre.3"
11
  ]
12
  }
 
6
  "audio_resample_22050_16000.sentis",
7
  "audio_resample_44100_16000.sentis"
8
  ],
9
+ "version":[
10
+ "1.4.0"
11
  ]
12
  }