Example #1
0
 /// <summary>Creates a new AudioReader that can read the specified soundstream.</summary>
 /// <param name="s">The System.IO.Stream to read from.</param>
 /// <returns>A new OpenTK.Audio.AudioReader, which can be used to read from the specified sound stream.</returns>
 public AudioReader(Stream s)
 {
     try
     {
         lock (reader_lock)
         {
             foreach (AudioReader reader in readers)
             {
                 long pos = s.Position;
                 if (reader.Supports(s))
                 {
                     s.Position = pos;
                     implementation = (AudioReader)
                         reader.GetType().GetConstructor(
                             System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public |
                             System.Reflection.BindingFlags.Instance,
                             null,
                             new Type[] { typeof(Stream) },
                             null)
                         .Invoke(new object[] { s });
                     return;
                 }
                 s.Position = pos;
             }
         }
     }
     catch (Exception)
     {
         s.Close();
         throw;
     }
     throw new NotSupportedException("Could not find a decoder for the specified sound stream.");
 }
Example #2
0
        internal OpenTKAudioCue(Stream data, AudioContext ac)
        {
            this.ac = ac;

            buffer = AL.GenBuffer();
            ac.CheckErrors();

            source = AL.GenSource();
            ac.CheckErrors();

            AL.Source(source, ALSourcef.Gain, (float)this.Volume);
            ac.CheckErrors();

            using (AudioReader ar = new AudioReader(data))
            {
                SoundData d = ar.ReadToEnd();
                AL.BufferData(source, d);
                ac.CheckErrors();
            }

            AL.Source(source, ALSourcei.Buffer, buffer);
            ac.CheckErrors();

            this.VolumeChanged += new VolumeChangedEventHandler(OpenTKAudioCue_VolumeChanged);
            this.BalanceChanged += new BalanceChangedEventHandler(OpenTKAudioCue_BalanceChanged);
            this.FadeChanged += new FadeChangedEventHandler(OpenTKAudioCue_FadeChanged);
        }
Example #3
0
        public override IAudioBuffer InitAudioBuffer(Resources.Resource handle)
        {
            var bufferId = AL.GenBuffer();
            var sourceId = AL.GenSource();

            var reader = new AudioReader(handle.Stream);
            //Read
            var soundData = reader.ReadToEnd();
            AL.BufferData(bufferId, soundData);
            var error = AL.GetError();
            if (error != ALError.NoError)
            {
                Logger.Error("Could not read Audio, {0}", error);
                return null;
            }

            var audioBuffer = new OpenAlAudioBuffer(bufferId,sourceId,soundData.Data.Length, handle);

            //Prepare
            AL.Source(sourceId, ALSourcei.Buffer, (int)bufferId);

            //Add
            AllSamples.Add(audioBuffer);
            return audioBuffer;
        }
Example #4
0
		internal SoundEffect(string fileName)
		{
			AudioEngine.EnsureInit();

			buffer = AL.GenBuffer(); 
		
			var error = AL.GetError();
			if (error != ALError.NoError)
			{
				throw new OpenALException(error, "borked generation. ALError: " + error.ToString());
			}

			XRamExtension XRam = new XRamExtension();
			if (XRam.IsInitialized) 
				XRam.SetBufferMode(1, ref buffer, XRamExtension.XRamStorage.Hardware); 
			
			
			
			AudioReader sound = new AudioReader(fileName);
			var sounddata = sound.ReadToEnd();
			AL.BufferData(buffer, sounddata.SoundFormat.SampleFormatAsOpenALFormat, sounddata.Data, sounddata.Data.Length, sounddata.SoundFormat.SampleRate);
			error = AL.GetError();
			if ( error != ALError.NoError )
			{
			   throw new OpenALException(error, "unable to read " + fileName);
			}			
			
			_name = Path.GetFileNameWithoutExtension(fileName);
		}