Mostly working audio but gets out of sync and dies

This commit is contained in:
John Lorentzson 2025-04-08 21:03:29 +02:00
parent 2d46a14a37
commit 2dd6b4479f

View file

@ -17,6 +17,7 @@
#include <sys/audioio.h>
#include <pthread.h>
static const int samplesPerGo = 0x400;
static const int parkSignal = SIGUSR1;
static pthread_t mainThread;
static int sndfp;
@ -32,7 +33,7 @@ static void* soundcheckthread(void*) {
//sndfp = open("/dev/audio", 0, O_WRONLY);
audioDone = true;
unsigned long for_as_long_as_it_seems_we_should = 0;
uint16_t out[0x400 * 2];
uint16_t out[samplesPerGo * 2];
bool firstTime = true;
int oldeof;
while(1) {
@ -51,8 +52,8 @@ static void* soundcheckthread(void*) {
}
}
*/
long mix[0x400 * 2];
memset(mix, 0, 0x400 * 2 * sizeof(long));
long mix[samplesPerGo * 2];
memset(mix, 0, samplesPerGo * 2 * sizeof(long));
//printf("Cleared mix buffer\n");
// park the main thread since the game probably expects it to not be
// doing anything else while we're requesting audio frames.
@ -60,13 +61,13 @@ static void* soundcheckthread(void*) {
//pthread_kill(mainThread, parkSignal);
//printf("Unparked thread\n");
parent_callback(mix, 0x400);
parent_callback(mix, samplesPerGo);
// unpark the main thread now that we're not using game state anymore
audioDone = true;
//printf("Audio done\n");
for(int i = 0; i < 0x400 * 2; i++) {
for(int i = 0; i < samplesPerGo * 2; i++) {
//if(i % 2 == 0) {
short sample = mix[i];
if(mix[i] > 0x7FFF) {
@ -79,13 +80,13 @@ static void* soundcheckthread(void*) {
fputc(sample & 0xFF, sndfile);
//}
}
//write(sndfp, mix, 0x400 * 2);
//write(sndfp, mix, samplesPerGo * 2);
//write(sndfp, NULL, 0);
//ioctl(sndfp, AUDIO_GETINFO, &audioInfo);
//oldeof = audioInfo.play.eof;
for_as_long_as_it_seems_we_should = ((float)0x400 / (float)8000) * 1000000;
for_as_long_as_it_seems_we_should = ((float)(samplesPerGo - 0x7F) / (float)8000) * 1000000;
firstTime = false;
}
}
@ -110,10 +111,10 @@ unsigned long SoftwareMixerBackend_Init(void (*callback)(long *stream, size_t fr
info.sample_rate = 8000;
info.channels = 2;
info.precision = 16;
info.precision = 8;
info.encoding = AUDIO_ENCODING_LINEAR;
info.port = AUDIO_SPEAKER | AUDIO_HEADPHONE;
info.gain = 32;
info.gain = 128;
audioInfo.play = info;
ioctl(sndfp, AUDIO_SETINFO, &audioInfo);