File size: 2,997 Bytes
125ecbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Sentis;
/*
* Model to turn 44kHz and 22kHz audio to 16kHz
* ============================================
*
* Place the audioClip in the inputAudio field and press play
* The results will appear in the console
*/
public class AudioResample : MonoBehaviour
{
//Place the audio clip to resample here
public AudioClip inputAudio;
public AudioClip outputAudio;
public bool playFinalAudio = true;
IWorker engine;
BackendType backend = BackendType.GPUCompute;
Ops ops;
ITensorAllocator allocator;
void Start()
{
allocator = new TensorCachingAllocator();
ops = WorkerFactory.CreateOps(backend, allocator);
ConvertAudio();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ConvertAudio();
}
}
void ConvertAudio()
{
Debug.Log($"The frequency of the input audio clip is {inputAudio.frequency} Hz with {inputAudio.channels} channels.");
Model model;
if (inputAudio.frequency == 44100)
{
model = ModelLoader.Load(Application.streamingAssetsPath + "/audio_resample_44100_16000.sentis");
}
else if (inputAudio.frequency == 22050)
{
model = ModelLoader.Load(Application.streamingAssetsPath + "/audio_resample_22050_16000.sentis");
}
else
{
Debug.Log("Only frequencies of 44kHz and 22kHz are compatible");
return;
}
engine = WorkerFactory.CreateWorker(backend, model);
int channels = inputAudio.channels;
int size = inputAudio.samples * channels;
float[] data = new float[size];
inputAudio.GetData(data, 0);
using var input = new TensorFloat(new TensorShape(1, size), data);
engine.Execute(input);
float[] outData;
var output = engine.PeekOutput() as TensorFloat;
if (inputAudio.frequency == 44100)
{
using var A = output.ShallowReshape(new TensorShape( output.shape[1] / 2 , 2)) as TensorFloat;
using var B = ops.Slice(A, new[] { 0 }, new[] { 1 }, new[] { 1 }, new[] { 1 });
B.MakeReadable();
outData = B.ToReadOnlyArray();
}
else
{
output.MakeReadable();
outData = output.ToReadOnlyArray();
}
int samplesOut = outData.Length / channels;
outputAudio = AudioClip.Create("outputAudio", samplesOut, channels, 16000, false);
outputAudio.SetData(outData, 0);
Debug.Log($"The audio has been converted to 16Khz with {channels} channels.");
if (playFinalAudio)
{
GetComponent<AudioSource>().PlayOneShot(outputAudio);
}
}
private void OnDestroy()
{
ops?.Dispose();
allocator?.Dispose();
}
}
|