Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

AudioClip.Create

static function Create(name: string, lengthSamples: int, channels: int, frequency: int, _3D: bool, stream: bool): AudioClip;
static function Create(name: string, lengthSamples: int, channels: int, frequency: int, _3D: bool, stream: bool, pcmreadercallback: PCMReaderCallback): AudioClip;
static function Create(name: string, lengthSamples: int, channels: int, frequency: int, _3D: bool, stream: bool, pcmreadercallback: PCMReaderCallback, pcmsetpositioncallback: PCMSetPositionCallback): 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.

	#pragma strict
	// Creates a 1 sec long audioclip, with a 440hz sinoid
	var position: int = 0;
	var sampleRate : int = 0;
	var frequency : float = 440;
	function Start () {
		var myClip = AudioClip.Create("MySinoid", 44100, 1, 44100, false, true, OnAudioRead, OnAudioSetPosition);
		sampleRate = AudioSettings.outputSampleRate;

audio.clip = myClip; audio.Play(); }

function OnAudioRead(data:float[]) { for (var count = 0; count < data.Length; count++) { data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * frequency * position / sampleRate)); position++; } }

function OnAudioSetPosition(newPosition:int) { position = newPosition; }