Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
sound_data.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2022 DS
4// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5// Copyright (C) 2011 Sebastian 'Bahamada' Rühl
6// Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
7// Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
8
9#pragma once
10
11#include "ogg_file.h"
12#include <memory>
13#include <tuple>
14#include <vector>
15
16namespace sound {
17
22{
24
25 explicit ISoundDataOpen(const OggFileDecodeInfo &decode_info) :
26 m_decode_info(decode_info) {}
27
28 virtual ~ISoundDataOpen() = default;
29
34 virtual bool isStreaming() const noexcept = 0;
35
52 virtual std::tuple<ALuint, ALuint, ALuint> getOrLoadBufferAt(ALuint offset) = 0;
53
54 static std::shared_ptr<ISoundDataOpen> fromOggFile(std::unique_ptr<RAIIOggFile> oggfile,
55 const std::string &filename_for_logging);
56};
57
62{
63 virtual ~ISoundDataUnopen() = default;
64
65 // Note: The ISoundDataUnopen is moved (see &&). It is not meant to be kept
66 // after opening.
67 virtual std::shared_ptr<ISoundDataOpen> open(const std::string &sound_name) && = 0;
68};
69
74{
75 std::string m_buffer;
76
77 explicit SoundDataUnopenBuffer(std::string &&buffer) : m_buffer(std::move(buffer)) {}
78
79 std::shared_ptr<ISoundDataOpen> open(const std::string &sound_name) && override;
80};
81
86{
87 std::string m_path;
88
89 explicit SoundDataUnopenFile(const std::string &path) : m_path(path) {}
90
91 std::shared_ptr<ISoundDataOpen> open(const std::string &sound_name) && override;
92};
93
99{
101
102 SoundDataOpenBuffer(std::unique_ptr<RAIIOggFile> oggfile,
103 const OggFileDecodeInfo &decode_info);
104
105 bool isStreaming() const noexcept override { return false; }
106
107 std::tuple<ALuint, ALuint, ALuint> getOrLoadBufferAt(ALuint offset) override
108 {
109 if (offset >= m_decode_info.length_samples)
110 return {0, m_decode_info.length_samples, 0};
111 return {m_buffer.get(), m_decode_info.length_samples, offset};
112 }
113};
114
122{
126 struct SoundBufferUntil final
127 {
128 ALuint m_end;
130 };
131
137 struct ContiguousBuffers final
138 {
139 ALuint m_start;
140 std::vector<SoundBufferUntil> m_buffers;
141 };
142
143 std::unique_ptr<RAIIOggFile> m_oggfile;
144 // A sorted vector of non-overlapping, non-contiguous `ContiguousBuffers`s.
145 std::vector<ContiguousBuffers> m_bufferss;
146
147 SoundDataOpenStream(std::unique_ptr<RAIIOggFile> oggfile,
148 const OggFileDecodeInfo &decode_info);
149
150 bool isStreaming() const noexcept override { return true; }
151
152 std::tuple<ALuint, ALuint, ALuint> getOrLoadBufferAt(ALuint offset) override;
153
154private:
155 // offset must be before after_it's m_start and after (after_it-1)'s last m_end
156 // new buffer will be inserted into m_bufferss before after_it
157 // returns same as getOrLoadBufferAt
158 std::tuple<ALuint, ALuint, ALuint> loadBufferAt(ALuint offset,
159 std::vector<ContiguousBuffers>::iterator after_it);
160};
161
162} // namespace sound
Definition al_extensions.cpp:11
Stores sound pcm data buffers.
Definition sound_data.h:22
virtual ~ISoundDataOpen()=default
virtual bool isStreaming() const noexcept=0
Iff the data is streaming, there is more than one buffer.
virtual std::tuple< ALuint, ALuint, ALuint > getOrLoadBufferAt(ALuint offset)=0
Load a buffer containing data starting at the given offset.
static std::shared_ptr< ISoundDataOpen > fromOggFile(std::unique_ptr< RAIIOggFile > oggfile, const std::string &filename_for_logging)
Definition sound_data.cpp:20
OggFileDecodeInfo m_decode_info
Definition sound_data.h:23
ISoundDataOpen(const OggFileDecodeInfo &decode_info)
Definition sound_data.h:25
Will be opened lazily when first used.
Definition sound_data.h:62
virtual ~ISoundDataUnopen()=default
virtual std::shared_ptr< ISoundDataOpen > open(const std::string &sound_name) &&=0
Metadata of an Ogg-Vorbis file, used for decoding.
Definition ogg_file.h:37
ALuint length_samples
Definition ogg_file.h:43
RAII wrapper for openal sound buffers.
Definition al_helpers.h:78
ALuint get() noexcept
Definition al_helpers.h:89
RAII wrapper for OggVorbis_File.
Definition ogg_file.h:50
Non-streaming opened sound data.
Definition sound_data.h:99
std::tuple< ALuint, ALuint, ALuint > getOrLoadBufferAt(ALuint offset) override
Load a buffer containing data starting at the given offset.
Definition sound_data.h:107
bool isStreaming() const noexcept override
Iff the data is streaming, there is more than one buffer.
Definition sound_data.h:105
RAIIALSoundBuffer m_buffer
Definition sound_data.h:100
A sorted non-empty vector of contiguous buffers.
Definition sound_data.h:138
ALuint m_start
Definition sound_data.h:139
std::vector< SoundBufferUntil > m_buffers
Definition sound_data.h:140
An OpenAL buffer that goes until m_end (exclusive).
Definition sound_data.h:127
ALuint m_end
Definition sound_data.h:128
RAIIALSoundBuffer m_buffer
Definition sound_data.h:129
Streaming opened sound data.
Definition sound_data.h:122
std::vector< ContiguousBuffers > m_bufferss
Definition sound_data.h:145
std::unique_ptr< RAIIOggFile > m_oggfile
Definition sound_data.h:143
bool isStreaming() const noexcept override
Iff the data is streaming, there is more than one buffer.
Definition sound_data.h:150
Sound file is in a memory buffer.
Definition sound_data.h:74
std::string m_buffer
Definition sound_data.h:75
SoundDataUnopenBuffer(std::string &&buffer)
Definition sound_data.h:77
Sound file is in file system.
Definition sound_data.h:86
SoundDataUnopenFile(const std::string &path)
Definition sound_data.h:89
std::string m_path
Definition sound_data.h:87