-
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
-
Can't you just edit the sound file in an audio application?
If not, what about applying a offset (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Sound.php#ab905677846558042022dd6ab15cddff0) when playing the sound?
-
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());
}
-
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();
-
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 :)
-
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?
-
Sorry, I've fixed the typos.
-
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.
-
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 (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368).