<commit_before>#include "csound.h"
#include <stdio.h>

#include <CUnit/Basic.h>

int init_suite1(void)
{
    return 0;
}

int clean_suite1(void)
{
    return 0;
}

void test_dev_list(void)
{
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    char *name, *type;
    int n = 0;

//    printf("Output ------------------------------\n");
    while(!csoundGetModule(csound, n++, &name, &type)) {
        if (strcmp(type, "midi") == 0) {
            csoundSetMIDIModule(csound,name);
            int i,ndevs = csoundGetMIDIDevList(csound,NULL,1);
            CS_MIDIDEVICE *devs = (CS_MIDIDEVICE *) malloc(ndevs*sizeof(CS_MIDIDEVICE));
            csoundGetMIDIDevList(csound,devs,1);
//            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
                CU_ASSERT_STRING_EQUAL(devs[i].midi_module, name);
//                printf(" %d: %s %s (%s)\n", i,
//                       devs[i].device_id, devs[i].device_name, devs[i].interface_name);
            }
            free(devs);
        } else if (strcmp(type, "audio") == 0) {
            csoundSetRTAudioModule(csound,name);
            int i,ndevs = csoundGetAudioDevList(csound,NULL,1);
            CS_AUDIODEVICE *devs = (CS_AUDIODEVICE *) malloc(ndevs*sizeof(CS_AUDIODEVICE));
            csoundGetAudioDevList(csound,devs,1);
//            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
//                CU_ASSERT_STRING_EQUAL(devs[i].rt_module, name);
//                printf(" %d: %s (%s)\n", i, devs[i].device_id, devs[i].device_name);
            }
            free(devs);
        }
    }

    printf("\nInput ------------------------------\n");
    n = 0;
    while(!csoundGetModule(csound, n++, &name, &type)) {
        if (strcmp(type, "midi") == 0) {
            csoundSetMIDIModule(csound,name);
            int i,ndevs = csoundGetMIDIDevList(csound,NULL,0);
            CS_MIDIDEVICE *devs = (CS_MIDIDEVICE *) malloc(ndevs*sizeof(CS_MIDIDEVICE));
            csoundGetMIDIDevList(csound,devs,0);
//            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
                CU_ASSERT_STRING_EQUAL(devs[i].midi_module, name) ;
//                printf(" %d: %s %s (%s)\n", i,
//                       devs[i].device_id, devs[i].device_name, devs[i].interface_name);
            }
            free(devs);
        } else if (strcmp(type, "audio") == 0) {
            csoundSetRTAudioModule(csound,name);
            int i,ndevs = csoundGetAudioDevList(csound,NULL,0);
            CS_AUDIODEVICE *devs = (CS_AUDIODEVICE *) malloc(ndevs*sizeof(CS_AUDIODEVICE));
            csoundGetAudioDevList(csound,devs,0);
            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
//                CU_ASSERT_STRING_EQUAL(devs[i].rt_module, name);
//                printf(" %d: %s (%s)\n", i, devs[i].device_id, devs[i].device_name);
            }
            free(devs);
        }
    }
    csoundDestroy(csound);
}

int key_callback_evt(void *userData, void *p,
                     unsigned int type)
{
    int *prev = (int *) userData;
    *((int *) p) = *prev;
    *prev += 1;
    return CSOUND_SUCCESS;
}

int key_callback_txt(void *userData, void *p,
                     unsigned int type)
{
    int *prev = (int *) userData;
    *((int *) p) =  *prev;
    *prev += 1;
    return CSOUND_SUCCESS;
}

void test_keyboard_io(void)
{
    int ret, err, prev = 100;
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    ret = csoundRegisterKeyboardCallback(csound, key_callback_evt, &prev, CSOUND_CALLBACK_KBD_EVENT);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    ret = csoundRegisterKeyboardCallback(csound, key_callback_txt, &prev, CSOUND_CALLBACK_KBD_TEXT);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    const char  *instrument =
            "chn_k \"key\", 2\n"
            "instr 1 \n"
            "kkey sensekey\n"
            "chnset kkey, \"key\"\n"
            "kkey2, kdown2 sensekey\n"
            "chnset kkey2, \"key2\"\n"
            "chnset kdown2, \"down2\"\n"
//            "printk2 kkey2\n"
//            "printk2 kdown2\n"
            "endin \n";
    csoundCompileOrc(csound, instrument);
    csoundReadScore(csound, "i 1 0 1");
    ret = csoundStart(csound);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    ret = csoundPerformKsmps(csound);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    MYFLT val = csoundGetControlChannel(csound, "key", &err);
//    CU_ASSERT(err == CSOUND_SUCCESS);
    CU_ASSERT_DOUBLE_EQUAL(val, 100.0, 0.001);
    val = csoundGetControlChannel(csound, "key2", &err);
//    CU_ASSERT(err == CSOUND_SUCCESS);
    CU_ASSERT_DOUBLE_EQUAL(val, 101.0, 0.001);
    val = csoundGetControlChannel(csound, "down2", &err);
//    CU_ASSERT(err == CSOUND_SUCCESS);
    CU_ASSERT_DOUBLE_EQUAL(val, 1.0, 0.001);

    csoundDestroy(csound);
}


void test_audio_modules(void)
{
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    char *name, *type;
    int n = 0;

    while(!csoundGetModule(csound, n++, &name, &type)) {
        if (strcmp(type, "audio") == 0) {
            const char  *instrument =
                    "ksmps = 256\n"
                    "instr 1 \n"
                    "asig oscil 0.1, 440\n"
                    "out asig\n"
                    "endin \n";
            csoundSetOption(csound, "-B4096");
            csoundCompileOrc(csound, instrument);
            csoundReadScore(csound, "i 1 0 0.1\n e 0.2");
            csoundSetRTAudioModule(csound, name);
            csoundSetOutput(csound, "dac", NULL, NULL);
            int ret = csoundStart(csound);
            CU_ASSERT(ret == 0);
            ret = csoundPerform(csound);
            CU_ASSERT(ret > 0);
            csoundReset(csound);
        }
    }

}

void test_audio_hostbased(void)
{
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    const char  *instrument =
            "ksmps = 256\n"
            "instr 1 \n"
            "asig oscil 0.1, 440\n"
            "out asig\n"
            "endin \n";
    csoundSetOption(csound, "-B4096");
    csoundCompileOrc(csound, instrument);<commit_msg>Adjusted test to not fail if jack engine is not running.<commit_after>#include "csound.h"
#include <stdio.h>

#include <CUnit/Basic.h>

int init_suite1(void)
{
    return 0;
}

int clean_suite1(void)
{
    return 0;
}

void test_dev_list(void)
{
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    char *name, *type;
    int n = 0;

//    printf("Output ------------------------------\n");
    while(!csoundGetModule(csound, n++, &name, &type)) {
        if (strcmp(type, "midi") == 0) {
            csoundSetMIDIModule(csound,name);
            int i,ndevs = csoundGetMIDIDevList(csound,NULL,1);
            CS_MIDIDEVICE *devs = (CS_MIDIDEVICE *) malloc(ndevs*sizeof(CS_MIDIDEVICE));
            csoundGetMIDIDevList(csound,devs,1);
//            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
                CU_ASSERT_STRING_EQUAL(devs[i].midi_module, name);
//                printf(" %d: %s %s (%s)\n", i,
//                       devs[i].device_id, devs[i].device_name, devs[i].interface_name);
            }
            free(devs);
        } else if (strcmp(type, "audio") == 0) {
            csoundSetRTAudioModule(csound,name);
            int i,ndevs = csoundGetAudioDevList(csound,NULL,1);
            CS_AUDIODEVICE *devs = (CS_AUDIODEVICE *) malloc(ndevs*sizeof(CS_AUDIODEVICE));
            csoundGetAudioDevList(csound,devs,1);
//            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
//                CU_ASSERT_STRING_EQUAL(devs[i].rt_module, name);
//                printf(" %d: %s (%s)\n", i, devs[i].device_id, devs[i].device_name);
            }
            free(devs);
        }
    }

    printf("\nInput ------------------------------\n");
    n = 0;
    while(!csoundGetModule(csound, n++, &name, &type)) {
        if (strcmp(type, "midi") == 0) {
            csoundSetMIDIModule(csound,name);
            int i,ndevs = csoundGetMIDIDevList(csound,NULL,0);
            CS_MIDIDEVICE *devs = (CS_MIDIDEVICE *) malloc(ndevs*sizeof(CS_MIDIDEVICE));
            csoundGetMIDIDevList(csound,devs,0);
//            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
                CU_ASSERT_STRING_EQUAL(devs[i].midi_module, name) ;
//                printf(" %d: %s %s (%s)\n", i,
//                       devs[i].device_id, devs[i].device_name, devs[i].interface_name);
            }
            free(devs);
        } else if (strcmp(type, "audio") == 0) {
            csoundSetRTAudioModule(csound,name);
            int i,ndevs = csoundGetAudioDevList(csound,NULL,0);
            CS_AUDIODEVICE *devs = (CS_AUDIODEVICE *) malloc(ndevs*sizeof(CS_AUDIODEVICE));
            csoundGetAudioDevList(csound,devs,0);
            printf("Module %d:  %s (%s): %i devices\n", n, name, type, ndevs);
            for(i=0; i < ndevs; i++) {
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_id);
                CU_ASSERT_PTR_NOT_NULL(devs[i].device_name);
//                CU_ASSERT_STRING_EQUAL(devs[i].rt_module, name);
//                printf(" %d: %s (%s)\n", i, devs[i].device_id, devs[i].device_name);
            }
            free(devs);
        }
    }
    csoundDestroy(csound);
}

int key_callback_evt(void *userData, void *p,
                     unsigned int type)
{
    int *prev = (int *) userData;
    *((int *) p) = *prev;
    *prev += 1;
    return CSOUND_SUCCESS;
}

int key_callback_txt(void *userData, void *p,
                     unsigned int type)
{
    int *prev = (int *) userData;
    *((int *) p) =  *prev;
    *prev += 1;
    return CSOUND_SUCCESS;
}

void test_keyboard_io(void)
{
    int ret, err, prev = 100;
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    ret = csoundRegisterKeyboardCallback(csound, key_callback_evt, &prev, CSOUND_CALLBACK_KBD_EVENT);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    ret = csoundRegisterKeyboardCallback(csound, key_callback_txt, &prev, CSOUND_CALLBACK_KBD_TEXT);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    const char  *instrument =
            "chn_k \"key\", 2\n"
            "instr 1 \n"
            "kkey sensekey\n"
            "chnset kkey, \"key\"\n"
            "kkey2, kdown2 sensekey\n"
            "chnset kkey2, \"key2\"\n"
            "chnset kdown2, \"down2\"\n"
//            "printk2 kkey2\n"
//            "printk2 kdown2\n"
            "endin \n";
    csoundCompileOrc(csound, instrument);
    csoundReadScore(csound, "i 1 0 1");
    ret = csoundStart(csound);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    ret = csoundPerformKsmps(csound);
    CU_ASSERT(ret == CSOUND_SUCCESS);
    MYFLT val = csoundGetControlChannel(csound, "key", &err);
//    CU_ASSERT(err == CSOUND_SUCCESS);
    CU_ASSERT_DOUBLE_EQUAL(val, 100.0, 0.001);
    val = csoundGetControlChannel(csound, "key2", &err);
//    CU_ASSERT(err == CSOUND_SUCCESS);
    CU_ASSERT_DOUBLE_EQUAL(val, 101.0, 0.001);
    val = csoundGetControlChannel(csound, "down2", &err);
//    CU_ASSERT(err == CSOUND_SUCCESS);
    CU_ASSERT_DOUBLE_EQUAL(val, 1.0, 0.001);

    csoundDestroy(csound);
}


void test_audio_modules(void)
{
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    char *name, *type;
    int n = 0;

    while(!csoundGetModule(csound, n++, &name, &type)) {
        if (strcmp(type, "audio") == 0) {
            const char  *instrument =
                    "ksmps = 256\n"
                    "instr 1 \n"
                    "asig oscil 0.1, 440\n"
                    "out asig\n"
                    "endin \n";
            csoundSetOption(csound, "-B4096");
            csoundCompileOrc(csound, instrument);
            csoundReadScore(csound, "i 1 0 0.1\n e 0.2");
            csoundSetRTAudioModule(csound, name);
            csoundSetOutput(csound, "dac", NULL, NULL);
            int ret = csoundStart(csound);
            if (strcmp(name, "jack") != 0) { // Jack module would fail this test if jack is not running
              CU_ASSERT(ret == 0);
            }
            if (ret == 0) {
              ret = csoundPerform(csound);
              CU_ASSERT(ret > 0);
            }
            csoundReset(csound);
        }
    }

}

void test_audio_hostbased(void)
{
    CSOUND  *csound;
    csound = csoundCreate(NULL);
    const char  *instrument =
            "ksmps = 256\n"
            "instr 1 \n"
            "asig oscil 0.1, 440\n"
            "out asig\n"
            "endin \n";
    csoundSetOption(csound, "-B4096");
    csoundCompileOrc(csound, instrument);
{'fraction_non_alphanumeric': 0.1526009859465823, 'fraction_numerical': 0.015157089250239128, 'mean_word_length': 2.0799909358712894, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '761615', 'n_tokens_mistral': 5094, 'n_tokens_neox': 4832, 'n_words': 883}