Untested maybe improved audio code (no more pre-computed sleeps)

This commit is contained in:
John Lorentzson 2025-04-18 21:40:02 +02:00
parent 3b0418a25f
commit b1d3549723

View file

@ -17,7 +17,7 @@
#include <sys/audioio.h>
#include <pthread.h>
static const int samplesPerGo = 0x200;
static const int samplesPerGo = 1600;
static const int parkSignal = SIGUSR1;
static pthread_t mainThread;
static int sndfp;
@ -25,36 +25,25 @@ static FILE* sndfile;
static void (*parent_callback)(long *stream, size_t frames_total);
static bool audioDone = false;
static void soundSetup() {
static inline void setUpSoundFrame(long *mix) {
memset(mix, 0, samplesPerGo * 2 * sizeof(long));
parent_callback(mix, samplesPerGo);
}
static void* soundcheckthread(void*) {
printf("Entered sound thread.\n");
unsigned long for_as_long_as_it_seems_we_should = 0;
bool firstTime = true;
int counter = 0;
while(1) {
long mix[samplesPerGo * 2];
memset(mix, 0, samplesPerGo * 2 * sizeof(long));
parent_callback(mix, samplesPerGo);
for_as_long_as_it_seems_we_should = ((float)(samplesPerGo) / (float)8000) * 1000000;
static inline void soundWait() {
audio_info_t audioInfo;
ioctl(sndfp, AUDIO_GETINFO, &audioInfo);
//printf("%d\n", audioInfo.play.error);
if(counter > 4 && audioInfo.play.error == 0) {
usleep(for_as_long_as_it_seems_we_should);
} else {
printf("ae\n");
audioInfo.play.error = 0;
ioctl(sndfp, AUDIO_SETINFO, &audioInfo);
int playedBefore = audioInfo.play.samples;
int playedNow = audioInfo.play.samples;
while(playedNow - playedBefore < samplesPerGo) {
Backend_Delay(10);
ioctl(sndfp, AUDIO_GETINFO, &audioInfo);
playedNow = audioInfo.play.samples;
}
}
static inline void feedOutSound(long* mix) {
for(int i = 0; i < samplesPerGo * 2; i++) {
//if(i % 2 == 0) {
short sample = mix[i];
if(mix[i] > 0x7FFF) {
sample = 0x7FFF;
@ -64,11 +53,24 @@ static void* soundcheckthread(void*) {
}
fputc((sample >> 8) & 0xFF, sndfile);
fputc(sample & 0xFF, sndfile);
//}
}
}
counter += 1;
firstTime = false;
static void* soundcheckthread(void*) {
printf("Entered sound thread.\n");
long mix[samplesPerGo * 2];
setUpSoundFrame(mix);
feedOutSound(mix);
setUpSoundFrame(mix);
feedOutSound(mix);
while(1) {
setUpSoundFrame(mix);
// Waiting for more to be needed
soundWait();
feedOutSound(mix);
}
}