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 <future>
8#include <memory>
9#include <span>
10#include <string>
11#include <string_view>
12#include <vector>
13#if defined(__has_include) && __has_include(<optional>)
14#include <optional>
15using std::optional;
16#else
17#include <experimental/optional>
19#endif
20
21namespace jngl {
22
23class Channel;
24class Sound;
25struct SoundParams;
26
32class SoundFile {
33public:
42 explicit SoundFile(const std::string& filename, std::launch policy = std::launch::async);
43 ~SoundFile();
44 SoundFile(const SoundFile&) = delete;
45 SoundFile& operator=(const SoundFile&) = delete;
46 SoundFile(SoundFile&&) noexcept;
47 SoundFile& operator=(SoundFile&&) noexcept;
48
59 static std::shared_ptr<SoundFile> get(std::string_view filename);
60
62 void play();
63
65 void play(Channel&);
66
68 void stop();
69
74 void stop(Channel&);
75
77 bool isPlaying();
78
80 void loop();
81 void loop(Channel&);
82
84 void setVolume(float v);
85
89 void load();
90
92 std::chrono::milliseconds length() const;
93
98 float progress() const;
99
101 std::span<float> getSamples();
102
103private:
106 std::weak_ptr<Sound> sound;
107 std::shared_ptr<std::vector<float>> buffer;
108};
109
110} // namespace jngl
An audio channel, different channels could be for example: "Music", "Speech" and "Sound Effects".
Definition Channel.hpp:30
Sound loaded from an OGG file.
Definition SoundFile.hpp:32
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 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.
void loop()
Play the sound in a loop. Can also be stopped using stop()
JNGL's main namespace.