Пример #1
0
        public void LoadSound(string soundId, string path)    //读取声音资源
        {
            //Generate a buffer
            int buffer = -1;

            Al.alGenBuffers(1, out buffer);
            int errorCode = Al.alGetError();

            if (errorCode != Al.AL_NO_ERROR)
            {
                return;
            }

            int   format;
            float frequency;
            int   size;

            if (!System.IO.File.Exists(path))
            {
                return;
            }
            IntPtr data = Alut.alutLoadMemoryFromFile(path, out format, out size, out frequency);

            if (data == IntPtr.Zero)
            {
                return;
            }
            //Load wav data into the generated buffer
            Al.alBufferData(buffer, format, data, size, (int)frequency);
            //Everything seems ok, add it to the libarary
            _soundIdentifiler.Add(soundId, new SoundSource(buffer, path));

            System.Diagnostics.Debug.WriteLine("LoadSound's buffer: " + buffer.ToString());
            System.Diagnostics.Debug.WriteLine(_soundIdentifiler.Count.ToString());
        }
Пример #2
0
        private void loadSound(String key, String value)
        {
            int    format, length, sourceId, bufferId;
            float  frequency;
            IntPtr buffer = IntPtr.Zero;

            if (key.Length == 0 || value.Length == 0)
            {
                Console.WriteLine("ERRO: Key:Value errados ao carregar som");
                return;
            }

            if (File.Exists(Settings.Default.SoundDirectory + value))
            {
                SoundEffect sound = null;

                //if (sourceId != 0)
                //{
                buffer = Alut.alutLoadMemoryFromFile(Settings.Default.SoundDirectory + value, out format, out length, out frequency);

                Al.alGenBuffers(1, out bufferId);
                Al.alGenSources(1, out sourceId);
                Al.alBufferData(bufferId, format, buffer, length, (int)frequency);

                float gain = 1.0f;

                if (key == "MusicaAmbiente1")
                {
                    gain = 0.5f;
                }

                sound           = new SoundEffect(sourceId, bufferId, buffer, new Vector3D(0.0, 0.0, 0.0), new Vector3D(0.0, 0.0, 0.0), true, 1.0f, gain);
                sound.SoundName = key;
                sound.setup();
                //}
                //else
                //{
                //    Console.WriteLine("AVISO: O Som '{0}' existe mas não foi possível carregá-lo", value);
                //}

                this.Sounds.Add(key, sound);
            }
            else
            {
                Console.WriteLine("AVISO: O Som '{0}' não existe.", value);
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="shouldLoop"></param>
        public void Load(string fileName, bool shouldLoop)
        {
            // Load our sound
            //int looping;
            buffer = Alut.alutLoadMemoryFromFile(fileName, out format, out length, out frequency);
            //looping = shouldLoop ? Al.AL_TRUE : Al.AL_FALSE;

            Al.alGenSources(1, out sourceId);
            Al.alGenBuffers(1, out bufferId);
            Al.alBufferData(bufferId, format, buffer, length, (int)frequency);
            Al.alSourcei(sourceId, Al.AL_BUFFER, bufferId);

            //Alut.alutUnloadWAV(format, out buffer, length, frequency);

            // Set the pitch
            Al.alSourcef(sourceId, Al.AL_PITCH, 1.0f);
            // Set the gain
            Al.alSourcef(sourceId, Al.AL_GAIN, 1.0f);
            // Set looping to loop
            Al.alSourcei(sourceId, Al.AL_LOOPING, 0);
        }
Пример #4
0
        public void LoadSound(string soundId, string path)
        {
            int buffer = -1;

            Al.alGenBuffers(1, out buffer);

            int errorCode = Al.alGetError();

            System.Diagnostics.Debug.Assert(errorCode == Al.AL_NO_ERROR);

            int   format;
            float frequency;
            int   size;

            System.Diagnostics.Debug.Assert(File.Exists(path));
            IntPtr data = Alut.alutLoadMemoryFromFile(path, out format, out size, out frequency);

            //System.Diagnostics.Debug.Assert(data != IntPtr.Zero);

            Al.alBufferData(buffer, format, data, size, (int)frequency);
            _soundIdentifier.Add(soundId, new SoundSource(buffer, path));
        }
Пример #5
0
        /*
         * This function will load our sample data from the disk using the Alut
         * utility and send the data into OpenAL as a buffer. A source is then
         * also created to play that buffer.
         */
        private static bool LoadALData()
        {
            // Variables to load into.
            int    format;
            int    size;
            IntPtr data = IntPtr.Zero;
            float  frequency;

            //int loop;

            // Generate an OpenAL buffer.
            Al.alGenBuffers(NUM_BUFFERS, buffer);
            if (Al.alGetError() != Al.AL_NO_ERROR)
            {
                return(false);
            }

            string fileName = "";

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson03.Battle.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[BATTLE], format, data, size, (int)frequency);
                //Alut.a(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson03.Gun1.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[GUN1], format, data, size, (int)frequency);
                //Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson03.Gun2.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[GUN2], format, data, size, (int)frequency);
                // Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Generate an OpenAL source.
            Al.alGenSources(NUM_SOURCES, source);
            if (Al.alGetError() != Al.AL_NO_ERROR)
            {
                return(false);
            }

            // Bind the buffer with the source.
            Al.alSourcei(source[BATTLE], Al.AL_BUFFER, buffer[BATTLE]);
            Al.alSourcef(source[BATTLE], Al.AL_PITCH, 1.0f);
            Al.alSourcef(source[BATTLE], Al.AL_GAIN, 1.0f);
            Al.alSourcefv(source[BATTLE], Al.AL_POSITION, sourcePosition[BATTLE]);
            Al.alSourcefv(source[BATTLE], Al.AL_VELOCITY, sourceVelocity[BATTLE]);
            Al.alSourcei(source[BATTLE], Al.AL_LOOPING, Al.AL_TRUE);

            Al.alSourcei(source[GUN1], Al.AL_BUFFER, buffer[GUN1]);
            Al.alSourcef(source[GUN1], Al.AL_PITCH, 1.0f);
            Al.alSourcef(source[GUN1], Al.AL_GAIN, 1.0f);
            Al.alSourcefv(source[GUN1], Al.AL_POSITION, sourcePosition[GUN1]);
            Al.alSourcefv(source[GUN1], Al.AL_VELOCITY, sourceVelocity[GUN1]);
            Al.alSourcei(source[GUN1], Al.AL_LOOPING, Al.AL_FALSE);

            Al.alSourcei(source[GUN2], Al.AL_BUFFER, buffer[GUN2]);
            Al.alSourcef(source[GUN2], Al.AL_PITCH, 1.0f);
            Al.alSourcef(source[GUN2], Al.AL_GAIN, 1.0f);
            Al.alSourcefv(source[GUN2], Al.AL_POSITION, sourcePosition[GUN2]);
            Al.alSourcefv(source[GUN2], Al.AL_VELOCITY, sourceVelocity[GUN2]);
            Al.alSourcei(source[GUN2], Al.AL_LOOPING, Al.AL_FALSE);

            // Do a final error check and then return.
            if (Al.alGetError() == Al.AL_NO_ERROR)
            {
                return(true);
            }

            return(false);
        }
Пример #6
0
        /*
         * This function will load our sample data from the disk using the Alut
         * utility and send the data into OpenAL as a buffer. A source is then
         * also created to play that buffer.
         */
        private static bool LoadALData()
        {
            // Variables to load into.
            int    format;
            int    size;
            IntPtr data = IntPtr.Zero;
            float  frequency;

            //int loop;

            // Generate an OpenAL buffer.
            Al.alGenBuffers(1, out buffer);
            if (Al.alGetError() != Al.AL_NO_ERROR)
            {
                return(false);
            }

            // Attempt to locate the file.
            string fileName = "OpenAlExamples.Lesson02.Footsteps.wav";

            if (File.Exists(fileName))
            {
                //fileName = fileName;
            }
            else if (File.Exists("Data/" + fileName))
            {
                fileName = "Data/" + fileName;
            }
            else if (File.Exists("../../Data/" + fileName))
            {
                fileName = "../../Data/" + fileName;
            }
            else
            {
                return(false);
            }

            // Load wav.
            data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
            if (data == IntPtr.Zero)
            {
                return(false);
            }

            // Load wav data into the generated buffer.
            Al.alBufferData(buffer, format, data, size, (int)frequency);
            //Alut.alutUnloadWAV(format, out data, size, frequency);

            // Generate an OpenAL source.
            Al.alGenSources(1, out source);
            if (Al.alGetError() != Al.AL_NO_ERROR)
            {
                return(false);
            }

            // Bind the buffer with the source.
            Al.alSourcei(source, Al.AL_BUFFER, buffer);
            Al.alSourcef(source, Al.AL_PITCH, 1.0f);
            Al.alSourcef(source, Al.AL_GAIN, 1.0f);
            Al.alSourcefv(source, Al.AL_POSITION, sourcePosition);
            Al.alSourcefv(source, Al.AL_VELOCITY, sourceVelocity);
            Al.alSourcei(source, Al.AL_LOOPING, Al.AL_TRUE);

            // Do a final error check and then return.
            if (Al.alGetError() == Al.AL_NO_ERROR)
            {
                return(true);
            }

            return(false);
        }
Пример #7
0
        /*
         * This function will load our sample data from the disk using the Alut
         * utility and send the data into OpenAL as a buffer. A source is then
         * also created to play that buffer.
         */
        private static bool LoadALData()
        {
            // Variables to load into.
            int    format;
            int    size;
            IntPtr data = IntPtr.Zero;
            float  frequency;

            //int loop;

            // Generate an OpenAL buffer.
            Al.alGenBuffers(NUM_BUFFERS, buffer);
            if (Al.alGetError() != Al.AL_NO_ERROR)
            {
                return(false);
            }

            string fileName = "";

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson05.Thunder.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[THUNDER], format, data, size, (int)frequency);
                //Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson05.Waterdrop.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[WATERDROP], format, data, size, (int)frequency);
                //Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson05.Stream.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[STREAM], format, data, size, (int)frequency);
                //Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson05.Rain.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[RAIN], format, data, size, (int)frequency);
                //Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson05.Ocean.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[OCEAN], format, data, size, (int)frequency);
                // Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Attempt to locate the file.
            fileName = FindFile("OpenAlExamples.Lesson05.Chimes.wav");
            if (fileName != null)
            {
                // Load wav.
                data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
                if (data == IntPtr.Zero)
                {
                    return(false);
                }

                // Load wav data into the generated buffer.
                Al.alBufferData(buffer[CHIMES], format, data, size, (int)frequency);
                //Alut.alutUnloadWAV(format, out data, size, frequency);
            }
            else
            {
                return(false);
            }

            // Do a final error check and then return.
            if (Al.alGetError() == Al.AL_NO_ERROR)
            {
                return(true);
            }

            return(false);
        }
Пример #8
0
        private static void Init()
        {
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            Al.alListenerfv(Al.AL_POSITION, listenerPosition);
            Al.alListenerfv(Al.AL_VELOCITY, listenerVelocity);
            Al.alListenerfv(Al.AL_ORIENTATION, listenerOrientation);

            Al.alGetError();
            Al.alGenBuffers(NUM_BUFFERS, buffer);
            if (Al.alGetError() != Al.AL_NO_ERROR)
            {
                Console.WriteLine("Error creating buffers.");
                Environment.Exit(-1);
            }

            string fileName = "";

            fileName = FindFile("OpenAlExamples.Boxes.C.wav");
            if (fileName == null)
            {
                Environment.Exit(-1);
            }
            data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
            Al.alBufferData(buffer[0], format, data, size, (int)frequency);
            //Alut.alutUnloadWAV(format, out data, size, frequency);

            fileName = FindFile("OpenAlExamples.Boxes.B.wav");
            if (fileName == null)
            {
                Environment.Exit(-1);
            }
            data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
            Al.alBufferData(buffer[1], format, data, size, (int)frequency);
            //Alut.alutUnloadWAV(format, out data, size, frequency);

            fileName = FindFile("OpenAlExamples.Boxes.A.wav");
            if (fileName == null)
            {
                Environment.Exit(-1);
            }
            data = Alut.alutLoadMemoryFromFile(fileName, out format, out size, out frequency);
            Al.alBufferData(buffer[2], format, data, size, (int)frequency);
            //Alut.alutUnloadWAV(format, out data, size, frequency);

            Al.alGetError();
            Al.alGenSources(NUM_SOURCES, source);
            if (Al.alGetError() != Al.AL_NO_ERROR)
            {
                Console.WriteLine("Error creating sources.");
                Environment.Exit(-1);
            }

            Al.alSourcef(source[0], Al.AL_PITCH, 1.0f);
            Al.alSourcef(source[0], Al.AL_GAIN, 1.0f);
            Al.alSourcefv(source[0], Al.AL_POSITION, redPosition);
            Al.alSourcefv(source[0], Al.AL_VELOCITY, redVelocity);
            Al.alSourcei(source[0], Al.AL_BUFFER, buffer[0]);
            Al.alSourcei(source[0], Al.AL_LOOPING, Al.AL_TRUE);

            Al.alSourcef(source[1], Al.AL_PITCH, 1.0f);
            Al.alSourcef(source[1], Al.AL_GAIN, 1.0f);
            Al.alSourcefv(source[1], Al.AL_POSITION, greenPosition);
            Al.alSourcefv(source[1], Al.AL_VELOCITY, greenVelocity);
            Al.alSourcei(source[1], Al.AL_BUFFER, buffer[1]);
            Al.alSourcei(source[1], Al.AL_LOOPING, Al.AL_TRUE);

            Al.alSourcef(source[2], Al.AL_PITCH, 1.0f);
            Al.alSourcef(source[2], Al.AL_GAIN, 1.0f);
            Al.alSourcefv(source[2], Al.AL_POSITION, bluePosition);
            Al.alSourcefv(source[2], Al.AL_VELOCITY, blueVelocity);
            Al.alSourcei(source[2], Al.AL_BUFFER, buffer[2]);
            Al.alSourcei(source[2], Al.AL_LOOPING, Al.AL_TRUE);
        }