返回介绍

AudioClip.Create 创建

发布于 2019-12-18 15:37:21 字数 8654 浏览 2007 评论 0 收藏 0

JavaScript => public static function Create(name: string, lengthSamples: int, channels: int, frequency: int, stream: bool): AudioClip;
JavaScript => public static function Create(name: string, lengthSamples: int, channels: int, frequency: int, stream: bool, pcmreadercallback: AudioClip.PCMReaderCallback): AudioClip;
JavaScript => public static function Create(name: string, lengthSamples: int, channels: int, frequency: int, stream: bool, pcmreadercallback: AudioClip.PCMReaderCallback, pcmsetpositioncallback: AudioClip.PCMSetPositionCallback): AudioClip;
JavaScript => public static function Create(name: string, lengthSamples: int, channels: int, frequency: int, _3D: bool, stream: bool): AudioClip;
JavaScript => public static function Create(name: string, lengthSamples: int, channels: int, frequency: int, _3D: bool, stream: bool, pcmreadercallback: AudioClip.PCMReaderCallback): AudioClip;
JavaScript => public static function Create(name: string, lengthSamples: int, channels: int, frequency: int, _3D: bool, stream: bool, pcmreadercallback: AudioClip.PCMReaderCallback, pcmsetpositioncallback: AudioClip.PCMSetPositionCallback): AudioClip;

C# => public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool stream);
C# => public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool stream, AudioClip.PCMReaderCallback pcmreadercallback);
C# => public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool stream, AudioClip.PCMReaderCallback pcmreadercallback, AudioClip.PCMSetPositionCallback pcmsetpositioncallback);
C# => public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool _3D, bool stream);
C# => public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool _3D, bool stream, AudioClip.PCMReaderCallback pcmreadercallback);
C# => public static AudioClip Create(string name, int lengthSamples, int channels, int frequency, bool _3D, bool stream, AudioClip.PCMReaderCallback pcmreadercallback, AudioClip.PCMSetPositionCallback pcmsetpositioncallback);

废弃注意,该音频剪辑的_3D参数被弃用,使用AudioSource的spatialBlend属性。

Parameters 参数

nameName of clip.
剪辑的名称。
lengthSamplesNumber of sample frames.
采样的帧数。
channelsNumber of channels per frame.
每帧的声道数。
frequencySample frequency of clip.
剪辑的采样频率。
_3DAudio clip is played back in 3D.
音频剪辑3D模式播放。
streamTrue if clip is streamed, that is if the pcmreadercallback generates data on the fly.
如果为true,剪辑是流媒体。
pcmreadercallbackThis callback is invoked to generate a block of sample data. Non-streamed clips call this only once at creation time while streamed clips call this continuously.
这个回调被调用来生成采样数据块。非流剪辑调用调用是一次性创建,而流剪辑调用是连续的。
pcmsetpositioncallbackThis callback is invoked whenever the clip loops or changes playback position.
每当剪辑循环或改变播放位置,这个回调被调用。

Returns 返回

AudioClip A reference to the created AudioClip.

返回引用创建的剪辑。

Description 描述

Creates a user AudioClip with a name and with the given length in samples, channels and frequency.

创建一个给定名称、采样长度、声道和频率的用户音频剪辑。

Set your own audio data with SetData. Use the PCMReaderCallback and PCMSetPositionCallback delegates to get a callback whenever the clip reads data and changes the position. If stream is true, Unity will on demand read in small chunks of data. If it's false, all the samples will be read during the creation.

用SetData设置你自己的音频数据,每当剪辑读取数据和改变位置,使用PCMReaderCallback和PCMSetPositionCallback委托来获取一个回调。如果stream为true,Unity将根据需要读取小块数据;如果为false,所有采样将在创建过程中读取。

JavaScript:

#pragma strict
public var position: int = 0;
public var samplerate: int = 44100;
public var frequency: float = 440;
function Start() {
	var myClip: AudioClip = AudioClip.Create("MySinusoid", samplerate * 2, 1, samplerate, true, OnAudioRead, OnAudioSetPosition);
	var aud: AudioSource = GetComponent.<AudioSource>();
	aud.clip = myClip;
	aud.Play();
}
function OnAudioRead(data: float[]) {
	var count: int = 0;
	while ( count < data.Length ) {
		data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * frequency * position / samplerate));
		position++;
		count++;
	}
}
function OnAudioSetPosition(newPosition: int) {
	position = newPosition;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public int position = 0;
    public int samplerate = 44100;
    public float frequency = 440;
    void Start() {
        AudioClip myClip = AudioClip.Create("MySinusoid", samplerate * 2, 1, samplerate, true, OnAudioRead, OnAudioSetPosition);
        AudioSource aud = GetComponent<AudioSource>();
        aud.clip = myClip;
        aud.Play();
    }
    void OnAudioRead(float[] data) {
        int count = 0;
        while (count < data.Length) {
            data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * frequency * position / samplerate));
            position++;
            count++;
        }
    }
    void OnAudioSetPosition(int newPosition) {
        position = newPosition;
    }
}

AudioClip

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文