I would like to create a sound from script and wonder if that is possible inside unity.
Imagine a audiosource that outputs a function like
float generate()
{
return Math.sin(Time.time*10000.0f);
}
Jesper
I would like to create a sound from script and wonder if that is possible inside unity.
Imagine a audiosource that outputs a function like
float generate()
{
return Math.sin(Time.time*10000.0f);
}
Jesper
Using sin and cosine waves with a fusion of cot and cosec you can create monaural and bianuaral tone frequencies. This is rather efficient to create on the fly, it is better if you create the sounds before hand in a 3rd party application. All though I am not 100% sure on the circumstance you will need it for.
Many, many, years ago I built a synth for my final project in grad school. It was really just a digital to analogue converter with some amplifiers. I fed it data from my hand built computer in real time. I used this algorithm http://en.wikipedia.org/wiki/Karplus%E2%80%93Strong_string_synthesis Although it is remarkably simple I was able to produce an amazing number of realistic sounds with minor variations in the algorithm. It was a lot of fun to play with!
Tried this in 3.5
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Procedural : MonoBehaviour
{
public float frequency = 50.0f;
void Start ()
{
audio.Play();
}
void OnGUI()
{
frequency = GUI.HorizontalSlider(new Rect(0,0,Screen.width,20),
frequency, 0.0f, 100.0f);
}
float offset = 0.0f;
void OnAudioFilterRead(float[] data, int channels)
{
for(int i = 0; i < data.Length; i++)
{
data[i] = Mathf.Sin(offset + (Mathf.PI*2*frequency*i)/44100);
}
offset = offset + (Mathf.PI*2*frequency*data.Length)/44100;
}
}