-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.h
87 lines (73 loc) · 2.09 KB
/
mixer.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
/* REminiscence - Flashback interpreter
* Copyright (C) 2005-2011 Gregory Montoir
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MIXER_H__
#define MIXER_H__
#include "intern.h"
struct MixerChunk {
uint8_t *data;
uint32_t len;
MixerChunk()
: data(0), len(0) {
}
int8_t getPCM(int offset) const {
if (offset < 0) {
offset = 0;
} else if (offset >= (int)len) {
offset = len - 1;
}
return (int8_t)data[offset];
}
};
struct MixerChannel {
uint8_t active;
uint8_t volume;
MixerChunk chunk;
uint32_t chunkPos;
uint32_t chunkInc;
};
struct SystemStub;
struct Mixer {
typedef bool (*PremixHook)(void *userData, int8_t *buf, int len);
enum {
NUM_CHANNELS = 4,
FRAC_BITS = 12,
MAX_VOLUME = 64
};
SystemStub *_stub;
MixerChannel _channels[NUM_CHANNELS];
PremixHook _premixHook;
void *_premixHookData;
Mixer(SystemStub *stub);
void init();
void free();
void setPremixHook(PremixHook premixHook, void *userData);
void play(const MixerChunk *mc, uint16_t freq, uint8_t volume);
void stopAll();
uint32_t getSampleRate() const;
void mix(int8_t *buf, int len);
static void addclamp(int8_t &a, int b);
static void mixCallback(void *param, int8_t *buf, int len);
};
template <class T>
int resampleLinear(T *sample, int pos, int step, int fracBits) {
const int inputPos = pos >> fracBits;
const int inputFrac = pos & ((1 << fracBits) - 1);
int out = sample->getPCM(inputPos);
out += (sample->getPCM(inputPos + 1) - out) * inputFrac >> fracBits;
return out;
}
#endif // MIXER_H__