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

Author Topic: Applying echo effect using sfml audio  (Read 4864 times)

0 Members and 1 Guest are viewing this topic.

eyadof

  • Newbie
  • *
  • Posts: 3
    • View Profile
Applying echo effect using sfml audio
« on: February 17, 2014, 01:47:53 am »
hi  :)

i'm pretty new with sfml but it was really simple to use, but now I'm having some problems with sfml-audio, I'm trying to apply echo effect on sound by replaying the same sf::SoundBuffer in another sf::Sound object with small latency but I still fail in every way i try.

is it possible to make an echo using sfml ?or there is another way ?

Thanks.
« Last Edit: February 17, 2014, 01:50:57 am by eyadof »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Applying echo effect using sfml audio
« Reply #1 on: February 17, 2014, 02:42:25 am »
Replaying the sound buffer in more sound objects is one possible way of doing it (and probably the easiest). You could also mix the echo into the sound buffer itself (either externally, or internally - real-time or not).

Since you tried the former method, could you tell us what exactly you call a fail?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eyadof

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Applying echo effect using sfml audio
« Reply #2 on: February 17, 2014, 09:48:49 am »
i'm sorry for annoying everybody my mistake was really dump  :-[ , I declared the new Sound object in the wrong scope, and now it is working fine and this how I make it:

#include <SFML/Audio.hpp>
void Echo();
//That what I missed
sf::SoundBuffer buffer;
sf::Sound sound;
sf::Sound echo;

int main ()
{
    buffer.loadFromFile("simple.wav");
    sound.setBuffer(buffer);
    echo.setBuffer(buffer);
    sound.play();
    sf::sleep(sf::seconds(8)); //just for start the echo after 8 sec
    Echo();
    while(sound.Playing);
    return 0;
}

void Echo(){
    echo.setPlayingOffset(sound.getPlayingOffset());
    sf::sleep(sf::seconds(0.2f));
    echo.play();
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Applying echo effect using sfml audio
« Reply #3 on: February 17, 2014, 09:00:40 pm »
You forgot the SoundBuffer?  :o

Rather than creating the objects in global scope, you could create them within main and then pass them to the functions that use them (by reference). Or, you might want to use classes.

Also, you should possibly look into using sf::Clock to timing your echoes rather than just blocking using sleep as this stops the program from being able to do anything else. I don't know how new you are, though, so I can understand that you'd use sleep to quickly get something to actually run :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eyadof

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Applying echo effect using sfml audio
« Reply #4 on: February 17, 2014, 11:14:54 pm »
Haha  :D , I didn't forget the buffer of course but there was a mess in declarations of those three objects.

In my real situation I'm actually using a class to wrap up the working with sound functionality which is used by other objects and I made some scope errors  :-X, This code is just a demo (an awful one  :-\), maybe I'm looking like a totally rookie programmers but it's the "deadlines pressure" which make the impossible possible :D.

Thank you about the sf:Clock clue that is how it's done but as the doc said about sleep:
 
Quote
Make the current thread sleep for a given duration.

I don't mind if the echo sound thread blocked less than a second.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Applying echo effect using sfml audio
« Reply #5 on: February 18, 2014, 12:54:03 am »
Quote
Make the current thread sleep for a given duration.

I don't mind if the echo sound thread blocked less than a second.
The thread that is put to sleep is the current one. That is, the one running the code, not the separate one that SFML creates for playing sounds. I don't think it's possible to access those threads but I may be mistaken.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything