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

Author Topic: how to record rendered audio ?  (Read 2724 times)

0 Members and 1 Guest are viewing this topic.

cppa

  • Newbie
  • *
  • Posts: 2
    • View Profile
how to record rendered audio ?
« on: December 08, 2014, 04:12:31 pm »
Hello,

I would like to "record" (to WAV file for example) everything (multiple sfSound) that is played during the player's game.
Can it be done with SFML ?

Thank you for any help

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: how to record rendered audio ?
« Reply #1 on: December 08, 2014, 06:47:01 pm »
You can't directly record the single sf::Sounds, however you can capture the whole audio output (stereomix). So if you play music or get a Skype message you'd also hear it on the recording.
Besides that it depends on the devices provided by the OS/driver and the name of the device might be in the language of your OS.

On a German Windows 8.1 install, I could use this code:

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <vector>
#include <string>

int main(int, char const**)
{
    auto sr = sf::SoundBufferRecorder::getAvailableDevices();

    for(auto &s : sr)
        std::cout << s << std::endl;

    sf::SoundBufferRecorder rec;
    rec.setDevice("Stereomix (IDT High Definition Audio CODEC)");

    sf::Sound sound(rec.getBuffer());

    sf::RenderWindow window(sf::VideoMode(480, 320), "SFML window");
    window.setKeyRepeatEnabled(false);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            else if (event.type == sf::Event::KeyPressed) {
                                sound.stop();
                                rec.start();
            }
            else if (event.type == sf::Event::KeyReleased) {
                                rec.stop();
                                sound.setBuffer(rec.getBuffer());
                                sound.play();
            }
        }

        window.clear();
        window.display();
    }
}
 

The first part will list all the available recording devices. Then I select the "Stereomix" which is essentially what comes out of your speakers. Next I set the buffer of the sound to the recording buffer. To stop key press events from being repeated I disable the key repeat feature.
Now when you hold the key, it will start recording the sound and as soon as you release the key, it will stop recording and play it back to you.

If you have the sound buffers there would always be the possibility of manually manipulating the sound data, but if you want to do complex audio processing, you're better off looking at dedicated libraries.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: how to record rendered audio ?
« Reply #2 on: December 08, 2014, 07:10:36 pm »
Note that setDevice was added after 2.1.

cppa

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: how to record rendered audio ?
« Reply #3 on: December 09, 2014, 09:15:05 am »
Thank you for this fast answer, I will try this way.