Paul Bird
commited on
Commit
•
125ecbf
1
Parent(s):
8973ee5
Upload 3 files
Browse files- AudioResample.cs +109 -0
- audio_resample_22050_16000.sentis +0 -0
- audio_resample_44100_16000.sentis +0 -0
AudioResample.cs
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
using System.Collections;
|
2 |
+
using System.Collections.Generic;
|
3 |
+
using UnityEngine;
|
4 |
+
using Unity.Sentis;
|
5 |
+
|
6 |
+
/*
|
7 |
+
* Model to turn 44kHz and 22kHz audio to 16kHz
|
8 |
+
* ============================================
|
9 |
+
*
|
10 |
+
* Place the audioClip in the inputAudio field and press play
|
11 |
+
* The results will appear in the console
|
12 |
+
*/
|
13 |
+
|
14 |
+
public class AudioResample : MonoBehaviour
|
15 |
+
{
|
16 |
+
//Place the audio clip to resample here
|
17 |
+
public AudioClip inputAudio;
|
18 |
+
|
19 |
+
public AudioClip outputAudio;
|
20 |
+
|
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 |
+
|
38 |
+
// Update is called once per frame
|
39 |
+
void Update()
|
40 |
+
{
|
41 |
+
if (Input.GetKeyDown(KeyCode.Space))
|
42 |
+
{
|
43 |
+
ConvertAudio();
|
44 |
+
}
|
45 |
+
}
|
46 |
+
|
47 |
+
void ConvertAudio()
|
48 |
+
{
|
49 |
+
Debug.Log($"The frequency of the input audio clip is {inputAudio.frequency} Hz with {inputAudio.channels} channels.");
|
50 |
+
Model model;
|
51 |
+
if (inputAudio.frequency == 44100)
|
52 |
+
{
|
53 |
+
model = ModelLoader.Load(Application.streamingAssetsPath + "/audio_resample_44100_16000.sentis");
|
54 |
+
}
|
55 |
+
else if (inputAudio.frequency == 22050)
|
56 |
+
{
|
57 |
+
model = ModelLoader.Load(Application.streamingAssetsPath + "/audio_resample_22050_16000.sentis");
|
58 |
+
}
|
59 |
+
else
|
60 |
+
{
|
61 |
+
Debug.Log("Only frequencies of 44kHz and 22kHz are compatible");
|
62 |
+
return;
|
63 |
+
}
|
64 |
+
|
65 |
+
engine = WorkerFactory.CreateWorker(backend, model);
|
66 |
+
|
67 |
+
int channels = inputAudio.channels;
|
68 |
+
int size = inputAudio.samples * channels;
|
69 |
+
float[] data = new float[size];
|
70 |
+
inputAudio.GetData(data, 0);
|
71 |
+
using var input = new TensorFloat(new TensorShape(1, size), data);
|
72 |
+
|
73 |
+
engine.Execute(input);
|
74 |
+
|
75 |
+
float[] outData;
|
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 |
+
|
91 |
+
int samplesOut = outData.Length / channels;
|
92 |
+
|
93 |
+
outputAudio = AudioClip.Create("outputAudio", samplesOut, channels, 16000, false);
|
94 |
+
outputAudio.SetData(outData, 0);
|
95 |
+
|
96 |
+
Debug.Log($"The audio has been converted to 16Khz with {channels} channels.");
|
97 |
+
|
98 |
+
if (playFinalAudio)
|
99 |
+
{
|
100 |
+
GetComponent<AudioSource>().PlayOneShot(outputAudio);
|
101 |
+
}
|
102 |
+
}
|
103 |
+
|
104 |
+
private void OnDestroy()
|
105 |
+
{
|
106 |
+
ops?.Dispose();
|
107 |
+
allocator?.Dispose();
|
108 |
+
}
|
109 |
+
}
|
audio_resample_22050_16000.sentis
ADDED
Binary file (594 kB). View file
|
|
audio_resample_44100_16000.sentis
ADDED
Binary file (596 kB). View file
|
|