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

Author Topic: Cropping SoundBuffer  (Read 3614 times)

0 Members and 1 Guest are viewing this topic.

ksneiders

  • Newbie
  • *
  • Posts: 9
    • View Profile
Cropping SoundBuffer
« on: March 28, 2014, 11:48:34 am »
Hello SFML community!

So, i have a problem -

In a project that i am working on, there is a need to cut off a small part(1/20 sec) of the sound buffer from the beginning.

So essentially i made a function that takes the soundbuffer, removes the first 2205 (44100 / 20) samples and returns it. Then i make a sound from the returned buffer, but it wont play!
Is there any special tricks or things that i should know when doing these type of things?
Maybe there is a better way of doing this?

here is the function:

sf::SoundBuffer BufferHelper::RemoveFromBeginning(sf::SoundBuffer Buffer)
{
        const sf::Int16* Samples = Buffer.getSamples();

        std::vector<sf::Int16> vectorBufferNew;
        vectorBufferNew.reserve(Buffer.getSampleCount() - 2205);
        vectorBufferNew.resize(Buffer.getSampleCount() - 2205);

        for (int i = 0; i < Buffer.getSampleCount() - 2205; i++)
        {
                vectorBufferNew[i] = Samples[2205 + i];
        }

        sf::SoundBuffer FinalBuffer;
        FinalBuffer.loadFromSamples(&vectorBufferNew[0], vectorBufferNew.size(), 1, 44100);

        return FinalBuffer;
}
 

Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Cropping SoundBuffer
« Reply #1 on: March 28, 2014, 11:55:08 am »
Can't you just edit the sound file in an audio application?

If not, what about applying a offset when playing the sound?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Cropping SoundBuffer
« Reply #2 on: March 28, 2014, 12:20:36 pm »
You should show us how you call the function and create/play the sound.

And this would be much better:

void BufferHelper::RemoveFromBeginning(const sf::SoundBuffer& src, sf::SoundBuffer& dst)
{
    const std::size_t cut = 2205; // should rather be calculated from sample rate and channel count

    dst.loadFromSamples(src.getSamples() + cut, src.getSampleCount() - cut, src.channelCount(), src.sampleRate());
}
« Last Edit: March 31, 2014, 10:16:28 am by Laurent »
Laurent Gomila - SFML developer

ksneiders

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Cropping SoundBuffer
« Reply #3 on: March 28, 2014, 12:44:43 pm »
You should show us how you call the function and create/play the sound.

And this would be much better:


Well, ok, the function will be a bit complicated, since i want the program to remember the part that was cut of, so it can be added back later! The way i wanted to do this by giving two arguments by reference - an array that would store the information and an integer which would count the times the piece is cut off. Can you maybe suggest a better solution?

About the channel count - it will always be only one channel, since all the sounds are recorded using SoundBufferRecorder.

I call the function like this -


sf::Sound s;
sf::SoundBuffer buf = RemoveFromBeginning(buffer1);
s.setBuffer(buf);
s.setLoop(true);
s.play();

 
« Last Edit: March 28, 2014, 03:27:01 pm by ksneiders »

ksneiders

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Cropping SoundBuffer
« Reply #4 on: March 31, 2014, 09:40:22 am »
So, okay...

I have debugging and debugging, all the values of the arrays seem okay, but when i try to assign the SoundBuffer to a sound (
 sound.setBuffer(buffer)
) It just doest work.
Does really noone have any ideas?

You should show us how you call the function and create/play the sound.

And this would be much better:

void BufferHelper::RemoveFromBeginning(const sf::SoundBuffer src, sf::Sound§Buffer& dst)
{
    const std::size_t cut = 2205; // should rather be calculated from sample rate and channel count

    src.loadFromSamples(dst.getSamples() + cut, dst.getSampleCount() - cut, dst.channelCount(), dst.sampleRate());
}

Can you explain the syntax and meaning of
sf::Sound§Buffer& dst
?
I really have hope that this piece of code could solve my problem, but i just dont get this part :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Cropping SoundBuffer
« Reply #5 on: March 31, 2014, 09:59:01 am »
The § is a typo, otherwise it's just a reference parameter. Also, Laurent confused src and dst ;)

But the idea should be clear: avoid the expensive copies. In your code, you have up to three (!) copies that are not needed (parameter, return type, assignment when calling the function). Compilers with (N)RVO may optimize two of them, but before move semantics you have no guarantee.

What does "it just doest work" mean? Do you make sure that the referenced sound buffer is still valid when the sound is playing?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Cropping SoundBuffer
« Reply #6 on: March 31, 2014, 10:16:44 am »
Sorry, I've fixed the typos.
Laurent Gomila - SFML developer

ksneiders

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Cropping SoundBuffer
« Reply #7 on: March 31, 2014, 10:18:58 am »
What does "it just doest work" mean? Do you make sure that the referenced sound buffer is still valid when the sound is playing?
[/quote]

Well, while debugging, the value of the sound is
+               a       <Information not available, no symbols loaded for sfml-audio-2.dll>     sf::Sound
 

And what do you mean by checking if it i valid? I have no verfication implemented yet.


I thing the thing to do now is trying Laurent's code, maybe that solves my problem.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Cropping SoundBuffer
« Reply #8 on: March 31, 2014, 10:30:25 am »
Well, while debugging, the value of the sound is
+               a       <Information not available, no symbols loaded for sfml-audio-2.dll>     sf::Sound
 
That means debug symbols are not loaded, but that doesn't affect the program semantics.

And what do you mean by checking if it i valid? I have no verfication implemented yet.
Verify it by looking at the code, not at runtime.

Otherwise, read this post.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything