Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Sound Experiments in Managed DirectX

, 16 Feb 2007
Using static and streaming sound buffers in Managed DirectX.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES
//  OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  � 2006 Gary W. Schwede  Stream Computers, Inc. All rights reserved.
//  Contact: gary at streamcomputers dot com. Permission to incorporate
//  all or part of this code in your application is given on the condition
//  that this notice accompanies it in your code and documentation.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using StreamComputers.Riff;
using StreamComputers.Utilities;

namespace StreamComputers.MdxDirectSound
{
	/// <summary>
	/// Implements a MDX SecondaryBuffer loaded with wave data from a whole RIFF WAVE file.
	/// </summary>
	public class StaticSoundBuffer : MdxSoundBuffer
	{
		public StaticSoundBuffer(string fileName, BufferDescription bufferDescription, Device dev)
		{
			try
			{
				m_SecondaryBuffer = new SecondaryBuffer(fileName, bufferDescription, dev);
				m_SecondaryBuffer.SetCurrentPosition(0);
				DebugConsole.Write("Upon allocation, ");
				DebugConsole.WriteLine(MdxInfo.BufferCapsAsString(m_SecondaryBuffer.Caps));
			}
			catch (OutOfMemoryException)
			{
				MessageBox.Show("This sound file is too large\n to load into a Static buffer.\n"
					+ "Select Streaming Buffer instead.");
				throw;
			}
			catch (ArgumentException)
			{
				MessageBox.Show("File is not a valid Managed DirectSound RIFF file:"
					 + "\n Please select another.");
				throw;
			}	
		}

		public override void Play()
		{
			base.Play();
			try
			{
				m_SecondaryBuffer.Play(0,BufferPlayFlags.Default);
			}
			catch(System.OutOfMemoryException)
			{
				MessageBox.Show("This sound file is too large\n  to play using a Static buffer.\n"
					+ "Select Streaming Buffer instead.");
				throw;
			}
		}

		/// <summary>
		/// Pauses and resumes play. If not playing nor paused, has no effect.
		/// </summary>
		/// <returns>Buffer.PlayPosition at time of call</returns>
		public override int Pause()
		{
			base.Pause();
			int playPos = m_SecondaryBuffer.PlayPosition;
			if (m_SecondaryBuffer.Status.Playing)
			{
				m_SecondaryBuffer.Stop();
				m_Paused = true;
			}
			else if(m_Paused)
			{
				m_SecondaryBuffer.Play(0,BufferPlayFlags.Default);
				m_Paused = false;
			}
			return playPos ;
		}

		/// <summary>
		/// Stops play and resets Buffer.PlayPosition to 0 (the beginning).
		/// </summary>
		public override void Stop()
		{
			base.Stop();
			m_SecondaryBuffer.Stop();
			m_SecondaryBuffer.SetCurrentPosition(0);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

DrGary83
Software Developer (Senior)
United States United States
My life and career have been a bit unusual (mostly in good ways). So, I'm grateful every day for the opportunities God's given me to do different things and see different aspects of life.
 
Education: B.S. Physics '73 (atmospheric physics, sounding rockets), M.S. Computer Science '76 (radio astronomy, fuzzy controllers, music pattern recognition and visualization) New Mexico Tech; Ph.D. Engineering '83 (parallel computer architecture, digital signal processing, economics) U.C. Berkeley.
 
I'm married to Susan, a wonderful woman whom I met in a Computer Architecture class at U.C. Berkeley.
 
Professional activities: Digital systems engineer, digital audio pioneer, founder or key in several tech startups, consulting engineer, expert witness. I'm currently developing a multithreading framework in C# .NET, that makes it almost easy to write correct programs for multicore processors. I'm also implementing a new transform for recognizing, editing, and processing signals, especially sound.
 
I'm an occasional essayist, public speaker, and podcaster, and free-market space advocate. I enjoy good wine, good music, good friends, and cats.
 
If you think your project could use a different point of view, I'm available for consulting work in the San Francisco Bay area, or (preferrably) via the net.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 16 Feb 2007
Article Copyright 2006 by DrGary83
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid