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-2024 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 <string>
10#include <vector>
11#if defined(__has_include) && __has_include(<optional>)
12#include <optional>
13using std::optional;
14#else
15#include <experimental/optional>
17#endif
18
19namespace jngl {
20
21class Channel;
22class Sound;
23struct SoundParams;
24
29class SoundFile {
30public:
39 explicit SoundFile(const std::string& filename, std::launch policy = std::launch::async);
40 ~SoundFile();
41 SoundFile(const SoundFile&) = delete;
42 SoundFile& operator=(const SoundFile&) = delete;
43 SoundFile(SoundFile&&) noexcept;
44 SoundFile& operator=(SoundFile&&) noexcept;
45
47 void play();
48
50 void play(Channel&);
51
53 void stop();
54
59 void stop(Channel&);
60
62 bool isPlaying();
63
65 void loop();
66 void loop(Channel&);
67
69 void setVolume(float v);
70
74 void load();
75
77 std::chrono::milliseconds length() const;
78
81 float progress() const;
82
83private:
84 std::shared_ptr<Sound> sound_;
85 std::shared_ptr<std::vector<float>> buffer;
86};
87
88} // namespace jngl
An audio channel, different channels could be for example: "Music", "Speech" and "Sound Effects".
Definition Channel.hpp:29
Sound loaded from an OGG file.
Definition SoundFile.hpp:29
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.
bool isPlaying()
Whether the sound is still playing at least once.
void stop()
Stop the last started sound.
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.