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 <sys/audioio.h>
#include <pthread.h> #include <pthread.h>
static const int samplesPerGo = 0x200; static const int samplesPerGo = 1600;
static const int parkSignal = SIGUSR1; static const int parkSignal = SIGUSR1;
static pthread_t mainThread; static pthread_t mainThread;
static int sndfp; static int sndfp;
@ -25,36 +25,25 @@ 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 inline void setUpSoundFrame(long *mix) {
memset(mix, 0, samplesPerGo * 2 * sizeof(long));
parent_callback(mix, samplesPerGo);
} }
static void* soundcheckthread(void*) { static inline void soundWait() {
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;
audio_info_t audioInfo; audio_info_t audioInfo;
ioctl(sndfp, AUDIO_GETINFO, &audioInfo); ioctl(sndfp, AUDIO_GETINFO, &audioInfo);
//printf("%d\n", audioInfo.play.error); int playedBefore = audioInfo.play.samples;
int playedNow = audioInfo.play.samples;
if(counter > 4 && audioInfo.play.error == 0) { while(playedNow - playedBefore < samplesPerGo) {
usleep(for_as_long_as_it_seems_we_should); Backend_Delay(10);
} else { ioctl(sndfp, AUDIO_GETINFO, &audioInfo);
printf("ae\n"); playedNow = audioInfo.play.samples;
audioInfo.play.error = 0;
ioctl(sndfp, AUDIO_SETINFO, &audioInfo);
} }
}
static inline void feedOutSound(long* mix) {
for(int i = 0; i < samplesPerGo * 2; i++) { for(int i = 0; i < samplesPerGo * 2; i++) {
//if(i % 2 == 0) {
short sample = mix[i]; short sample = mix[i];
if(mix[i] > 0x7FFF) { if(mix[i] > 0x7FFF) {
sample = 0x7FFF; sample = 0x7FFF;
@ -64,11 +53,24 @@ static void* soundcheckthread(void*) {
} }
fputc((sample >> 8) & 0xFF, sndfile); fputc((sample >> 8) & 0xFF, sndfile);
fputc(sample & 0xFF, sndfile); fputc(sample & 0xFF, sndfile);
//}
} }
}
counter += 1; static void* soundcheckthread(void*) {
firstTime = false; 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);
} }
} }