SFML community forums

Help => Audio => Topic started by: BartekMaciag on January 31, 2014, 04:52:10 pm

Title: Two sound buffer into one.
Post by: BartekMaciag on January 31, 2014, 04:52:10 pm
Is sfml have any way to create one buffer from two others buffers  ?
For example:
sf::SoundBuffer sb1;
sf::SoundBuffer sb2;
sf::SoundBuffer sb3;
sb3 = sb2 + sb1;
 
Title: AW: Two sound buffer into one.
Post by: eXpl0it3r on January 31, 2014, 05:31:26 pm
No, SFML doesn't provide such functionality. You could abstract things away however, by providing your own class that you can assign multiple buffers and automatically let them play on after the other. ;)
Title: Re: Two sound buffer into one.
Post by: FRex on January 31, 2014, 05:40:33 pm
If rate and channel count is same you could add samples and load them into another buffer easily (if it's different it's harder and requires processing them a bit).
Title: Re: Two sound buffer into one.
Post by: Antonio9227 on February 05, 2014, 12:20:26 pm
As FRex said, it is pretty easy to do this when your sounds have the same sample rate and channels count.
Here is a sample code I just wrote, if you need to do the action multiple times I suggest making a function out of it.
    sf::SoundBuffer sb1;
    sf::SoundBuffer sb2;
    sf::SoundBuffer sb3;

//Declare a array of shorts to store all the samples from both buffers
//We use dynamic allocation to give it the proper size to hold the data
    short *samples= new short[sb1.getSampleCount() + sb2.getSampleCount()];

//Copy all the samples of sb1 into our array
    for(int i=0; i<sb1.getSampleCount(); i++)
        samples[i]=sb1.getSamples()[i];

//Copy all the samples of sb2 into our array, in addition to the samples from sb1
    for(int i=0; i<sb2.getSampleCount; i++)
        samples[i+sb1.getSampleCount()]=sb2.getSamples()[i];

//Finally load sb3 from samples
//  sb3.loadFromSamples( <samples> , <how many samples to load> , <how many channels our sound has> , <our sampleRate>)
    sb3.loadFromSamples(samples,sb1.getSampleCount() + sb2.getSampleCount(),sb1.getChannelCount(),sb1.getSampleRate());

//After using the array, free the memory
        delete [] samples;
*This will only work properly if the sounds have the same samplerate and channel count!

However, SFML does not support doing this directly :)
Title: Re: Two sound buffer into one.
Post by: Foaly on February 05, 2014, 06:09:14 pm
Some time ago I wanted to do the same thing and I found this article (http://www.vttoth.com/CMS/index.php/technical-notes/68) very interessting.
When you simply add to soundBuffers there is a chance that there might be an overflow, if you have two very loud signals. The article shows a way to avoid that, by using a different mixing algorithm.
Sadly I can't remember where I put the code....
Title: Re: Two sound buffer into one.
Post by: zsbzsb on February 05, 2014, 06:18:39 pm
Some time ago I wanted to do the same thing and I found this article (http://www.vttoth.com/CMS/index.php/technical-notes/68) very interessting.
When you simply add to soundBuffers there is a chance that there might be an overflow, if you have two very loud signals. The article shows a way to avoid that, by using a different mixing algorithm.
Sadly I can't remember where I put the code....

That is useful for mixing two sound buffers, however I do believe the OP wanted to simply append two sound buffers which does not require any mixing.  ;)
Title: Re: Two sound buffer into one.
Post by: Foaly on February 08, 2014, 04:10:56 pm
Oh sorry I missed that! I was confused because of the code posted above, which also mixes two soundbuffers (and is also quiet bad performance-wise).

Appending a soundbuffer to another is even easier, you simply have to create a std::array of sf::Int16 which is big enough to hold both of your soundbuffers data (i.e. sb1.getSampleCount() + sb2.getSampleCount()). Then you use getSamples() and the sample count together with a memory copying function from the standart library to copy the data of the two soundBuffers into your array. Afterwards you use loadFromSamples() to load a new soundBuffer from the array.
Title: Re: Two sound buffer into one.
Post by: Hapax on February 09, 2014, 01:23:46 am
The OP doesn't actually state enough information to know whether they mean mix or append. It'd be nice if the OP let everyone know if they got what they needed  ::)

That said, I've found some interesting information in this thread :)