예제 #1
0
        int ReadDataFromDataStream(IntPtr buffer, int needRead)
        {
            OpenALDataStreamSound currentDataStreamSound = (OpenALDataStreamSound)currentSound;

            if (tempDataStreamReadArray.Length < needRead)
            {
                tempDataStreamReadArray = new byte[needRead];
            }

            int readed = currentDataStreamSound.dataReadCallback(tempDataStreamReadArray, 0,
                                                                 needRead);

            if (readed != 0)
            {
                Marshal.Copy(tempDataStreamReadArray, 0, buffer, readed);
            }

            if (readed < 16)
            {
                readed = Math.Min(needRead, 16);
                NativeUtils.ZeroMemory(buffer, readed);
            }

            return(readed);
        }
예제 #2
0
        //!!!!было. file stream менялся, пока отключено
        //protected override Sound _SoundCreate( VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, SoundModes mode )
        //{
        //	criticalSection.Enter();

        //	OpenALSound sound;
        //	bool initialized;

        //	if( (int)( mode & SoundModes.Stream ) == 0 )
        //	{
        //		sound = new OpenALSampleSound( stream, soundType, null, mode, out initialized );
        //		if( closeStreamAfterReading )
        //			stream.Dispose();
        //	}
        //	else
        //		sound = new OpenALFileStreamSound( stream, closeStreamAfterReading, soundType, null, mode, out initialized );

        //	if( !initialized )
        //	{
        //		sound.Dispose();
        //		sound = null;
        //	}

        //	criticalSection.Leave();

        //	return sound;
        //}

        protected override Sound _SoundCreateDataBuffer(SoundModes mode, int channels, int frequency, int bufferSize, DataReadDelegate dataReadCallback)
        {
            criticalSection.Enter();

            Sound sound;

            if ((mode & SoundModes.Record) != 0)
            {
                OpenALCaptureSound captureSound = new OpenALCaptureSound(mode, channels, frequency, bufferSize);
                if (captureSound.alCaptureDevice == IntPtr.Zero)
                {
                    criticalSection.Leave();
                    return(null);
                }
                sound = captureSound;
            }
            else
            {
                sound = new OpenALDataStreamSound(mode, channels, frequency, bufferSize, dataReadCallback);
            }

            criticalSection.Leave();

            return(sound);
        }
예제 #3
0
        unsafe void DataStreamStartPlay()
        {
            OpenALDataStreamSound dataStreamSound = (OpenALDataStreamSound)currentSound;

            for (int n = 0; n < dataStreamSound.alDataBuffers.Length; n++)
            {
                int readed = ReadDataFromDataStream((IntPtr)streamBuffer, streamBufferSize);

                int alFormat = (currentSound.channels == 1) ? Al.AL_FORMAT_MONO16 :
                               Al.AL_FORMAT_STEREO16;
                Al.alBufferData(dataStreamSound.alDataBuffers[n], alFormat, streamBuffer,
                                readed, currentSound.frequency);
            }

            fixed(int *pAlDataBuffers = dataStreamSound.alDataBuffers)
            {
                Al.alSourceQueueBuffers(alSource, dataStreamSound.alDataBuffers.Length,
                                        pAlDataBuffers);
            }
        }
예제 #4
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);
            }
        }
예제 #5
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();
        }
예제 #6
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();
        }
예제 #7
0
		public override Sound SoundCreateDataBuffer( SoundMode mode, int channels, int frequency,
			int bufferSize, DataReadDelegate dataReadCallback )
		{
			criticalSection.Enter();

			Sound sound;

			if( ( mode & SoundMode.Record ) != 0 )
			{
				OpenALCaptureSound captureSound = new OpenALCaptureSound(
					mode, channels, frequency, bufferSize );
				if( captureSound.alCaptureDevice == IntPtr.Zero )
				{
					criticalSection.Leave();
					return null;
				}
				sound = captureSound;
			}
			else
			{
				sound = new OpenALDataStreamSound( mode, channels, frequency, bufferSize,
					dataReadCallback );
			}

			criticalSection.Leave();

			return sound;
		}