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