Пример #1
0
        // helper function for creating sound
        private byte[] create16bit_sound(SoundDelegate fn, double frequency, double amp, double duration, bool periodic)
        {
            int sampleRate = 44100;
            int length     = (int)(sampleRate * duration);

            byte[] wavedata = new byte[length * 2];

            int waveformPeriod = (int)(sampleRate / frequency);

            for (int i = 0; i < length; i++)
            {
                if (i <= waveformPeriod || !periodic)
                {
                    double dbl = fn(i);
                    short  sh  = (short)(dbl * amp * short.MaxValue);

                    wavedata[i * 2]     = (byte)(sh & 0x00FF); // low byte
                    wavedata[i * 2 + 1] = (byte)(sh >> 8);     // high byte
                }
                else  // we have already computed the wave, it is periodic. Good optimization!
                {
                    int prevspot = i % waveformPeriod;
                    wavedata[i * 2]     = wavedata[prevspot * 2];
                    wavedata[i * 2 + 1] = wavedata[prevspot * 2 + 1];
                }
            }
            return(wavedata);
        }
Пример #2
0
        public byte[] sine(double frequency, double amp, double duration)
        {
            double        timeScale = frequency * 2 * Math.PI / (double)44100;
            SoundDelegate fn        = delegate(int i) { return(Math.Sin(i * timeScale)); };

            return(create16bit_sound(fn, frequency, amp, duration, true));
        }
Пример #3
0
        /// <summary>
        /// Thread that processes FMOD calls.
        /// </summary>
        private void CommandLoop()
        {
            // Initialze a bunch of static values
            UpVector.x   = 0.0f;
            UpVector.y   = 1.0f;
            UpVector.z   = 0.0f;
            ZeroVector.x = 0.0f;
            ZeroVector.y = 0.0f;
            ZeroVector.z = 0.0f;

            allSounds   = new Dictionary <IntPtr, MediaObject>();
            allChannels = new Dictionary <IntPtr, MediaObject>();

            // Initialize the command queue.
            queue = new Queue <SoundDelegate>();

            // Initialize the FMOD sound package
            InitFMOD();
            initDone.Set();
            if (!SoundSystemAvailable)
            {
                return;
            }

            SoundDelegate action = null;

            while (true)
            {
                if (soundCancelToken.IsCancellationRequested)
                {
                    break;
                }
                // Wait for something to show up in the queue.
                lock (queue)
                {
                    while (queue.Count == 0)
                    {
                        Monitor.Wait(queue);
                    }
                    action = queue.Dequeue();
                }

                if (soundCancelToken.IsCancellationRequested)
                {
                    break;
                }
                // We have an action, so call it.
                try
                {
                    action();
                    action = null;
                }
                catch (Exception e)
                {
                    Logger.Log("Error in sound action:\n    " + e.Message + "\n" + e.StackTrace,
                               Helpers.LogLevel.Error);
                }
            }
        }
Пример #4
0
 protected virtual void Start()
 {
     _scoreScript = GameObject.FindWithTag(Tags.UITag).GetComponent<Score>();
     _cameraShakeScript = GameObject.FindWithTag(Tags.mainCameraTag).GetComponent<CameraShake>();
     _objectPoolScript = GameObject.FindWithTag(Tags.objectPoolTag).GetComponent<ObjectPool>();
     _sounds = GameObject.FindWithTag("SoundsObject").GetComponent<Sounds>();
     source = _sounds.GetComponent<AudioSource>();
     soundDelegate = playSound;
 }
Пример #5
0
 protected virtual void Start()
 {
     _scoreScript       = GameObject.FindWithTag(Tags.UITag).GetComponent <Score>();
     _cameraShakeScript = GameObject.FindWithTag(Tags.MainCameraTag).GetComponent <CameraShake>();
     _objectPoolScript  = GameObject.FindWithTag(Tags.ObjectPoolTag).GetComponent <ObjectPool>();
     _sounds            = GameObject.FindWithTag("SoundsObject").GetComponent <Sounds>();
     _source            = _sounds.GetComponent <AudioSource>();
     _soundDelegate     = playSound;
 }
Пример #6
0
        // frequency is unused.
        public byte[] whitenoise(double frequency, double amp, double duration)
        {
            Random        r  = new Random();
            SoundDelegate fn = delegate(int i)
            {
                return(r.NextDouble() * 2 - 1.0);  //random number between -1 and 1
            };

            return(create16bit_sound(fn, frequency, amp, duration, false));
        }
Пример #7
0
 private void Start()
 {
     _cameraShakeScript  = GameObject.FindWithTag(Tags.MainCameraTag).GetComponent <CameraShake>();
     soundDelegate       = playSound;
     _sounds             = GameObject.FindWithTag("SoundsObject").GetComponent <Sounds>();
     source              = _sounds.GetComponent <AudioSource>();
     _playerScript       = GetComponentInParent <Player>();
     _cooldownManager    = GetComponentInParent <Cooldowns>();
     this.gameObject.tag = Tags.AbilityButtonsTag;
 }
Пример #8
0
 void OnDisable()
 {
     // Debug.Log("ondisable");
     AudioQueues.Clear();
     queuesIndex    = -1;
     OnStartPlay    = null;
     OnStopPlay     = null;
     OnPausePlay    = null;
     source.enabled = false;
 }
Пример #9
0
        public byte[] square(double frequency, double amp, double duration)
        {
            double        period   = 44100.0 / frequency;
            double        cutpoint = (period / 2.0); //duty cycle could be changed here
            SoundDelegate fn       = delegate(int i)
            {
                return((i % period) > cutpoint ? 1.0 : -1.0);
            };

            return(create16bit_sound(fn, frequency, amp, duration, true));
        }
Пример #10
0
        public byte[] sawtooth(double frequency, double amp, double duration)
        {
            double        period = 44100.0 / frequency;
            double        slope  = 2 / period; //because it goes from -1 to 1 in one period
            SoundDelegate fn     = delegate(int i)
            {
                return((i % period) * slope - 1);
            };

            return(create16bit_sound(fn, frequency, amp, duration, true));
        }
Пример #11
0
        /// <summary>
        /// Put a delegate call on the command queue.  These will be executed on
        /// the FMOD control thread.   All FMOD calls must happen there.
        /// </summary>
        /// <param name="action"></param>
        protected void invoke(SoundDelegate action)
        {
            // Do nothing if queue not ready yet.
            if (queue == null)
            {
                return;
            }

            // Put that on the queue and wake up the background thread.
            lock (queue)
            {
                queue.Enqueue(action);
                Monitor.Pulse(queue);
            }
        }
Пример #12
0
 void Awake()
 {
     if (instance == null)
     {
         instance = this;
         DontDestroyOnLoad(this.gameObject);
     }
     else if (instance != this)
     {
         UnityEngine.Debug.LogWarning("SingleTone Error : " + this.name);
         Destroy(gameObject);
     }
     bgm       = transform.Find("BGM").GetComponent <AudioSource>();
     monosound = transform.Find("MONO").GetComponent <AudioSource>();
 }
Пример #13
0
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
        else
        {
            UnityEngine.Debug.LogError("SingleTone Error : " + this.name);
            Destroy(gameObject);
        }

        bgm = transform.Find("BGM").GetComponent <SoundObject>();
    }
Пример #14
0
        public byte[] triangle(double frequency, double amp, double duration)
        {
            double        period = 44100.0 / frequency;
            double        slope  = 4 / period; // it goes from -1 to 1 in half period
            SoundDelegate fn     = delegate(int i)
            {
                double v = (i % period);
                if (v < period / 2.0)
                {
                    return(v * slope - 1);
                }
                else
                {
                    return((v - period / 2) * -slope + 1);
                }
            };

            return(create16bit_sound(fn, frequency, amp, duration, true));
        }
Пример #15
0
        public byte[] sine_sum(double[] frequencies, double[] weights, double amp, double duration)
        {
            double[] timeScales = new double[frequencies.Length];
            for (int i = 0; i < frequencies.Length; i++)
            {
                timeScales[i] = frequencies[i] * 2.0 * Math.PI / (double)44100;
            }

            double sumweights = 0;

            for (int i = 0; i < weights.Length; i++)
            {
                sumweights += weights[i];
            }

            SoundDelegate fn = delegate(int t)
            {
                double val = 0;
                for (int i = 0; i < timeScales.Length; i++)
                {
                    val += Math.Sin(t * timeScales[i]) * weights[i];
                }
                val /= sumweights; //normalize
                if (val > 1)
                {
                    return(1);
                }
                else if (val < -1)
                {
                    return(-1);
                }
                else
                {
                    return(val);
                }
            };

            return(create16bit_sound(fn, 1, amp, duration, false)); // not necessarily periodic
        }
Пример #16
0
        // frequency is here interpreted as a factor
        public byte[] rednoise(double frequency, double amp, double duration)
        {
            double factor = frequency / 440; // ? scales distance that the "particle" moves

            Random        r        = new Random();
            double        location = 0.0; //random walk
            SoundDelegate fn       = delegate(int i)
            {
                location += (r.NextDouble() * factor - factor / 2);
                if (location > 1)
                {
                    location = 1;
                }
                else if (location < -1)
                {
                    location = -1;
                }
                return(location);
            };

            return(create16bit_sound(fn, frequency, amp, duration, false));
        }
Пример #17
0
        /// <summary>
        /// Put a delegate call on the command queue.  These will be executed on
        /// the FMOD control thread.   All FMOD calls must happen there.
        /// </summary>
        /// <param name="action"></param>
        protected void invoke(SoundDelegate action)
        {
            // Do nothing if queue not ready yet.
            if (queue == null) return;

            // Put that on the queue and wake up the background thread.
            lock (queue)
            {
                queue.Enqueue( action );
                Monitor.Pulse(queue);
            }
        }
Пример #18
0
        // helper function for creating sound
        private byte[] create16bit_sound(SoundDelegate fn, double frequency, double amp, double duration, bool periodic)
        {
            int sampleRate = 44100;
            int length = (int)(sampleRate * duration);
            byte[] wavedata = new byte[length * 2];

            int waveformPeriod = (int)(sampleRate / frequency);
            for (int i = 0; i < length; i++)
            {
                if (i <= waveformPeriod || !periodic)
                {
                    double dbl = fn(i);
                    short sh = (short)(dbl * amp * short.MaxValue);

                    wavedata[i * 2] = (byte)(sh & 0x00FF); // low byte
                    wavedata[i * 2 + 1] = (byte)(sh >> 8); // high byte
                }
                else  // we have already computed the wave, it is periodic. Good optimization!
                {
                    int prevspot = i % waveformPeriod;
                    wavedata[i * 2] = wavedata[prevspot * 2];
                    wavedata[i * 2 + 1] = wavedata[prevspot * 2 + 1];
                }
            }
            return wavedata;
        }
 public Cat(SoundDelegate soundDelagate)
 {
     Sound = soundDelagate;
 }