Ejemplo n.º 1
0
        void UpdateVolume2()
        {
            float value = CurrentVirtualChannel.GetTotalVolume() * CurrentVirtualChannel.GetRolloffFactor();

            Al.alSourcef(alSource, Al.AL_GAIN, value);
            OpenALSoundWorld.CheckError();
        }
Ejemplo n.º 2
0
        void UpdateVelocity2()
        {
            Vec3 value = CurrentVirtualChannel.Velocity;

            Al.alSource3f(alSource, Al.AL_VELOCITY, value.X, value.Y, value.Z);
            OpenALSoundWorld.CheckError();
        }
Ejemplo n.º 3
0
        unsafe protected override void PreDetachVirtualChannel()
        {
            OpenALSoundWorld.criticalSection.Enter();

            Al.alSourceStop(alSource);
            OpenALSoundWorld.CheckError();

            if (currentSound is OpenALDataBufferSound)
            {
                if (currentSound is OpenALFileStreamSound)
                {
                    OpenALSoundWorld.Instance.fileStreamRealChannels.Remove(this);
                }

                if (streamBuffer != null)
                {
                    NativeUtils.Free((IntPtr)streamBuffer);
                    streamBuffer     = null;
                    streamBufferSize = 0;
                }
            }

            Al.alSourcei(alSource, Al.AL_BUFFER, 0);
            OpenALSoundWorld.CheckError();

            currentSound = null;

            OpenALSoundWorld.criticalSection.Leave();
        }
Ejemplo n.º 4
0
        void UpdatePosition2()
        {
            Vec3 value = CurrentVirtualChannel.Position;

            Al.alSource3f(alSource, Al.AL_POSITION, value.X, value.Y, value.Z);
            OpenALSoundWorld.CheckError();
        }
Ejemplo n.º 5
0
        void UpdatePan2()
        {
            float value = CurrentVirtualChannel.Pan;

            MathFunctions.Clamp(ref value, -1, 1);
            Al.alSource3f(alSource, Al.AL_POSITION, value * .1f, 0, 0);
            OpenALSoundWorld.CheckError();
        }
Ejemplo n.º 6
0
        void UpdateVelocity2()
        {
            Vector3 value = CurrentVirtualChannel.Velocity;

            //!!!!double
            Al.alSource3f(alSource, Al.AL_VELOCITY, (float)value.X, (float)value.Y, (float)value.Z);
            OpenALSoundWorld.CheckError();
        }
Ejemplo n.º 7
0
        void UpdatePosition2()
        {
            Vector3 value = CurrentVirtualChannel.Position;

            //!!!!!double
            Al.alSource3f(alSource, Al.AL_POSITION, (float)value.X, (float)value.Y, (float)value.Z);
            OpenALSoundWorld.CheckError();
        }
Ejemplo n.º 8
0
        void UpdateSample()
        {
            int state;

            Al.alGetSourcei(alSource, Al.AL_SOURCE_STATE, out state);
            OpenALSoundWorld.CheckError();
            if (state == Al.AL_STOPPED)
            {
                CurrentVirtualChannel.Stop();
            }
        }
Ejemplo n.º 9
0
        void FileStreamStartPlay()
        {
            for (int n = 0; n < streamAlDataBuffers.Length; n++)
            {
                if (!FileStream(streamAlDataBuffers[n]))
                {
                    return;
                }

                Al.alSourceQueueBuffers(alSource, 1, ref streamAlDataBuffers[n]);
                OpenALSoundWorld.CheckError();
            }
        }
Ejemplo n.º 10
0
        unsafe bool FileStream(int alStreamBuffer)
        {
            OpenALFileStreamSound fileStreamSound = (OpenALFileStreamSound)currentSound;

            int size = 0;

            streamDataAvailable = true;

            while (size < streamBufferSize)
            {
                byte *pointer = streamBuffer + size;

                int readBytes = fileStreamSound.vorbisFile.read((IntPtr)pointer,
                                                                streamBufferSize - size, 0, 2, 1, IntPtr.Zero);

                //convert to mono for 3D
                if (readBytes > 0 && fileStreamSound.needConvertToMono)
                {
                    readBytes /= 2;

                    for (int n = 0; n < readBytes; n += 2)
                    {
                        pointer[n + 0] = pointer[n * 2 + 0];
                        pointer[n + 1] = pointer[n * 2 + 1];
                    }
                }

                if (readBytes > 0)
                {
                    size += readBytes;
                }
                else
                {
                    break;
                }
            }

            if (size == 0)
            {
                streamDataAvailable = false;
                return(false);
            }

            int alFormat = (currentSound.channels == 1) ? Al.AL_FORMAT_MONO16 :
                           Al.AL_FORMAT_STEREO16;

            Al.alBufferData(alStreamBuffer, alFormat, streamBuffer, size, currentSound.frequency);
            OpenALSoundWorld.CheckError();

            return(true);
        }
Ejemplo n.º 11
0
        //

        unsafe protected bool GenerateBuffers(int count)
        {
            alDataBuffers = new int[count];
            fixed(int *pAlDataBuffers = alDataBuffers)
            {
                Al.alGenBuffers(alDataBuffers.Length, pAlDataBuffers);
            }

            if (OpenALSoundWorld.CheckError())
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 12
0
        protected override void OnDispose()
        {
            OpenALSoundWorld.criticalSection.Enter();

            if (alBuffer != 0)
            {
                Al.alDeleteBuffers(1, ref alBuffer);
                OpenALSoundWorld.CheckError();
                alBuffer = 0;
            }

            OpenALSoundWorld.criticalSection.Leave();

            base.OnDispose();
        }
Ejemplo n.º 13
0
        unsafe internal protected override void PreDetachVirtualChannel()
        {
            OpenALSoundWorld.criticalSection.Enter();

            Al.alSourceStop(alSource);
            OpenALSoundWorld.CheckError();

            //never delete buffer because cannot delete. maybe need some time to free buffer internally
            ////delete al data buffers
            //if( streamAlDataBuffers != null )
            //{
            //	fixed ( int* pAlDataBuffers = streamAlDataBuffers )
            //		Al.alDeleteBuffers( streamAlDataBuffers.Length, pAlDataBuffers );
            //	OpenALSoundWorld.CheckError();
            //	streamAlDataBuffers = null;
            //}

            if (currentSound is OpenALDataBufferSound)
            {
                if (currentSound is OpenALFileStreamSound)
                {
                    OpenALSoundWorld.fileStreamRealChannels.Remove(this);
                }

                if (streamBuffer != null)
                {
                    NativeUtility.Free((IntPtr)streamBuffer);
                    streamBuffer     = null;
                    streamBufferSize = 0;
                }
            }

            fileStreamVorbisFile?.Dispose();
            fileStreamVorbisFile = null;
            fileStreamVorbisFileReader?.Dispose();
            fileStreamVorbisFileReader = null;

            Al.alSourcei(alSource, Al.AL_BUFFER, 0);
            OpenALSoundWorld.CheckError();

            currentSound = null;

            OpenALSoundWorld.criticalSection.Leave();
        }
Ejemplo n.º 14
0
        unsafe protected override void OnDispose()
        {
            OpenALSoundWorld.criticalSection.Enter();

            if (alDataBuffers != null)
            {
                fixed(int *pAlDataBuffers = alDataBuffers)
                {
                    Al.alDeleteBuffers(alDataBuffers.Length, pAlDataBuffers);
                }

                OpenALSoundWorld.CheckError();
                alDataBuffers = null;
            }

            OpenALSoundWorld.criticalSection.Leave();

            base.OnDispose();
        }
Ejemplo n.º 15
0
        unsafe void UpdateDataStream()
        {
            OpenALDataStreamSound dataStreamSound = (OpenALDataStreamSound)currentSound;

            int alFormat = (currentSound.channels == 1) ? Al.AL_FORMAT_MONO16 : Al.AL_FORMAT_STEREO16;

            int processed;

            Al.alGetSourcei(alSource, Al.AL_BUFFERS_PROCESSED, out processed);
            OpenALSoundWorld.CheckError();

            while (processed != 0)
            {
                int alBuffer = 0;

                int readed = ReadDataFromDataStream((IntPtr)streamBuffer, streamBufferSize);

                Al.alSourceUnqueueBuffers(alSource, 1, ref alBuffer);
                OpenALSoundWorld.CheckError();

                Al.alBufferData(alBuffer, alFormat, streamBuffer, readed, currentSound.frequency);
                OpenALSoundWorld.CheckError();

                Al.alSourceQueueBuffers(alSource, 1, ref alBuffer);
                OpenALSoundWorld.CheckError();

                processed--;
            }

            int state;

            Al.alGetSourcei(alSource, Al.AL_SOURCE_STATE, out state);
            OpenALSoundWorld.CheckError();

            if (state != Al.AL_PLAYING)
            {
                Al.alGetSourcei(alSource, Al.AL_BUFFERS_PROCESSED, out processed);
                Al.alSourcePlay(alSource);
            }
        }
Ejemplo n.º 16
0
        public void UpdateFileStreamFromThread()
        {
            if (currentSound == null)
            {
                return;
            }

            //update buffers
            int processed;

            Al.alGetSourcei(alSource, Al.AL_BUFFERS_PROCESSED, out processed);
            OpenALSoundWorld.CheckError();
            while (processed != 0)
            {
                int alStreamBuffer = 0;
                Al.alSourceUnqueueBuffers(alSource, 1, ref alStreamBuffer);

                OpenALSoundWorld.CheckError();
                FileStream(alStreamBuffer);
                if (streamDataAvailable)
                {
                    Al.alSourceQueueBuffers(alSource, 1, ref alStreamBuffer);
                    OpenALSoundWorld.CheckError();
                }

                processed--;
            }

            //play if buffer stopped (from behind internal buffers processed)
            int state;

            Al.alGetSourcei(alSource, Al.AL_SOURCE_STATE, out state);
            OpenALSoundWorld.CheckError();
            bool stoppedNoQueued = false;

            if (state == Al.AL_STOPPED)
            {
                int queued;
                Al.alGetSourcei(alSource, Al.AL_BUFFERS_QUEUED, out queued);
                if (queued != 0)
                {
                    Al.alSourcePlay(alSource);
                }
                else
                {
                    stoppedNoQueued = true;
                }
            }

            //file stream played

            OpenALFileStreamSound fileStreamSound = (OpenALFileStreamSound)currentSound;

            if (!streamDataAvailable && stoppedNoQueued)
            {
                if ((currentSound.Mode & SoundMode.Loop) != 0)
                {
                    //loop play. we need recreate vorbis file

                    //stop and unqueues sources
                    Al.alSourceStop(alSource);
                    Al.alGetSourcei(alSource, Al.AL_BUFFERS_PROCESSED, out processed);
                    OpenALSoundWorld.CheckError();
                    while (processed != 0)
                    {
                        int alStreamBuffer = 0;
                        Al.alSourceUnqueueBuffers(alSource, 1, ref alStreamBuffer);
                        OpenALSoundWorld.CheckError();
                        processed--;
                    }

                    //recreate vorbis file
                    fileStreamSound.Rewind();

                    FileStreamStartPlay();

                    //Pause = false;
                    Al.alSourcePlay(alSource);
                    OpenALSoundWorld.CheckError();
                }
                else
                {
                    CurrentVirtualChannel.Stop();
                }
            }
        }
Ejemplo n.º 17
0
        unsafe protected override void PostAttachVirtualChannel()
        {
            OpenALSoundWorld.criticalSection.Enter();

            currentSound = (OpenALSound)CurrentVirtualChannel.CurrentSound;

            OpenALSampleSound     sampleSound     = currentSound as OpenALSampleSound;
            OpenALDataBufferSound streamSound     = null;
            OpenALFileStreamSound fileStreamSound = null;
            OpenALDataStreamSound dataStreamSound = null;

            if (sampleSound == null)
            {
                streamSound     = currentSound as OpenALDataBufferSound;
                fileStreamSound = currentSound as OpenALFileStreamSound;
                dataStreamSound = currentSound as OpenALDataStreamSound;
            }

            //create streamBuffer
            if (fileStreamSound != null)
            {
                int bufferSize = 0;

                int numSamples = (int)fileStreamSound.vorbisFile.pcm_total(-1);
                int channels;
                int rate;
                fileStreamSound.vorbisFile.get_info(-1, out channels, out rate);

                if (fileStreamSound.needConvertToMono)
                {
                    channels = 1;
                }

                int sizeInBytes = numSamples * channels * 2;

                bufferSize = sizeInBytes / 2;
                if (bufferSize > 65536 * 4)
                {
                    bufferSize = 65536 * 4;
                }

                streamBufferSize = bufferSize;
                streamBuffer     = (byte *)NativeUtils.Alloc(NativeMemoryAllocationType.SoundAndVideo, streamBufferSize);
            }

            if (dataStreamSound != null)
            {
                streamBufferSize = dataStreamSound.bufferSize;
                streamBuffer     = (byte *)NativeUtils.Alloc(NativeMemoryAllocationType.SoundAndVideo,
                                                             streamBufferSize);
            }

            //init source

            bool mode3d = (currentSound.Mode & SoundMode.Mode3D) != 0;
            bool loop   = (currentSound.Mode & SoundMode.Loop) != 0;

            if (alSource == 0)
            {
                Al.alGenSources(1, out alSource);
                if (OpenALSoundWorld.CheckError())
                {
                    PreDetachVirtualChannel();
                    OpenALSoundWorld.criticalSection.Leave();
                    return;
                }
            }


            if (sampleSound != null)
            {
                //no stream sound
                Al.alSourcei(alSource, Al.AL_BUFFER, sampleSound.alBuffer);
                if (OpenALSoundWorld.CheckError())
                {
                    PreDetachVirtualChannel();
                    OpenALSoundWorld.criticalSection.Leave();
                    return;
                }
            }

            if (fileStreamSound != null)
            {
                FileStreamStartPlay();
            }

            if (dataStreamSound != null)
            {
                DataStreamStartPlay();
            }

            Al.alSourcei(alSource, Al.AL_SOURCE_RELATIVE, mode3d ? Al.AL_FALSE : Al.AL_TRUE);

            //update parameters
            if (mode3d)
            {
                UpdatePosition2();
                UpdateVelocity2();
            }
            else
            {
                UpdatePan2();
            }
            UpdatePitch2();
            UpdateVolume2();

            if (sampleSound != null)
            {
                Al.alSourcei(alSource, Al.AL_LOOPING, loop ? Al.AL_TRUE : Al.AL_FALSE);
            }
            else
            {
                Al.alSourcei(alSource, Al.AL_LOOPING, Al.AL_FALSE);
            }
            if (OpenALSoundWorld.CheckError())
            {
                PreDetachVirtualChannel();
                OpenALSoundWorld.criticalSection.Leave();
                return;
            }

            UpdateTime2();

            //unpause
            Al.alSourcePlay(alSource);
            OpenALSoundWorld.CheckError();

            //add to fileStreamChannels
            if (fileStreamSound != null)
            {
                OpenALSoundWorld.Instance.fileStreamRealChannels.Add(this);
            }

            OpenALSoundWorld.criticalSection.Leave();
        }
Ejemplo n.º 18
0
 void UpdateTime2()
 {
     Al.alSourcef(alSource, Al.AL_SEC_OFFSET, CurrentVirtualChannel.Time);
     OpenALSoundWorld.CheckError();
 }
Ejemplo n.º 19
0
        unsafe internal protected override void PostAttachVirtualChannel()
        {
            OpenALSoundWorld.criticalSection.Enter();

            currentSound = (OpenALSound)CurrentVirtualChannel.Sound;

            OpenALSampleSound     sampleSound     = currentSound as OpenALSampleSound;
            OpenALDataBufferSound streamSound     = null;
            OpenALFileStreamSound fileStreamSound = null;
            OpenALDataStreamSound dataStreamSound = null;

            if (sampleSound == null)
            {
                streamSound     = currentSound as OpenALDataBufferSound;
                fileStreamSound = currentSound as OpenALFileStreamSound;
                dataStreamSound = currentSound as OpenALDataStreamSound;
            }

            //create streamBuffer
            if (fileStreamSound != null)
            {
                var stream = OpenALSoundWorld.CreateFileStream2(currentSound.Name);
                if (stream == null)
                {
                    //Log.Warning( string.Format( "Creating sound \"{0}\" failed.", currentSound.Name ) );
                    PreDetachVirtualChannel();
                    OpenALSoundWorld.criticalSection.Leave();
                    return;
                }
                fileStreamVorbisFile       = new VorbisFile.File();
                fileStreamVorbisFileReader = new VorbisFileReader(stream, true);
                if (!fileStreamVorbisFileReader.OpenVorbisFile(fileStreamVorbisFile))
                {
                    //Log.Warning( string.Format( "Creating sound \"{0}\" failed.", currentSound.Name ) );
                    PreDetachVirtualChannel();
                    OpenALSoundWorld.criticalSection.Leave();
                    return;
                }

                int numSamples = (int)fileStreamVorbisFile.pcm_total(-1);
                fileStreamVorbisFile.get_info(-1, out var channels, out var frequency);

                //int numSamples = (int)fileStreamSound.vorbisFile.pcm_total( -1 );
                //fileStreamSound.vorbisFile.get_info( -1, out var channels, out var rate );

                if (fileStreamSound.needConvertToMono)
                {
                    channels = 1;
                }

                int sizeInBytes = numSamples * channels * 2;

                int bufferSize = sizeInBytes / 2;
                if (bufferSize > 65536 * 4)
                {
                    bufferSize = 65536 * 4;
                }

                streamBufferSize = bufferSize;
                streamBuffer     = (byte *)NativeUtility.Alloc(NativeUtility.MemoryAllocationType.SoundAndVideo, streamBufferSize);
            }

            if (dataStreamSound != null)
            {
                streamBufferSize = dataStreamSound.bufferSize;
                streamBuffer     = (byte *)NativeUtility.Alloc(NativeUtility.MemoryAllocationType.SoundAndVideo, streamBufferSize);
            }

            //create al data buffers
            if (fileStreamSound != null || dataStreamSound != null)
            {
                if (streamAlDataBuffers == null)
                {
                    streamAlDataBuffers = new int[2];

                    fixed(int *pAlDataBuffers = streamAlDataBuffers)
                    Al.alGenBuffers(streamAlDataBuffers.Length, pAlDataBuffers);

                    if (OpenALSoundWorld.CheckError())
                    {
                        streamAlDataBuffers = null;
                        PreDetachVirtualChannel();
                        OpenALSoundWorld.criticalSection.Leave();
                        return;
                    }
                }
            }

            //init source

            bool mode3d = (currentSound.Mode & SoundModes.Mode3D) != 0;
            bool loop   = (currentSound.Mode & SoundModes.Loop) != 0;

            if (alSource == 0)
            {
                Al.alGenSources(1, out alSource);
                if (OpenALSoundWorld.CheckError())
                {
                    PreDetachVirtualChannel();
                    OpenALSoundWorld.criticalSection.Leave();
                    return;
                }
            }


            if (sampleSound != null)
            {
                //no stream sound
                Al.alSourcei(alSource, Al.AL_BUFFER, sampleSound.alBuffer);
                if (OpenALSoundWorld.CheckError())
                {
                    PreDetachVirtualChannel();
                    OpenALSoundWorld.criticalSection.Leave();
                    return;
                }
            }

            if (fileStreamSound != null)
            {
                FileStreamStartPlay();
            }

            if (dataStreamSound != null)
            {
                DataStreamStartPlay();
            }

            Al.alSourcei(alSource, Al.AL_SOURCE_RELATIVE, mode3d ? Al.AL_FALSE : Al.AL_TRUE);

            //update parameters
            if (mode3d)
            {
                UpdatePosition2();
                UpdateVelocity2();
            }
            else
            {
                UpdatePan2();
            }
            UpdatePitch2();
            UpdateVolume2();

            if (sampleSound != null)
            {
                Al.alSourcei(alSource, Al.AL_LOOPING, loop ? Al.AL_TRUE : Al.AL_FALSE);
            }
            else
            {
                Al.alSourcei(alSource, Al.AL_LOOPING, Al.AL_FALSE);
            }
            if (OpenALSoundWorld.CheckError())
            {
                PreDetachVirtualChannel();
                OpenALSoundWorld.criticalSection.Leave();
                return;
            }

            UpdateTime2();

            //unpause
            Al.alSourcePlay(alSource);
            OpenALSoundWorld.CheckError();

            //add to fileStreamChannels
            if (fileStreamSound != null)
            {
                OpenALSoundWorld.fileStreamRealChannels.Add(this);
            }

            OpenALSoundWorld.criticalSection.Leave();
        }
Ejemplo n.º 20
0
 void UpdatePitch2()
 {
     Al.alSourcef(alSource, Al.AL_PITCH, CurrentVirtualChannel.GetTotalPitch());
     OpenALSoundWorld.CheckError();
 }
Ejemplo n.º 21
0
        unsafe public OpenALSampleSound(VirtualFileStream stream, SoundType soundType, string name, SoundModes mode, out bool initialized)
        {
            initialized = false;

            byte[] samples;
            int    sizeInBytes;
            float  timeLength;

            if (string.Compare(Path.GetExtension(name), ".ogg", true) == 0)
            {
                //ogg

                VorbisFileReader vorbisFileReader = new VorbisFileReader(stream, false);
                VorbisFile.File  vorbisFile       = new VorbisFile.File();

                if (!vorbisFileReader.OpenVorbisFile(vorbisFile))
                {
                    vorbisFile.Dispose();
                    vorbisFileReader.Dispose();

                    Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" (Reading failed).", name);
                    return;
                }

                int numSamples = (int)vorbisFile.pcm_total(-1);

                vorbisFile.get_info(-1, out channels, out frequency);
                timeLength = (float)vorbisFile.time_total(-1);

                sizeInBytes = numSamples * channels * 2;
                samples     = new byte[sizeInBytes];

                fixed(byte *pSamples = samples)
                {
                    int samplePos = 0;

                    while (samplePos < sizeInBytes)
                    {
                        int readBytes = vorbisFile.read((IntPtr)(pSamples + samplePos), sizeInBytes - samplePos, 0, 2, 1, IntPtr.Zero);
                        if (readBytes <= 0)
                        {
                            break;
                        }
                        samplePos += readBytes;
                    }
                }

                vorbisFile.Dispose();
                vorbisFileReader.Dispose();
            }
            else if (string.Compare(Path.GetExtension(name), ".wav", true) == 0)
            {
                //wav

                string error;
                if (!WavLoader.Load(stream, out channels, out frequency, out samples, out sizeInBytes, out error))
                {
                    Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" ({1}).", name, error);
                    return;
                }

                timeLength = (float)(samples.Length / channels / 2) / (float)frequency;
            }
            else
            {
                Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" (Unknown file type).", name);
                return;
            }

            //create buffer

            Al.alGenBuffers(1, out alBuffer);

            int alFormat = (channels == 1) ? Al.AL_FORMAT_MONO16 : Al.AL_FORMAT_STEREO16;

            //bug fix: half volume mono 2D sounds
            //convert to stereo
            if ((mode & SoundModes.Mode3D) == 0 && alFormat == Al.AL_FORMAT_MONO16)
            {
                byte[] stereoSamples = new byte[sizeInBytes * 2];
                for (int n = 0; n < sizeInBytes; n += 2)
                {
                    stereoSamples[n * 2 + 0] = samples[n];
                    stereoSamples[n * 2 + 1] = samples[n + 1];
                    stereoSamples[n * 2 + 2] = samples[n];
                    stereoSamples[n * 2 + 3] = samples[n + 1];
                }
                samples      = stereoSamples;
                alFormat     = Al.AL_FORMAT_STEREO16;
                sizeInBytes *= 2;
            }

            //convert to mono for 3D
            if ((mode & SoundModes.Mode3D) != 0 && channels == 2)
            {
                byte[] oldSamples = samples;
                samples = new byte[oldSamples.Length / 2];
                for (int n = 0; n < samples.Length; n += 2)
                {
                    samples[n + 0] = oldSamples[n * 2 + 0];
                    samples[n + 1] = oldSamples[n * 2 + 1];
                }
                alFormat     = Al.AL_FORMAT_MONO16;
                sizeInBytes /= 2;
            }

            fixed(byte *pSamples = samples)
            Al.alBufferData(alBuffer, alFormat, pSamples, sizeInBytes, frequency);

            if (OpenALSoundWorld.CheckError())
            {
                Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\".", name);
                return;
            }

            Init(name, mode, timeLength, channels, frequency);
            initialized = true;
        }