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#include "macros.h"
11
12#include "gb/audio.h"
13#include "util/circle-buffer.h"
14
15struct GBADMA;
16
17extern const unsigned GBA_AUDIO_SAMPLES;
18extern const int GBA_AUDIO_VOLUME_MAX;
19
20struct GBAAudioFIFO {
21 struct CircleBuffer fifo;
22 int dmaSource;
23 int8_t sample;
24};
25
26DECL_BITFIELD(GBARegisterSOUNDCNT_HI, uint16_t);
27DECL_BITS(GBARegisterSOUNDCNT_HI, Volume, 0, 2);
28DECL_BIT(GBARegisterSOUNDCNT_HI, VolumeChA, 2);
29DECL_BIT(GBARegisterSOUNDCNT_HI, VolumeChB, 3);
30DECL_BIT(GBARegisterSOUNDCNT_HI, ChARight, 8);
31DECL_BIT(GBARegisterSOUNDCNT_HI, ChALeft, 9);
32DECL_BIT(GBARegisterSOUNDCNT_HI, ChATimer, 10);
33DECL_BIT(GBARegisterSOUNDCNT_HI, ChAReset, 11);
34DECL_BIT(GBARegisterSOUNDCNT_HI, ChBRight, 12);
35DECL_BIT(GBARegisterSOUNDCNT_HI, ChBLeft, 13);
36DECL_BIT(GBARegisterSOUNDCNT_HI, ChBTimer, 14);
37DECL_BIT(GBARegisterSOUNDCNT_HI, ChBReset, 15);
38
39DECL_BITFIELD(GBARegisterSOUNDBIAS, uint16_t);
40DECL_BITS(GBARegisterSOUNDBIAS, Bias, 0, 10);
41DECL_BITS(GBARegisterSOUNDBIAS, Resolution, 14, 2);
42
43struct GBAAudio {
44 struct GBA* p;
45
46 struct GBAudio psg;
47 struct GBAAudioFIFO chA;
48 struct GBAAudioFIFO chB;
49
50 int16_t lastLeft;
51 int16_t lastRight;
52 int clock;
53
54 uint8_t volume;
55 bool volumeChA;
56 bool volumeChB;
57 bool chARight;
58 bool chALeft;
59 bool chATimer;
60 bool chBRight;
61 bool chBLeft;
62 bool chBTimer;
63 bool enable;
64
65 size_t samples;
66 unsigned sampleRate;
67
68 GBARegisterSOUNDBIAS soundbias;
69
70 int32_t nextEvent;
71 int32_t eventDiff;
72 int32_t nextSample;
73
74 int32_t sampleInterval;
75
76 bool forceDisableChA;
77 bool forceDisableChB;
78 int masterVolume;
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