SFML - Simple and Fast Multimedia Library

SFML 3.1.0
Loading...
Searching...
No Matches
SoundBuffer.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/SoundChannel.hpp>
33
34#include <SFML/System/Time.hpp>
35
36#include <filesystem>
37#include <unordered_set>
38#include <vector>
39
40#include <cstddef>
41#include <cstdint>
42
43
44namespace sf
45{
46class Sound;
47class InputSoundFile;
48class InputStream;
49
54 class SFML_AUDIO_API SoundBuffer
55{
56public:
64 SoundBuffer() = default;
65
72 SoundBuffer(const SoundBuffer& copy);
73
87 explicit SoundBuffer(const std::filesystem::path& filename);
88
103 SoundBuffer(const void* data, std::size_t sizeInBytes);
104
118 explicit SoundBuffer(InputStream& stream);
119
136 SoundBuffer(const std::int16_t* samples,
137 std::uint64_t sampleCount,
138 unsigned int channelCount,
139 unsigned int sampleRate,
140 const std::vector<SoundChannel>& channelMap);
141
146 ~SoundBuffer();
147
161 [[nodiscard]] bool loadFromFile(const std::filesystem::path& filename);
162
177 [[nodiscard]] bool loadFromMemory(const void* data, std::size_t sizeInBytes);
178
192 [[nodiscard]] bool loadFromStream(InputStream& stream);
193
210 [[nodiscard]] bool loadFromSamples(const std::int16_t* samples,
211 std::uint64_t sampleCount,
212 unsigned int channelCount,
213 unsigned int sampleRate,
214 const std::vector<SoundChannel>& channelMap);
215
227 [[nodiscard]] bool saveToFile(const std::filesystem::path& filename) const;
228
241 [[nodiscard]] const std::int16_t* getSamples() const;
242
254 [[nodiscard]] std::uint64_t getSampleCount() const;
255
268 [[nodiscard]] unsigned int getSampleRate() const;
269
281 [[nodiscard]] unsigned int getChannelCount() const;
282
294 [[nodiscard]] const std::vector<SoundChannel>& getChannelMap() const;
295
304 [[nodiscard]] Time getDuration() const;
305
314 SoundBuffer& operator=(const SoundBuffer& right);
315
316private:
317 friend class Sound;
318
327 [[nodiscard]] bool initialize(InputSoundFile& file);
328
339 [[nodiscard]] bool update(unsigned int channelCount, unsigned int sampleRate, const std::vector<SoundChannel>& channelMap);
340
347 void attachSound(Sound* sound) const;
348
355 void detachSound(Sound* sound) const;
356
358 // Types
360 using SoundList = std::unordered_set<Sound*>;
361
363 // Member data
365 std::vector<std::int16_t> m_samples;
366 unsigned int m_sampleRate{44100};
367 std::vector<SoundChannel> m_channelMap{SoundChannel::Mono};
368 Time m_duration;
369 mutable SoundList m_sounds;
370};
371
372} // namespace sf
373
374
Export.hpp
SFML_AUDIO_API
#define SFML_AUDIO_API
Definition Audio/Export.hpp:42
SoundChannel.hpp
Time.hpp
sf::InputSoundFile
Provide read access to sound files.
Definition InputSoundFile.hpp:52
sf::InputStream
Abstract class for custom file input streams.
Definition InputStream.hpp:46
sf::SoundBuffer::getChannelCount
unsigned int getChannelCount() const
Get the number of channels used by the sound.
sf::SoundBuffer::SoundBuffer
SoundBuffer(const std::filesystem::path &filename)
Construct the sound buffer from a file.
sf::SoundBuffer::getDuration
Time getDuration() const
Get the total duration of the sound.
sf::SoundBuffer::getSampleRate
unsigned int getSampleRate() const
Get the sample rate of the sound.
sf::SoundBuffer::getChannelMap
const std::vector< SoundChannel > & getChannelMap() const
Get the map of position in sample frame to sound channel.
sf::SoundBuffer::SoundBuffer
SoundBuffer(const std::int16_t *samples, std::uint64_t sampleCount, unsigned int channelCount, unsigned int sampleRate, const std::vector< SoundChannel > &channelMap)
Construct the sound buffer from an array of audio samples.
sf::SoundBuffer::Sound
friend class Sound
Definition SoundBuffer.hpp:317
sf::SoundBuffer::loadFromFile
bool loadFromFile(const std::filesystem::path &filename)
Load the sound buffer from a file.
sf::SoundBuffer::getSamples
const std::int16_t * getSamples() const
Get the array of audio samples stored in the buffer.
sf::SoundBuffer::getSampleCount
std::uint64_t getSampleCount() const
Get the number of samples stored in the buffer.
sf::SoundBuffer::SoundBuffer
SoundBuffer()=default
Default constructor.
sf::SoundBuffer::loadFromSamples
bool loadFromSamples(const std::int16_t *samples, std::uint64_t sampleCount, unsigned int channelCount, unsigned int sampleRate, const std::vector< SoundChannel > &channelMap)
Load the sound buffer from an array of audio samples.
sf::SoundBuffer::SoundBuffer
SoundBuffer(const SoundBuffer &copy)
Copy constructor.
sf::SoundBuffer::saveToFile
bool saveToFile(const std::filesystem::path &filename) const
Save the sound buffer to an audio file.
sf::SoundBuffer::operator=
SoundBuffer & operator=(const SoundBuffer &right)
Overload of assignment operator.
sf::SoundBuffer::loadFromStream
bool loadFromStream(InputStream &stream)
Load the sound buffer from a custom stream.
sf::SoundBuffer::~SoundBuffer
~SoundBuffer()
Destructor.
sf::SoundBuffer::SoundBuffer
SoundBuffer(InputStream &stream)
Construct the sound buffer from a custom stream.
sf::SoundBuffer::SoundBuffer
SoundBuffer(const void *data, std::size_t sizeInBytes)
Construct the sound buffer from a file in memory.
sf::SoundBuffer::loadFromMemory
bool loadFromMemory(const void *data, std::size_t sizeInBytes)
Load the sound buffer from a file in memory.
sf::Sound
Regular sound that can be played in the audio environment.
Definition Sound.hpp:48
sf::Time
Represents a time value.
Definition Time.hpp:42
sf::SoundChannel::Mono
@ Mono
Definition SoundChannel.hpp:44
sf
Definition AudioResource.hpp:36