+ SceUID thid;
+
+ sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
+ if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
+
+ samples_made = samples_done = 0;
+ samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
+ 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)
+{
+ static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
+ int ret, stereo;
+
+ samples_made = samples_done = 0;
+
+ if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
+ PsndRerate(Pico.m.frame_count ? 1 : 0);
+ }
+ stereo=(PicoOpt&8)>>3;
+
+ samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
+ if (PsndRate <= 22050) samples_block /= 2;
+ sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
+
+ lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
+ PsndRate, PsndLen, stereo, Pico.m.pal, samples_block);
+
+ while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
+ sceAudio_5C37C0AE();
+ ret = sceAudio_38553111(samples_block/2, PsndRate, 2); // seems to not need that stupid 64byte alignment
+ if (ret < 0) {
+ lprintf("sceAudio_38553111() failed: %i\n", ret);
+ sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
+ noticeMsgTime = sceKernelGetSystemTimeLow();
+ currentConfig.EmuOpt &= ~4;
+ } else {
+ PicoWriteSound = writeSound;
+ memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
+ snd_playptr = sndBuffer_endptr - samples_block;
+ samples_made = samples_block; // send 1 empty block first..
+ PsndOut = sndBuffer;
+ PsndRate_old = PsndRate;
+ PicoOpt_old = PicoOpt;
+ pal_old = Pico.m.pal;
+ }
+}
+
+static void sound_end(void)
+{
+ samples_made = samples_done = 0;
+ while (sceAudioOutput2GetRestSample() > 0)
+ psp_msleep(100);
+ sceAudio_5C37C0AE();
+}
+
+static void sound_deinit(void)
+{
+ sound_thread_exit = 1;
+ sceKernelSignalSema(sound_sem, 1);
+ sceKernelDeleteSema(sound_sem);
+ sound_sem = -1;
+}
+
+static void writeSound(int len)
+{
+ int ret;