/// <summary>
        /// Called when Extensions is initalised. Will not update removed or added drivers
        /// </summary>
        void GetRecordingInformation()
        {
            RESULT result = RESULT.OK;

            result = LowLevelSystem.getRecordNumDrivers(out NumberOfDrivers, out NumberOfConnectedDrivers);
            CheckResult(result, "FMOD.GetRecordNumDrivers");

            if (NumberOfDrivers == 0)
            {
                UnityEngine.Debug.LogWarning("FMOD Extensions: No recording drivers found!");
                return;
            }

            for (int i = 0; i < NumberOfDrivers; i++)
            {
                RecordDriver tempDriver = new RecordDriver();
                LowLevelSystem.getRecordDriverInfo(i, out tempDriver.name, Settings.Instance.DriverNameLength, out tempDriver.guid, out tempDriver.systemRate, out tempDriver.speakerMode, out tempDriver.speakerModeChannels, out tempDriver.state);
                tempDriver.id = i;

                if (!allRecordDrivers.ContainsKey(i))
                {
                    allRecordDrivers.Add(i, tempDriver);
                }
            }
        }
Exemple #2
0
        private Sound LoadNextSound(LowLevelSystem system)
        {
            if (++_soundIndex >= _sounds.Length)
            {
                _soundIndex = 0;
            }

            string fileName = GetContentPath(_sounds[_soundIndex]);

            Console.WriteLine("Loading {0}", _sounds[_soundIndex]);

            var sound = system.CreateStream(name: fileName, mode: Mode.Default);

            if (_currentSound.HasValue)
            {
                // Because of some difference between Mono and CLR we can't dispose the sound here
                // while being called from the callback. The sound has finished playing, but for some
                // reason Mono will crash in the callback handler from the native side if we dispose here.
                // Putting it in a list to be disposed later worked good on Mono. CLR didn't have this issue.
                _disposeList.Add(_currentSound.Value);
            }

            _currentSound = sound;

            return(sound);
        }
        // TODO: Check for more effective way of clearing removed drivers and adding new ones
        /// <summary>
        /// Called if the number of recording drivers is not equal to the number of drivers we have reference to.
        /// Will completely clear and refill our dictionary of drivers.
        /// Plus, it will stop all recording sounds.
        /// </summary>
        void RefreshRecordingDrivers()
        {
            if (OnDriverRefresh != null)
            {
                OnDriverRefresh.Invoke();
            }

            // Remove all our previous drivers as they're going to probably be in the wrong order or just not there anymore
            allRecordDrivers.Clear();

            // Get number of drivers
            RESULT result = RESULT.OK;

            result = LowLevelSystem.getRecordNumDrivers(out NumberOfDrivers, out NumberOfConnectedDrivers);
            CheckResult(result, "FMOD.GetRecordNumDrivers");

            // Add our new list of drivers
            for (int i = 0; i < NumberOfDrivers; i++)
            {
                RecordDriver tempDriver = new RecordDriver();
                LowLevelSystem.getRecordDriverInfo(i, out tempDriver.name, Settings.Instance.DriverNameLength, out tempDriver.guid, out tempDriver.systemRate, out tempDriver.speakerMode, out tempDriver.speakerModeChannels, out tempDriver.state);
                tempDriver.id = i;
                allRecordDrivers.Add(tempDriver.id, tempDriver);
            }

            // If there are sounds recording, stop them.
            // (Assumption) FMOD will stop recording anyway as connections will be lost
            if (recordingSounds.Count > 0)
            {
                foreach (KeyValuePair <int, RecordingSound> entry in recordingSounds)
                {
                    entry.Value.Stop();
                }
            }
        }
Exemple #4
0
        private Channel PlaySound(LowLevelSystem system, Sound sound)
        {
            Console.WriteLine("Playing");

            //Play the sound, but start it paused
            var channel = system.PlaySound(sound, null, true);

            //Set a callback on the channel, this is called for all events on the channel
            channel.SetCallback((type, data1, data2) =>
            {
                //When we get the "end" event start playing the next sound
                if (type == ChannelControlCallbackType.End)
                {
                    Console.WriteLine("Callback: Finished, playing next sound");
                    PlaySound(system, LoadNextSound(system));
                }
            });

            //Unpause the channel
            channel.Pause = false;

            //Save this channel
            _currentChannel = channel;

            return(channel);
        }
Exemple #5
0
        public override void Execute()
        {
            // Create a LowLevelSystem - this is the most basic system and has no support for FMOD studio projects
            using (var system = new LowLevelSystem())
            {
                // Set up the demo to call update on the system
                Pump(system);

                // Create a new sound object
                using (var sound = system.CreateSound(name: GetContentPath("Front_Center.wav"), mode: Mode.LoopNormal))
                {
                    Console.WriteLine("Playing sound, Length {0}ms", sound.GetLength(TimeUnit.Milliseconds));

                    // Begin playing the sound, this returns the "channel" which is playing this sound
                    var channel = system.PlaySound(sound, null, false);

                    // Wait until any key is pressed
                    WaitForKeypress(() => Console.WriteLine("Position {0} ms", channel.GetPosition(TimeUnit.Milliseconds)));

                    // Stop the sound playing
                    channel.Stop();

                    Console.WriteLine(channel.IsPlaying);
                }
            }
        }
        public void TestMethod1()
        {
            using (var system = new LowLevelSystem(preInit: a => { a.Output = OutputMode.NoSound; }))
            {
                //Load a sound to play
                var sound = system.CreateStream(name: "Content/Front_Center.wav", mode: Mode.Default);

                // Begin playing the sound
                var channel = system.PlaySound(sound, null, false);

                bool flag = true;

                channel.SetCallback((type, data1, data2) =>
                {
                    if (type == ChannelControlCallbackType.End)
                    {
                        //Now that the sound is done, dispose it
                        //With issue #23 this should crash on mono
                        sound.Dispose();

                        flag = false;
                    }
                });

                //Loop until the flag is set to false
                while (flag)
                {
                    system.Update();
                }
            }
        }
Exemple #7
0
        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // Dispose our LowLevelSystem class
                    LowLevelSystem.Dispose();
                    LowLevelSystem = null;
                }

                _system.release().Check();

                _disposed = true;
            }
        }
        public override void Execute()
        {
            // Create a LowLevelSystem - this is the most basic system and has no support for FMOD studio projects
            using (var system = new LowLevelSystem())
            {
                // Set up the demo to call update on the system
                Pump(system);

                // Create a new sound object
                using (var sound = system.CreateSound(name: GetContentPath("test.wav"), mode: Mode.Default))
                {
                    // Begin playing the sound, this returns the "channel" which is playing this sound
                    var channel = system.PlaySound(sound, null, false);

                    channel.SetCallback((type, data1, data2) =>
                    {
                        if (type == ChannelControlCallbackType.End)
                        {
                            Console.WriteLine("Callback: Finished playing sound");
                        }
                    });

                    //temp: Force GC collection to test callback handling
                    GC.Collect();

                    // Wait until any key is pressed
                    WaitForKeypress(() =>
                    {

                        //temp: Force GC collection to test callback handling
                        GC.Collect();

                        var pos = channel.GetPosition(TimeUnit.Milliseconds);
                        if (pos.HasValue)
                            Console.WriteLine("Position {0}ms", pos.Value);
                    });

                    // Stop the sound playing
                    channel.Stop();
                }
            }
        }
Exemple #9
0
        public override void Execute()
        {
            // Create a LowLevelSystem - this is the most basic system and has no support for FMOD studio projects
            using (var system = new LowLevelSystem())
            {
                // Set up the demo to call update on the system
                Pump(system);

                //Load up the first sound
                var startSound = LoadNextSound(system);

                // Begin playing the sound, this returns the "channel" which is playing this sound
                // See inside the PlaySound method for how the callback is setup and used
                PlaySound(system, startSound);

                //temp: Force GC collection to test callback handling
                GC.Collect();

                // Wait until any key is pressed
                WaitForKeypress(() =>
                {
                    // Dispose sounds that aren't used anymore
                    _disposeList.ForEach(x => x.Dispose());
                    _disposeList.Clear();

                    //temp: Force GC collection to test callback handling
                    GC.Collect();

                    var pos = _currentChannel?.GetPosition(TimeUnit.Milliseconds);
                    if (pos.HasValue)
                    {
                        Console.WriteLine("Position {0}ms", pos.Value);
                    }
                });

                // Stop the sound playing
                _currentChannel?.Stop();
            }

            Console.WriteLine("Done");
        }
Exemple #10
0
        public System(int maxChannels = 1024, InitFlags flags = InitFlags.LiveUpdate, LowLevelInitFlags lowLevelFlags = LowLevelInitFlags.Normal, AdvancedInitializationSettings advancedSettings = default(AdvancedInitializationSettings), Action<IPreInitilizeLowLevelSystem> preInit = null)
        {
            //Load native dependencies
            Native.Load();

            //Create studio system
            FMOD.Studio.System.create(out _system).Check();

            //Create low level system
            FMOD.System lowLevel;
            _system.getLowLevelSystem(out lowLevel).Check();
            LowLevelSystem = new LowLevelSystem(lowLevel);

            if (preInit != null)
                preInit(LowLevelSystem);

            //Set advanced settings
            LowLevelSystem.SetAdvancedSettings(advancedSettings);

            //Initialize
            _system.initialize(maxChannels, (INITFLAGS)flags, (FMOD.INITFLAGS)lowLevelFlags, IntPtr.Zero).Check();
        }
Exemple #11
0
        public System(int maxChannels = 1024, InitFlags flags = InitFlags.LiveUpdate, LowLevelInitFlags lowLevelFlags = LowLevelInitFlags.Normal, AdvancedInitializationSettings advancedSettings = default(AdvancedInitializationSettings), Action <IPreInitilizeLowLevelSystem> preInit = null)
        {
            //Load native dependencies
            Native.Load();

            //Create studio system
            FMOD.Studio.System.create(out _system).Check();

            //Create low level system
            FMOD.System lowLevel;
            _system.getLowLevelSystem(out lowLevel).Check();
            LowLevelSystem = new LowLevelSystem(lowLevel);

            if (preInit != null)
            {
                preInit(LowLevelSystem);
            }

            //Set advanced settings
            LowLevelSystem.SetAdvancedSettings(advancedSettings);

            //Initialize
            _system.initialize(maxChannels, (INITFLAGS)flags, (FMOD.INITFLAGS)lowLevelFlags, IntPtr.Zero).Check();
        }
        private IEnumerator WaitForBuffer(RecordingSound recordingSound)
        {
            RESULT result = RESULT.OK;

            while (recordingSound.recordPos < Settings.Instance.RecordingLatency)
            {
                bool recording = false;
                result = LowLevelSystem.isRecording(recordingSound.driver.id, out recording);

                if (!recording)
                {
                    recordingSound.Stop();
                    continue;
                }
                else
                {
                    result = LowLevelSystem.getRecordPosition(recordingSound.driver.id, out recordingSound.recordPos);
                    CheckResult(result, "FMOD.GetRecordPosition");
                }
                yield return(null);
            }
            PlayRecording(recordingSound);
            yield return(null);
        }
        void Update()
        {
            // Get the listener position
            LowLevelSystem.get3DListenerAttributes(0, out pos, out vel, out forward, out up);
            listPos = ExtensionsUtils.ToUnityVector(pos);

            // Update the position of the low level sounds
            for (int i = 0; i < attachedInstances.Count; i++)
            {
                if (!attachedInstances[i].channel.hasHandle() || attachedInstances[i].transform == null)
                {
                    attachedInstances.RemoveAt(i);
                    continue;
                }
                VECTOR instancePos = attachedInstances[i].transform.position.ToFMODVector();
                VECTOR zero        = new VECTOR();
                VECTOR alt         = new VECTOR();

                if (attachedInstances[i].rigidbody)
                {
                    VECTOR instanceVel = attachedInstances[i].rigidbody.velocity.ToFMODVector();
                    attachedInstances[i].channel.set3DAttributes(ref instancePos, ref instanceVel, ref alt);
                }
                attachedInstances[i].channel.set3DAttributes(ref instancePos, ref zero, ref alt);
            }

            // Check currently playing programmer sounds and remove them from the list if they stop playing or are invalid
            for (int i = 0; i < programmerInstances.Count; i++)
            {
                if (!programmerInstances[i].hasHandle() || !programmerInstances[i].isValid())
                {
                    UnityEngine.Debug.Log("Removing programmer sound from list");
                    programmerInstances.RemoveAt(i);
                }
            }
        }
Exemple #14
0
        public override void Execute()
        {
            // Create a LowLevelSystem - this is the most basic system and has no support for FMOD studio projects
            using (var system = new LowLevelSystem())
            {
                // Set up the demo to call update on the system
                Pump(system);

                // Create a new sound object
                using (var sound = system.CreateSound(name: GetContentPath("test.wav"), mode: Mode.LoopNormal))
                {
                    // Begin playing the sound, this returns the "channel" which is playing this sound
                    var channel = system.PlaySound(sound, null, false);

                    // Wait until any key is pressed
                    WaitForKeypress(() => Console.WriteLine("Position {0} ms", channel.GetPosition(TimeUnit.Milliseconds)));

                    // Stop the sound playing
                    channel.Stop();

                    Console.WriteLine(channel.IsPlaying);
                }
            }
        }
 void SetupCallback()
 {
     LowLevelSystem.setCallback(SystemCallback, FMOD.SYSTEM_CALLBACK_TYPE.DEVICELISTCHANGED);
 }
Exemple #16
0
 protected void Pump(LowLevelSystem system)
 {
     _lowLevelSystem = system;
 }
Exemple #17
0
 public void CreateSystem()
 {
     using (var sys = new LowLevelSystem(preInit: TestInitSystem))
     {
     }
 }
 public void CreateSystem()
 {
     using (var sys = new LowLevelSystem(preInit: TestInitSystem))
     {
     }
 }
Exemple #19
0
 protected void Pump(LowLevelSystem system)
 {
     _lowLevelSystem = system;
 }