How to play sound with 7.1 audio system sfml? - c++

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;
}

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 sfml 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.

Related

Visual Studio doesn't want me to code or something (Deleting debug)

So I was checking the files for my C++ game, and I see the .exe file already created. I coded a bit to see if it works, and when I debugged it, it said the .exe file was not found. And indeed it was not there. Did I do something wrong? Here is the code
#include <iostream>
#include <SFMl\Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(320, 480), "The Game!");
while (window.isOpen())
{
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
window.clear(Color::White);
window.display();
}
return 0;
}
I tested your snippet and it worked fine.
Try to turn off your antivirus software, or test it on other devices.

Problem with SMFL Syntax in C++ Visual Studio importing png

-I am new to C++ and SFML and want to import a png file.
-It worked a view times, but afterwards I got the
message "Build failed, run last success?" most of the time. Sometimes its still working.
-there is no "real" error, so its hard to figure out, what the problem is
-I read earlier, that switching from Debug to Release-Mode could be a reason, but it didnt helped
its working when I dont use:
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
-> but, ofcourse, the sprite is missing then.
I would be happy to have a solution/reason for this or a topic I missed so far to read/ learn about.
Im happy about advices.
Thanks so far.
Alex
VISUAL STUDIO 2019
x64
SFML-2.5.1
WHOLE CODE:
#include"SFML/Graphics.hpp"
#include<iostream>
#include"main.h"
int main(int argc, char** argv[])
{
sf::RenderWindow window(sf::VideoMode(1200,800), "bimWindow");
sf::RectangleShape rs(sf::Vector2f(1000, 700));
rs.setFillColor(sf::Color::Green);
sf::Event event;
sf::Texture texture;
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(100,100);
//sprite.scale(sf::Vector2f(3, 3));
rs.setPosition(80, 80);
// run the program as long as the window is open:
while (window.isOpen())
{
//let window open i guess.
while (window.pollEvent(event)); //stay true as long aas it didnt happen or so
{
// "close requested" event:close the window
if(event.type == sf::Event::Closed)
window.close();
}
//RENDER:
window.clear();
window.draw(rs);
window.draw(sprite);
window.display();
}
return 0;
}
Try making sure that the "assets/player.png" file is inside your solution directory (the folder with the .sln file.) Your code seems to run fine on my end, and that is the only error I can think of. If you can, it would help to have the error message(s) that you are receiving. If the png file is in the right place and you still get errors, I would recommend reinstalling SFML and following a tutorial online to ensure that you get everything set up properly.

How can I set a sleep between loop using sdl-mixer?

I'm trying to play music using SDL2_Mixer library in Ubuntu 18.04.
I succeed to play .wav file loop forever as below code.
#define ERROR_BEEP "../audio/beep.wav"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
void main()
{
SDL_AudioSpec wavSpec;
SDL_Init(SDL_INIT_AUDIO)
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) >= 0)
SDL_LoadWAV(ERROR_BEEP, &wavSpec, &wavBuffer, &wavLength);
Mix_Music *now_playing_= Mix_LoadMUS(ERROR_BEEP);
Mix_PlayMusic(now_playing_, -1);
SDL_Delay(10000);
Mix_HaltMusic();
Mix_FreeMusic(now_playing_);
now_playing_=NULL;
}
My beep.wav plays well repeatedly. However, the beep file is played immediately and the alarm sound is too loud. (ex: beep!beep!beep!)
Is there any way to put sleep between beep repeating loop? (ex: beep!...beep!...beep!...)

Weird error when running my SFML game?

So, I'm making a game using SFML in C++, but when I tried adding a image a really weird OpenGL(i think) error popped up. Doesn't make any sense at all.
First my console was spammed with random text and symbols, then the application crashed and visual studio told me this :
Exception thrown at 0x618EDBF4 (vcruntime140.dll) in SFML_Game.exe: 0xC0000005: Access violation reading location 0x00C54000.
It sounds like it has something to do with not being able to read my picture file I added, though i don't know whats wring with it.
The location of the image is in the same folder my "SFML_Game.vcxproj" is.
I also have no chance of seeing if "Could not load player image" was printed in console since the spam is too quick.
Edit I can see that the picture failed to load now, here's a picture... :
Console Picture
Here's my current code :
#include<iostream>
#include<SFML/Graphics.hpp>
#include <string>
int main(int argc, char* argv[])
{
// Creates a window
sf::RenderWindow Window(sf::VideoMode(800, 600), "SFML Game Engine");
sf::Texture pTexture;
sf::Sprite playerImage;
if (!pTexture.loadFromFile("Player.png"))
{
std::cout << "Could not load player image" << std::endl;
}
playerImage.setTexture(pTexture);
while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent(Event) && Window.hasFocus())
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
}
}
Window.draw(playerImage);
Window.display();
}
}
So, it seems that my code was fine and it was a linker error.
I didn't know the difference between static-dynamic, static and dynamic libraries, so I just put them all in there, after removing some of them it started working.
At least in debugging mode.
Pretty much, if someone else has this problem, look up how to set SFML up on their website instead of a video tutorial. ^^

Cannot load an image file as texture using C++ and SFML 2.1

I am trying to output an image onto the screen using SFML 2.1, C++, and MS Visual Studio Professional 2013. I am getting an unexpected error when trying to load a file into a texture. It outputs a whole bunch of random characters. I'm sure if its how I configured the SFML library with Visual Studio or a problem with the code. Can anyone solve this problem? Thanks.
Here is a screenshot of what it looks like when I run the program (http://i.stack.imgur.com/uMdLT.png):
This is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}
For all configuration properties, they look like this:
Linker -> General (http://i.stack.imgur.com/NZg7P.png):
Linker -> Input (http://i.stack.imgur.com/1tPaB.png):
**Please note that if I did not configured the SFML library as I did, then I would be getting an error from my system saying msvcr110d.dll is missing.
Ok I managed to fix this, here is what you need to do:
Set your SUBSYSTEM to WINDOWS:
Add "sfml-main.lib" to your libraries:
Change your Runtime library to /MD. This is because you're not using the debug versions of the SFML 2.1 libraries (and probably can't in VS2013).
Make sure your "fighter jet.png" image is in the right place. By default Visual Studio uses the Project Directory as the working directory. So put the png in the same folder as your vcxproj file.
Are you sure that the picture is in the execution directory of your program ?
You should avoid spaces in file's name (fighter_jet.png).
If you're not sure about execution directory, try with absolute path (to be sure it's a path problem, and not a picture's problem).
I hope it helps.
I've tried this code on my system (Xcode - OSX), with one of my picture, and it works.
Have you tried with another picture ?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "My First SFML Game!"); // initializing
sf::Texture jetTexture;
sf::Sprite jetImage;
// Getting Error here!
if (!jetTexture.loadFromFile("fighter jet.png"))
throw std::runtime_error("Could not load fighter jet.png");
jetImage.setTexture(jetTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(jetImage);
window.display();
}
return 0;
}