Creates a user AudioClip with a name and with the given length in samples, channels and frequency.
#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; }
using UnityEngine; using System.Collections; public class Example : MonoBehaviour { public int position = 0; public int sampleRate = 0; public float frequency = 440; void Start() { AudioClip myClip = AudioClip.Create("MySinoid", 44100, 1, 44100, false, true, OnAudioRead, OnAudioSetPosition); sampleRate = AudioSettings.outputSampleRate; audio.clip = myClip; audio.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; } }
import UnityEngine import System.Collections public class Example(MonoBehaviour): public position as int = 0 public sampleRate as int = 0 public frequency as float = 440 def Start() as void: myClip as AudioClip = AudioClip.Create('MySinoid', 44100, 1, 44100, false, true, OnAudioRead, OnAudioSetPosition) sampleRate = AudioSettings.outputSampleRate audio.clip = myClip audio.Play() def OnAudioRead(data as (float)) as void: count as int = 0 while count < data.Length: data[count] = Mathf.Sign(Mathf.Sin(((((2 * Mathf.PI) * frequency) * position) / sampleRate))) position++ count++ def OnAudioSetPosition(newPosition as int) as void: position = newPosition