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

Author Topic: Two sound buffer into one.  (Read 6348 times)

0 Members and 1 Guest are viewing this topic.

BartekMaciag

  • Newbie
  • *
  • Posts: 4
    • View Profile
Two sound buffer into one.
« 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;
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Two sound buffer into one.
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Two sound buffer into one.
« Reply #2 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).
Back to C++ gamedev with SFML in May 2023

Antonio9227

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Two sound buffer into one.
« Reply #3 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 :)
« Last Edit: February 05, 2014, 12:22:06 pm by Antonio9227 »

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Two sound buffer into one.
« Reply #4 on: February 05, 2014, 06:09:14 pm »
Some time ago I wanted to do the same thing and I found this article 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....

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Two sound buffer into one.
« Reply #5 on: February 05, 2014, 06:18:39 pm »
Some time ago I wanted to do the same thing and I found this article 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.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Two sound buffer into one.
« Reply #6 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Two sound buffer into one.
« Reply #7 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 :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything