SFML: sound and music

This article is part of a tutorial dedicated to SFML. The reader may consult the other parts of the tutorial by following this link : SFML tutorial.

This example shows how to play music and sound.

/*!
 * \file    main.cpp
 * \brief   Music and sound examples (http://www.lucidarme.me/?p=6127)
 * \author  Philippe Lucidarme
 * \version 1.0
 * \date    12/20/2016
 */
 
#include <SFML/Audio.hpp>
#include <iostream>
 
 
// Wait for ms milliseconds
void delay_ms(int ms)
{
    sf::Clock Timer;
    while (Timer.getElapsedTime().asMilliseconds()<ms);
}
 
 
 
int main()
{
    // _____________________________
    // ::: load background music :::
 
    sf::Music music;
    if (!music.openFromFile("../music.ogg"))
    {
        std::cerr << "Error while loading background music file" << std::endl;
        return -1;
    }
 
 
 
    // __________________
    // ::: Load sound :::
 
    sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("../birds.wav"))
    {
        std::cerr << "Error while loading sound file" << std::endl;
        return -1;
    }
 
    // ____________________
    // :::  Play sounds :::
 
    // Play music
    std::cout << "Start background music" << std::endl;
    music.play();
    delay_ms(1500);
 
    // Play wav sound over the music
    std::cout << "Play wav sound" << std::endl;
    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.play();
    delay_ms(1500);
 
    // Increase pitch
    std::cout << "Increase pitch" << std::endl;
    music.setPitch(1.4);
    delay_ms(1500);
 
    // Reset pitch
    std::cout << "Reset pitch" << std::endl;
    music.setPitch(1.);
    delay_ms(1500);
 
 
    // Modify volume
    std::cout << "Fade out" << std::endl;
    for (int vol=100;vol>5;vol--)
    {
        music.setVolume(vol);
        delay_ms(50);
    }
 
    // Stop music
    std::cout << "Stop music" << std::endl;
    music.stop();
    return 0;
}

Download from Git

Files, sharders and source codes can be downloaded for GitLab : https://gitlab.com/philippe.lucidarme/sfml_tutorial/tree/master/part_009

<- Back to SFML tutorial main menu

Leave a Reply

Your email address will not be published. Required fields are marked *