all repos — mgba @ adc57d0da7a09098cfc73a15cd034aa721474b68

mGBA Game Boy Advance Emulator

src/gb/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 GB_AUDIO_H
  7#define GB_AUDIO_H
  8
  9#include "util/common.h"
 10
 11#include "third-party/blip_buf/blip_buf.h"
 12
 13DECL_BITFIELD(GBAudioRegisterDuty, uint8_t);
 14DECL_BITS(GBAudioRegisterDuty, Length, 0, 6);
 15DECL_BITS(GBAudioRegisterDuty, Duty, 6, 2);
 16
 17DECL_BITFIELD(GBAudioRegisterSweep, uint8_t);
 18DECL_BITS(GBAudioRegisterSweep, StepTime, 0, 3);
 19DECL_BIT(GBAudioRegisterSweep, Direction, 3);
 20DECL_BITS(GBAudioRegisterSweep, InitialVolume, 4, 4);
 21
 22DECL_BITFIELD(GBAudioRegisterControl, uint16_t);
 23DECL_BITS(GBAudioRegisterControl, Rate, 0, 11);
 24DECL_BITS(GBAudioRegisterControl, Frequency, 0, 11);
 25DECL_BIT(GBAudioRegisterControl, Stop, 14);
 26DECL_BIT(GBAudioRegisterControl, Restart, 15);
 27
 28DECL_BITFIELD(GBAudioRegisterSquareSweep, uint8_t);
 29DECL_BITS(GBAudioRegisterSquareSweep, Shift, 0, 3);
 30DECL_BIT(GBAudioRegisterSquareSweep, Direction, 3);
 31DECL_BITS(GBAudioRegisterSquareSweep, Time, 4, 3);
 32
 33DECL_BITFIELD(GBAudioRegisterBank, uint8_t);
 34DECL_BIT(GBAudioRegisterBank, Size, 5);
 35DECL_BIT(GBAudioRegisterBank, Bank, 6);
 36DECL_BIT(GBAudioRegisterBank, Enable, 7);
 37
 38DECL_BITFIELD(GBAudioRegisterBankVolume, uint8_t);
 39DECL_BITS(GBAudioRegisterBankVolume, VolumeGB, 5, 2);
 40DECL_BITS(GBAudioRegisterBankVolume, VolumeGBA, 5, 3);
 41
 42DECL_BITFIELD(GBAudioRegisterNoiseFeedback, uint8_t);
 43DECL_BITS(GBAudioRegisterNoiseFeedback, Ratio, 0, 3);
 44DECL_BIT(GBAudioRegisterNoiseFeedback, Power, 3);
 45DECL_BITS(GBAudioRegisterNoiseFeedback, Frequency, 4, 4);
 46
 47DECL_BITFIELD(GBAudioRegisterNoiseControl, uint8_t);
 48DECL_BIT(GBAudioRegisterNoiseControl, Stop, 6);
 49DECL_BIT(GBAudioRegisterNoiseControl, Restart, 7);
 50
 51DECL_BITFIELD(GBRegisterNR50, uint8_t);
 52DECL_BITS(GBRegisterNR50, VolumeRight, 0, 3);
 53DECL_BITS(GBRegisterNR50, VolumeLeft, 4, 3);
 54
 55DECL_BITFIELD(GBRegisterNR51, uint8_t);
 56DECL_BIT(GBRegisterNR51, Ch1Right, 0);
 57DECL_BIT(GBRegisterNR51, Ch2Right, 1);
 58DECL_BIT(GBRegisterNR51, Ch3Right, 2);
 59DECL_BIT(GBRegisterNR51, Ch4Right, 3);
 60DECL_BIT(GBRegisterNR51, Ch1Left, 4);
 61DECL_BIT(GBRegisterNR51, Ch2Left, 5);
 62DECL_BIT(GBRegisterNR51, Ch3Left, 6);
 63DECL_BIT(GBRegisterNR51, Ch4Left, 7);
 64
 65DECL_BITFIELD(GBAudioEnable, uint8_t);
 66DECL_BIT(GBAudioEnable, PlayingCh1, 0);
 67DECL_BIT(GBAudioEnable, PlayingCh2, 1);
 68DECL_BIT(GBAudioEnable, PlayingCh3, 2);
 69DECL_BIT(GBAudioEnable, PlayingCh4, 3);
 70DECL_BIT(GBAudioEnable, Enable, 7);
 71
 72struct GB;
 73struct GBAudioEnvelope {
 74	uint8_t length;
 75	uint8_t duty;
 76	uint8_t stepTime;
 77	uint8_t initialVolume;
 78	bool direction;
 79	int currentVolume;
 80	int dead;
 81	int32_t nextStep;
 82};
 83
 84struct GBAudioSquareControl {
 85	uint16_t frequency;
 86	bool stop;
 87	int hi;
 88	int32_t nextStep;
 89	int32_t endTime;
 90};
 91
 92struct GBAudioChannel1 {
 93	uint8_t shift;
 94	uint8_t time;
 95	bool direction;
 96
 97	int32_t nextSweep;
 98
 99	struct GBAudioEnvelope envelope;
100	struct GBAudioSquareControl control;
101	int8_t sample;
102};
103
104struct GBAudioChannel2 {
105	struct GBAudioEnvelope envelope;
106	struct GBAudioSquareControl control;
107	int8_t sample;
108};
109
110struct GBAudioChannel3 {
111	bool size;
112	bool bank;
113	bool enable;
114
115	unsigned length;
116	uint8_t volume;
117
118	uint16_t rate;
119	bool stop;
120	int32_t endTime;
121
122	uint32_t wavedata[8];
123	int8_t sample;
124};
125
126struct GBAudioChannel4 {
127	struct GBAudioEnvelope envelope;
128
129	uint8_t ratio;
130	uint8_t frequency;
131	bool power;
132	bool stop;
133	int32_t endTime;
134
135	uint32_t lfsr;
136	int8_t sample;
137};
138
139struct GBAudio {
140	struct GB* p;
141	struct GBAudioChannel1 ch1;
142	struct GBAudioChannel2 ch2;
143	struct GBAudioChannel3 ch3;
144	struct GBAudioChannel4 ch4;
145
146	blip_t* left;
147	blip_t* right;
148	int16_t lastLeft;
149	int16_t lastRight;
150	int clock;
151	int32_t clockRate;
152
153	uint8_t volumeRight;
154	uint8_t volumeLeft;
155	bool ch1Right;
156	bool ch2Right;
157	bool ch3Right;
158	bool ch4Right;
159	bool ch1Left;
160	bool ch2Left;
161	bool ch3Left;
162	bool ch4Left;
163
164	bool playingCh1;
165	bool playingCh2;
166	bool playingCh3;
167	bool playingCh4;
168
169	int32_t nextEvent;
170	int32_t eventDiff;
171	int32_t nextSample;
172
173	int32_t sampleInterval;
174
175	int32_t nextCh1;
176	int32_t nextCh2;
177	int32_t nextCh3;
178	int32_t nextCh4;
179	bool enable;
180
181	size_t samples;
182	bool forceDisableCh[4];
183};
184
185void GBAudioInit(struct GBAudio* audio, size_t samples);
186void GBAudioDeinit(struct GBAudio* audio);
187void GBAudioReset(struct GBAudio* audio);
188
189void GBAudioWriteNR10(struct GBAudio* audio, uint8_t);
190void GBAudioWriteNR11(struct GBAudio* audio, uint8_t);
191void GBAudioWriteNR12(struct GBAudio* audio, uint8_t);
192void GBAudioWriteNR13(struct GBAudio* audio, uint8_t);
193void GBAudioWriteNR14(struct GBAudio* audio, uint8_t);
194
195void GBAudioWriteNR21(struct GBAudio* audio, uint8_t);
196void GBAudioWriteNR22(struct GBAudio* audio, uint8_t);
197void GBAudioWriteNR23(struct GBAudio* audio, uint8_t);
198void GBAudioWriteNR24(struct GBAudio* audio, uint8_t);
199
200void GBAudioWriteNR30(struct GBAudio* audio, uint8_t);
201void GBAudioWriteNR31(struct GBAudio* audio, uint8_t);
202void GBAudioWriteNR32(struct GBAudio* audio, uint8_t);
203void GBAudioWriteNR33(struct GBAudio* audio, uint8_t);
204void GBAudioWriteNR34(struct GBAudio* audio, uint8_t);
205
206void GBAudioWriteNR41(struct GBAudio* audio, uint8_t);
207void GBAudioWriteNR42(struct GBAudio* audio, uint8_t);
208void GBAudioWriteNR43(struct GBAudio* audio, uint8_t);
209void GBAudioWriteNR44(struct GBAudio* audio, uint8_t);
210
211void GBAudioWriteNR50(struct GBAudio* audio, uint8_t);
212void GBAudioWriteNR51(struct GBAudio* audio, uint8_t);
213void GBAudioWriteNR52(struct GBAudio* audio, uint8_t);
214
215int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles);
216void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right);
217
218#endif