Esempio n. 1
0
 internal GLAudioPlayThread(CachedSound data, GLAudioSource source, bool doLoop = false, float volume = 1.0f)
 {
     mData   = data;
     mSource = source;
     mDoLoop = doLoop;
     mVolume = volume;
 }
Esempio n. 2
0
        private static void TryInitAudio()
        {
            int tries = 0;

            while (mAudioOn == false && tries < 10)
            {
                try
                {
                    mDeviceID = Alc.OpenDevice(null);
                    KWLogger.Log("Initializing OpenAL (Attempt #" + tries + ")", "GLAudioEngine::TryInitAudio");
                    Console.WriteLine("Initializing audio engine OpenAL (Attempt #" + tries + ")... ");
                    int[] attributes = new int[0];
                    mContext = Alc.CreateContext(mDeviceID, attributes);
                    Alc.MakeContextCurrent(mContext);
                    var version = AL.Get(ALGetString.Version);
                    var vendor  = AL.Get(ALGetString.Vendor);

                    if (version == null)
                    {
                        KWLogger.Log("No Audio devices found.", "GLAudioEngine::TryInitAudio", LogLevel.Error);
                        throw new Exception("No Audio devices found.");
                    }

                    Console.Write('\t' + version + " " + vendor);
                    Console.WriteLine(" Init complete.");
                    KWLogger.Log("Audio initialized: " + version + " " + vendor, "GLAudioEngine::TryInitAudio");
                    mAudioOn = true;
                }

                catch (Exception)
                {
                    mAudioOn = false;
                }

                if (mAudioOn == false)
                {
                    tries++;
                    Thread.Sleep(500);
                }
            }
            IsInitializing = false;

            if (mAudioOn)
            {
                for (int i = 0; i < mChannels; i++)
                {
                    GLAudioSource s = new GLAudioSource();
                    mSources.Add(s);
                }
            }
            else
            {
                Console.WriteLine("\t\t(Giving up on initializing the audio engine. Sorry.)");
                KWLogger.Log("Giving up on initializing the audio engine. Sorry.", "GLAudioEngine::TryInitAudio", LogLevel.Error);
            }
        }
Esempio n. 3
0
        public static int SoundPlay(string sound, bool looping, float volume = 1.0f)
        {
            if (!mAudioOn)
            {
                Console.WriteLine("Error playing audio: audio device not available.");
                return(-1);
            }
            volume = volume >= 0 && volume <= 1.0f ? volume : 1.0f;

            CachedSound soundToPlay = null;

            if (CachedSounds.ContainsKey(sound))
            {
                soundToPlay = CachedSounds[sound];
            }
            else
            {
                soundToPlay = new CachedSound(sound);
                CachedSounds.Add(sound, soundToPlay);
            }

            GLAudioSource source        = null;
            int           channelNumber = -1;

            for (int i = 0; i < mChannels; i++)
            {
                if (!mSources[i].IsPlaying)
                {
                    source        = mSources[i];
                    channelNumber = i;
                    source.SetFileName(sound);
                    break;
                }
            }
            if (source == null)
            {
                Console.WriteLine("Error playing audio file: all " + mChannels + " channels are busy.");
                return(-1);
            }

            AL.Source(source.GetSourceId(), ALSourcei.Buffer, soundToPlay.GetBufferPointer());
            if (looping)
            {
                AL.Source(source.GetSourceId(), ALSourceb.Looping, true);
            }
            else
            {
                AL.Source(source.GetSourceId(), ALSourceb.Looping, false);
            }
            AL.Source(source.GetSourceId(), ALSourcef.Gain, volume);
            AL.SourcePlay(source.GetSourceId());
            return(channelNumber);
        }