/// <summary> /// Performs initialization steps by providing the audio stream parameters. /// </summary> /// <param name="channelCount">The number of channels of current <see cref="SoundStream"/> object.</param> /// <param name="sampleRate">The sample rate, in samples per second.</param> protected virtual void Initialize(int channelCount, int sampleRate) { // Reset the current states _channelCount = channelCount; _sampleRate = sampleRate; _processed = 0; _isStreaming = false; // Deduce the format from the number of channels _format = AudioDevice.GetFormat(channelCount); // Check if the format is valid if (_format == 0) { throw new NotSupportedException("The specified number of channels (" + _channelCount.ToString() + ") is not supported."); } }
private void Update(int channelCount, int sampleRate) { // Check parameters if (channelCount <= 0 || sampleRate <= 0) { throw new ArgumentException(); } // Find the best format according to the number of channels var format = AudioDevice.GetFormat(channelCount); if (format == 0) { throw new Exception("Failed to load sound buffer (unsupported number of channels: " + channelCount.ToString() + ")"); } // First make a copy of the list of sounds so we can reattach later var sounds = new List <Sound>(_sounds); // Detach the buffer from the sounds that use it (to avoid OpenAL errors) foreach (var sound in sounds) { sound.ResetBuffer(); } // fill the buffer int size = _samples.Length * sizeof(short); ALChecker.Check(() => AL.BufferData(_buffer, format, _samples, size, sampleRate)); // Compute the duration _duration = TimeSpan.FromSeconds((float)_samples.Length / sampleRate / channelCount); // Now reattach the buffer to the sounds that use it foreach (var sound in sounds) { sound.Buffer = this; } }