src/gba/audio.h (view raw)
1/* Copyright (c) 2013-2016 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 GBA_AUDIO_H
7#define GBA_AUDIO_H
8
9#include "util/common.h"
10
11#include "core/log.h"
12#include "gb/audio.h"
13#include "util/circle-buffer.h"
14
15mLOG_DECLARE_CATEGORY(GBA_AUDIO);
16
17struct GBADMA;
18
19extern const unsigned GBA_AUDIO_SAMPLES;
20extern const int GBA_AUDIO_VOLUME_MAX;
21
22struct GBAAudioFIFO {
23 struct CircleBuffer fifo;
24 int dmaSource;
25 int8_t sample;
26};
27
28DECL_BITFIELD(GBARegisterSOUNDCNT_HI, uint16_t);
29DECL_BITS(GBARegisterSOUNDCNT_HI, Volume, 0, 2);
30DECL_BIT(GBARegisterSOUNDCNT_HI, VolumeChA, 2);
31DECL_BIT(GBARegisterSOUNDCNT_HI, VolumeChB, 3);
32DECL_BIT(GBARegisterSOUNDCNT_HI, ChARight, 8);
33DECL_BIT(GBARegisterSOUNDCNT_HI, ChALeft, 9);
34DECL_BIT(GBARegisterSOUNDCNT_HI, ChATimer, 10);
35DECL_BIT(GBARegisterSOUNDCNT_HI, ChAReset, 11);
36DECL_BIT(GBARegisterSOUNDCNT_HI, ChBRight, 12);
37DECL_BIT(GBARegisterSOUNDCNT_HI, ChBLeft, 13);
38DECL_BIT(GBARegisterSOUNDCNT_HI, ChBTimer, 14);
39DECL_BIT(GBARegisterSOUNDCNT_HI, ChBReset, 15);
40
41DECL_BITFIELD(GBARegisterSOUNDBIAS, uint16_t);
42DECL_BITS(GBARegisterSOUNDBIAS, Bias, 0, 10);
43DECL_BITS(GBARegisterSOUNDBIAS, Resolution, 14, 2);
44
45struct GBAAudio {
46 struct GBA* p;
47
48 struct GBAudio psg;
49 struct GBAAudioFIFO chA;
50 struct GBAAudioFIFO chB;
51
52 int16_t lastLeft;
53 int16_t lastRight;
54 int clock;
55
56 uint8_t volume;
57 bool volumeChA;
58 bool volumeChB;
59 bool chARight;
60 bool chALeft;
61 bool chATimer;
62 bool chBRight;
63 bool chBLeft;
64 bool chBTimer;
65 bool enable;
66
67 size_t samples;
68 unsigned sampleRate;
69
70 GBARegisterSOUNDBIAS soundbias;
71
72 int32_t nextEvent;
73 int32_t eventDiff;
74 int32_t nextSample;
75
76 int32_t sampleInterval;
77
78 bool forceDisableChA;
79 bool forceDisableChB;
80 int masterVolume;
81};
82
83struct GBAStereoSample {
84 int16_t left;
85 int16_t right;
86};
87
88void GBAAudioInit(struct GBAAudio* audio, size_t samples);
89void GBAAudioReset(struct GBAAudio* audio);
90void GBAAudioDeinit(struct GBAAudio* audio);
91
92void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples);
93
94int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
95void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
96
97void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
98void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
99void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
100void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
101void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
102void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
103void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
104void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
105void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
106void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
107void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
108void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
109void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
110void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value);
111
112void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
113void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
114void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles);
115
116struct GBASerializedState;
117void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state);
118void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state);
119
120float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRatio);
121
122#endif