• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
CdrTomalak

Threading and sound. Annoying bug.

2 posts in this topic

Hi,

 

I've been trying to pin down an annoying performance bug in my C# 2d game engine.

 

It only occurs when I have sound enabled, and I am trying to figure out what I am doing wrong. I suspect multiple things, since the sound functionality was implemented quite a while ago to achieve basic functionality. At this stage in development it is now a higher priority. I need to resolve it.

 

When I implemented sound it was after much experimentation with sound APIs. I tried SoundPlayer, MediaPlayer, and WindowsMediaPlayer - with the latter the only one I could get working in an acceptable manner.

 

My implementation was based around setting up an array of players for each sound I wanted to play. They are all instantiated up-front. The method by which they are used is:

1. Search through player array until you find one which isn't currently playing.

2. Play the sound.

 

Now for some reason, which I can't figure out, this implementation is causing a wierd problem. I have a method which fires when you die, and from what I can tell this does NOTHING with sound whatsoever. It basically waits 5 seconds before restarting the game. But with sound on the screen flickers for a millisecond, something in sharpdevelop refreshes, and it's as if there is a sudden lag in the game. From a functionality point of view I don't have a problem  - but the performance is unacceptable.

 

To give you an idea of what I'm doing, here is some code from my sound initialisation method:

                if(soundLibrary=="WindowsMediaPlayer")
                {    
                
                    // Music player
                    WMP_Music = new WMPLib.WindowsMediaPlayer();
                    WMP_Music.settings.autoStart = false;
                    WMP_Music.URL = "D:\\Programming\\SFX\\MUSIC\\IN_GAME_1.wav";
                
                    // Array of players ----------------------------------------
                    WMP_EATEN_GHOST_Array = new WMPLib.WindowsMediaPlayer[EATEN_GHOST_noOfPlayers];
                    WMP_EATEN_PILL_Array = new WMPLib.WindowsMediaPlayer[EATEN_PILL_noOfPlayers];
                    WMP_EATEN_POWERPILL_Array = new WMPLib.WindowsMediaPlayer[EATEN_POWERPILL_noOfPlayers];
                    WMP_KILLED_BY_GHOST_Array = new WMPLib.WindowsMediaPlayer[KILLED_BY_GHOST_noOfPlayers];    
                    
                    for(int i=0;i<WMP_EATEN_GHOST_Array.Length;i++)
                    {
                        WMP_EATEN_GHOST_Array[i] = new WMPLib.WindowsMediaPlayer();
                        WMP_EATEN_GHOST_Array[i].settings.autoStart = false;
                        WMP_EATEN_GHOST_Array[i].URL = "D:\\Programming\\SFX\\EATEN_GHOST\\Hit_55.wav";

                        
                        WMP_EATEN_PILL_Array[i] = new WMPLib.WindowsMediaPlayer();
                        WMP_EATEN_PILL_Array[i].settings.autoStart = false;
                        WMP_EATEN_PILL_Array[i].URL = "D:\\Programming\\SFX\\EATEN_PILL\\Hit_43_s.wav";

                        
                        WMP_EATEN_POWERPILL_Array[i] = new WMPLib.WindowsMediaPlayer();
                        WMP_EATEN_POWERPILL_Array[i].settings.autoStart = false;
                        WMP_EATEN_POWERPILL_Array[i].URL = "D:\\Programming\\SFX\\EATEN_POWERPILL\\Hit_49.wav";

                        
                        WMP_KILLED_BY_GHOST_Array[i] = new WMPLib.WindowsMediaPlayer();
                        WMP_KILLED_BY_GHOST_Array[i].settings.autoStart = false;
                        WMP_KILLED_BY_GHOST_Array[i].URL = "D:\\Programming\\SFX\\KILLED_BY_GHOST\\Hit_55.wav";

                    }

So with these set up, here's the code which plays the sound:

		public void PlayPCM_WindowsMediaPlayer_FX(string effectIn)
		{
			int i=-1;
			bool found = false;
			
			if(soundEnabled)
			{
				switch(effectIn)
				{
					case "KILLED_BY_GHOST":
						// 1.	Choose a media player which is not playing.
						i = 0;
						do
						{
							if(WMP_KILLED_BY_GHOST_Array[i].playState == WMPLib.WMPPlayState.wmppsPlaying)
							{
								// Keep looking
								i++;
							}
							else
							{
								found = true;
							}
						} while( (i<WMP_KILLED_BY_GHOST_Array.Length) && (!found));
						// Now 'i' holds the index of the player which is not playing.
						
						// 2.	Play effect					
						if(found)
						{
							Thread thread = new Thread(new ThreadStart( WMP_KILLED_BY_GHOST_Array[i].controls.play ));
							thread.Start();
												
						}
						break;
						
					case "EATEN_PILL":
						// 1.	Choose a media player which is not playing.
						i = 0;
						do
						{
							if(WMP_EATEN_PILL_Array[i].playState == WMPLib.WMPPlayState.wmppsPlaying)
							{
								// Keep looking
								i++;
							}
							else
							{
								found = true;
							}
						} while( (i<WMP_EATEN_PILL_Array.Length) && (!found));
						// Now 'i' holds the index of the player which is not playing.
						
						// 2.	Play effect	
						if(found)
						{					
							
							Thread thread = new Thread(new ThreadStart( WMP_EATEN_PILL_Array[i].controls.play ));
							thread.Start();
									
						}
						
						break;
					case "EATEN_GHOST":
						// 1.	Choose a media player which is not playing.
						i = 0;
						do
						{
							if(WMP_EATEN_GHOST_Array[i].playState == WMPLib.WMPPlayState.wmppsPlaying)
							{
								// Keep looking
								i++;
							}
							else
							{
								found = true;
							}
						} while( (i<WMP_EATEN_GHOST_Array.Length) && (!found));
						// Now 'i' holds the index of the player which is not playing.
						
						// 2.	Play effect					
						if(found)
						{
							Thread thread = new Thread(new ThreadStart( WMP_EATEN_GHOST_Array[i].controls.play ));
							thread.Start();
									
						}
						break;
					case "EATEN_POWERPILL":
						// 1.	Choose a media player which is not playing.
						i = 0;
						do
						{
							if(WMP_EATEN_POWERPILL_Array[i].playState == WMPLib.WMPPlayState.wmppsPlaying)
							{
								// Keep looking
								i++;
							}
							else
							{
								found = true;
							}
						} while( (i<WMP_EATEN_POWERPILL_Array.Length) && (!found));
						// Now 'i' holds the index of the player which is not playing.
						
						// 2.	Play effect					
						if(found)
						{
							Thread thread = new Thread(new ThreadStart( WMP_EATEN_POWERPILL_Array[i].controls.play ));
							thread.Start();
												
						}
						break;
				} // switch
			} // soundEnabled
		}		

It's pretty clunky I know, but it worked at the time - well enough anyway. Can anyone see anything which would explain why I get a screen flicker?

 

My threading expertise is really basic at the moment.

 

Thanks in advance blink.png

0

Share this post


Link to post
Share on other sites

Starting a thread for each sound played seems really heavyweight. Why do you even need to use threads here? (I'm not familiar with the WindowsMediaPlayer API). How many threads are running when this problem happens?

 

Just from a quick glance at the code, I think I already see a threading bug. You're checking for (playState == WMPLib.WMPPlayState.wmppsPlaying). When would a player actually enter that state? I assume it's when the play delegate is called. But there's no guarantee that it has been called by the time thread.Start() returns. So you could easily enter this code again, and a choose player which is not wmppsPlaying but already has a thread started for it.

1

Share this post


Link to post
Share on other sites

Thanks Phil. The only reason I'm using threads is that when I play without a thread all execution waits for the sound to finish. Can you recommend a simple method I could use? I thought that sound playing had to be done via threading due to the wait issue, but I could be very wrong here!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0