forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_generic.h
120 lines (104 loc) · 3.75 KB
/
audio_generic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EP_AUDIO_GENERIC_H
#define EP_AUDIO_GENERIC_H
#include "audio.h"
#include "audio_secache.h"
#include "audio_decoder_base.h"
#include "audio_generic_midiout.h"
#include <memory>
/**
* A software implementation for handling EasyRPG Audio utilizing the
* AudioDecoder for BGM and AudioSeCache for fast SE playback.
*
* Inheriting implementations have to:
* 1. Init the audio system in the constructor (and deinit in destructor)
* 2. Start a thread (or a callback) which invokes the Decode function to
* fill the output buffer and controls access to the audio api of the
* target platform.
* 3. Initialize the "output_format" (must match the format of the hardware)
* 4. Implement LockMutex and UnlockMutex. Locking and Unlocking when
* calling Decode must be done manually.
* 5. Implement update function (optional)
*/
class GenericAudio : public AudioInterface {
public:
GenericAudio(const Game_ConfigAudio& cfg);
virtual ~GenericAudio() = default;
void BGM_Play(Filesystem_Stream::InputStream stream, int volume, int pitch, int fadein) override;
void BGM_Pause() override;
void BGM_Resume() override;
void BGM_Stop() override;
bool BGM_PlayedOnce() const override;
bool BGM_IsPlaying() const override;
int BGM_GetTicks() const override;
void BGM_Fade(int fade) override;
void BGM_Volume(int volume) override;
void BGM_Pitch(int pitch) override;
std::string BGM_GetType() const override;
void SE_Play(std::unique_ptr<AudioSeCache> se, int volume, int pitch) override;
void SE_Stop() override;
virtual void Update() override;
void vGetConfig(Game_ConfigAudio&) const override {}
void SetFormat(int frequency, AudioDecoder::Format format, int channels);
virtual void LockMutex() const = 0;
virtual void UnlockMutex() const = 0;
void Decode(uint8_t* output_buffer, int buffer_length);
private:
struct BgmChannel {
int id;
std::unique_ptr<AudioDecoderBase> decoder;
GenericAudio* instance = nullptr;
bool paused;
bool stopped;
bool midi_out_used = false;
void Stop();
void SetPaused(bool newPaused);
int GetTicks() const;
void SetFade(int fade);
void SetVolume(int volume);
void SetPitch(int pitch);
bool IsUsed() const;
};
struct SeChannel {
int id;
std::unique_ptr<AudioDecoderBase> decoder;
GenericAudio* instance = nullptr;
bool paused;
bool stopped;
};
struct Format {
int frequency;
AudioDecoder::Format format;
int channels;
};
Format output_format = {};
bool PlayOnChannel(BgmChannel& chan, Filesystem_Stream::InputStream stream, int volume, int pitch, int fadein);
bool PlayOnChannel(SeChannel& chan, std::unique_ptr<AudioSeCache> se, int volume, int pitch);
static constexpr unsigned nr_of_se_channels = 31;
static constexpr unsigned nr_of_bgm_channels = 2;
BgmChannel BGM_Channels[nr_of_bgm_channels];
SeChannel SE_Channels[nr_of_se_channels];
mutable bool BGM_PlayedOnceIndicator;
bool Muted;
std::vector<int16_t> sample_buffer = {};
std::vector<uint8_t> scrap_buffer = {};
unsigned scrap_buffer_size = 0;
std::vector<float> mixer_buffer = {};
std::unique_ptr<GenericAudioMidiOut> midi_thread;
};
#endif