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

Author Topic: Using Threads with Sound Functions  (Read 3315 times)

0 Members and 1 Guest are viewing this topic.

ClaudioVC

  • Newbie
  • *
  • Posts: 10
    • View Profile
Using Threads with Sound Functions
« on: February 16, 2017, 06:46:17 am »
Hi all,
Well, im working in a software to a Loop Pedal, so i need to play sounds (recorded in the moment) at the same time that im recording other sounds. example:

Quote
> blah blah
> sound.play();
> recorder.start();
> blah blah

and i need those function start at the same time. i was thinking use this but it didnt work :

Quote
> blah blah
> sf::Thread thread(sound.play());
> thread.launch();
> recorder.start();
> blah blah

Im new using the SFML library, and i really dont know how use SFML threads and the tutorials just talk about how use it with c++ void function, but nothing about sfml functions (or i didnt realize) so any help will help.
Sorry for any english mistake.

I have Windows 10, and im working with CodeForce.

Thanks!
« Last Edit: February 16, 2017, 11:33:06 pm by ClaudioVC »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using Threads with Sound Functions
« Reply #1 on: February 16, 2017, 01:17:24 pm »
Sounds are automatically started in their own thread so you can start it and continue with your program.
So, just your first code block would probably be the way to do it.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ClaudioVC

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Using Threads with Sound Functions
« Reply #2 on: February 16, 2017, 05:08:06 pm »
Sounds are automatically started in their own thread so you can start it and continue with your program.
So, just your first code block would probably be the way to do it.

Oh ok, i didnt know that, but, each sound starts in their own thread?. Because as a loop pedal, it will have a lot of sound recorded playing at the moment and i need all these sounds start at the same time, so i think each sound need to work in a different thread (not really, im thoght how to do it just with 2 thread).

So, if a have 2 sounds and i need those start at the same time, each sound will start in a different thread? or the SOUND LIBRARY have their own thread?.

The real problem that i have is, when i recorded the first sound, i need that sound playing in the background (as loop) while im recording the second sound, when i finish to record the second sound, both have to play at the same time. Well, the problem is: When i play the first sound and im recording the second one, there is a delay between those, but when i finish to record the second one and they both are playing there is not a delay (but i need to have the tempo on my brain and not listen the first sound).

i hope u can understand me :(

Thanks for your help! :D

PS: If someone dont know how a loop pedal works here there is a video:  https://youtu.be/DV0TJZ7Kp40?t=14s
« Last Edit: February 16, 2017, 06:32:55 pm by ClaudioVC »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using Threads with Sound Functions
« Reply #3 on: February 16, 2017, 08:11:40 pm »
You can start multiple sounds and they play on their own without bothering the others, yes. Therefore, you can play multiple sounds at once (including starting at different times) and they will just play until they finish or you stop them.

Recording is another question.
If your recording is set up to record everything you hear then when you play back what you have already recorded, it should then record that too.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ClaudioVC

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Using Threads with Sound Functions
« Reply #4 on: February 16, 2017, 11:09:38 pm »
Hi again, i know its hard to explain my problem so i did some images. This is the problem jaj :



This is the code that im using:

Quote

sound.setLoop(true);
sound2.setLoop(true);

----------blah----------

                if(Event.key.code == sf::Keyboard::R){
                    recorder.start();
                    std::cout<<"> Recording First Loop"<<std::endl;
                }
                else if(Event.key.code == sf::Keyboard::S){
                    recorder.stop();
                    buffer = recorder.getBuffer();
                    sound.setBuffer(buffer);
                    duration_loop = buffer.getDuration().asMilliseconds();
                    sound.play();
                    std::cout<<"> Recording Second Loop"<<std::endl;
                    recorder.start();
                    clock.restart();
                    while(clock.getElapsedTime().asMilliseconds()<duration_loop)
                        continue;
                    recorder.stop();
                    buffer2 = recorder.getBuffer();
                    sound2.setBuffer(buffer2);
                    sound2.play();
                    duration_loop2 = buffer2.getDuration().asMilliseconds();
                    std::cout<<"First Loop Time: "<<duration_loop<<std::endl;
                    std::cout<<"Second Loop Time: "<<duration_loop2<<std::endl;
                }

and this is what i got:



i know the time of the sound are right, but how i explained in the first images, when you hear both, there is a delay.

Thanks again for help me :)

« Last Edit: February 16, 2017, 11:34:59 pm by ClaudioVC »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using Threads with Sound Functions
« Reply #5 on: February 16, 2017, 11:48:53 pm »
I haven't used SFML's audio recording as yet so I have no experience of how it responds.

However, there are two things that could be of note:
  • When you stop recording, are you copying the buffer? This could cause a delay.
  • It's likely that there will be some audio buffer on your soundcard; this playback to be "laggy".

If it's the laggy playback, try making sure nothing else is running - including the IDE - and in release mode. Also, check your soundcard settings and look for a playback buffer and try reducing it. Might also need a restart of your system too.

If it's your recording buffer being copied, try recording directly into the sound buffer instead of copying it. Is that how recording works?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ClaudioVC

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Using Threads with Sound Functions
« Reply #6 on: February 17, 2017, 12:03:35 am »
I haven't used SFML's audio recording as yet so I have no experience of how it responds.

However, there are two things that could be of note:
  • When you stop recording, are you copying the buffer? This could cause a delay.
  • It's likely that there will be some audio buffer on your soundcard; this playback to be "laggy".

If it's the laggy playback, try making sure nothing else is running - including the IDE - and in release mode. Also, check your soundcard settings and look for a playback buffer and try reducing it. Might also need a restart of your system too.

If it's your recording buffer being copied, try recording directly into the sound buffer instead of copying it. Is that how recording works?

Well, this is the first part of the code:
 
Quote
    sf::SoundBufferRecorder recorder;
    sf::SoundBuffer buffer;
    sf::SoundBuffer buffer2;
    sf::Sound sound;
    sf::Sound sound2;
    sound.setLoop(true);
    sound2.setLoop(true);
    float duration_loop;
    float duration_loop2;

So i dont copying the buffer cause i made a buffer for each audio, so it both exist always.
About SoundCard, im using a professional one, because i need this software to play music in high quality.

I dont really know if im answering your question right. My english is not so good. :(

I mean the real problem is that i cant stop a record of a sound and play it at the same time, there is a delay between stop record and play :(

Also, i noticed that im in the wrong forum, i was right first when i was asking about thread and sound (i think), but now i have problem just with SFML's Audio, have i to post there?

thanks for reply.
« Last Edit: February 17, 2017, 12:24:56 am by ClaudioVC »