SFML community forums

Help => Audio => Topic started by: treez on February 28, 2016, 05:03:12 pm

Title: New audio file 2 time longer than original.
Post by: treez on February 28, 2016, 05:03:12 pm
            

                cout << "Enter the name of the audio\n>";
                                cin >> path;

                                sf::SoundBuffer buffer;
                                buffer.loadFromFile(path.c_str());

                                sf::Int16 * samples = new sf::Int16[buffer.getSampleCount()];

                                int ind = 0, progress = 0, last = 0;
                                float running = 1.0f;
                                string bin_code = "";

                                for (int i = 0; i < buffer.getSampleCount(); i++)
                                {
                                        samples[i] = buffer.getSamples()[i];
                                }
                                sf::SoundBuffer buff;

                                buff.loadFromSamples(samples, buffer.getSampleCount(), buffer.getChannelCount(), buffer.getSampleRate());
                                buff.saveToFile("test.wav");

                                delete[] samples;

 

The file that is created is two times longer then original. Its not bigger (in size) but it has duration 2 times longer than original. Also, when i try to play it with SFML it doest play but when I try to play it with Windows media player it does play (when it exceeds the duration of the song the music is still "playing" but there's no sound).
Title: Re: New audio file 2 time longer than original.
Post by: eXpl0it3r on February 28, 2016, 05:57:06 pm
What version of SFML are you using?
Title: Re: New audio file 2 time longer than original.
Post by: treez on February 28, 2016, 06:07:39 pm
It believe it is latest version for Visual Studio.
Title: Re: New audio file 2 time longer than original.
Post by: Hapax on February 28, 2016, 10:33:11 pm
Is it possible that the original sound has more than 2 channels? I'm not sure that SFML is prepared to deal with more than 2.
Title: Re: New audio file 2 time longer than original.
Post by: treez on February 28, 2016, 11:18:48 pm
I checked if the sound has 2 channels and it does. When I play the original file everything works fine.
Title: Re: New audio file 2 time longer than original.
Post by: Hapax on February 29, 2016, 01:38:35 am
I tested your code (almost identical) on a short, stereo wave file and it worked fine for me (tested it with the latest release version - v2.3. (http://www.sfml-dev.org/download/sfml/2.3.2/)2 - for Visual Studio).

Is your sound file long? By "duration 2 times longer", do you mean that it plays it lower-sounding (an octave), or does it repeat the sound twice or go silent for the extra duration?

Do you get this effect with all samples or just one?
Could you provide a sample that you have the problem with so others can test it (have you tried resaving it elsewhere first)?
Title: Re: New audio file 2 time longer than original.
Post by: treez on February 29, 2016, 11:01:58 am
My sound file is 5:07 mins long.

By 2 times longer I mean that when I play it, it plays normal for 5:07 mins and then from 5:08 to 10:14 is just pure silence.

I'm not really sure what do you mean by : "Do you get this effect with all samples or just one?"

Basically I want to get all the samples, change them a little bit and then make a new file.

My code is identical to the code that I posted. Those calculations that I am making in my program (changing the samples) are not included on purpose, for simplicity sake.
Title: Re: New audio file 2 time longer than original.
Post by: Laurent on February 29, 2016, 03:07:44 pm
What's your version of SFML? Could be related to this bug (https://github.com/SFML/SFML/commit/42a6967128d3dc7916f8862068ce836a32972283) which was fixed in SFML 2.3.2.
Title: Re: New audio file 2 time longer than original.
Post by: treez on February 29, 2016, 07:30:39 pm
I was not sure before but now when you asked I downloaded latest version once again and the file is still not correct.
Title: Re: New audio file 2 time longer than original.
Post by: Hapax on February 29, 2016, 08:09:58 pm
Are you able to provide a sound that you are having trouble with? If not, can you test it with a sound that everyone has access to (e.g. a free sound on the internet)?
Have you tried resaving them using other software? Other formats?

I'm not really sure what do you mean by : "Do you get this effect with all samples or just one?"
Basically I want to get all the samples, change them a little bit and then make a new file.
The word sample can be confusing as it is used/abused for many purposes. I was asking if it happens with all sound sample (i.e. sound clip) that you have; I wasn't referring to individual sample values within the sound data.
Title: Re: New audio file 2 time longer than original.
Post by: treez on March 01, 2016, 06:12:54 pm
I used an online YT to wav converter to get a wav file.

I think that SFML still has a bug. I tried this two lines of code:

buffer.loadFromFile(path.c_str());
buffer.saveToFile("test.wav");
 

And test.wav still is still produced like before. (the duration is 2 times longer).
I guess I leave it like this. If someone knows what could be the issue please let me know.

EDIT: if someone has a link to download a free .wav file plase let me know. But I believe that the file is not the issue here.
Title: Re: New audio file 2 time longer than original.
Post by: Laurent on March 01, 2016, 07:05:38 pm
There is a wav file in the SFML SDK.
Title: Re: New audio file 2 time longer than original.
Post by: jamesL on March 01, 2016, 07:46:08 pm

EDIT: if someone has a link to download a free .wav file plase let me know. But I believe that the file is not the issue here.

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=free%20.wav%20files
Title: Re: New audio file 2 time longer than original.
Post by: Hapax on March 02, 2016, 01:36:12 am
You should test it with sound files with a different number of channels: 1, 2 (maybe more if you want to be thorough).

Try resaving (load into the editor and then save it) your own file using software such as Audacity.

Is there a reason you're passing a c string to the loadFromFile() function? This isn't related to the problem; I was just wondering.
Title: Re: New audio file 2 time longer than original.
Post by: treez on March 03, 2016, 04:41:55 pm
I downloaded Audacity and converted an mp3 file to wav. I tested it and the bug is still there.

I am passing a c string because of the purposes of my program.
Title: Re: New audio file 2 time longer than original.
Post by: eXpl0it3r on March 04, 2016, 12:30:45 am
I tried it myself and have not run into the issue you're describing. Neither with the audio files provided in the SFML examples, nor with other stereo and mono files, nor with different output formats OGG, WAV, FLAC.

This is the code I used:

#include <SFML/Audio.hpp>

int main()
{
    sf::SoundBuffer buf;
    buf.loadFromFile("input.wav");
    std::vector<sf::Int16> samples(buf.getSampleCount());

    for(std::size_t i = 0; i < buf.getSampleCount(); i++)
    {
        samples[i] = buf.getSamples()[i];
    }

    sf::SoundBuffer buff;
    buff.loadFromSamples(samples.data(), buf.getSampleCount(), buf.getChannelCount(), buf.getSampleRate());
    buff.saveToFile("output.wav");
}
 

I have however run into a memory allocation issue with the OGG writer, but this is unrelated to your problems.
Title: Re: New audio file 2 time longer than original.
Post by: Hapax on March 07, 2016, 01:41:57 pm
If it's present when just directly resaving the file too, as you posted, it narrows down the culprit. e.g. it's not a mistake with the manual management of memory or anything.

I am passing a c string because of the purposes of my program.
The reason I mention it is because you pass the string with the method c_str() so that it only sends the c string. However, the function you send it to takes std::strings so needs to construct one from the c string that you send it. If you sent the string directly, this 'extra work' would be unnecessary.