JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
SoundFile.hpp
Go to the documentation of this file.
1// Copyright 2019-2026 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include "SoundPanning.hpp"
8
9#include <future>
10#include <memory>
11#include <span>
12#include <string>
13#include <string_view>
14#include <vector>
15#if defined(__has_include) && __has_include(<optional>)
16#include <optional>
17using std::optional;
18#else
19#include <experimental/optional>
21#endif
22
23namespace jngl {
24
25class Channel;
26class Sound;
27struct SoundParams;
28
34class SoundFile {
35public:
44 explicit SoundFile(const std::string& filename, std::launch policy = std::launch::async);
45 ~SoundFile();
46 SoundFile(const SoundFile&) = delete;
47 SoundFile& operator=(const SoundFile&) = delete;
48 SoundFile(SoundFile&&) noexcept;
49 SoundFile& operator=(SoundFile&&) noexcept;
50
61 static std::shared_ptr<SoundFile> get(std::string_view filename);
62
64 void play();
65
67 void play(Channel&);
68
70 void stop();
71
76 void stop(Channel&);
77
79 bool isPlaying();
80
82 void loop();
83 void loop(Channel&);
84
94 [[nodiscard]] SoundPanning playWithPanning(Channel&);
95
101 [[nodiscard]] SoundPanning loopWithPanning(Channel&);
102
104 void setVolume(float v);
105
109 void load();
110
112 std::chrono::milliseconds length() const;
113
118 float progress() const;
119
121 std::span<float> getSamples();
122
123private:
126 std::weak_ptr<Sound> sound;
127 std::shared_ptr<std::vector<float>> buffer;
128};
129
130} // namespace jngl
Contains jngl::SoundPanning class.
An audio channel, different channels could be for example: "Music", "Speech" and "Sound Effects".
Definition Channel.hpp:30
Sound loaded from an OGG or MP3 file.
Definition SoundFile.hpp:34
SoundPanning playWithPanning()
Like play(), but the sound can be positioned in the stereo field.
void play()
Play the sound once. If called twice the sound would also play twice.
SoundFile(const std::string &filename, std::launch policy=std::launch::async)
Load an OGG or MP3 file called filename.
void load()
Block until the sound file has been fully decompressed and loaded.
std::span< float > getSamples()
Returns the raw audio samples of this SoundFile.
bool isPlaying()
Whether the sound is still playing at least once.
void stop()
Stop the last started sound.
static std::shared_ptr< SoundFile > get(std::string_view filename)
Returns a shared pointer to a SoundFile from JNGL's internal cache, creates it with async loading if ...
std::chrono::milliseconds length() const
Returns the duration in ms.
void setVolume(float v)
Set volume in [0, ∞]. Default is 1.0f.
float progress() const
Returns playing progress in [0, 1], can be used with length() to determine how much time has passed.
SoundPanning loopWithPanning()
Like loop(), but the sound can be positioned in the stereo field.
void loop()
Play the sound in a loop. Can also be stopped using stop()
A sound which can be positioned in the stereo field.
JNGL's main namespace.