+
+/* sound stuff */
+#define SOUND_BLOCK_SIZE_NTSC (2940*2) // 1024 // 1152
+#define SOUND_BLOCK_SIZE_PAL (3528*2)
+#define SOUND_BLOCK_COUNT 4
+
+static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
+static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
+static int samples_made = 0, samples_done = 0, samples_block = 0;
+static int sound_thread_exit = 0;
+static SceUID sound_sem = -1;
+
+static void writeSound(int len);
+
+static int sound_thread(SceSize args, void *argp)
+{
+ int ret;
+
+ lprintf("sound_thread: started, priority %i\n", sceKernelGetThreadCurrentPriority());
+
+ while (!sound_thread_exit)
+ {
+ if (samples_made - samples_done < samples_block) {
+ // wait for data...
+ //lprintf("sthr: wait... (%i/%i)\n", samples_done, samples_made);
+ ret = sceKernelWaitSema(sound_sem, 1, 0);
+ //lprintf("sthr: sceKernelWaitSema: %i\n", ret);
+ continue;
+ }
+
+ //lprintf("sthr: got data: %i\n", samples_made - samples_done);
+
+ ret = sceAudio_E0727056(PSP_AUDIO_VOLUME_MAX, snd_playptr);
+
+ samples_done += samples_block;
+ snd_playptr += samples_block;
+ if (snd_playptr >= sndBuffer_endptr)
+ snd_playptr = sndBuffer;
+ if (ret)
+ lprintf("sthr: outf: %i; pos %i/%i\n", ret, samples_done, samples_made);
+ }
+
+ lprintf("sthr: exit\n");
+ sceKernelExitDeleteThread(0);
+ return 0;
+}
+
+static void sound_init(void)
+{
+ SceUID thid;
+
+ sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
+ if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
+
+ sound_thread_exit = 0;
+ thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
+ if (thid >= 0)
+ {
+ sceKernelStartThread(thid, 0, 0);
+ }
+ else
+ lprintf("sceKernelCreateThread failed: %i\n", thid);
+}
+
+static void sound_prepare(void)