include/mgba/internal/ds/audio.h (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef DS_AUDIO_H
7#define DS_AUDIO_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/log.h>
14#include <mgba/core/timing.h>
15
16mLOG_DECLARE_CATEGORY(DS_AUDIO);
17
18DECL_BITFIELD(DSRegisterSOUNDxCNT, uint32_t);
19DECL_BITS(DSRegisterSOUNDxCNT, VolumeMul, 0, 7);
20DECL_BITS(DSRegisterSOUNDxCNT, VolumeDiv, 8, 2);
21DECL_BIT(DSRegisterSOUNDxCNT, Hold, 15);
22DECL_BITS(DSRegisterSOUNDxCNT, Panning, 16, 7);
23DECL_BITS(DSRegisterSOUNDxCNT, Duty, 24, 3);
24DECL_BITS(DSRegisterSOUNDxCNT, Repeat, 27, 2);
25DECL_BITS(DSRegisterSOUNDxCNT, Format, 29, 2);
26DECL_BIT(DSRegisterSOUNDxCNT, Busy, 31);
27
28struct DSAudio;
29struct DSAudioChannel {
30 struct DSAudio* p;
31 int index;
32
33 struct mTimingEvent updateEvent;
34
35 uint32_t source;
36 uint32_t loopPoint;
37 uint32_t length;
38 uint32_t offset;
39
40 unsigned volume;
41 unsigned divider;
42 int panning;
43 int format;
44 int repeat;
45
46 uint32_t period;
47 int16_t sample;
48
49 int duty;
50 bool high;
51 uint16_t lfsr;
52
53 int adpcmOffset;
54
55 int16_t adpcmStartSample;
56 int adpcmStartIndex;
57
58 int16_t adpcmSample;
59 int adpcmIndex;
60
61 bool enable;
62};
63
64struct DS;
65struct DSAudio {
66 struct DS* p;
67 struct blip_t* left;
68 struct blip_t* right;
69
70 struct DSAudioChannel ch[16];
71
72 int16_t lastLeft;
73 int16_t lastRight;
74 int clock;
75
76 int16_t sampleLeft;
77 int16_t sampleRight;
78
79 size_t samples;
80 unsigned sampleRate;
81
82 int32_t sampleInterval;
83 unsigned sampleDrift;
84
85 bool forceDisableCh[16];
86 int bias;
87 int masterVolume;
88
89 struct mTimingEvent sampleEvent;
90};
91
92void DSAudioInit(struct DSAudio*, size_t samples);
93void DSAudioDeinit(struct DSAudio*);
94void DSAudioReset(struct DSAudio*);
95
96void DSAudioResizeBuffer(struct DSAudio* audio, size_t samples);
97
98void DSAudioWriteSOUNDCNT_LO(struct DSAudio*, int chan, uint16_t value);
99void DSAudioWriteSOUNDCNT_HI(struct DSAudio*, int chan, uint16_t value);
100void DSAudioWriteSOUNDTMR(struct DSAudio*, int chan, uint16_t value);
101void DSAudioWriteSOUNDPNT(struct DSAudio*, int chan, uint16_t value);
102void DSAudioWriteSOUNDSAD(struct DSAudio*, int chan, uint32_t value);
103void DSAudioWriteSOUNDLEN(struct DSAudio*, int chan, uint32_t value);
104
105CXX_GUARD_END
106
107#endif