foxygit / fake-08-xbox360 Log in
commit 5d5f1d3581c4f39363d36486fe6e0b05bd77e6f2
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sun Jan 30 16:00:10 2022 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sun Jan 30 16:00:10 2022 -0700

    Attempt to fix audio using AO instead of SDL
---
 platform/miyoomini/Makefile                 |   4 +-
 platform/miyoomini/source/MiyooMiniHost.cpp |  12 ++-
 platform/miyoomini/source/ao.c              | 147 ++++++++++++++++++++++++++++
 platform/miyoomini/source/ao.h              |  12 +++
 4 files changed, 170 insertions(+), 5 deletions(-)

diff --git a/platform/miyoomini/Makefile b/platform/miyoomini/Makefile
index 7c4deb6..50d3861 100644
--- a/platform/miyoomini/Makefile
+++ b/platform/miyoomini/Makefile
@@ -9,7 +9,7 @@
 TARGET		:=	FAKE08
 BUILD		:=	build
 SOURCES		:=	${SOURCES} source
-INCLUDES	:=	${INCLUDES}
+INCLUDES	:=	${INCLUDES} source

 #---------------------------------------------------------------------------------
 # options for code generation
@@ -28,7 +28,7 @@ CFLAGS	+=	$(INCLUDE)

 CXXFLAGS	:= $(CFLAGS) -fno-rtti -std=gnu++17

-LIBS	:= -lSDL
+LIBS	:= -lSDL -lmi_ao

 LDFLAGS	:= $(LIBS)

diff --git a/platform/miyoomini/source/MiyooMiniHost.cpp b/platform/miyoomini/source/MiyooMiniHost.cpp
index 0c88157..eb1f25f 100644
--- a/platform/miyoomini/source/MiyooMiniHost.cpp
+++ b/platform/miyoomini/source/MiyooMiniHost.cpp
@@ -17,6 +17,8 @@ using namespace std;
 // sdl
 #include <SDL/SDL.h>

+#include "ao.h"
+
 #define SCREEN_SIZE_X 640
 #define SCREEN_SIZE_Y 480

@@ -87,11 +89,11 @@ void postFlipFunction(){
 }

 void audioCleanup(){
-    SDL_PauseAudio(1);
-
+    AO_PauseAudio(1);
+
     audioInitialized = false;

-    SDL_CloseAudio();
+    AO_CloseAudio();
 }


@@ -111,6 +113,9 @@ void audioSetup(){
     want.callback = FillAudioDeviceBuffer;


+    AO_OpenAudio(&want);
+    AO_PauseAudio(0);
+    /*
     int audioOpenRes = SDL_OpenAudio(&want, &have);
     if (audioOpenRes < 0) {
         Logger_Write("Failed to open audio: %s", SDL_GetError());
@@ -121,6 +126,7 @@ void audioSetup(){
         SDL_PauseAudio(0);
         audioInitialized = true;
     }
+    */
 }

 void _setSourceRect(int xoffset, int yoffset) {
diff --git a/platform/miyoomini/source/ao.c b/platform/miyoomini/source/ao.c
new file mode 100644
index 0000000..e84df84
--- /dev/null
+++ b/platform/miyoomini/source/ao.c
@@ -0,0 +1,147 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <pthread.h>
+#include <linux/input.h>
+#include <SDL/SDL.h>
+#include <sdkdir/mi_sys.h>
+#include <sdkdir/mi_ao.h>
+
+#define	NUM_FRAMES		4	// 8192/(512*2*2)
+//#define	NUM_FRAMES		2
+#define	USLEEP_MIN		0	// Threshold time for reading ahead without usleep
+					//	when the time remaining until the next frame is less
+					// (usleep resolution is 10000us(10ms) in the case of miyoomini)
+
+pthread_t 	audiothread_pt;
+pthread_mutex_t	audiolock_mx;
+uint32_t	audiothread_cancel;
+SDL_AudioSpec	audiospec;
+uint8_t		*audio_buffer;
+int		audio_paused;
+
+//
+//	AO Audio playback thread
+//
+void* audiothread(void* param) {
+	MI_AUDIO_Frame_t	AoSendFrame;
+	struct timeval tod;
+	int usleepclock;
+	uint64_t startclock, clock_freqframes;
+	uint32_t i, framecounter;
+
+	void (*fill)(void*, uint8_t*, int) = audiospec.callback;
+	void *udata = audiospec.userdata;
+
+	memset(&AoSendFrame, 0, sizeof(AoSendFrame));
+	AoSendFrame.apVirAddr[0] = audio_buffer;
+	AoSendFrame.u32Len = audiospec.size;
+	memset(audio_buffer, 0, audiospec.size);
+
+	// Buffer initial frames
+	for (i=NUM_FRAMES; i>0; i--) {
+		MI_AO_SendFrame(0, 0, &AoSendFrame, 0);
+	}
+
+	clock_freqframes = audiospec.samples * 1000000;
+	framecounter = 0;
+	gettimeofday(&tod, NULL);
+	startclock = tod.tv_usec + tod.tv_sec * 1000000;
+
+	while(!audiothread_cancel) {
+		// Wait until next frame
+		framecounter++;
+		if (framecounter == (uint32_t)audiospec.freq) {
+			framecounter = 0;
+			startclock += clock_freqframes;
+		}
+		gettimeofday(&tod, NULL);
+		usleepclock = framecounter * clock_freqframes / audiospec.freq
+				 + startclock - (tod.tv_usec + tod.tv_sec * 1000000);
+		if (usleepclock > USLEEP_MIN) usleep(usleepclock - USLEEP_MIN);
+
+		// Request filling audio_buffer to callback function
+		if (!audio_paused) {
+			pthread_mutex_lock(&audiolock_mx);
+			(*fill)(udata, audio_buffer, audiospec.size);
+			pthread_mutex_unlock(&audiolock_mx);
+		}
+
+		// Playback
+		MI_AO_SendFrame(0, 0, &AoSendFrame, 0);
+
+		// Clear Buffer , per SDL1.2 spec (SDL2 does not clear)
+		memset(audio_buffer, 0,audiospec.size);
+	}
+
+	return 0;
+}
+
+//
+//	Open AO Audio in place of SDL_OpenAudio
+//		supports signed 16bit only
+//
+void AO_OpenAudio(SDL_AudioSpec *spec){
+	MI_AUDIO_Attr_t	attr;
+
+	memcpy(&audiospec, spec, sizeof(audiospec));
+	memset(&attr, 0, sizeof(attr));
+	attr.eSamplerate = (MI_AUDIO_SampleRate_e)audiospec.freq;
+	attr.eSoundmode = (MI_AUDIO_SoundMode_e)(audiospec.channels - 1);
+	attr.u32ChnCnt = audiospec.channels;
+	attr.u32PtNumPerFrm = audiospec.samples;
+
+	audiospec.size = audiospec.samples * audiospec.channels * 2;
+	audio_buffer = (uint8_t*)malloc(audiospec.size);
+
+	MI_AO_SetPubAttr(0,&attr);
+	MI_AO_Enable(0);
+	MI_AO_EnableChn(0,0);
+
+	MI_AO_SetVolume(0,0);
+	MI_AO_ClearChnBuf(0,0);
+
+	audiolock_mx = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
+
+	audio_paused = 1;
+	audiothread_cancel = 0;
+	pthread_create(&audiothread_pt, NULL, &audiothread, NULL);
+}
+
+//
+//	Close AO Audio in place of SDL_CloseAudio
+//
+void AO_CloseAudio(void){
+
+	audiothread_cancel = 1;
+	pthread_join(audiothread_pt, NULL);
+
+	MI_AO_ClearChnBuf(0,0);
+	MI_AO_DisableChn(0,0);
+	MI_AO_Disable(0);
+
+	free(audio_buffer);
+}
+
+//
+//	Pause AO Audio in place of SDL_PauseAudio
+//
+void AO_PauseAudio(int pause_on){
+	audio_paused = pause_on;
+	// unnecessary if buffer clearing is of SDL1.2 spec
+	// MI_AO_SetMute(0, (pause_on ? TRUE : FALSE));
+}
+
+//
+//	Lock Audio Mutex in place of SDL_LockAudio
+//
+void AO_LockAudio(void){
+	pthread_mutex_lock(&audiolock_mx);
+}
+
+//
+//	Unlock Audio Mutex in place of SDL_UnlockAudio
+//
+void AO_UnlockAudio(void){
+	pthread_mutex_unlock(&audiolock_mx);
+}
diff --git a/platform/miyoomini/source/ao.h b/platform/miyoomini/source/ao.h
new file mode 100644
index 0000000..5b2475d
--- /dev/null
+++ b/platform/miyoomini/source/ao.h
@@ -0,0 +1,12 @@
+#ifndef __AO_H__
+#define __AO_H__
+
+#include <SDL/SDL.h>
+
+void	AO_OpenAudio(SDL_AudioSpec *spec);
+void	AO_CloseAudio(void);
+void	AO_PauseAudio(int pause_on);
+void	AO_LockAudio(void);
+void	AO_UnlockAudio(void);
+
+#endif