# port/audio: the software SPU2. Integer-only
# code with no platform dependencies; the sndn2 host and the SDL output sit
# on top of it (ico_sndn2, below).
add_library(ico_audio STATIC
    adpcm.c
    audio_lock.c
    spu2.c
    spu2_sd.c
    spu2_tables.c
    volume.c
    mix_gain.c)
target_include_directories(ico_audio PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(ico_audio PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})

add_executable(adpcm_test test/adpcm_test.c)
target_compile_options(adpcm_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(adpcm_test PRIVATE ico_audio)

# spu2_test's encoder and frequency checks use libm (test-only; the checksum
# scene is integer-only).
add_executable(spu2_test test/spu2_test.c)
target_compile_options(spu2_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(spu2_test PRIVATE ico_audio)
if(NOT WIN32)
    target_link_libraries(spu2_test PRIVATE m)
endif()

add_executable(volume_test test/volume_test.c)
target_compile_options(volume_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(volume_test PRIVATE ico_audio)

# mix_gain_test: the music, effects and film gains on the voice volume
# words (mix_gain.h): the identity at 100 %, the scaling, sweep words, the
# cache and its re-issue
add_executable(mix_gain_test test/mix_gain_test.c)
target_compile_options(mix_gain_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(mix_gain_test PRIVATE ico_audio)

# adpcm_slots_test: the request daemon's full-slot rule: a stream
# already fading out is closed at once and gives its slot up, else the PS2's
# path runs.  The game's adpcm_init.c with the driver and disc stubbed.
ico_include_dirs(fumi _ico_adpcm_slots_incs)
add_executable(adpcm_slots_test test/adpcm_slots_test.c)
target_include_directories(adpcm_slots_test PRIVATE ${_ico_adpcm_slots_incs}
                           "${CMAKE_SOURCE_DIR}/port/include")
target_compile_definitions(adpcm_slots_test PRIVATE ICO_HOST=1)
target_compile_options(adpcm_slots_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})

if(ICO_RUN_TESTS)
    add_test(NAME adpcm_slots COMMAND adpcm_slots_test)
    add_test(NAME volume COMMAND volume_test)
    add_test(NAME mix_gain COMMAND mix_gain_test)
    add_test(NAME adpcm COMMAND adpcm_test)
    add_test(NAME spu2 COMMAND spu2_test)
endif()

# --- The Sg sequencer, the SNDN2DRV host and the output ---------------------
# sce/libsndn2/sound.c is the EE sequencer the game calls (the decomp's
# libsndn2.a(sound.o), kept upstream like ico2/); it is decomp-shaped game
# code, so it takes the game's warning set and ICO_HOST.
# sndn2_host.c and stream.c are the IOP driver behind its RPC; audio_host.c
# renders the SPU2 once per vsync into the sinks (wav.c, and out_sdl.c in
# the window build when SDL3 is there).
add_library(ico_sndn2 STATIC
    "${CMAKE_SOURCE_DIR}/sce/libsndn2/sound.c"
    sndn2_host.c
    libsd_irx.c
    stream.c
    stream_watch.c
    audio_host.c
    wav.c)
target_include_directories(ico_sndn2
    PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}"
    PRIVATE "${CMAKE_SOURCE_DIR}/sce/libsndn2" "${ICO_COMPAT_DIR}" "${CMAKE_SOURCE_DIR}/port/platform")
target_compile_options(ico_sndn2 PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
set_source_files_properties("${CMAKE_SOURCE_DIR}/sce/libsndn2/sound.c" PROPERTIES
    COMPILE_DEFINITIONS "ICO_HOST=1"
    COMPILE_OPTIONS "${ICO_GAME_WARNINGS};-Wno-sign-compare;-Wno-unused-parameter;-Wno-unused-variable;-Wno-unused-but-set-variable;-Wno-pointer-sign")
target_link_libraries(ico_sndn2 PUBLIC ico_audio ico_port_data ico_platform ico_options)
if(NOT WIN32)
    target_link_libraries(ico_sndn2 PUBLIC m) # exp2 for the pitch table fallback
endif()
if(NOT ICO_HEADLESS AND TARGET SDL3::SDL3)
    target_sources(ico_sndn2 PRIVATE out_sdl.c)
    target_compile_definitions(ico_sndn2 PRIVATE ICO_AUDIO_SDL=1)
    target_link_libraries(ico_sndn2 PUBLIC SDL3::SDL3)
    add_executable(audio_queue_test test/audio_queue_test.c)
    target_compile_options(audio_queue_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
    target_link_libraries(audio_queue_test PRIVATE ico_audio SDL3::SDL3)
    ico_copy_sdl3_dll(audio_queue_test)
    # audio_pull_test: out_sdl.c's pull path on a device-less stream over the
    # real SPU2: a writer thread under the audio lock and a pulling thread
    # render the same frames as one thread; 10 s free-running with the
    # accounting of frames and silence; the ramp out of and into silence
    # when the lock is held; the 10 s line and the mode choice
    add_executable(audio_pull_test test/audio_pull_test.c)
    target_compile_options(audio_pull_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
    target_link_libraries(audio_pull_test PRIVATE ico_audio SDL3::SDL3)
    ico_copy_sdl3_dll(audio_pull_test)
    if(ICO_RUN_TESTS)
        add_test(NAME audio_queue COMMAND audio_queue_test)
        add_test(NAME audio_pull COMMAND audio_pull_test)
    endif()
endif()
if(TARGET ico_pc)
    target_link_libraries(ico_pc PRIVATE ico_sndn2)
endif()

add_executable(sndn2_test test/sndn2_test.c)
target_include_directories(sndn2_test PRIVATE "${ICO_COMPAT_DIR}" "${CMAKE_SOURCE_DIR}/port/platform")
target_compile_options(sndn2_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(sndn2_test PRIVATE ico_sndn2)
if(ICO_RUN_TESTS)
    # the disc's pitch table is checked when the image is there (else skipped)
    add_test(NAME sndn2 COMMAND sndn2_test "${ICO_DISC_IMAGE}")
endif()

# adpcm_stream_test: the game's adpcm_init.c on the real sequencer, driver
# and SPU2 (the sound-data table, the SPU channels and the disc reader stood
# in for): a stream reopened on faded voices, the ending's six-stream shape,
# a stream of minutes with ring wraps; the stream log lines
add_executable(adpcm_stream_test test/adpcm_stream_test.c)
target_include_directories(adpcm_stream_test PRIVATE ${_ico_adpcm_slots_incs}
                           "${CMAKE_SOURCE_DIR}/port/include")
target_compile_definitions(adpcm_stream_test PRIVATE ICO_HOST=1)
target_compile_options(adpcm_stream_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})
target_link_libraries(adpcm_stream_test PRIVATE ico_sndn2)
if(ICO_RUN_TESTS)
    add_test(NAME adpcm_stream COMMAND adpcm_stream_test)
endif()

# libsd_irx_test: the LIBSD.IRX reader on a synthetic module and its
# fallback; the real module is sndn2_test's
add_executable(libsd_irx_test test/libsd_irx_test.c)
target_compile_options(libsd_irx_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(libsd_irx_test PRIVATE ico_sndn2)
if(ICO_RUN_TESTS)
    add_test(NAME libsd_irx COMMAND libsd_irx_test)
endif()

# pan_test: the mirror mode's left/right swap of the
# rendered blocks (ico_audio_pan_mirror) and the option it reads
# (host_config.c: the toml writer behind ico_config, as options_test has it)
add_executable(pan_test test/pan_test.c "${CMAKE_SOURCE_DIR}/port/platform/host_config.c")
target_include_directories(pan_test PRIVATE "${CMAKE_SOURCE_DIR}/port/platform")
target_compile_options(pan_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(pan_test PRIVATE ico_sndn2)
if(ICO_RUN_TESTS)
    add_test(NAME audio_pan COMMAND pan_test)
endif()

# spu2_bench: `--check` is the 10 s
# render CRC test of five scenes against the frame-by-frame renderer's
# golden values; without arguments it times the render per vsync.
# spu2_prof is the same harness over a copy of the SPU2 built with
# SPU2_PROFILE (per-stage clock_gettime laps); it is not a test.
add_executable(spu2_bench test/spu2_bench.c)
target_compile_options(spu2_bench PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(spu2_bench PRIVATE ico_audio)
add_executable(spu2_prof test/spu2_bench.c adpcm.c spu2.c spu2_sd.c spu2_tables.c)
target_compile_definitions(spu2_prof PRIVATE SPU2_PROFILE=1)
target_compile_options(spu2_prof PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
if(ICO_RUN_TESTS)
    add_test(NAME spu2_render_crc COMMAND spu2_bench --check)
endif()
