+ short *endptr = &sndBuffer[SOUND_DEF_BLOCK_SIZE*SOUND_BLOCK_COUNT*2];
+ 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 >= 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)
+{
+ 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) {
+ sound_rerate(Pico.m.frame_count ? 1 : 0);
+ }
+ stereo=(PicoOpt&8)>>3;
+ samples_block = SOUND_DEF_BLOCK_SIZE;
+ if (PsndRate < 44100) samples_block = SOUND_DEF_BLOCK_SIZE / 2;
+ if (PsndRate < 22050) samples_block = SOUND_DEF_BLOCK_SIZE / 4;
+
+ 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/*stereo ? 2 : 1*/);
+ lprintf("sceAudio_38553111() ret: %i\n", ret);
+ 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 {
+// int ret = sceAudioSetChannelDataLen(ret, PsndLen); // a try..
+// lprintf("sceAudioSetChannelDataLen: %i\n", ret);
+ PicoWriteSound = writeSound;
+ memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
+ snd_playptr = sndBuffer;
+ PsndOut = sndBuffer;
+ PsndRate_old = PsndRate;
+ PicoOpt_old = PicoOpt;
+ pal_old = Pico.m.pal;
+ }
+}
+
+static void sound_end(void)
+{
+ int ret;
+ while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
+ ret = sceAudio_5C37C0AE();
+ lprintf("sound_end: sceAudio_5C37C0AE ret %i\n", ret);
+}
+
+static void sound_deinit(void)
+{
+ sound_thread_exit = 1;
+ sceKernelSignalSema(sound_sem, 1);
+}
+
+static void writeSound(int len)
+{
+ int ret;
+ short *endptr = &sndBuffer[SOUND_DEF_BLOCK_SIZE*SOUND_BLOCK_COUNT*2];