Search code examples
c++audiosfml

How to play sound with 7.1 audio system sfml?


I am trying to record some sound with sfml and then play it back. I have previously done this successfully with my old headphones that i believe had a 5.1 sound system. But now when i try to do the same thing with my new headphones (7.1 sound). The code throws this error.

AL lib: (EE) SetChannelMap: Failed to match front-center channel (2) in channel map.

I have tried restarting visual studio. Restarting my computer. Resetting the cache in visual studio.

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <thread>
#include <chrono>

int main()
{
    sf::ContextSettings settings;
    settings.antialiasingLevel = 8;
    sf::RenderWindow window;
    window.create(sf::VideoMode(800, 500), "Audio check", sf::Style::Close | sf::Style::Resize);
    if (!sf::SoundBufferRecorder::isAvailable())
    {
        // error: audio capture is not available on this system
        std::cout << "Something went wrong" << std::endl;
    }

    // create the recorder
    sf::SoundBufferRecorder recorder;

    recorder.start(44100);
    //record the audio for 5 sec
    std::this_thread::sleep_for(std::chrono::milliseconds(5000));

    recorder.stop();

    //get the buffer from the recorder and play it back
    const sf::SoundBuffer& buffer = recorder.getBuffer();
    sf::Sound sound(buffer);
    sound.play();

    sf::Event event;
    while (window.isOpen()) {

        while (window.pollEvent(event)) {
            switch (event.type) {
            case sf::Event::Closed:
                window.close();
                break;
            }
        }
        window.clear(sf::Color::Blue);
        window.display();
    }

    return EXIT_SUCCESS;
}

Solution

  • Actually SFML doesn't support more than 2 channels recording. If we check documentation of the method setChannelCount() of the class SoundRecorder it only supports up to 2 (channels).


    Edit

    From openAL library, which is based on (emphasis mine):

    hexagon.ambdec

    Specifies a flat-front hexagonal speaker setup for 7.1 Surround output. The front left and right speakers are placed at +30 and -30 degrees, the side speakers are placed at +90 and -90 degrees, and the back speakers are placed at +150 and -150 degrees. Although this is for 7.1 output, no front-center speaker is defined for the decoder, meaning that speaker will be silent for 3D sound (however it may still be used with AL_SOFT_direct_channels or ALC_EXT_DEDICATED output). A "proper" 7.1 decoder may be provided in the future, but due to the nature of the speaker configuration will have trade-offs.

    It seems the library doesn't count with a actual 7.1 decoder.