From a098c8a5e6bc729a2cf50f6cd095e02fbbd094c9 Mon Sep 17 00:00:00 2001 From: diamant3 Date: Wed, 21 Sep 2022 16:32:11 +0800 Subject: [PATCH] add SDL2_mixer sample --- README.md | 554 ++++++++++++++++++++++++++++-------------- images/sdl2_mixer.png | Bin 0 -> 887 bytes 2 files changed, 377 insertions(+), 177 deletions(-) create mode 100644 images/sdl2_mixer.png diff --git a/README.md b/README.md index ca90fdb..1579991 100644 --- a/README.md +++ b/README.md @@ -326,62 +326,62 @@ int setup_callbacks(void) int main(void) { - SceCtrlData pad; - - pspDebugScreenInit(); - setup_callbacks(); - - sceCtrlSetSamplingCycle(0); - sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG); - - while (!done) - { - pspDebugScreenSetXY(0, 2); - sceCtrlReadBufferPositive(&pad, 1); - - printf("Analog X = %d, ", pad.Lx); - printf("Analog Y = %d \n", pad.Ly); - - if (pad.Buttons != 0) - { - if (pad.Buttons & PSP_CTRL_SQUARE) - { - printf("Square pressed! \n"); - } - if (pad.Buttons & PSP_CTRL_TRIANGLE) - { - printf("Triangle pressed! \n"); - } - if (pad.Buttons & PSP_CTRL_CIRCLE) - { - printf("Circle pressed! \n"); - } - if (pad.Buttons & PSP_CTRL_CROSS) - { - printf("Cross pressed! \n"); - } - - if (pad.Buttons & PSP_CTRL_UP) - { - printf("Square pressed! \n"); - } - if (pad.Buttons & PSP_CTRL_DOWN) - { - printf("Triangle pressed! \n"); - } - if (pad.Buttons & PSP_CTRL_LEFT) - { - printf("Square pressed! \n"); - } - if (pad.Buttons & PSP_CTRL_RIGHT) - { - printf("Triangle pressed! \n"); - } - } - } - - sceKernelExitGame(); - return 0; + SceCtrlData pad; + + pspDebugScreenInit(); + setup_callbacks(); + + sceCtrlSetSamplingCycle(0); + sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG); + + while (!done) + { + pspDebugScreenSetXY(0, 2); + sceCtrlReadBufferPositive(&pad, 1); + + printf("Analog X = %d, ", pad.Lx); + printf("Analog Y = %d \n", pad.Ly); + + if (pad.Buttons != 0) + { + if (pad.Buttons & PSP_CTRL_SQUARE) + { + printf("Square pressed! \n"); + } + if (pad.Buttons & PSP_CTRL_TRIANGLE) + { + printf("Triangle pressed! \n"); + } + if (pad.Buttons & PSP_CTRL_CIRCLE) + { + printf("Circle pressed! \n"); + } + if (pad.Buttons & PSP_CTRL_CROSS) + { + printf("Cross pressed! \n"); + } + + if (pad.Buttons & PSP_CTRL_UP) + { + printf("Square pressed! \n"); + } + if (pad.Buttons & PSP_CTRL_DOWN) + { + printf("Triangle pressed! \n"); + } + if (pad.Buttons & PSP_CTRL_LEFT) + { + printf("Square pressed! \n"); + } + if (pad.Buttons & PSP_CTRL_RIGHT) + { + printf("Triangle pressed! \n"); + } + } + } + + sceKernelExitGame(); + return 0; } @@ -453,30 +453,30 @@ PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU); /* Exit callback */ int exitCallback(int arg1, int arg2, void *common) { - sceKernelExitGame(); - return 0; + sceKernelExitGame(); + return 0; } /* Callback thread */ int callbackThread(SceSize args, void *argp) { - int cbid; + int cbid; - cbid = sceKernelCreateCallback("Exit Callback", (void*) exitCallback, NULL); - sceKernelRegisterExitCallback(cbid); - sceKernelSleepThreadCB(); + cbid = sceKernelCreateCallback("Exit Callback", (void*) exitCallback, NULL); + sceKernelRegisterExitCallback(cbid); + sceKernelSleepThreadCB(); - return 0; + return 0; } /* Sets up the callback thread and returns its thread id */ int setupCallbacks(void) { - int thid = 0; + int thid = 0; - thid = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0); - if (thid >= 0) { - sceKernelStartThread(thid, 0, 0); - } - return thid; + thid = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0); + if (thid >= 0) { + sceKernelStartThread(thid, 0, 0); + } + return thid; } /* Main code */ @@ -493,25 +493,25 @@ typedef struct { float currentFunction(const float time) { double x; - float t = modf((time / (2 * PI)), &x); + float t = modf((time / (2 * PI)), &x); switch(function) { - case 0: // SINE - return sinf(time); - case 1: // SQUARE - if (t < 0.5f) { - return -0.2f; - } else { - return 0.2f; - } - case 2: // TRIANGLE - if (t < 0.5f) { - return (t * 2.0f) - 0.5f; - } else { - return 0.5f - (t - 0.5f) * 2.0f; - } - default: - return 0.0f; + case 0: // SINE + return sinf(time); + case 1: // SQUARE + if (t < 0.5f) { + return -0.2f; + } else { + return 0.2f; + } + case 2: // TRIANGLE + if (t < 0.5f) { + return (t * 2.0f) - 0.5f; + } else { + return 0.5f - (t - 0.5f) * 2.0f; + } + default: + return 0.0f; } } @@ -520,107 +520,107 @@ float currentFunction(const float time) { 16-bit, stereo. */ void audioCallback(void* buf, unsigned int length, void *userdata) { const float sampleLength = 1.0f / sampleRate; - const float scaleFactor = SHRT_MAX - 1.0f; + const float scaleFactor = SHRT_MAX - 1.0f; static float freq0 = 440.0f; sample_t* ubuf = (sample_t*) buf; - int i; - - if (frequency != freq0) { - currentTime *= (freq0 / frequency); - } - for (i = 0; i < length; i++) { - short s = (short) (scaleFactor * currentFunction(2.0f * PI * frequency * currentTime)); - ubuf[i].l = s; - ubuf[i].r = s; - currentTime += sampleLength; - } - if (currentTime * frequency > 1.0f) { - double d; - currentTime = modf(currentTime * frequency, &d) / frequency; - } - - freq0 = frequency; + int i; + + if (frequency != freq0) { + currentTime *= (freq0 / frequency); + } + for (i = 0; i < length; i++) { + short s = (short) (scaleFactor * currentFunction(2.0f * PI * frequency * currentTime)); + ubuf[i].l = s; + ubuf[i].r = s; + currentTime += sampleLength; + } + if (currentTime * frequency > 1.0f) { + double d; + currentTime = modf(currentTime * frequency, &d) / frequency; + } + + freq0 = frequency; } /* Read the analog stick and adjust the frequency */ void controlFrequency(void) { static int oldButtons = 0; - const int zones[6] = {30, 70, 100, 112, 125, 130}; - const float response[6] = {0.0f, 0.1f, 0.5f, 1.0f, 4.0f, 8.0f}; - const float minFreq = 32.0f; - const float maxFreq = 7040.0f; - SceCtrlData pad; - float direction; - int changedButtons; - int i, v; - - sceCtrlReadBufferPositive(&pad, 1); - - v = pad.Ly - 128; - if (v < 0) { - direction = 1.0f; - v = -v; - } else { - direction = -1.0f; - } - - for (i = 0; i < 6; i++) { - if (v < zones[i]) { - frequency += (response[i] * direction); - break; - } - } - - if (frequency < minFreq) { - frequency = minFreq; - } else if (frequency > maxFreq) { - frequency = maxFreq; - } - - changedButtons = pad.Buttons & (~oldButtons); - if (changedButtons & PSP_CTRL_CROSS) { - function++; - if (function > 2) { - function = 0; - } - } - - oldButtons = pad.Buttons; + const int zones[6] = {30, 70, 100, 112, 125, 130}; + const float response[6] = {0.0f, 0.1f, 0.5f, 1.0f, 4.0f, 8.0f}; + const float minFreq = 32.0f; + const float maxFreq = 7040.0f; + SceCtrlData pad; + float direction; + int changedButtons; + int i, v; + + sceCtrlReadBufferPositive(&pad, 1); + + v = pad.Ly - 128; + if (v < 0) { + direction = 1.0f; + v = -v; + } else { + direction = -1.0f; + } + + for (i = 0; i < 6; i++) { + if (v < zones[i]) { + frequency += (response[i] * direction); + break; + } + } + + if (frequency < minFreq) { + frequency = minFreq; + } else if (frequency > maxFreq) { + frequency = maxFreq; + } + + changedButtons = pad.Buttons & (~oldButtons); + if (changedButtons & PSP_CTRL_CROSS) { + function++; + if (function > 2) { + function = 0; + } + } + + oldButtons = pad.Buttons; } int main(void) { - pspDebugScreenInit(); - setupCallbacks(); - - pspAudioInit(); - pspAudioSetChannelCallback(0, audioCallback, NULL); - - sceCtrlSetSamplingCycle(0); - sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG); - - printf("Press up and down to select frequency\nPress X to change function\n"); - - while(1) { - sceDisplayWaitVblankStart(); - pspDebugScreenSetXY(0,2); - printf("freq = %.2f \n", frequency); - - switch(function) { - case 0: - printf("SINE WAVE. \n"); - break; - case 1: - printf("SQUARE WAVE. \n"); - break; - case 2: - printf("TRIANGLE WAVE. \n"); - break; - } - - controlFrequency(); - } - - return 0; + pspDebugScreenInit(); + setupCallbacks(); + + pspAudioInit(); + pspAudioSetChannelCallback(0, audioCallback, NULL); + + sceCtrlSetSamplingCycle(0); + sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG); + + printf("Press up and down to select frequency\nPress X to change function\n"); + + while(1) { + sceDisplayWaitVblankStart(); + pspDebugScreenSetXY(0,2); + printf("freq = %.2f \n", frequency); + + switch(function) { + case 0: + printf("SINE WAVE. \n"); + break; + case 1: + printf("SQUARE WAVE. \n"); + break; + case 2: + printf("TRIANGLE WAVE. \n"); + break; + } + + controlFrequency(); + } + + return 0; } @@ -785,6 +785,206 @@ More documentation on SDL can be found + +CMakeLists.txt: + +
+cmake_minimum_required(VERSION 3.0)
+
+project(sdl2_mixer)
+
+add_executable(${PROJECT_NAME} main.c)
+
+find_package(PkgConfig REQUIRED)
+pkg_search_module(SDL2 REQUIRED sdl2)
+
+target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
+
+target_link_libraries(${PROJECT_NAME} PRIVATE
+    ${SDL2_LIBRARIES}
+)
+
+if(PSP)
+    target_link_libraries(${PROJECT_NAME} PRIVATE
+        SDL2main
+        SDL2_mixer
+        vorbisfile
+        vorbis
+        ogg
+    )
+    create_pbp_file(
+        TARGET ${PROJECT_NAME}
+        ICON_PATH NULL
+        BACKGROUND_PATH NULL
+        PREVIEW_PATH NULL
+        TITLE ${PROJECT_NAME}
+    )
+endif()
+
+ +Building can be done with: + +
+mkdir build && cd build
+psp-cmake ..
+make
+
+ +

This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and you need an audio file to test the program, put it in a directory in ms0:/MUSIC/ and then rename the audio file same as name on your MUSIC_PATH macro in your C code and the PSP can run it.

+ +

+ ## Libraries There are many C and C++ libraries available within the PSPDEV toolchain which can add functionality to your program. Some examples: diff --git a/images/sdl2_mixer.png b/images/sdl2_mixer.png new file mode 100644 index 0000000000000000000000000000000000000000..dffedb4408cef71dd3627d1d762655360d22c408 GIT binary patch literal 887 zcmeAS@N?(olHy`uVBq!ia0y~yV0-|?A{~)y?T+C!GXoa@vr&YSps@zQnWvObOEZRF}O4J+f#-m0Sg!#%^KNQqR{ADEK&j) zKyegWIKyE9D^MI8?F%$O3@DFGV>1|L5t6|#xW8;|fB3J#fqRPsGn4KGOa>D#2Sf^u zfn5}61Xvxni2^nT*aDCg4hD8dC_;<^xev_|K#_O+K$lR}I&8LJ^HdK1ma}{gTr`Lk oMD$~eZkUBg(M{v@%D}M7ruW48_xqS0vw*DfboFyt=akR{0KA*XegFUf literal 0 HcmV?d00001