本文整理汇总了C#中UnityEngine.AudioClip.GetData方法的典型用法代码示例。如果您正苦于以下问题:C# AudioClip.GetData方法的具体用法?C# AudioClip.GetData怎么用?C# AudioClip.GetData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.AudioClip
的用法示例。
在下文中一共展示了AudioClip.GetData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetClipData
//Returns data from an AudioClip as a byte array.
public static byte[] GetClipData(AudioClip _clip)
{
//Get data
float[] floatData = new float[_clip.samples * _clip.channels];
_clip.GetData(floatData,0);
//convert to byte array
byte[] byteData = new byte[floatData.Length * 4];
Buffer.BlockCopy(floatData, 0, byteData, 0, byteData.Length);
return(byteData);
}
示例2: _StartMicrophone
private IEnumerator _StartMicrophone()
{
deviceName = PlayerPrefs.GetString("VJMicrophone.deviceName", "");
while(micSemaphor > 0) {
string currentDeviceName = deviceName;
CheckSampleRate(currentDeviceName);
Debug.Log("Microphone starting: " + currentDeviceName + " sampling rate:" + sampleRate);
micClip = Microphone.Start(currentDeviceName, true, clipLength, sampleRate);
float[] samples = new float[(int)(analysisWindow * sampleRate)];
audio.clip = micClip;
audio.Play();
while (currentDeviceName == deviceName && micSemaphor > 0) {
yield return 0;
int position = Microphone.GetPosition(deviceName);
if (position < samples.Length) position += clipLength * sampleRate;
micClip.GetData(samples, position - samples.Length);
audio.timeSamples = position;
float rms = 0.0f;
foreach (float lvl in samples) {
rms += lvl * lvl;
}
rms = Mathf.Sqrt(rms / samples.Length);
level = Mathf.Clamp01( 0.5f * (2.0f + Mathf.Log10(rms)) );
}
audio.Stop();
audio.clip = null;
Debug.Log("Microphone stopping: " + currentDeviceName );
Microphone.End(currentDeviceName);
}
}
示例3: Concat
public static AudioClip Concat(this AudioClip audioClip, AudioClip otherClip)
{
int length = audioClip.samples >= otherClip.samples ? audioClip.samples : otherClip.samples;
AudioClip clipSum = AudioClip.Create(audioClip.name + " + " + otherClip.name, length, audioClip.channels, audioClip.frequency, false);
float[] dataSum;
float[] otherData;
if (audioClip.samples >= otherClip.samples)
{
dataSum = new float[audioClip.samples];
audioClip.GetData(dataSum, 0);
otherData = new float[otherClip.samples];
otherClip.GetData(otherData, 0);
}
else
{
dataSum = new float[otherClip.samples];
otherClip.GetData(dataSum, 0);
otherData = new float[audioClip.samples];
audioClip.GetData(otherData, 0);
}
for (int i = 0; i < otherData.Length; i++)
dataSum[i] += otherData[i];
clipSum.SetData(dataSum, 0);
return clipSum;
}
示例4: OVRHapticsClip
/// <summary>
/// Creates a haptics clip from the specified audio clip.
/// </summary>
public OVRHapticsClip(AudioClip audioClip, int channel = 0)
{
float[] audioData = new float[audioClip.samples * audioClip.channels];
audioClip.GetData(audioData, 0);
InitializeFromAudioFloatTrack(audioData, audioClip.frequency, audioClip.channels, channel);
}
示例5: TrimSilence
public static AudioClip TrimSilence(AudioClip clip, float min) {
var samples = new float[clip.samples];
clip.GetData(samples, 0);
return TrimSilence(new List<float>(samples), min, clip.channels, clip.frequency);
}
示例6: GetAudioClipSamples
private float[] GetAudioClipSamples(AudioClip clip)
{
float[] buffer = new float[clip.samples * clip.channels];
clip.GetData(buffer, 0);
return buffer;
}
示例7: Sampler
//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
public Sampler(AudioClip sample)
{
output = new AudioSignal();
pos = 0;
// get samples data
len = sample.samples;
data = new float[len];
sample.GetData (data, 0);
}
示例8: TrimSilence
public static AudioClip TrimSilence(AudioClip clip, float min)
{
// do nothing if clip size is 0
if ( clip.samples == 0 )
return clip;
var samples = new float[clip.samples];
clip.GetData(samples, 0);
return TrimSilence(new List<float>(samples), min, clip.channels, clip.frequency);
}
示例9: ConvertSoundToString
public string ConvertSoundToString(AudioClip clip)
{
string base64 = "";
if (clip != null)
{
float[] fBytes = new float[clip.samples * clip.channels];
byte[] bytes = new byte[fBytes.Length * 4];
clip.GetData(fBytes, 0);
Buffer.BlockCopy(fBytes, 0, bytes, 0, bytes.Length);
base64 = Convert.ToBase64String(bytes);
}
return base64;
}
示例10: ConvertClipToByte
public static Byte[] ConvertClipToByte(AudioClip clip)
{
if (clip == null)
{
Debug.Log("GetClipData audio.clip is null");
return null;
}
float[] samples = new float[clip.samples];
clip.GetData(samples, 0);
Byte[] outData = new byte[samples.Length * 2];
//Int16[] intData = new Int16[samples.Length];
//converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
int rescaleFactor = 32767; //to convert float to Int16
for (int i = 0; i < samples.Length; i++)
{
short temshort = (short)(samples[i] * rescaleFactor);
Byte[] temdata = System.BitConverter.GetBytes(temshort);
outData[i * 2] = temdata[0];
outData[i * 2 + 1] = temdata[1];
}
if (outData == null || outData.Length <= 0)
{
Debug.Log("GetClipData intData is null");
return null;
}
//return intData;
return outData;
}
示例11: ConvertAndWrite
static void ConvertAndWrite(FileStream fileStream, AudioClip clip) {
var samples = new float[clip.samples];
clip.GetData(samples, 0);
Int16[] intData = new Int16[samples.Length];
//converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
Byte[] bytesData = new Byte[samples.Length * 2];
//bytesData array is twice the size of
//dataSource array because a float converted in Int16 is 2 bytes.
int rescaleFactor = 32767; //to convert float to Int16
for (int i = 0; i<samples.Length; i++) {
intData[i] = (short) (samples[i] * rescaleFactor);
Byte[] byteArr = new Byte[2];
byteArr = BitConverter.GetBytes(intData[i]);
byteArr.CopyTo(bytesData, i * 2);
}
fileStream.Write(bytesData, 0, bytesData.Length);
}
示例12: printOutAudioClip
public static void printOutAudioClip( AudioClip audioClip, ref AnimationCurve curve, float scaleX = 1f )
{
// Debug.Log("Audio channels:"+audioClip.channels+" frequency:"+audioClip.frequency+" length:"+audioClip.length+" samples:"+audioClip.samples);
float[] samples = new float[audioClip.samples * audioClip.channels];
audioClip.GetData(samples, 0);
int i = 0;
Keyframe[] frames = new Keyframe[samples.Length];
while (i < samples.Length) {
frames[i] = new Keyframe( (float)i * scaleX, samples[i] );
++i;
}
curve = new AnimationCurve( frames );
}
示例13: lipSync
private void lipSync(AudioClip clip, int frame)
{
frame = ((int)(frame / NUMFRAMESLIPSYNC)) * NUMFRAMESLIPSYNC;
int init = Mathf.CeilToInt(Globals.MILISPERFRAME * clip.frequency);
float[] samples = new float[init * clip.channels * 3];
init *= frame % (int)(clip.length * Globals.FRAMESPERSECOND);
clip.GetData(samples, init);
float midSamples = 0;
for(int i = 0; i < samples.Length; ++i) {
midSamples += Mathf.Abs(samples[i]);
}
midSamples /= samples.Length;
Component[] children = GetComponentsInChildren<Component>(true);
foreach(Component child in children) {
if((child.name.StartsWith("exp_") && child.name.EndsWith("_m")) || child.name.StartsWith("lipsync_"))
child.gameObject.SetActive(false);
if(midSamples < 0.05f && mMouthClose && child.name == mExpression + "_m")
child.gameObject.SetActive(true);
else if(midSamples < 0.05f && !mMouthClose && child.name == "lipsync_00")
child.gameObject.SetActive(true);
else if(midSamples >= 0.05f && midSamples < 0.1f && child.name == "lipsync_06")
child.gameObject.SetActive(true);
else if(midSamples >= 0.1f && midSamples < 0.15f && child.name == "lipsync_07")
child.gameObject.SetActive(true);
else if(midSamples >= 0.15f && midSamples < 0.2f && child.name == "lipsync_01")
child.gameObject.SetActive(true);
else if(midSamples >= 0.2f && midSamples < 0.25f && child.name == "lipsync_04")
child.gameObject.SetActive(true);
else if(midSamples >= 0.25f && midSamples < 0.3f && child.name == "lipsync_05")
child.gameObject.SetActive(true);
else if(midSamples >= 0.3f && midSamples < 0.35f && child.name == "lipsync_06")
child.gameObject.SetActive(true);
else if(midSamples >= 0.35f && midSamples < 0.4f && child.name == "lipsync_07")
child.gameObject.SetActive(true);
else if(midSamples >= 0.4f && midSamples < 0.45f && child.name == "lipsync_08")
child.gameObject.SetActive(true);
else if(midSamples >= 0.45f && child.name == "lipsync_09")
child.gameObject.SetActive(true);
/*if(midSamples < 0.05f && mMouthClose && child.name == mExpression + "_m")
child.gameObject.SetActive(true);
else if(midSamples < 0.05f && !mMouthClose && child.name == "lipsync_00")
child.gameObject.SetActive(true);
else if(midSamples >= 0.05f && midSamples < 0.1f && child.name == "lipsync_01")
child.gameObject.SetActive(true);
else if(midSamples >= 0.1f && midSamples < 0.15f && child.name == "lipsync_02")
child.gameObject.SetActive(true);
else if(midSamples >= 0.15f && midSamples < 0.2f && child.name == "lipsync_03")
child.gameObject.SetActive(true);
else if(midSamples >= 0.2f && midSamples < 0.25f && child.name == "lipsync_04")
child.gameObject.SetActive(true);
else if(midSamples >= 0.25f && midSamples < 0.3f && child.name == "lipsync_05")
child.gameObject.SetActive(true);
else if(midSamples >= 0.3f && midSamples < 0.35f && child.name == "lipsync_06")
child.gameObject.SetActive(true);
else if(midSamples >= 0.35f && midSamples < 0.4f && child.name == "lipsync_07")
child.gameObject.SetActive(true);
else if(midSamples >= 0.4f && midSamples < 0.45f && child.name == "lipsync_08")
child.gameObject.SetActive(true);
else if(midSamples >= 0.45f && child.name == "lipsync_09")
child.gameObject.SetActive(true);*/
/*if(midSamples<0.01f && child.name==mExpression+"_m")
child.gameObject.SetActive(true);
else if(midSamples>=0.01f && midSamples<0.05f && child.name=="lipsync_01")
child.gameObject.SetActive(true);
else if(midSamples>=0.05f && midSamples<0.1f && child.name=="lipsync_02")
child.gameObject.SetActive(true);
else if(midSamples>=0.1f && midSamples<0.15f && child.name=="lipsync_03")
child.gameObject.SetActive(true);
else if(midSamples>=0.15f && midSamples<0.2f && child.name=="lipsync_04")
child.gameObject.SetActive(true);
else if(midSamples>=0.2f && midSamples<0.25f && child.name=="lipsync_05")
child.gameObject.SetActive(true);
else if(midSamples>=0.25f && midSamples<0.3f && child.name=="lipsync_06")
child.gameObject.SetActive(true);
else if(midSamples>=0.3f && midSamples<0.35f && child.name=="lipsync_07")
child.gameObject.SetActive(true);
else if(midSamples>=0.35f && midSamples<0.4f && child.name=="lipsync_08")
child.gameObject.SetActive(true);
else if(midSamples>=0.4f && child.name=="lipsync_09")
child.gameObject.SetActive(true);*/
}
}
示例14: Volume
public static AudioClip Volume(AudioClip clip, float volume)
{
var data = new float[clip.samples];
clip.GetData(data, 0);
data = Volume(data, volume);
var filtered = AudioClip.Create(clip.name, clip.samples, 1, clip.frequency, false, false);
filtered.SetData(data, 0);
return filtered;
}
示例15: CreatePreview
public static Texture2D CreatePreview(AudioClip aud, int width, int height, Color color, PreviewType previewType)
{
int step = Mathf.CeilToInt((aud.samples * aud.channels) / width);
float[] samples = new float[aud.samples * aud.channels];
//workaround to prevent the error in the function getData when Audio Importer loadType is "compressed in memory"
string path = AssetDatabase.GetAssetPath(aud);
AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
AudioImporterLoadType audioLoadTypeBackup = audioImporter.loadType;
audioImporter.loadType = AudioImporterLoadType.StreamFromDisc;
AssetDatabase.ImportAsset(path);
//getData after the loadType changed
aud.GetData(samples, 0);
//restore the loadType
audioImporter.loadType = audioLoadTypeBackup;
AssetDatabase.ImportAsset(path);
Texture2D img = new Texture2D(width, height, TextureFormat.RGBA32, false);
if (previewType == PreviewType.wave)
{
Color[] xy = new Color[width * height];
for (int x = 0; x < width * height; x++)
{
xy[x] = new Color(0, 0, 0, 0);
//xy[x] = new Color(0, 1, 0, 0.2f);
}
img.SetPixels(xy);
int i = 0;
while (i < width)
{
int barHeight = Mathf.CeilToInt(Mathf.Clamp(Mathf.Abs(samples[i * step]) * height, 0, height));
int add = samples[i * step] > 0 ? 1 : -1;
for (int j = 0; j < barHeight; j++)
{
img.SetPixel(i, Mathf.FloorToInt(height / 2) - (Mathf.FloorToInt(barHeight / 2) * add) + (j * add), color);
}
++i;
}
img.Apply();
}
else if (previewType == PreviewType.bar)
{
img = new Texture2D(width, 1, TextureFormat.RGBA32, false);
int i = 0;
while (i < width)
{
//int barHeight = Mathf.CeilToInt(Mathf.Clamp(Mathf.Abs(samples[i * step]) * height, 0, height));
//int add = samples[i * step] > 0 ? 1 : -1;
float colorIntensity = Mathf.Clamp(Mathf.Abs(samples[i * step]) * 10f, 0, 1);
Color colorReturn = new Color(color.r / colorIntensity, color.g / colorIntensity, color.b / colorIntensity, colorIntensity / 4f);
img.SetPixel(i, 0, colorReturn);
++i;
}
img.Apply();
}
else if (previewType == PreviewType.both)
{
/*
Color[] xy = new Color[width * height];
for (int x = 0; x < width * height; x++)
{
xy[x] = new Color(1, 0, 0, 1);
//xy[x] = new Color(0, 1, 0, 0.2f);
}
img.SetPixels(xy);
*/
int i = 0;
while (i < width)
{
int barHeight = Mathf.CeilToInt(Mathf.Clamp(Mathf.Abs(samples[i * step]) * height, 0, height));
int add = samples[i * step] > 0 ? 1 : -1;
float colorIntensity = Mathf.Clamp(Mathf.Abs(samples[i * step]) * 10f, 0, 1);
Color colorReturn = new Color(color.r / colorIntensity, color.g / colorIntensity, color.b / colorIntensity, colorIntensity / 6f);
for (int j = 0; j < height; j++)
{
img.SetPixel(i, j, colorReturn);
}
for (int j = 0; j < barHeight; j++)
{
img.SetPixel(i, Mathf.FloorToInt(height / 2) - (Mathf.FloorToInt(barHeight / 2) * add) + (j * add), color);
}
++i;
}
img.Apply();
//.........这里部分代码省略.........