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 sampleInterval;
73
74 bool forceDisableChA;
75 bool forceDisableChB;
76 int masterVolume;
77
78 struct mTimingEvent sampleEvent;
79};
80
81struct GBAStereoSample {
82 int16_t left;
83 int16_t right;
84};
85
86void GBAAudioInit(struct GBAAudio* audio, size_t samples);
87void GBAAudioReset(struct GBAAudio* audio);
88void GBAAudioDeinit(struct GBAAudio* audio);
89
90void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples);
91
92int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
93void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
94
95void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
96void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
97void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
98void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
99void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
100void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
101void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
102void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
103void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
104void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
105void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
106void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
107void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
108void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value);
109
110void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
111void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
112void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles);
113
114struct GBASerializedState;
115void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state);
116void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state);
117
118float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRatio);
119
120#endif