Working but very choppy audio

This commit is contained in:
John Lorentzson 2025-04-08 20:23:30 +02:00
parent ffdd2d5d7f
commit 2d46a14a37

View file

@ -20,13 +20,16 @@
static const int parkSignal = SIGUSR1; static const int parkSignal = SIGUSR1;
static pthread_t mainThread; static pthread_t mainThread;
static int sndfp; static int sndfp;
static FILE* sndfile;
static void (*parent_callback)(long *stream, size_t frames_total); static void (*parent_callback)(long *stream, size_t frames_total);
static bool audioDone = false; static bool audioDone = false;
static void soundSetup() {
}
static void* soundcheckthread(void*) { static void* soundcheckthread(void*) {
printf("Entered sound thread.\n"); printf("Entered sound thread.\n");
//sndfp = open("/dev/audio", 0, O_WRONLY); //sndfp = open("/dev/audio", 0, O_WRONLY);
FILE* sndfile = fopen("/dev/audio", "wb");
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[0x400 * 2];
@ -49,7 +52,7 @@ static void* soundcheckthread(void*) {
} }
*/ */
long mix[0x400 * 2]; long mix[0x400 * 2];
memset(mix, 0, 0x400 * 2 * 4); memset(mix, 0, 0x400 * 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.
@ -64,15 +67,23 @@ static void* soundcheckthread(void*) {
//printf("Audio done\n"); //printf("Audio done\n");
for(int i = 0; i < 0x400 * 2; i++) { for(int i = 0; i < 0x400 * 2; i++) {
if(i % 2 == 0) { //if(i % 2 == 0) {
fputc((mix[i] >> 8) & 0xFF, sndfile); short sample = mix[i];
} if(mix[i] > 0x7FFF) {
sample = 0x7FFF;
}
if(mix[i] < -0x7FFF) {
sample = -0x7FFF;
}
fputc((sample >> 8) & 0xFF, sndfile);
fputc(sample & 0xFF, sndfile);
//}
} }
//write(sndfp, mix, 0x400 * 2); //write(sndfp, mix, 0x400 * 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)0x400 / (float)8000) * 1000000;
firstTime = false; firstTime = false;
@ -85,27 +96,28 @@ static void threadPark(int signo) {
unsigned long SoftwareMixerBackend_Init(void (*callback)(long *stream, size_t frames_total)) { unsigned long SoftwareMixerBackend_Init(void (*callback)(long *stream, size_t frames_total)) {
parent_callback = callback; parent_callback = callback;
sndfp = open("/dev/audio", 0, O_WRONLY);
if(sndfp == -1) { if(sndfp == -1) {
Backend_PrintError("Failed to open audio device.\n"); Backend_PrintError("Failed to open audio device.\n");
return 0; return 0;
} }
sndfile = fopen("/dev/audio", "wb");
sndfp = fileno(sndfile);
audio_info_t audioInfo; audio_info_t audioInfo;
ioctl(sndfp, AUDIO_GETINFO, &audioInfo); ioctl(sndfp, AUDIO_GETINFO, &audioInfo);
audio_prinfo_t info = audioInfo.play; audio_prinfo_t info = audioInfo.play;
info.sample_rate = 8000; info.sample_rate = 8000;
info.channels = 2; info.channels = 2;
info.precision = 8; info.precision = 16;
info.encoding = AUDIO_ENCODING_LINEAR; info.encoding = AUDIO_ENCODING_LINEAR;
info.port = AUDIO_SPEAKER; info.port = AUDIO_SPEAKER | AUDIO_HEADPHONE;
info.gain = 32; info.gain = 32;
audioInfo.play = info; audioInfo.play = info;
ioctl(sndfp, AUDIO_SETINFO, &audioInfo); ioctl(sndfp, AUDIO_SETINFO, &audioInfo);
close(sndfp); //close(sndfp);
if(signal(parkSignal, threadPark) == SIG_ERR) { if(signal(parkSignal, threadPark) == SIG_ERR) {
Backend_PrintError("Failed to register thread park signal handler for audio system.\n"); Backend_PrintError("Failed to register thread park signal handler for audio system.\n");