public SoundEffect(AudioDevice device, string filename) { dev = device; List<byte> totalBytes = new List<byte> (); byte[] buffer = new byte[4096]; var stream = File.OpenRead (filename); ALFormat fmt; int sampleRate; using (var decoder = DecoderFactory.GetDecoderFromStream (stream)) { int read = 0; fmt = decoder.Format; sampleRate = decoder.SampleRate; while ((read = decoder.Read (4096, buffer)) == 4096) { totalBytes.AddRange (buffer); } totalBytes.AddRange (buffer.Take (read)); } dev.EnsureAudioThread(() => { alBuffer = AL.GenBuffer(); AL.BufferData( alBuffer, fmt, totalBytes.ToArray(), totalBytes.Count, sampleRate ); }); }
internal StreamingAudio(AudioDevice device, ALFormat format, int sampleRate) { bufferFormat = format; while (!device.ready) ; var sid = AL.GenSource (); sourceId = (int)sid; AudioDevice.CheckALError (); bufferIds = AL.GenBuffers (4); AudioDevice.CheckALError (); this.device = device; this.sampleRate = sampleRate; }
public SoundEffect(AudioDevice device, Sound sound) : this(device, sound.Filename) { }
/// <summary> /// Initializes a new instance of the <see cref="nginz.SoundManager"/> class. /// </summary> public SoundManager() { Device = new AudioDevice (); }
/// <summary> /// Initializes a new instance of the <see cref="nginz.SoundManager"/> class. /// </summary> public SoundManager() { Device = new AudioDevice(); }
internal void Update() { //manage state if (currentState == AudioPlayState.Stopped) { AL.SourceStop(sourceId); device.Remove(this); threadRunning = false; if (!userStopped) { if (PlaybackFinished != null) { PlaybackFinished(this, true); } } userStopped = false; return; } var state = AL.GetSourceState(sourceId); AudioDevice.CheckALError(); if (currentState == AudioPlayState.Paused) { if (state != ALSourceState.Paused) { AL.SourcePause(sourceId); } return; } //load buffers int processed_count; AL.GetSource(sourceId, ALGetSourcei.BuffersProcessed, out processed_count); while (processed_count > 0) { int bid = AL.SourceUnqueueBuffer(sourceId); if (bid != 0 && !finished) { int length = BufferNeeded(this, buffer); finished = length <= 0; if (!finished) { AL.BufferData(bid, bufferFormat, buffer, length, sampleRate); AL.SourceQueueBuffer(sourceId, bid); } } --processed_count; } //check buffer if (state == ALSourceState.Stopped && !finished) { AL.SourcePlay(sourceId); } //are we finished? if (finished && state == ALSourceState.Stopped) { device.Remove(this); currentState = AudioPlayState.Stopped; threadRunning = false; Empty(); if (PlaybackFinished != null) { PlaybackFinished(this, false); } } }