Esempio n. 1
0
        protected override void OnDispose()
        {
            DirectSoundWorld.criticalSection.Enter();

            if (vorbisFile != null)
            {
                vorbisFile.Dispose();
                vorbisFile = null;
            }
            if (vorbisFileReader != null)
            {
                vorbisFileReader.Dispose();
                vorbisFileReader = null;
            }

            DirectSoundWorld.criticalSection.Leave();

            base.OnDispose();
        }
Esempio n. 2
0
        //

        unsafe public DirectFileStreamSound(VirtualFileStream stream, bool closeStreamAfterReading,
                                            SoundType soundType, string name, SoundMode mode, out bool initialized)
        {
            initialized = false;

            if (soundType == SoundType.Unknown)
            {
                if (name != null)
                {
                    soundType = GetSoundTypeByName(name);
                }
                else
                {
                    soundType = GetSoundTypeByStream(stream);
                }
            }

            if (soundType != SoundType.OGG)
            {
                DirectSoundWorld.Warning(string.Format(
                                             "Streaming is not supported for \"{0}\" files ({1}).", soundType, name));
                return;
            }

            vorbisFile = new VorbisFile.File();

            vorbisFileReader = new VorbisFileReader(stream, closeStreamAfterReading);

            if (!vorbisFileReader.OpenVorbisFile(vorbisFile))
            {
                vorbisFileReader.Dispose();
                DirectSoundWorld.Warning(string.Format("Creating sound \"{0}\" failed.", name));
                return;
            }

            int channels;
            int frequency;

            long numSamples = vorbisFile.pcm_total(-1);

            vorbisFile.get_info(-1, out channels, out frequency);

            //convert to mono for 3D
            if ((int)(mode & SoundMode.Mode3D) != 0 && channels == 2)
            {
                needConvertToMono = true;
                channels          = 1;
            }

            waveFormat = (WAVEFORMATEX *)NativeUtils.Alloc(NativeMemoryAllocationType.SoundAndVideo,
                                                           sizeof(WAVEFORMATEX));
            NativeUtils.ZeroMemory((IntPtr)waveFormat, sizeof(WAVEFORMATEX));
            waveFormat->wFormatTag      = DSound.WAVE_FORMAT_PCM;
            waveFormat->nChannels       = (ushort)channels;
            waveFormat->nSamplesPerSec  = (uint)frequency;
            waveFormat->wBitsPerSample  = 16;
            waveFormat->nBlockAlign     = (ushort)((waveFormat->nChannels * waveFormat->wBitsPerSample) / 8);
            waveFormat->nAvgBytesPerSec = waveFormat->nSamplesPerSec * waveFormat->nBlockAlign;

            double length = (double)numSamples / (double)frequency;

            Init(name, mode, (float)length, channels, frequency);
            initialized = true;
        }
Esempio n. 3
0
        //

        bool LoadSamplesFromStream(VirtualFileStream stream, SoundType soundType,
                                   out int channels, out int frequency, out float timeLength, out string error)
        {
            channels   = 0;
            frequency  = 0;
            timeLength = 0;
            error      = null;

            switch (soundType)
            {
            case SoundType.OGG:
            {
                VorbisFileReader vorbisFileReader = new VorbisFileReader(stream, false);
                VorbisFile.File  vorbisFile       = new VorbisFile.File();

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

                    error = "Reading failed";
                    return(false);
                }

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

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

                int size        = numSamples * channels;
                int sizeInBytes = size * 2;
                soundSamples = new byte[sizeInBytes];

                unsafe
                {
                    fixed(byte *pSoundSamples = soundSamples)
                    {
                        int samplePos = 0;

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

                            if (readBytes <= 0)
                            {
                                break;
                            }

                            samplePos += readBytes;
                        }
                    }
                }

                vorbisFile.Dispose();
                vorbisFileReader.Dispose();
            }
                return(true);

            case SoundType.WAV:
            {
                int sizeInBytes;
                if (!WavLoader.Load(stream, out channels, out frequency, out soundSamples,
                                    out sizeInBytes, out error))
                {
                    return(false);
                }
                timeLength = (float)(soundSamples.Length / channels / 2) / (float)frequency;
            }
                return(true);
            }

            error = "Unknown file type";
            return(false);
        }
Esempio n. 4
0
		protected override void OnDispose()
		{
			DirectSoundWorld.criticalSection.Enter();

			if( vorbisFile != null )
			{
				vorbisFile.Dispose();
				vorbisFile = null;
			}
			if( vorbisFileReader != null )
			{
				vorbisFileReader.Dispose();
				vorbisFileReader = null;
			}

			DirectSoundWorld.criticalSection.Leave();

			base.OnDispose();
		}
Esempio n. 5
0
		//

		unsafe public DirectFileStreamSound( VirtualFileStream stream, bool closeStreamAfterReading,
			SoundType soundType, string name, SoundMode mode, out bool initialized )
		{
			initialized = false;

			if( soundType == SoundType.Unknown )
			{
				if( name != null )
					soundType = GetSoundTypeByName( name );
				else
					soundType = GetSoundTypeByStream( stream );
			}

			if( soundType != SoundType.OGG )
			{
				DirectSoundWorld.Warning( string.Format(
					"Streaming is not supported for \"{0}\" files ({1}).", soundType, name ) );
				return;
			}

			vorbisFile = new VorbisFile.File();

			vorbisFileReader = new VorbisFileReader( stream, closeStreamAfterReading );

			if( !vorbisFileReader.OpenVorbisFile( vorbisFile ) )
			{
				vorbisFileReader.Dispose();
				DirectSoundWorld.Warning( string.Format( "Creating sound \"{0}\" failed.", name ) );
				return;
			}

			int channels;
			int frequency;

			long numSamples = vorbisFile.pcm_total( -1 );
			vorbisFile.get_info( -1, out channels, out frequency );

			//convert to mono for 3D
			if( (int)( mode & SoundMode.Mode3D ) != 0 && channels == 2 )
			{
				needConvertToMono = true;
				channels = 1;
			}

			waveFormat = (WAVEFORMATEX*)NativeUtils.Alloc( NativeMemoryAllocationType.SoundAndVideo,
				sizeof( WAVEFORMATEX ) );
			NativeUtils.ZeroMemory( (IntPtr)waveFormat, sizeof( WAVEFORMATEX ) );
			waveFormat->wFormatTag = DSound.WAVE_FORMAT_PCM;
			waveFormat->nChannels = (ushort)channels;
			waveFormat->nSamplesPerSec = (uint)frequency;
			waveFormat->wBitsPerSample = 16;
			waveFormat->nBlockAlign = (ushort)( ( waveFormat->nChannels * waveFormat->wBitsPerSample ) / 8 );
			waveFormat->nAvgBytesPerSec = waveFormat->nSamplesPerSec * waveFormat->nBlockAlign;

			double length = (double)numSamples / (double)frequency;

			Init( name, mode, (float)length, channels, frequency );
			initialized = true;
		}
Esempio n. 6
0
		//

		bool LoadSamplesFromStream( VirtualFileStream stream, SoundType soundType,
			out int channels, out int frequency, out float timeLength, out string error )
		{
			channels = 0;
			frequency = 0;
			timeLength = 0;
			error = null;

			switch( soundType )
			{
			case SoundType.OGG:
				{
					VorbisFileReader vorbisFileReader = new VorbisFileReader( stream, false );
					VorbisFile.File vorbisFile = new VorbisFile.File();

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

						error = "Reading failed";
						return false;
					}

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

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

					int size = numSamples * channels;
					int sizeInBytes = size * 2;
					soundSamples = new byte[ sizeInBytes ];

					unsafe
					{
						fixed( byte* pSoundSamples = soundSamples )
						{
							int samplePos = 0;
							while( samplePos < sizeInBytes )
							{
								int readBytes = vorbisFile.read( (IntPtr)( pSoundSamples + samplePos ),
									sizeInBytes - samplePos, 0, 2, 1, IntPtr.Zero );

								if( readBytes <= 0 )
									break;

								samplePos += readBytes;
							}
						}
					}

					vorbisFile.Dispose();
					vorbisFileReader.Dispose();
				}
				return true;

			case SoundType.WAV:
				{
					int sizeInBytes;
					if( !WavLoader.Load( stream, out channels, out frequency, out soundSamples,
						out sizeInBytes, out error ) )
					{
						return false;
					}
					timeLength = (float)( soundSamples.Length / channels / 2 ) / (float)frequency;
				}
				return true;

			}

			error = "Unknown file type";
			return false;
		}