SFML community forums

Help => Audio => Topic started by: Temophor on May 07, 2015, 11:07:20 pm

Title: how to cut the end of a sound?
Post by: Temophor on May 07, 2015, 11:07:20 pm
hey guys!

I'm recording a sound, unfortunately it puts like 0.5 seconds of non-recorded silence at the end of the recording.
I want the recording to loop seamlessly.
I found this code which cuts the sound from the beginning:

sf::SoundBuffer withoutSilence;
const std::size_t cut = buffer.getSampleRate() * buffer.getChannelCount() * 0.5f;;
withoutSilence.loadFromSamples(buffer.getSamples() + cut, buffer.getSampleCount() - cut, buffer.getChannelCount(), buffer.getSampleRate());

I then feed the new buffer appropriately to create, play and loop the sound.
I don't quite understand the loadFromSamples function. It successfully cuts the sound, but from the beginning instead from the end.

Any ideas?
Thank you :)
Temophor
Title: Re: how to cut the end of a sound?
Post by: Temophor on May 07, 2015, 11:12:38 pm
Nevermind, I just solved it myself:

buffer.getSamples() + cut determines the starting position of the new buffer. So of course I just want buffer.getSamples() here to start from the beginning (and not cut the beginning.
buffer.getSampleCount() - cut is the number of remaining samples. If I didn't cut in the beginning by not adding cut to the starting point, it will cut the audio at the end, just like intented.

cheers,
Temophor
Title: Re: how to cut the end of a sound?
Post by: Nexus on May 07, 2015, 11:13:34 pm
I don't quite understand the loadFromSamples function.
Then look at its documentation (http://www.sfml-dev.org/documentation/2.2/classsf_1_1SoundBuffer.php#a63da986e144b578135edd48e51c565e8). It clearly describes what the first and second argument mean.

It successfully cuts the sound, but from the beginning instead from the end.
That's not surprising. You're adding an offset to the start of the samples array.

By the way, it would be easier to use a software like Audacity to perform such modifications ;)