SFML 3.0.2
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-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/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
79 explicit InputSoundFile(const std::filesystem::path& filename);
80
93 InputSoundFile(const void* data, std::size_t sizeInBytes);
94
106 explicit InputSoundFile(InputStream& stream);
107
124 [[nodiscard]] bool openFromFile(const std::filesystem::path& filename);
125
138 [[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
139
151 [[nodiscard]] bool openFromStream(InputStream& stream);
152
159 [[nodiscard]] std::uint64_t getSampleCount() const;
160
167 [[nodiscard]] unsigned int getChannelCount() const;
168
175 [[nodiscard]] unsigned int getSampleRate() const;
176
188 [[nodiscard]] const std::vector<SoundChannel>& getChannelMap() const;
189
199 [[nodiscard]] Time getDuration() const;
200
207 [[nodiscard]] Time getTimeOffset() const;
208
215 [[nodiscard]] std::uint64_t getSampleOffset() const;
216
234 void seek(std::uint64_t sampleOffset);
235
248 void seek(Time timeOffset);
249
259 [[nodiscard]] std::uint64_t read(std::int16_t* samples, std::uint64_t maxCount);
260
265 void close();
266
267 private:
272 struct SFML_AUDIO_API StreamDeleter
273 {
274 StreamDeleter(bool theOwned);
275
276 // To accept ownership transfer from usual std::unique_ptr<T>
277 template <typename T>
278 StreamDeleter(const std::default_delete<T>&);
279
280 void operator()(InputStream* ptr) const;
281
282 bool owned{true};
283 };
284
286 // Member data
288 std::unique_ptr<SoundFileReader> m_reader;
289 std::unique_ptr<InputStream, StreamDeleter> m_stream{nullptr, false};
290 std::uint64_t m_sampleOffset{};
291 std::uint64_t m_sampleCount{};
292 unsigned int m_sampleRate{};
293 std::vector<SoundChannel> m_channelMap;
294};
295
296} // namespace sf
297
298
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