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-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 <vector>
37
38#include <cstddef>
39#include <cstdint>
40
41
42namespace sf
43{
44class Time;
45class InputStream;
46
52{
53public:
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
267private:
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
#define SFML_AUDIO_API
Provide read access to sound files.
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