SFML - Simple and Fast Multimedia Library

SFML 3.0.2
Loading...
Searching...
No Matches
Music.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
30#include <SFML/Audio/Export.hpp>
31
32#include <SFML/Audio/SoundStream.hpp>
33
34#include <filesystem>
35#include <memory>
36#include <optional>
37
38#include <cstddef>
39#include <cstdint>
40
41
42namespace sf
43{
44class Time;
45class InputStream;
46class InputSoundFile;
47
52 class SFML_AUDIO_API Music : public SoundStream
53{
54public:
59 template <typename T>
60 struct Span
61 {
62 T offset{};
63 T length{};
64 };
65
66 // Associated `Span` type
67 using TimeSpan = Span<Time>;
68
75 Music();
76
96 explicit Music(const std::filesystem::path& filename);
97
119 Music(const void* data, std::size_t sizeInBytes);
120
140 explicit Music(InputStream& stream);
141
146 ~Music() override;
147
152 Music(Music&&) noexcept;
153
158 Music& operator=(Music&&) noexcept;
159
179 [[nodiscard]] bool openFromFile(const std::filesystem::path& filename);
180
202 [[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
203
223 [[nodiscard]] bool openFromStream(InputStream& stream);
224
231 [[nodiscard]] Time getDuration() const;
232
248 [[nodiscard]] TimeSpan getLoopPoints() const;
249
270 void setLoopPoints(TimeSpan timePoints);
271
272protected:
284 [[nodiscard]] bool onGetData(Chunk& data) override;
285
292 void onSeek(Time timeOffset) override;
293
304 std::optional<std::uint64_t> onLoop() override;
305
306private:
315 [[nodiscard]] std::uint64_t timeToSamples(Time position) const;
316
325 [[nodiscard]] Time samplesToTime(std::uint64_t samples) const;
326
328 // Member data
330 struct Impl;
331 std::unique_ptr<Impl> m_impl;
332};
333
334} // namespace sf
335
336
Export.hpp
SFML_AUDIO_API
#define SFML_AUDIO_API
Definition Audio/Export.hpp:42
SoundStream.hpp
sf::InputSoundFile
Provide read access to sound files.
Definition InputSoundFile.hpp:52
sf::InputStream
Abstract class for custom file input streams.
Definition InputStream.hpp:46
sf::Music::Music
Music()
Default constructor.
sf::Music::onSeek
void onSeek(Time timeOffset) override
Change the current playing position in the stream source.
sf::Music::onLoop
std::optional< std::uint64_t > onLoop() override
Change the current playing position in the stream source to the loop offset.
sf::Music::getDuration
Time getDuration() const
Get the total duration of the music.
sf::Music::openFromStream
bool openFromStream(InputStream &stream)
Open a music from an audio file in a custom stream.
sf::Music::Music
Music(Music &&) noexcept
Move constructor.
sf::Music::onGetData
bool onGetData(Chunk &data) override
Request a new chunk of audio samples from the stream source.
sf::Music::Music
Music(const std::filesystem::path &filename)
Construct a music from an audio file.
sf::Music::TimeSpan
Span< Time > TimeSpan
Definition Music.hpp:67
sf::Music::openFromFile
bool openFromFile(const std::filesystem::path &filename)
Open a music from an audio file.
sf::Music::getLoopPoints
TimeSpan getLoopPoints() const
Get the positions of the of the sound's looping sequence.
sf::Music::Music
Music(InputStream &stream)
Construct a music from an audio file in a custom stream.
sf::Music::Music
Music(const void *data, std::size_t sizeInBytes)
Construct a music from an audio file in memory.
sf::Music::setLoopPoints
void setLoopPoints(TimeSpan timePoints)
Sets the beginning and duration of the sound's looping sequence using sf::Time.
sf::Music::openFromMemory
bool openFromMemory(const void *data, std::size_t sizeInBytes)
Open a music from an audio file in memory.
sf::Music::~Music
~Music() override
Destructor.
sf::SoundStream::SoundStream
SoundStream(SoundStream &&) noexcept
Move constructor.
sf::Time
Represents a time value.
Definition Time.hpp:42
sf
Definition AudioResource.hpp:36
sf::Music::Span
Structure defining a time range using the template type.
Definition Music.hpp:61
sf::Music::Span< Time >::offset
Time offset
Definition Music.hpp:62
sf::Music::Span< Time >::length
Time length
Definition Music.hpp:63
sf::SoundStream::Chunk
Structure defining a chunk of audio data to stream.
Definition SoundStream.hpp:59