Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Playing sound causes frame or line skips.  (Read 3357 times)

0 Members and 2 Guests are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Playing sound causes frame or line skips.
« on: October 15, 2023, 03:58:39 am »
I'm having a problem with a function I made and I was wondering if anyone could help me figure out the problem.I have a file called playsound.h,and a .cpp file for that. When the function runs,a sound plays as wanted but it causes a problem. I'm not sure if it causes a lag or a line skip,or a frame skip or what.All I know is that it causes image skips and it causes the program to get its variables all messed up so I assume it has something to do with skipping some code. So the question is" Why does it do this?".


playsound.h

#pragma once
#include <SFML\Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace sf;
#include <string>
using namespace std;


extern Sound channel[50];
extern SoundBuffer noise[50];
extern string lastsound[50];
void playsound(int channelnumber,string soundDirectory);



playsound.cpp

#include <SFML\Audio.hpp>
using namespace sf;
#include "playsound.h"
#include <string>
using namespace std;

Sound channel[50];
SoundBuffer noise[50];
string lastsound[50];

void playsound(int channelnumber,string soundDirectory)
{

        if(soundDirectory!=lastsound[channelnumber])
        {
                noise[channelnumber].loadFromFile(soundDirectory);
                channel[channelnumber].setBuffer(noise[channelnumber]);

                if(channel[channelnumber].getStatus()!=Sound::Playing)
                {
                        channel[channelnumber].play();
                        lastsound[channelnumber]=soundDirectory;
                }
        }
        if(channel[channelnumber].getStatus()!=Sound::Playing)
        {
                lastsound[channelnumber]="";
        }
}

 


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Playing sound causes frame or line skips.
« Reply #1 on: October 16, 2023, 12:20:35 pm »
It looks like you're loading the sound from a file every time you ask it to play it. This (file access and moving around large pieces of memory) could be causing some slow-down.

It's mostly better to load all of the sound buffers first, then just assign them to sounds when you need to play them.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Playing sound causes frame or line skips.
« Reply #2 on: October 17, 2023, 07:54:14 pm »
I only played the sound once and the function only loads the file once every start. Could that alone be too much to handle?

Update:
I tried it without using any arrays and theres no change.
« Last Edit: October 17, 2023, 08:12:33 pm by Me-Myself-And-I »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Playing sound causes frame or line skips.
« Reply #3 on: October 18, 2023, 11:54:57 am »
Another issue here is globals. Keeping SFML things (especially things like heavy resources such as sound buffers) should never be global. There are technical reasons for this with these objects but it's good practice to avoid this with everything if possible.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Playing sound causes frame or line skips.
« Reply #4 on: October 18, 2023, 11:28:08 pm »
The part that seems most important to me is "it causes the program to get its variables all messed up". That sounds like memory corruption, depending on what exactly messed up implies.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Playing sound causes frame or line skips.
« Reply #5 on: October 19, 2023, 04:45:26 pm »
I'm thinkin it gets the variables messed up because it causes large variable changing sections of code to be skipped.I tried making it local but that didn't work.I did however find out that moving that calling function fixes it.It would be good though to know why this changes anything.
So I moved it directly past the function it was skipping the rest of.
       


bool click(int left,int top,int width,int height)
{
                if(Mouse::isButtonPressed(Mouse::Left))
                {
                       
                        if(IntRect(left,top,width,height).contains(MPosition.x,MPosition.y))
                        {
                                if(released)
                                {
                                        released=false;
                                       
                                       
                                        //playsound() was here
                                       
                                       
                                        return true;
                                }
                        }
                        else
                        {
                                if(released)
                                {
                                       
                                       
                                }
                        }
                       
                        released=false;
                       
                }
                else
                {
                        released=true;
                        return false;
                       
                }
       
}      

void loop()
{
       
        MPosition=window.mapPixelToCoords(Mouse::getPosition(window));
       
        if(room==1)
        {
                if(leave)
                        tback.loadFromFile("1.png");
                leave=false;
       
                if(click(501,256,104,54))//click function
                {
                        playsound(0,"room.wav");//moved to here
                        room=2;
                        leave=true;
                }
               
        }
}