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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - BjornVB

Pages: [1]
1
Audio / Re: Playing multiple music files doesn't work
« on: September 23, 2023, 02:42:51 pm »
Alright, so that works really well! Thanks for the suggestion.
Got rid of the semaphore- and thread-related code, made a list of sf::Music object pointers and it works flawlessly. Can play dozens of music files simultaneously now.

Thanks a bunch!

2
Audio / Playing multiple music files doesn't work
« on: September 22, 2023, 10:39:45 am »
Using SFML, I want to be able to play multiple music files at once.
My code currently looks like this:

#include <SFML/Audio.hpp>
#include <iostream>
#include <thread>
#include <vector>
#include <semaphore.h>

using namespace std;

void playMusic(const string &filename, float volume = 50.0f, float pitch = 1.0f) {
    sf::Music music;
    if (!music.openFromFile(filename)) {
        cerr << "Failed to load " << filename << endl;
        return;
    }
    music.setPitch(pitch);
    music.setVolume(volume);
    music.play();

    while (music.getStatus() == sf::Music::Playing) {
        this_thread::sleep_for(chrono::milliseconds(100));
    }

    music.stop();
}

int main() {
    vector<string> musicFiles = {"../res/test.wav", "../res/test.mp3"};

    vector<thread> threads;

    sem_t sem;
    sem_init(&sem, 0, 1);
    for (size_t i = 0; i < musicFiles.size(); i++) {
        sem_wait(&sem);

        string filename = musicFiles[i];
        float pitch = 1.0f + (i * 0.1f);

        threads.emplace_back([pitch, filename, &sem]() {
            playMusic(filename, 50.0f, pitch);
            sem_post(&sem);
        });
    }

    for (auto &thread: threads) {
        thread.join();
    }

    return 0;
}

For each music file, the application makes a new thread and starts playing the file on that thread.

However, when I run the application, only one music file starts playing at a time.

Is there any way to fix this and relocate these music objects to the heap, or is this truly the limit of SFML?

3
Yep, that works, thanks for the help!

4
So having the DLLs in the library folder itself is not enough? Do I just make a copy of any missing DLLs from the SFML library?

5
Audio / Making sf::Music or sf::Sound object not working (finishes process)
« on: September 20, 2023, 02:07:21 pm »
I'm getting into building an application using the C++ SFML library. (I'm on Windows using CLion and CMake)

Currently, my code is looking like this:

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

int main() {
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML Audio Test");

    sf::Music music{};
    if (!music.openFromFile("res/test.mp3")) {
        std::cout << "Failed to load music" << std::endl;
        return 1;
    }

    if (music.getStatus() != sf::Music::Status::Playing) {
        std::cout << "Music is not playing" << std::endl;
        return 1;
    }

    music.play();

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
    }

    return 0;
}

However, when I try to run this program, the window never opens, sound/music never plays, and I get this in my console.

D:\PATH\NAME.exe
Process finished with exit code -1073741515 (0xC0000135)

When I remove the code related to music (sf::Music music{}; --> music.play();), the window does open and the program runs correctly.

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.26)
project(adaptive_audio)

set(CMAKE_CXX_STANDARD 17)

add_executable(adaptive_audio main.cpp)

set(SFML_STATIC_LIBRARIES TRUE)
set(SFML_DIR "./libraries/SFML/lib/cmake/SFML")

find_package(SFML COMPONENTS system audio network graphics window REQUIRED)

include_directories("./libraries/SFML/include/SFML")
target_link_libraries(adaptive_audio sfml-system sfml-audio sfml-network sfml-graphics sfml-window)

What am I doing wrong?

Pages: [1]