Esempio n. 1
0
        internal SoundEffect(string fileName)
        {
            AudioEngine.EnsureInit();

            buffer = AL.GenBuffer();

            var error = AL.GetError();

            if (error != ALError.NoError)
            {
                throw new OpenALException(error, "borked generation. ALError: " + error.ToString());
            }

            XRamExtension XRam = new XRamExtension();

            if (XRam.IsInitialized)
            {
                XRam.SetBufferMode(1, ref buffer, XRamExtension.XRamStorage.Hardware);
            }



            AudioReader sound     = new AudioReader(fileName);
            var         sounddata = sound.ReadToEnd();

            AL.BufferData(buffer, sounddata.SoundFormat.SampleFormatAsOpenALFormat, sounddata.Data, sounddata.Data.Length, sounddata.SoundFormat.SampleRate);
            error = AL.GetError();
            if (error != ALError.NoError)
            {
                throw new OpenALException(error, "unable to read " + fileName);
            }

            _name = Path.GetFileNameWithoutExtension(fileName);
        }
Esempio n. 2
0
        internal OpenTKAudioCue(Stream data, AudioContext ac)
        {
            this.ac = ac;

            buffer = AL.GenBuffer();
            ac.CheckErrors();

            source = AL.GenSource();
            ac.CheckErrors();

            AL.Source(source, ALSourcef.Gain, (float)this.Volume);
            ac.CheckErrors();

            using (AudioReader ar = new AudioReader(data))
            {
                SoundData d = ar.ReadToEnd();
                AL.BufferData(source, d);
                ac.CheckErrors();
            }

            AL.Source(source, ALSourcei.Buffer, buffer);
            ac.CheckErrors();

            this.VolumeChanged  += new VolumeChangedEventHandler(OpenTKAudioCue_VolumeChanged);
            this.BalanceChanged += new BalanceChangedEventHandler(OpenTKAudioCue_BalanceChanged);
            this.FadeChanged    += new FadeChangedEventHandler(OpenTKAudioCue_FadeChanged);
        }
Esempio n. 3
0
		public AL_SoundBuffer(Stream inStream)
		{
			using (AudioReader reader = new AudioReader(inStream))
			{
				buffer = AL.GenBuffer();
				AL.BufferData(buffer, reader.ReadToEnd());
			}
		}
Esempio n. 4
0
		public AL_Music(Stream inStream)
		{
			using (AudioReader reader = new AudioReader(inStream))
			{
				buffer = AL.GenBuffer();
				source = AL.GenSource();

				AL.BufferData(buffer, reader.ReadToEnd());
				AL.Source(source, ALSourcei.Buffer, buffer);
			}

			OnSetLoop(true);
		}
Esempio n. 5
0
        public void soundEngine()
        {
            AudioReader ar;
            int         buff;
            int         state;

            // start off at a capacity of 2, will double when necessary
            int [] currentSounds  = new int[2];
            int[]  soundBluePrint = new int[2]; // to build a list of sounds which will be copied
            // into currentSounds -- this is so I don't have to modify currentSounds while it
            // is being used, which could cause problems since it is a reference object
            int nSounds;  // current number of sounds

            while (true)
            {
                swh.WaitOne();
                lock (locker)
                {
                    if (!somethingToPlay)  // a Set may have been called while requests were processed
                                           // below
                    {
                        continue;
                    }
                }
                int k;
                lock (locker)
                {
                    somethingToPlay = false;
                    nSounds         = 0;
                    if (currentSounds.Length < request.Length)
                    {
                        Array.Resize(ref currentSounds, request.Length);
                    }
                    for (k = 0; k < nrequests; k++)
                    {
                        buff = AL.GenBuffer();
                        currentSounds[nSounds] = AL.GenSource();
                        ar = new AudioReader(sound[request[k]]);
                        AL.BufferData(buff, ar.ReadToEnd());
                        AL.Source(currentSounds[nSounds], ALSourcei.Buffer, buff);
                        nSounds++;
                    }
                    // request array is used up, so clear it
                    if (request.Length > 2)
                    {
                        Array.Resize(ref request, 2);
                    }
                    nrequests = 0;
                }
                AL.SourcePlay(nSounds, currentSounds);
                do
                {
                    Thread.Sleep(100); // seems like I kind set it less than 100 without sound distortion
                    int nCurrentSounds = nSounds;
                    nSounds = 0;
                    // check if something new to play
                    // if new sounds are played, we cannot mess with the currentSounds array, which is being used --
                    // we have to build up a soundBluePrint array
                    lock (locker)
                    {
                        if (somethingToPlay)
                        {
                            somethingToPlay = false;
                            if (nCurrentSounds + request.Length > soundBluePrint.Length)
                            {
                                Array.Resize(ref soundBluePrint, nCurrentSounds + request.Length);
                            }
                            for (k = 0; k < nrequests; k++)
                            {
                                buff = AL.GenBuffer();
                                soundBluePrint[nSounds] = AL.GenSource();
                                ar = new AudioReader(sound[request[k]]);
                                AL.BufferData(buff, ar.ReadToEnd());
                                AL.Source(soundBluePrint[nSounds], ALSourcei.Buffer, buff);
                                nSounds++;
                            }
                            if (request.Length > 2)
                            {
                                Array.Resize(ref request, 2);
                            }
                            nrequests = 0;
                        }
                    }

                    // Query the sources to find out if any stop playing
                    int temp = nCurrentSounds;
                    for (int j = 0; j < temp; j++)
                    {
                        AL.GetSource(currentSounds[j], ALGetSourcei.SourceState, out state);
                        if ((ALSourceState)state == ALSourceState.Playing)
                        {
                            // I want to pause them here, so I can include them with the new
                            // sounds later -- if played again without being paused, they
                            // will start from the beginning.  I want them to be played
                            // simulatenously with the new sounds, but to pick up where
                            // they left off
                            AL.SourcePause(currentSounds[j]);
                            soundBluePrint[nSounds] = currentSounds[j];
                            nSounds++;
                        }
                        else
                        {
                            // free these resources for later sounds
                            AL.DeleteSource(currentSounds[j]);
                        }
                    }
                    if (nSounds > 0)
                    {
                        if (currentSounds.Length < nSounds)
                        {
                            Array.Resize(ref currentSounds, nSounds);
                        }
                        for (k = 0; k < nSounds; k++)
                        {
                            currentSounds[k] = soundBluePrint[k];
                        }
                        AL.SourcePlay(nSounds, currentSounds);
                    }
                } while (nSounds > 0);
            }
        }