Linux keyboard and sound problems

Hi,

I’m at the final stage of the project from DrT’s course at coursera.

First: The autofire does not work as intended using the keyboard.

It did work using the mouse, I’m reusing the same logic. You should be able to spam the space bar for rapid fire and when you keep it pressed there should be a constant fire rate. It does shoot in weird bursts then and another burst when the space bar is released.

// shoot if appropriate
if (keyState.IsKeyDown(Keys.Space) && health > 0)
{
    if (canShoot)
    {
        canShoot = false;
        Projectile projectile = new Projectile(ProjectileType.FrenchFries, Game1.GetProjectileSprite(ProjectileType.FrenchFries), drawRectangle.X + drawRectangle.Width / 2, drawRectangle.Y + drawRectangle.Height + GameConstants.FrenchFriesProjectileOffset, GameConstants.FrenchFriesProjectileSpeed);
        Game1.AddProjectile(projectile);
        shootSound.Play();
    }
    else
    {
        elapsedCooldownMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
    }
}
if (elapsedCooldownMilliseconds >= GameConstants.BurgerTotalCooldownMilliseconds || keyState.IsKeyUp(Keys.Space))
{
    canShoot = true;
    elapsedCooldownMilliseconds = 0;
}

Second: Sound does just produce terrible noise. There are several shoot and bounce sounds, which were provided as .wav files. The pipeline tool did not complain. It worked for the shooting sounds but after adding the bouncing sounds it just a high pitched noise.

[oppa@oppa-hex-ssd ~]$ uname -a
Linux oppa-hex-ssd.oppator 4.15.12-301.fc27.x86_64 #1 SMP Thu Mar 22 19:25:27 

UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Any ideas, esp. with the keyboard issue ? I guess the sound problem is down to some problems with converting from .wav to .xnb. In a first exercise I had a ‘boing’ sound which was just some weird sound after converting.

Happy Easter,
Stephan

Edit: Yep, there is one very short ‘tick’ sound for the bounce effect which is corrupted by the “translation” from .wav to .xnb. When I remove these from the project the rest of the sound effects are fine. Still the auto fire problem.

:flushed:The sound problem was my fault. This tick sound was played 120 times per second, 2 times per Update().

Still the keyboard weirdness.

I don’t see anything wrong with that code snippet; when are you updating the keyboard state? Also, what’s the value of GameConstants.BurgerTotalCooldownMilliseconds, and which version of Linux are you using?

Print some information to the console, such as which keys it’s registering as pressed in keyState, and post your findings here.

If you wrap your code in triple backticks (instead of single backticks) it will be properly formatted :slight_smile:
Edit: I went ahead and edited it

I don’t see anything wrong with that code snippet; when are you updating the keyboard state? Also, what’s the value of GameConstants.BurgerTotalCooldownMilliseconds, and which version of Linux are you using?

Fedora 27.

public const int BurgerTotalCooldownMilliseconds = 500;

protected override void Update (GameTime gameTime)
{
    // For Mobile devices, this logic will close the Game when the Back button is pressed
    // Exit() is obsolete on iOS
    #if !__IOS__
    if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
        Keyboard.GetState ().IsKeyDown (Keys.Escape))
    {
        Exit ();
    }
    #endif

    // get current mouse state and update burger
    // MouseState mouse = Mouse.GetState();
    KeyboardState keyState = Keyboard.GetState();
    burger.Update(gameTime, keyState);

    // update other game objects
    ...
}

Ah, is that the problem ? Keyboard.GetState() is called 2 times.
Edit2: Nope, that’s not the problem. I’ve commented out that part of code. Does not fix it.

Now, after adding sound it’s better but still showing up.

If you wrap your code in triple backticks (instead of single backticks) it will be properly formatted

Sorry, didn’t know.
Edit: Looks like this triple backtick thing has flaws too… :slight_smile:

Can you record a video showing how it’s behaving?

Done ! Where to upload, YT ?


friggin loud ! The final burst comes after releasing the space bar.

They need to be on a new line by themselves :slight_smile: You should be able to see in the preview if you formatted it correctly.

I’m pretty sure this isn’t a MonoGame keyboard issue. Can you provide a minimal sample with the bug? It might help you figure out what’s wrong by yourself and if not it’s easier for someone else to diagnose your problem.

If I remember correctly, there were some problems with Input related to SDL version MonoGame was using at some point, but the problem was fixed in newer builds (development versions?).

Anyway, try something like this for handling your Input: (I didn’t test the code but it should work)

// Simple Input class example
static class Input
{
    private static KeyboardState currentKey;
    private static KeyboardState oldKey;

    private static MouseState currentMouse;
    private static MouseState oldMouse;

    public static void UpdateCurrentState()
    {
        currentKey   = Keyboard.GetState();
        currentMouse = Mouse.GetState();
    }

    public static void UpdateOldState()
    {
        oldKey   = currentKey;
        oldMouse = currentMouse;
    }

    public static bool SpaceHeld()
    {
        return currentKey.IsKeyDown(Keys.Space);
    }

    public static bool SpacePressed()
    {
        return (currentKey.IsKeyDown(Keys.Space) && oldKey.IsKeyUp(Keys.Space));
    }

    public static bool SpaceReleased()
    {
        return (currentKey.IsKeyUp(Keys.Space) && oldKey.IsKeyDown(Keys.Space));
    }
}


// Update in Game1.cs
protected override void Update(GameTime gameTime)
{
    Input.UpdateCurrentState();

    // Logic here, e.g.

    // if (Input.SpaceHeld())
    //     ++player.charge;

    // if (Input.SpacePressed())
    //     player.Shoot();

    // if (Input.SpaceReleased() && player.charge >= player.chargeMax)
    //     player.ChargedAttack();

    Input.UpdateOldState();
    base.Update(gameTime);
}

Hi,

I’ll try soon.

Now I do have a sound problem.

protected override void LoadContent ()
{
	// Create a new SpriteBatch, which can be used to draw textures.
	spriteBatch = new SpriteBatch (GraphicsDevice);
	// Increment 1: load opening screen and set opening screen draw rectangle
	openingScreenImage = Content.Load<Texture2D>(@"graphics\openingscreen");
	openingScreenDrawRectangle = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

	// Increment 2: create the board object (this will be moved before you're done with the project)
	StartGame();

        // Increment 5: load new game sound effect
	soundEffect = Content.Load<SoundEffect>(@"audio\applause");
}
if (gameOver){
	soundEffect.Play();
	StartGame();
}

This is really only done when the game is over. The sound is applause.wav, the wav is fine but in game it’s just some scratchy noise.

I guess it’s true that Linux and Gaming won’t be firends… :broken_heart:

Not all .wav files are “equal”; some are in different format or have different amount of channels. You could convert the sound to another .wav format or just download different sound from Internet, https://opengameart.org/ is a good site for art and audio.

Other than that:

  • In your Content Pipeline, make sure that the Processor is set to Sound Effect and Quality to Best.

  • Another thing to try is to use SoundEffectInstances, but you have to clean them up yourself:

    SoundEffect sound = GetSoundFromContentManagerOrSomething(“applause”); // Your SoundEffect
    SoundEffectInstance sei = sound.CreateInstance(); // Creating instance
    sei.Volume = volume; // Set the volume, pitch and pan
    sei.Pitch = pitch;
    sei.Pan = pan;
    sei.Play(); // Play the sound
    soundInstances.Add(sei); // Add the Instance to your List holding them

    // Cleaning up after you are done with the sounds or closing the game
    if (soundInstances.Count > 0)
    {
    for (int i = soundInstances.Count - 1; i >= 0; --i)
    {
    soundInstances[i].Stop();
    soundInstances[i].Dispose();
    soundInstances.RemoveAt(i);
    }
    soundInstances.TrimExcess();
    }

Again, I didn’t test the code but it should work with little tweaking.

Hi,

the settings in pipeline tool are fine. I was wondering how to check the converted sounds. But I guess there is no way. It’s hard to believe I’m the first person to do this project on Linux, but it’s not impossible though.

I’ve tried your Input class now: Exactly the same behaviour. :smiley:

			Input.UpdateCurrentState();
			if (Input.SpacePressed() || (Input.SpaceHeld() && canShoot)){
				canShoot = false;
				Projectile projectile = new Projectile(ProjectileType.FrenchFries, Game1.GetProjectileSprite(ProjectileType.FrenchFries), drawRectangle.X + drawRectangle.Width / 2, drawRectangle.Y + drawRectangle.Height + GameConstants.FrenchFriesProjectileOffset, GameConstants.FrenchFriesProjectileSpeed);
				Game1.AddProjectile(projectile);
				shootSound.Play();
			}
			if (Input.SpaceHeld() && !canShoot){
				elapsedCooldownMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
				if (elapsedCooldownMilliseconds >= GameConstants.BurgerTotalCooldownMilliseconds){
					canShoot = true;
					elapsedCooldownMilliseconds = 0;
				}
			}
			Input.UpdateOldState();

That’s the project with the sound issues

You are not the only person developing MG games on Linux, don’t worry :stuck_out_tongue:

I just downloaded and tested your project and the sound effects play just fine for me.

1 Like