1111public class ProceduralAudioController : MonoBehaviour {
1212/* This class is the main audio engine, that has certain embedded functions to it, so that
1313you may produce some interesting audio results.*/
14+
1415SawWave sawAudioWave ;
1516SquareWave squareAudioWave ;
1617SinusWave sinusAudioWave ;
1718
1819SinusWave amplitudeModulationOscillator ;
1920SinusWave frequencyModulationOscillator ;
20- //SinusWave amOsc;
21- float tt ;
22- float ttPrev ;
21+
2322public bool autoPlay ;
2423
2524[ Header ( "Volume / Frequency" ) ]
@@ -66,11 +65,12 @@ you may produce some interesting audio results.*/
6665private System . Random RandomNumber = new System . Random ( ) ;
6766
6867private double sampleRate ; // samples per second
69- private double myDspTime ; // dsp time
70- private double dataLen ; //the data length of each channel
71- double chunkTime ;
72- double stepTime ;
73- double currentTime ;
68+ //private double myDspTime; // dsp time
69+ private double dataLen ; // the data length of each channel
70+ double chunkTime ;
71+ double dspTimeStep ;
72+ double currentDspTime ;
73+
7474void Awake ( ) {
7575sawAudioWave = new SawWave ( ) ;
7676squareAudioWave = new SquareWave ( ) ;
@@ -80,13 +80,11 @@ void Awake(){
8080frequencyModulationOscillator = new SinusWave ( ) ;
8181
8282sampleRate = AudioSettings . outputSampleRate ;
83- myDspTime = AudioSettings . dspTime ;
84- currentTime = 0 ;
8583}
8684
8785void Update ( ) {
8886if ( autoPlay ) {
89- mainFrequency = Mathf . PingPong ( Time . time * 200.0f , 1900.0f ) + 100.0f ;
87+ mainFrequency = Mathf . PingPong ( Time . time * 200.0f , 1900.0f ) + 100.0f ;
9088sinusAudioWaveIntensity = Mathf . PingPong ( Time . time * 0.5f , 1.0f ) ;
9189squareAudioWaveIntensity = Mathf . PingPong ( Time . time * 0.6f , 1.0f ) ;
9290sawAudioWaveIntensity = Mathf . PingPong ( Time . time * 0.7f , 1.0f ) ;
@@ -97,35 +95,48 @@ void Update(){
9795}
9896
9997void OnAudioFilterRead ( float [ ] data , int channels ) {
100- currentTime = AudioSettings . dspTime ;
98+ /* This is called by the system
99+ suppose: sampleRate = 48000
100+ suppose: data.Length = 2048
101+ suppose: channels = 2
102+ then:
103+ dataLen = 2048/2 = 1024
104+ chunkTime = 1024 / 48000 = 0.0213333... so the chunk time is around 21.3 milliseconds.
105+ dspTimeStep = 0.0213333 / 1024 = 2.083333.. * 10^(-5) = 0.00002083333..sec = 0.02083 milliseconds
106+ keep note that 1 / dspTimeStep = 48000 ok!
107+ */
108+
109+ currentDspTime = AudioSettings . dspTime ;
101110dataLen = data . Length / channels ; // the actual data length for each channel
102111chunkTime = dataLen / sampleRate ; // the time that each chunk of data lasts
103- stepTime = chunkTime / dataLen ;
112+ dspTimeStep = chunkTime / dataLen ; // the time of each dsp step. (the time that each individual audio sample (actually a float value) lasts)
113+
114+ double preciseDspTime ;
104115for ( int i = 0 ; i < dataLen ; i ++ ) { // go through data chunk
105- double tt = currentTime + i * stepTime ;
116+ preciseDspTime = currentDspTime + i * dspTimeStep ;
106117double signalValue = 0.0 ;
107118double currentFreq = mainFrequency ;
108119if ( useFrequencyModulation ) {
109120double freqOffset = ( frequencyModulationOscillatorIntensity * mainFrequency * 0.75 ) / 100.0 ;
110- currentFreq += mapValueD ( frequencyModulationOscillator . calculateSignalValue ( tt , frequencyModulationOscillatorFrequency ) , - 1.0 , 1.0 , - freqOffset , freqOffset ) ;
111- frequencyModulationRangeOut = ( float ) frequencyModulationOscillator . calculateSignalValue ( tt , frequencyModulationOscillatorFrequency ) * 0.5f + 0.5f ;
121+ currentFreq += mapValueD ( frequencyModulationOscillator . calculateSignalValue ( preciseDspTime , frequencyModulationOscillatorFrequency ) , - 1.0 , 1.0 , - freqOffset , freqOffset ) ;
122+ frequencyModulationRangeOut = ( float ) frequencyModulationOscillator . calculateSignalValue ( preciseDspTime , frequencyModulationOscillatorFrequency ) * 0.5f + 0.5f ;
112123} else {
113124frequencyModulationRangeOut = 0.0f ;
114125}
115126
116127if ( useSinusAudioWave ) {
117- signalValue += sinusAudioWaveIntensity * sinusAudioWave . calculateSignalValue ( tt , currentFreq ) ;
128+ signalValue += sinusAudioWaveIntensity * sinusAudioWave . calculateSignalValue ( preciseDspTime , currentFreq ) ;
118129}
119130if ( useSawAudioWave ) {
120- signalValue += sawAudioWaveIntensity * sawAudioWave . calculateSignalValue ( tt , currentFreq ) ;
131+ signalValue += sawAudioWaveIntensity * sawAudioWave . calculateSignalValue ( preciseDspTime , currentFreq ) ;
121132}
122133if ( useSquareAudioWave ) {
123- signalValue += squareAudioWaveIntensity * squareAudioWave . calculateSignalValue ( tt , currentFreq ) ;
134+ signalValue += squareAudioWaveIntensity * squareAudioWave . calculateSignalValue ( preciseDspTime , currentFreq ) ;
124135}
125136
126137if ( useAmplitudeModulation ) {
127- signalValue *= mapValueD ( amplitudeModulationOscillator . calculateSignalValue ( tt , amplitudeModulationOscillatorFrequency ) , - 1.0 , 1.0 , 0.0 , 1.0 ) ;
128- amplitudeModulationRangeOut = ( float ) amplitudeModulationOscillator . calculateSignalValue ( tt , amplitudeModulationOscillatorFrequency ) * 0.5f + 0.5f ;
138+ signalValue *= mapValueD ( amplitudeModulationOscillator . calculateSignalValue ( preciseDspTime , amplitudeModulationOscillatorFrequency ) , - 1.0 , 1.0 , 0.0 , 1.0 ) ;
139+ amplitudeModulationRangeOut = ( float ) amplitudeModulationOscillator . calculateSignalValue ( preciseDspTime , amplitudeModulationOscillatorFrequency ) * 0.5f + 0.5f ;
129140} else {
130141amplitudeModulationRangeOut = 0.0f ;
131142}
0 commit comments