all repos — mgba @ 2f9bcf63b73e3edfd80a572981c3885bb30a09b9

mGBA Game Boy Advance Emulator

src/gba/gba-audio.c (view raw)

  1#include "gba-audio.h"
  2
  3#include "gba.h"
  4#include "gba-io.h"
  5#include "gba-thread.h"
  6
  7const unsigned GBA_AUDIO_SAMPLES = 512;
  8const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
  9
 10static void _sample(struct GBAAudio* audio);
 11
 12void GBAAudioInit(struct GBAAudio* audio) {
 13	audio->nextEvent = 0;
 14	audio->eventDiff = 0;
 15	audio->nextSample = 0;
 16	audio->sampleRate = 0x8000;
 17	audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
 18
 19	CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t));
 20	CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t));
 21	CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
 22	CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
 23
 24	pthread_mutex_init(&audio->bufferMutex, 0);
 25}
 26
 27void GBAAudioDeinit(struct GBAAudio* audio) {
 28	CircleBufferDeinit(&audio->left);
 29	CircleBufferDeinit(&audio->right);
 30	CircleBufferDeinit(&audio->chA.fifo);
 31	CircleBufferDeinit(&audio->chB.fifo);
 32
 33	pthread_mutex_destroy(&audio->bufferMutex);
 34}
 35
 36int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
 37	audio->nextEvent -= cycles;
 38	if (audio->nextEvent <= 0) {
 39		audio->nextSample -= audio->eventDiff;
 40		if (audio->nextSample <= 0) {
 41			_sample(audio);
 42			audio->nextSample += audio->sampleInterval;
 43		}
 44
 45		audio->nextEvent = audio->nextSample;
 46		audio->eventDiff = audio->nextEvent;
 47	}
 48	return audio->nextEvent;
 49}
 50
 51void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
 52	switch (info->dest) {
 53	case BASE_IO | REG_FIFO_A_LO:
 54		audio->chA.dmaSource = number;
 55		break;
 56	case BASE_IO | REG_FIFO_B_LO:
 57		audio->chB.dmaSource = number;
 58		break;
 59	default:
 60		GBALog(audio->p, GBA_LOG_GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
 61		return;
 62	}
 63	info->dstControl = DMA_FIXED;
 64}
 65
 66void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
 67	audio->ch1.sweep.packed = value;
 68}
 69
 70void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
 71	audio->ch1.wave.packed = value;
 72}
 73
 74void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
 75	audio->ch1.control.packed = value;
 76}
 77
 78void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
 79	audio->ch2.wave.packed = value;
 80}
 81
 82void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
 83	audio->ch2.control.packed = value;
 84}
 85
 86void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
 87	audio->ch3.bank.packed = value;
 88}
 89
 90void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
 91	audio->ch3.wave.packed = value;
 92}
 93
 94void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
 95	audio->ch3.control.packed = value;
 96}
 97
 98void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
 99	audio->ch4.wave.packed = value;
100}
101
102void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
103	audio->ch4.control.packed = value;
104}
105
106void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
107	audio->soundcntLo = value;
108}
109
110void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
111	audio->soundcntHi = value;
112}
113
114void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
115	audio->soundcntX = (value & 0xF0) | (audio->soundcntX & 0x0F);
116}
117
118void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
119	GBALog(audio->p, GBA_LOG_STUB, "Audio unimplemented");
120}
121
122void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
123	struct CircleBuffer* fifo;
124	switch (address) {
125	case REG_FIFO_A_LO:
126		fifo = &audio->chA.fifo;
127		break;
128	case REG_FIFO_B_LO:
129		fifo = &audio->chB.fifo;
130		break;
131	default:
132		GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
133		return;
134	}
135	while (!CircleBufferWrite32(fifo, value)) {
136		int32_t dummy;
137		CircleBufferRead32(fifo, &dummy);
138	}
139}
140
141void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId) {
142	struct GBAAudioFIFO* channel;
143	if (fifoId == 0) {
144		channel = &audio->chA;
145	} else if (fifoId == 1) {
146		channel = &audio->chB;
147	} else {
148		GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
149		return;
150	}
151	if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t)) {
152		struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
153		dma->nextCount = 4;
154		GBAMemoryServiceDMA(&audio->p->memory, channel->dmaSource, dma);
155	}
156	CircleBufferRead8(&channel->fifo, &channel->sample);
157}
158
159static void _sample(struct GBAAudio* audio) {
160	int32_t sampleLeft = 0;
161	int32_t sampleRight = 0;
162
163	if (audio->chALeft) {
164		sampleLeft += audio->chA.sample;
165	}
166
167	if (audio->chARight) {
168		sampleRight += audio->chA.sample;
169	}
170
171	if (audio->chBLeft) {
172		sampleLeft += audio->chB.sample;
173	}
174
175	if (audio->chBRight) {
176		sampleRight += audio->chB.sample;
177	}
178
179	pthread_mutex_lock(&audio->bufferMutex);
180	while (CircleBufferSize(&audio->left) + (GBA_AUDIO_SAMPLES * 2 / 5) >= audio->left.capacity) {
181		if (!audio->p->sync->audioWait) {
182			break;
183		}
184		GBASyncProduceAudio(audio->p->sync, &audio->bufferMutex);
185	}
186	CircleBufferWrite32(&audio->left, sampleLeft);
187	CircleBufferWrite32(&audio->right, sampleRight);
188	pthread_mutex_unlock(&audio->bufferMutex);
189}