Loading...
Searching...
No Matches
InputSoundFile.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 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 <vector>
37
38#include <cstddef>
39#include <cstdint>
40
41
42namespace sf
43{
44class Time;
45class InputStream;
46
52{
53public:
61 InputSoundFile() = default;
62
74 explicit InputSoundFile(const std::filesystem::path& filename);
75
88 InputSoundFile(const void* data, std::size_t sizeInBytes);
89
101 explicit InputSoundFile(InputStream& stream);
102
114 [[nodiscard]] bool openFromFile(const std::filesystem::path& filename);
115
128 [[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
129
141 [[nodiscard]] bool openFromStream(InputStream& stream);
142
149 [[nodiscard]] std::uint64_t getSampleCount() const;
150
157 [[nodiscard]] unsigned int getChannelCount() const;
158
165 [[nodiscard]] unsigned int getSampleRate() const;
166
178 [[nodiscard]] const std::vector<SoundChannel>& getChannelMap() const;
179
189 [[nodiscard]] Time getDuration() const;
190
197 [[nodiscard]] Time getTimeOffset() const;
198
205 [[nodiscard]] std::uint64_t getSampleOffset() const;
206
224 void seek(std::uint64_t sampleOffset);
225
238 void seek(Time timeOffset);
239
249 [[nodiscard]] std::uint64_t read(std::int16_t* samples, std::uint64_t maxCount);
250
255 void close();
256
257private:
262 struct SFML_AUDIO_API StreamDeleter
263 {
264 StreamDeleter(bool theOwned);
265
266 // To accept ownership transfer from usual std::unique_ptr<T>
267 template <typename T>
268 StreamDeleter(const std::default_delete<T>&);
269
270 void operator()(InputStream* ptr) const;
271
272 bool owned{true};
273 };
274
276 // Member data
278 std::unique_ptr<SoundFileReader> m_reader;
279 std::unique_ptr<InputStream, StreamDeleter> m_stream{nullptr, false};
280 std::uint64_t m_sampleOffset{};
281 std::uint64_t m_sampleCount{};
282 unsigned int m_sampleRate{};
283 std::vector<SoundChannel> m_channelMap;
284};
285
286} // namespace sf
287
288
#define SFML_AUDIO_API
InputSoundFile(const std::filesystem::path &filename)
Construct a sound file from the disk for reading.
bool openFromStream(InputStream &stream)
Open a sound file from a custom stream for reading.
InputSoundFile(const void *data, std::size_t sizeInBytes)
Construct a sound file in memory for reading.
bool openFromFile(const std::filesystem::path &filename)
Open a sound file from the disk for reading.
bool openFromMemory(const void *data, std::size_t sizeInBytes)
Open a sound file in memory for reading.
unsigned int getChannelCount() const
Get the number of channels used by the sound.
std::uint64_t getSampleCount() const
Get the total number of audio samples in the file.
InputSoundFile()=default
Default constructor.
unsigned int getSampleRate() const
Get the sample rate of the sound.
std::uint64_t getSampleOffset() const
Get the read offset of the file in samples.
void seek(Time timeOffset)
Change the current read position to the given time offset.
InputSoundFile(InputStream &stream)
Construct a sound file from a custom stream for reading.
Time getDuration() const
Get the total duration of the sound file.
const std::vector< SoundChannel > & getChannelMap() const
Get the map of position in sample frame to sound channel.
std::uint64_t read(std::int16_t *samples, std::uint64_t maxCount)
Read audio samples from the open file.
Time getTimeOffset() const
Get the read offset of the file in time.
void close()
Close the current file.
void seek(std::uint64_t sampleOffset)
Change the current read position to the given sample offset.
Abstract class for custom file input streams.
Represents a time value.
Definition Time.hpp:42