Example #1
0
        /// <summary>
        /// Loads sounds contained in a SND file.
        /// </summary>
        /// <param name="filepath">The filepath of the SND file to load sounds from.</param>
        /// <returns>A cached ReadOnlyDictionary mapping SoundIds to their respective SecondaryBuffers.</returns>
        private ReadOnlyDictionary <SoundId, byte[]> GetSounds(string filepath)
        {
            if (filepath == null)
            {
                throw new ArgumentNullException(nameof(filepath));
            }

            var sounds = new Dictionary <SoundId, byte[]>();

            using (IO.File file = GetSubSystem <IO.FileSystem>().OpenFile(filepath))
            {
                var header = new IO.FileHeaders.SoundFileHeader(file);

                file.SeekFromBeginning(header.SubheaderOffset);

                for (var i = 0; i != header.NumberOfSounds; ++i)
                {
                    var subheader = new IO.FileHeaders.SoundFileSubHeader(file);

                    if (sounds.ContainsKey(subheader.Id) == true)
                    {
                        Log.Write(LogLevel.Warning, LogSystem.SoundSystem, "Duplicate sound with ID {0} discarded.", subheader.Id);
                    }
                    else
                    {
                        try
                        {
                            sounds.Add(subheader.Id, CreateSoundBuffer(file, subheader.SoundLength));
                        }
                        catch
                        {
                            Log.Write(LogLevel.Warning, LogSystem.SoundSystem, "Error reading sound with ID {0}.", subheader.Id);
                        }
                    }

                    file.SeekFromBeginning(subheader.NextOffset);
                }
            }

            ReadOnlyDictionary <SoundId, byte[]> savedsounds = new ReadOnlyDictionary <SoundId, byte[]>(sounds);

            m_soundcache.Add(filepath, savedsounds);

            return(savedsounds);
        }
Example #2
0
		/// <summary>
		/// Loads sounds contained in a SND file.
		/// </summary>
		/// <param name="filepath">The filepath of the SND file to load sounds from.</param>
		/// <returns>A cached ReadOnlyDictionary mapping SoundIds to their respective SecondaryBuffers.</returns>
		ReadOnlyDictionary<SoundId, SecondaryBuffer> GetSounds(String filepath)
		{
			if (filepath == null) throw new ArgumentNullException("filepath");

			Dictionary<SoundId, SecondaryBuffer> sounds = new Dictionary<SoundId, SecondaryBuffer>();

			using (IO.File file = GetSubSystem<IO.FileSystem>().OpenFile(filepath))
			{
				IO.FileHeaders.SoundFileHeader header = new IO.FileHeaders.SoundFileHeader(file);

				file.SeekFromBeginning(header.SubheaderOffset);

				for (Int32 i = 0; i != header.NumberOfSounds; ++i)
				{
					IO.FileHeaders.SoundFileSubHeader subheader = new IO.FileHeaders.SoundFileSubHeader(file);

					if (sounds.ContainsKey(subheader.Id) == true)
					{
						Log.Write(LogLevel.Warning, LogSystem.SoundSystem, "Duplicate sound with ID {0} discarded.", subheader.Id);
					}
					else
					{
						try
						{
							sounds.Add(subheader.Id, CreateSoundBuffer(file, subheader.SoundLength));
						}
						catch
						{
							Log.Write(LogLevel.Warning, LogSystem.SoundSystem, "Error reading sound with ID {0}.", subheader.Id);
						}
					}

					file.SeekFromBeginning(subheader.NextOffset);
				}
			}

			ReadOnlyDictionary<SoundId, SecondaryBuffer> savedsounds = new ReadOnlyDictionary<SoundId, SecondaryBuffer>(sounds);
			m_soundcache.Add(filepath, savedsounds);

			return savedsounds;
		}