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-2024 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
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
53{
54public:
59 template <typename T>
60 struct Span
61 {
62 T offset{};
63 T length{};
64 };
65
66 // Associated `Span` type
68
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
#define SFML_AUDIO_API
Provide read access to sound files.
Abstract class for custom file input streams.
Music()
Default constructor.
void onSeek(Time timeOffset) override
Change the current playing position in the stream source.
std::optional< std::uint64_t > onLoop() override
Change the current playing position in the stream source to the loop offset.
Time getDuration() const
Get the total duration of the music.
bool openFromStream(InputStream &stream)
Open a music from an audio file in a custom stream.
Music(Music &&) noexcept
Move constructor.
bool onGetData(Chunk &data) override
Request a new chunk of audio samples from the stream source.
Music(const std::filesystem::path &filename)
Construct a music from an audio file.
Span< Time > TimeSpan
Definition Music.hpp:67
bool openFromFile(const std::filesystem::path &filename)
Open a music from an audio file.
TimeSpan getLoopPoints() const
Get the positions of the of the sound's looping sequence.
Music(InputStream &stream)
Construct a music from an audio file in a custom stream.
Music(const void *data, std::size_t sizeInBytes)
Construct a music from an audio file in memory.
void setLoopPoints(TimeSpan timePoints)
Sets the beginning and duration of the sound's looping sequence using sf::Time
bool openFromMemory(const void *data, std::size_t sizeInBytes)
Open a music from an audio file in memory.
~Music() override
Destructor.
SoundStream(SoundStream &&) noexcept
Move constructor.
Represents a time value.
Definition Time.hpp:42
Structure defining a time range using the template type.
Definition Music.hpp:61
Structure defining a chunk of audio data to stream.