Beispiel #1
0
        public OpenALAudio()
        {
            Instance = this;

            m_enableSound = true;
            m_soundVolume = 1.0f;
            m_enableMusic = true;
            m_musicVolume = 1.0f;

            // Init context
            m_context = new AudioContext();
            m_xram    = new XRamExtension();

            // Create some sources
            m_sound = new OpenALSoundSource[NUM_SOUND_SOURCES];
            for (int i = 0; i < m_sound.Length; ++i)
            {
                m_sound[i] = new OpenALSoundSource();
            }
            m_music  = new OpenALMusicPlayback[NUM_MUSIC_SOURCES];
            m_custom = new OpenALCustomPlayback[NUM_CUSTOM_SOURCES];

            UpdateSoundVolume();
            UpdateMusicVolume();
            App.CheckOpenALError();
        }
Beispiel #2
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);
		}
Beispiel #3
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);
        }
        public XRamDiagnostic()
        {
            Trace.WriteLine("--- X-RAM related errors ---");

            XRamExtension XRam = new XRamExtension();

            if (XRam.IsInitialized)
            {
                XRamFound = true;

                RamTotal = XRam.GetRamSize;
                RamFree  = XRam.GetRamFree;

                uint buffer;
                AL.GenBuffer(out buffer);

                foreach (XRamExtension.XRamStorage m in storagemodes)
                {
                    bool result = XRam.SetBufferMode(1, ref buffer, m);
                    BufferModes.Add(m.ToString(), m == XRam.GetBufferMode(ref buffer));
                }

                AL.DeleteBuffer(ref buffer);
            }
            else
            {
                XRamFound = false;
            }
        }
Beispiel #5
0
        public void Load(string filename)
        {
            XRamExtension XRam = new XRamExtension();

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

            BinaryReader br = new BinaryReader(File.OpenRead(filename));

            byte[] bytes = new byte[1];
            if (new string(br.ReadChars(4)) != "caff")
            {
                throw new Exception("input file not caff");
            }

            br.ReadBytes(4);             // rest of caf file header
            cafInfo = new CAFAudioFormat();

            do
            {
                string type = new string(br.ReadChars(4));
                long   size = BitConverter.ToInt64(br.ReadBytes(8).Reverse().ToArray(), 0);

                if (type == "data")
                {
                    bytes = new byte[size];
                    bytes = br.ReadBytes((int)size);
                }
                else if (type == "desc")
                {
                    cafInfo.InitWithData(br.ReadBytes((int)size));
                }
                else
                {
                    br.ReadBytes((int)size);
                }
            } while (bytes.Length == 1);

            br.Close();

            IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(bytes.Length);

            System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bytes.Length);
            AL.BufferData((uint)Buffer, cafInfo.GetALFormat(), ptr, bytes.Length, (int)cafInfo.mSampleRate);

            ALError error = AL.GetError();

            if (error != ALError.NoError)
            {
                // respond to load error etc.
                Console.WriteLine("borked buffer load. ALError: " + error.ToString());
            }

            AL.Source(Source, ALSourcei.Buffer, (int)Buffer);              // attach the buffer to a source
        }
Beispiel #6
0
        /// <summary>
        /// Initializes the audio manager.
        /// </summary>
        /// <param name="channels">The number of channels to use.</param>
        /// <param name="buffersPerChannel">The number of buffers each channel will contain.</param>
        /// <param name="bytesPerBuffer">The number of bytes in each buffer.</param>
        /// <param name="launchThread">If true, a separate thread will be launched to handle updating the sound manager.  
        ///                            Otherwise, a thread will not be launched and manual calls to Update() will be required.</param>
        public AudioManager(int channels, int buffersPerChannel, int bytesPerBuffer, bool launchThread)
        {
            if (instance != null) throw new Exception("Can't create more than one instances of AudioManager");

            if(AudioContext.CurrentContext == null)
                new AudioContext();
            Efx = new EffectsExtension();
            XRam = new XRamExtension();

            Init(channels, buffersPerChannel, bytesPerBuffer, launchThread);
        }
Beispiel #7
0
        public static void Init()
        {
            ac = new AudioContext();
            ac.CheckErrors();
            ac.MakeCurrent();
            eax_sup = ac.SupportsExtension("EAX3.0");
            if (eax_sup)
                xram = new XRamExtension();

            mp3_sup = ac.SupportsExtension("AL_EXT_mp3");
            devices = Alc.GetString(IntPtr.Zero, AlcGetStringList.AllDevicesSpecifier);
        }
Beispiel #8
0
        public static void Init()
        {
            ac = new AudioContext();
            ac.CheckErrors();
            ac.MakeCurrent();
            eax_sup = ac.SupportsExtension("EAX3.0");
            if (eax_sup)
            {
                xram = new XRamExtension();
            }

            mp3_sup = ac.SupportsExtension("AL_EXT_mp3");
            devices = Alc.GetString(IntPtr.Zero, AlcGetStringList.AllDevicesSpecifier);
        }
        /// <summary>
        /// Initializes OpenAL and loads two sound clips.
        /// </summary>
        public InteractivePlayerForm()
        {
            // Setup OpenTK audio stuff
            AudioContext ac = new AudioContext();
            XRamExtension xram = new XRamExtension();

            // Load the .ogg files
            guitar = new AudioClip("GuitarSample.ogg");
            boing = new AudioClip("BoingSample.ogg");

            externalClip = null;

            InitializeComponent();
        }
        /// <summary>
        /// Initialize the <see cref="SoundSystem"/>.
        /// </summary>
        /// <param name="bufferSize"></param>
        public void Initialize(int bufferSize = DEFAULT_BUFFER_SIZE)
        {
            if (_context == null)
            {
                _context = new ALContext();
            }

            BufferSize = bufferSize;

            XRam = new XRamExtension();
            Efx  = new EffectsExtension();

            // Init empty sources
            _sources = AL.GenSources(MAXIMUM_NUMBER_OF_SOURCES);
            ALChecker.CheckError();
        }
        static void Main(string[] args)
        {
            // Setup OpenTK audio stuff
            AudioContext ac = new AudioContext();
            XRamExtension xram = new XRamExtension();

            // Load the .ogg files
            string guitarFile = "GuitarSample.ogg";
            AudioClip guitarClip = new AudioClip(guitarFile);

            string boingFile = "BoingSample.ogg";
            AudioClip boingClip = new AudioClip(boingFile);

            // Play it.  The first time a clip is played, a background thread is
            // launched to handle all audio.  Playing the clip again or playing
            // a new clip will use the already existing thread.  To avoid
            // threading, construct your own AudioManager.
            guitarClip.Play();

            // Play the "boing" clip every 2 seconds during the guitar clip
            boingClip.Play();
            Thread.Sleep(2000);

            boingClip.Play();
            Thread.Sleep(2000);

            boingClip.Play();
            Thread.Sleep(2000);

            // Finally play some overlapping sounds to demonstrate a sound can
            // have more than one instance played at a time.
            boingClip.Play();
            Thread.Sleep(200);

            boingClip.Play();
            Thread.Sleep(200);

            boingClip.Play();
            Thread.Sleep(200);

            boingClip.Play();
            Thread.Sleep(200);

            // Give the last "boing" time to finish
            Thread.Sleep(800);
        }
        static void Main()
        {

            //string test = "Hydrate-Kenny_Beltrey.ogg";
            //AudioFile testFile = new AudioFile(test);
            //testFile.Play();
            // The 'using' idiom guarantees proper resource cleanup.
            // We request 30 UpdateFrame events per second, and unlimited
            // RenderFrame events (as fast as the computer can handle). (may change this in the future, or set it as an option)
            
            // Initialize the audio context and xram for sound
            AudioContext ac = new AudioContext();
            XRamExtension xr = new XRamExtension();
            using (GameEngine engine = new GameEngine())
            {
                engine.Run();
            }
        }
		/// <summary>
		/// Ends the execution of the application.
		/// </summary>
		public static void Terminate()
		{
			if (renderingThread != null)
			{
				renderingThread.Abort();
				renderingThread = null;

				wnd.Exit();

				lock (audioBuffers)
				{
					foreach (KeyValuePair<int, int> Pair in audioSources)
						AL.DeleteSource(Pair.Key);

					foreach (KeyValuePair<int, bool> Pair in audioBuffers)
						AL.DeleteBuffer(Pair.Key);

					audioSources.Clear();
					audioBuffers.Clear();
				}

				if (audioContext != null)
				{
					audioContext.Dispose();
					audioContext = null;
					xRam = null;
				}

				Environment.Exit(1);
			}
		}
        public void Load(string filename)
        {
            XRamExtension XRam = new XRamExtension();
            if (XRam.IsInitialized)
                XRam.SetBufferMode(1, ref Buffer, XRamExtension.XRamStorage.Hardware);

            BinaryReader br = new BinaryReader(File.OpenRead(filename));
            byte[] bytes = new byte[1];
            if (new string(br.ReadChars(4)) != "caff")
                throw new Exception("input file not caff");

            br.ReadBytes(4); // rest of caf file header
            cafInfo = new CAFAudioFormat();

            do {
                string type = new string(br.ReadChars(4));
                long size = BitConverter.ToInt64(br.ReadBytes(8).Reverse().ToArray(), 0);

                if (type == "data")
                {
                    bytes = new byte[size];
                    bytes = br.ReadBytes((int)size);
                }
                else if (type == "desc")
                {
                    cafInfo.InitWithData(br.ReadBytes((int)size));
                }
                else
                {
                    br.ReadBytes((int)size);
                }
            } while (bytes.Length == 1);

            br.Close();

            IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(bytes.Length);
            System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bytes.Length);
            AL.BufferData((uint)Buffer, cafInfo.GetALFormat(), ptr, bytes.Length, (int)cafInfo.mSampleRate);

            ALError error = AL.GetError();
            if (error != ALError.NoError)
            {
               // respond to load error etc.
                Console.WriteLine("borked buffer load. ALError: " + error.ToString());
            }

            AL.Source(Source, ALSourcei.Buffer, (int)Buffer ); // attach the buffer to a source
        }
        public XRamDiagnostic()
        {
            Trace.WriteLine("--- X-RAM related errors ---");

            XRamExtension XRam = new XRamExtension();
            if (XRam.IsInitialized)
            {
                XRamFound = true;

                RamTotal = XRam.GetRamSize;
                RamFree = XRam.GetRamFree;

                uint buffer;
                AL.GenBuffer(out buffer);

                foreach (XRamExtension.XRamStorage m in storagemodes)
                {
                    bool result = XRam.SetBufferMode(1, ref buffer, m);
                    BufferModes.Add(m.ToString(), m == XRam.GetBufferMode(ref buffer));
                }

                AL.DeleteBuffer(ref buffer);
            }
            else
                XRamFound = false;
        }
Beispiel #16
0
        public Audio(string[] wavFilesNames)
        {
            AC = new AudioContext();
            var XRam = new XRamExtension(); // must be instantiated per used Device if X-Ram is desired.

            // reserve n Handles
            myBuffers = AL.GenBuffers(wavFilesNames.Length);
            mySources = new int[wavFilesNames.Length];

            int count = 0;

            foreach (string fileName in wavFilesNames)
            {
                if (XRam.IsInitialized)
                {
                    XRam.SetBufferMode(1, ref myBuffers[count], XRamExtension.XRamStorage.Hardware); // optional
                }

                // Load a .wav file from disk. See example code at:
                // https://github.com/opentk/opentk/blob/develop/Source/Examples/OpenAL/1.1/Playback.cs#L21
                int channels, bits_per_sample, sample_rate;
                var sound_data = LoadWave(
                    File.Open(fileName, FileMode.Open),
                    out channels,
                    out bits_per_sample,
                    out sample_rate);
                var sound_format =
                    channels == 1 && bits_per_sample == 8 ? ALFormat.Mono8 :
                    channels == 1 && bits_per_sample == 16 ? ALFormat.Mono16 :
                    channels == 2 && bits_per_sample == 8 ? ALFormat.Stereo8 :
                    channels == 2 && bits_per_sample == 16 ? ALFormat.Stereo16 :
                    (ALFormat)0; // unknown

                AL.BufferData(myBuffers[count], sound_format, sound_data, sound_data.Length, sample_rate);
                if (AL.GetError() != ALError.NoError)
                {
                    // respond to load error etc.
                }



                AL.GenSources(1, out mySources[count]);                               // gen 1 Source Handles

                AL.Source(mySources[count], ALSourcei.Buffer, (int)myBuffers[count]); // attach the buffer to a source

                count++;
            }



            /*
             * // Create a sinus waveform through parameters, this currently requires Alut.dll in the application directory
             * if (XRam.IsInitialized)
             * {
             *  XRam.SetBufferMode(ref MyBuffers[1], XRamStorage.Hardware); // optional
             * }
             * MyBuffers[1] = Alut.CreateBufferWaveform(AlutWaveform.Sine, 500f, 42f, 1.5f);
             */
            // See next book page how to connect the buffers to sources in order to play them.

            // Cleanup on application shutdown
            //AL.DeleteBuffers(MyBuffers.Length, MyBuffers); // free previously reserved Handles
            //AC.Dispose();
        }
 public void StartAudioServices()
 {
     AudioContext ac = new AudioContext();
     XRamExtension xram = new XRamExtension();
 }
		private static void CheckAudioInstalled()
		{
			if (audioContext == null)
			{
				audioContext = new AudioContext();
				xRam = new XRamExtension();
			}
		}