SFML 3.1.0
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
32#include <SFML/Audio/SoundFileReader.hpp>
33
34#include <filesystem>
35#include <memory>
36#include <vector>
37
38#include <cstddef>
39#include <cstdint>
40
41
42 namespace sf
43{
44 class Time;
45 class InputStream;
46
51 class SFML_AUDIO_API InputSoundFile
52{
53 public:
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
257 private:
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
Export.hpp
SFML_AUDIO_API
#define SFML_AUDIO_API
Definition Audio/Export.hpp:42
SoundFileReader.hpp
sf::InputSoundFile::InputSoundFile
InputSoundFile(const std::filesystem::path &filename)
Construct a sound file from the disk for reading.
sf::InputSoundFile::openFromStream
bool openFromStream(InputStream &stream)
Open a sound file from a custom stream for reading.
sf::InputSoundFile::InputSoundFile
InputSoundFile(const void *data, std::size_t sizeInBytes)
Construct a sound file in memory for reading.
sf::InputSoundFile::openFromFile
bool openFromFile(const std::filesystem::path &filename)
Open a sound file from the disk for reading.
sf::InputSoundFile::openFromMemory
bool openFromMemory(const void *data, std::size_t sizeInBytes)
Open a sound file in memory for reading.
sf::InputSoundFile::getChannelCount
unsigned int getChannelCount() const
Get the number of channels used by the sound.
sf::InputSoundFile::getSampleCount
std::uint64_t getSampleCount() const
Get the total number of audio samples in the file.
sf::InputSoundFile::InputSoundFile
InputSoundFile()=default
Default constructor.
sf::InputSoundFile::getSampleRate
unsigned int getSampleRate() const
Get the sample rate of the sound.
sf::InputSoundFile::getSampleOffset
std::uint64_t getSampleOffset() const
Get the read offset of the file in samples.
sf::InputSoundFile::seek
void seek(Time timeOffset)
Change the current read position to the given time offset.
sf::InputSoundFile::InputSoundFile
InputSoundFile(InputStream &stream)
Construct a sound file from a custom stream for reading.
sf::InputSoundFile::getDuration
Time getDuration() const
Get the total duration of the sound file.
sf::InputSoundFile::getChannelMap
const std::vector< SoundChannel > & getChannelMap() const
Get the map of position in sample frame to sound channel.
sf::InputSoundFile::read
std::uint64_t read(std::int16_t *samples, std::uint64_t maxCount)
Read audio samples from the open file.
sf::InputSoundFile::getTimeOffset
Time getTimeOffset() const
Get the read offset of the file in time.
sf::InputSoundFile::close
void close()
Close the current file.
sf::InputSoundFile::seek
void seek(std::uint64_t sampleOffset)
Change the current read position to the given sample offset.
sf::InputStream
Abstract class for custom file input streams.
Definition InputStream.hpp:46
sf::Time
Represents a time value.
Definition Time.hpp:42
sf
Definition AudioResource.hpp:36